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 "tensorflow/core/common_runtime/eager/eager_executor.h" 20 #include "tensorflow/core/common_runtime/eager/tensor_handle.h" 21 #include "tensorflow/core/distributed_runtime/eager/eager_client.h" 22 #include "tensorflow/core/protobuf/eager_service.pb.h" 23 24 namespace tensorflow { 25 namespace eager { 26 27 // EnqueueNode is an implementation of EagerNode which enqueues an operation 28 // via RPC in a remote EagerService. 29 class RemoteExecuteNode : public tensorflow::EagerNode { 30 public: RemoteExecuteNode(tensorflow::uint64 id,std::unique_ptr<EnqueueRequest> request,EagerClient * eager_client,const gtl::InlinedVector<TensorHandle *,4> & inputs,std::function<void (const Status & status,const EnqueueResponse & response)> done_callback)31 RemoteExecuteNode( 32 tensorflow::uint64 id, std::unique_ptr<EnqueueRequest> request, 33 EagerClient* eager_client, 34 const gtl::InlinedVector<TensorHandle*, 4>& inputs, 35 std::function<void(const Status& status, const EnqueueResponse& response)> 36 done_callback) 37 : tensorflow::EagerNode(id), 38 request_(std::move(request)), 39 eager_client_(eager_client), 40 inputs_(inputs), 41 done_callback_(std::move(done_callback)) { 42 for (auto* handle : inputs_) { 43 handle->Ref(); 44 } 45 } 46 RemoteExecuteNode(tensorflow::uint64 id,std::unique_ptr<EnqueueRequest> request,EagerClient * eager_client)47 RemoteExecuteNode(tensorflow::uint64 id, 48 std::unique_ptr<EnqueueRequest> request, 49 EagerClient* eager_client) 50 : tensorflow::EagerNode(id), 51 request_(std::move(request)), 52 eager_client_(eager_client) {} 53 ~RemoteExecuteNode()54 ~RemoteExecuteNode() { 55 for (auto* handle : inputs_) { 56 handle->Unref(); 57 } 58 } 59 Run()60 tensorflow::Status Run() override { 61 EnqueueResponse response; 62 Status status; 63 Notification n; 64 eager_client_->EnqueueAsync(request_.get(), &response, 65 [&n, &status](const tensorflow::Status& s) { 66 status.Update(s); 67 n.Notify(); 68 }); 69 n.WaitForNotification(); 70 71 if (done_callback_) { 72 done_callback_(status, response); 73 } 74 75 return status; 76 } 77 78 private: 79 std::unique_ptr<EnqueueRequest> request_; 80 EagerClient* eager_client_; // Not owned, and must outlive this node. 81 82 // This is required to ensure that the tensor handles stay alive across the 83 // execution. 84 gtl::InlinedVector<TensorHandle*, 4> inputs_; 85 86 std::function<void(const Status& status, const EnqueueResponse& response)> 87 done_callback_; 88 }; 89 90 } // namespace eager 91 } // namespace tensorflow 92 93 #endif // TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_EAGER_REMOTE_EXECUTE_NODE_H_ 94