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 16 #ifndef TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_EAGER_REMOTE_EXECUTE_NODE_H_ 17 #define TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_EAGER_REMOTE_EXECUTE_NODE_H_ 18 19 #include <cstddef> 20 21 #include "absl/types/span.h" 22 #include "tensorflow/core/common_runtime/device.h" 23 #include "tensorflow/core/common_runtime/eager/eager_executor.h" 24 #include "tensorflow/core/common_runtime/eager/shape_inference.h" 25 #include "tensorflow/core/common_runtime/eager/tensor_handle.h" 26 #include "tensorflow/core/distributed_runtime/eager/eager_client.h" 27 #include "tensorflow/core/framework/function.h" 28 #include "tensorflow/core/framework/node_def.pb.h" 29 #include "tensorflow/core/lib/gtl/inlined_vector.h" 30 #include "tensorflow/core/protobuf/eager_service.pb.h" 31 32 namespace tensorflow { 33 namespace eager { 34 35 // RemoteExecuteNode is an implementation of EagerNode which enqueues 36 // an operation via RPC in a remote EagerService. 37 class RemoteExecuteNode : public AsyncEagerNode { 38 public: RemoteExecuteNode(std::unique_ptr<EnqueueRequest> request,Device * device,EagerClient * eager_client,const NodeDef & ndef,FunctionLibraryDefinition * lib_def,const gtl::InlinedVector<TensorHandle *,4> & inputs,absl::Span<TensorHandle * > retvals)39 RemoteExecuteNode(std::unique_ptr<EnqueueRequest> request, Device* device, 40 EagerClient* eager_client, const NodeDef& ndef, 41 FunctionLibraryDefinition* lib_def, 42 const gtl::InlinedVector<TensorHandle*, 4>& inputs, 43 absl::Span<TensorHandle*> retvals) 44 : AsyncEagerNode(), 45 request_(std::move(request)), 46 device_(device), 47 eager_client_(eager_client), 48 ndef_(ndef), 49 lib_def_(lib_def), 50 inputs_(inputs) { 51 // Copy the output handles, since the container for them might get 52 // destroyed. 53 for (auto handle : retvals) { 54 handle->Ref(); 55 retvals_.push_back(handle); 56 } 57 58 // This is required to ensure that the tensor handles stay alive across the 59 // execution. 60 for (auto handle : inputs_) { 61 handle->Ref(); 62 } 63 eager_client_->Ref(); 64 } 65 ~RemoteExecuteNode()66 ~RemoteExecuteNode() override { 67 for (auto handle : retvals_) { 68 handle->Unref(); 69 } 70 71 for (auto handle : inputs_) { 72 handle->Unref(); 73 } 74 eager_client_->Unref(); 75 } 76 Prepare()77 Status Prepare() override { 78 return RunShapeInference(ndef_, *lib_def_, inputs_, retvals_); 79 } 80 81 void RunAsync(StatusCallback done) override; 82 Abort(Status status)83 void Abort(Status status) override { 84 for (auto handle : retvals_) { 85 handle->Poison(status); 86 } 87 } 88 DebugString()89 string DebugString() const override { 90 string out = "[RemoteExecuteNode]"; 91 strings::StrAppend(&out, " request: ", request_->DebugString()); 92 strings::StrAppend(&out, ", target_device: ", device_->name()); 93 return out; 94 } 95 96 private: 97 std::unique_ptr<EnqueueRequest> request_; 98 Device* device_; // Not owned 99 EagerClient* eager_client_; // Not owned, and must outlive this node. 100 const NodeDef ndef_; 101 const FunctionLibraryDefinition* lib_def_; 102 gtl::InlinedVector<TensorHandle*, 4> inputs_; 103 gtl::InlinedVector<TensorHandle*, 2> retvals_; 104 }; 105 106 } // namespace eager 107 } // namespace tensorflow 108 109 #endif // TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_EAGER_REMOTE_EXECUTE_NODE_H_ 110