1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 #ifndef TENSORFLOW_CORE_COMMON_RUNTIME_EAGER_EXECUTE_H_ 16 #define TENSORFLOW_CORE_COMMON_RUNTIME_EAGER_EXECUTE_H_ 17 18 #include "absl/container/inlined_vector.h" 19 #include "absl/types/span.h" 20 #include "tensorflow/core/common_runtime/device.h" 21 #include "tensorflow/core/common_runtime/eager/context.h" 22 #include "tensorflow/core/common_runtime/eager/eager_operation.h" 23 #include "tensorflow/core/common_runtime/eager/kernel_and_device.h" 24 #include "tensorflow/core/common_runtime/eager/tensor_handle.h" 25 #include "tensorflow/core/framework/step_stats.pb.h" 26 #include "tensorflow/core/lib/core/status.h" 27 28 namespace tensorflow { 29 30 // Utility function that executes a fully constructed EagerOperation. 31 // There are a few possible different combinations of how things can be 32 // executed: 33 // - Async (the op context is configured to schedule asynchronously) 34 // Eager execute should return quickly after scheduling this operation to 35 // execute. 36 // - Remote (the op device is on a remote task) 37 // Eager execute will send an RPC to execute the op on a remote device. 38 // Note that in the Async + Remote case, EagerExecute should still return 39 // quickly, but it will schedule the op to be executed remotely. 40 // 41 // 'retvals' must point to a pre-allocated array of TensorHandle* and 42 // '*num_retvals' should be set to the size of this array. It is an error if 43 // the size of 'retvals' is less than the number of outputs. This call sets 44 // *num_retvals to the number of outputs. 45 Status EagerExecute(EagerOperation* op, TensorHandle** retvals, 46 int* num_retvals); 47 48 // Low-level utility to execute the kernel specified by `kernel` on 49 // `kernel->device()`, with the inputs op_inputs, in the context 'ctx'. 50 Status EagerKernelExecute( 51 EagerContext* ctx, const absl::InlinedVector<TensorHandle*, 4>& op_inputs, 52 const absl::optional<EagerFunctionParams>& eager_func_params, 53 const core::RefCountPtr<KernelAndDevice>& kernel, 54 GraphCollector* graph_collector, CancellationManager* cancellation_manager, 55 absl::Span<TensorHandle*> retvals, 56 const absl::optional<ManagedStackTrace>& stack_trace = {}); 57 58 // Low-level utility to copy a tensor handle from one device to another. If 59 // successful, result TensorHandle will be populated. If the caller requests for 60 // the mirror flag, EagerCopyToDevice will attempt to add a mirror to the 61 // original handle and update *result to point to h. Since this is not 62 // guaranteed, callers should always use the value in *result. 63 Status EagerCopyToDevice(TensorHandle* h, EagerContext* ctx, 64 EagerExecutor* executor, Device* device, bool mirror, 65 TensorHandle** result); 66 67 // Utility function that executes a fully constructed EagerOperation 68 // asynchronously on the local task. This function works differently from 69 // EagerExecute in several ways: 70 // - It supports local execution only. 71 // - It returns after launching the eager operation to run asynchronously. 72 // Different from EagerExecute with async context that apends the operation 73 // to the end of the eager executor schedule queue, this call bypasses the 74 // executor logic and directly launches op execution. Ops running through 75 // this call does NOT have an ordering and can be executed in parallel. 76 // - It takes a StatusCallback which will be triggered after execution with the 77 // execution status. 78 // 79 // Does not support custom device. 80 // 81 // 'retvals' must point to a pre-allocated array of TensorHandle* and 82 // '*num_retvals' should be set to the size of this array. It is an error if 83 // the size of 'retvals' is less than the number of outputs. This call sets 84 // *num_retvals to the number of outputs. 85 void EagerLocalExecuteAsync(EagerOperation* op, TensorHandle** retvals, 86 int* num_retvals, StatusCallback done); 87 88 } // namespace tensorflow 89 90 #endif // TENSORFLOW_CORE_COMMON_RUNTIME_EAGER_EXECUTE_H_ 91