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 // RpcOp is a TensorFlow op that sends and receives arbitrary messages. 17 // 18 // See docs in ../ops/rpc_op.cc. 19 20 #include <memory> 21 #include <string> 22 #include <vector> 23 24 #include "third_party/eigen3/Eigen/Core" 25 #include "tensorflow/core/framework/op_kernel.h" 26 #include "tensorflow/core/framework/tensor_types.h" 27 #include "tensorflow/core/lib/core/errors.h" 28 #include "tensorflow/core/lib/core/refcount.h" 29 #include "tensorflow/core/lib/gtl/stl_util.h" 30 #include "tensorflow/core/lib/strings/stringprintf.h" 31 #include "tensorflow/core/platform/env.h" 32 #include "tensorflow/core/platform/logging.h" 33 #include "tensorflow/core/util/rpc/call_container.h" 34 #include "tensorflow/core/util/rpc/rpc_factory.h" 35 #include "tensorflow/core/util/rpc/rpc_factory_registry.h" 36 37 namespace tensorflow { 38 39 class RpcOp : public AsyncOpKernel { 40 public: 41 explicit RpcOp(OpKernelConstruction* context) : AsyncOpKernel(context) { 42 OP_REQUIRES_OK(context, context->GetAttr("protocol", &protocol_)); 43 OP_REQUIRES(context, !protocol_.empty(), 44 errors::InvalidArgument("protocol must be non-empty.")); 45 bool fail_fast; 46 OP_REQUIRES_OK(context, context->GetAttr("fail_fast", &fail_fast)); 47 int64 timeout_in_ms; 48 OP_REQUIRES_OK(context, context->GetAttr("timeout_in_ms", &timeout_in_ms)); 49 50 RPCFactoryRegistry::RPCFactoryFn* rpc_factory_fn = 51 RPCFactoryRegistry::Global()->Get(protocol_); 52 OP_REQUIRES(context, rpc_factory_fn != nullptr, 53 errors::InvalidArgument("The protocol ", protocol_, 54 " was not recognized.")); 55 56 rpc_factory_.reset((*rpc_factory_fn)(context, fail_fast, timeout_in_ms)); 57 } 58 59 ~RpcOp() override {} 60 61 void ComputeAsync(OpKernelContext* ctx, DoneCallback done) override { 62 const Tensor& address_t = ctx->input(0); 63 const Tensor& method_t = ctx->input(1); 64 const Tensor& request_t = ctx->input(2); 65 66 OP_REQUIRES_ASYNC( 67 ctx, address_t.dims() == 0 || address_t.dims() == 1, 68 errors::InvalidArgument("address must be a scalar or vector."), done); 69 OP_REQUIRES_ASYNC( 70 ctx, method_t.dims() == 0 || method_t.dims() == 1, 71 errors::InvalidArgument("method must be a scalar or vector."), done); 72 OP_REQUIRES_ASYNC( 73 ctx, request_t.dims() == 0 || request_t.dims() == 1, 74 errors::InvalidArgument("request must be a scalar or vector."), done); 75 76 TensorShape output_shape({}); 77 for (const Tensor& t : {address_t, method_t, request_t}) { 78 if (t.dims() == 1) { 79 OP_REQUIRES_ASYNC( 80 ctx, 81 output_shape.dims() == 0 || 82 output_shape.dim_size(0) == t.dim_size(0), 83 errors::InvalidArgument( 84 "Input vector shapes don't match: ", output_shape.DebugString(), 85 " vs. ", t.shape().DebugString()), 86 done); 87 output_shape = t.shape(); 88 } 89 } 90 91 Tensor* response_t; 92 OP_REQUIRES_OK_ASYNC( 93 ctx, ctx->allocate_output(0, output_shape, &response_t), done); 94 95 const bool try_rpc = (ctx->num_outputs() > 1); 96 97 Tensor* status_code_t = nullptr; 98 Tensor* status_message_t = nullptr; 99 if (try_rpc) { 100 OP_REQUIRES_OK_ASYNC( 101 ctx, ctx->allocate_output(1, output_shape, &status_code_t), done); 102 OP_REQUIRES_OK_ASYNC( 103 ctx, ctx->allocate_output(2, output_shape, &status_message_t), done); 104 } 105 106 if (request_t.NumElements() == 0) { 107 // Special case, we finished early! 108 done(); 109 return; 110 } 111 112 int64 num_elements = output_shape.num_elements(); 113 114 rpc_factory_->Call(ctx, num_elements, address_t, method_t, request_t, 115 try_rpc, response_t, status_code_t, status_message_t, 116 std::move(done)); 117 } 118 119 private: 120 string protocol_; 121 std::unique_ptr<RPCFactory> rpc_factory_; 122 123 TF_DISALLOW_COPY_AND_ASSIGN(RpcOp); 124 }; 125 126 REGISTER_KERNEL_BUILDER(Name("Rpc").Device(DEVICE_CPU), RpcOp); 127 REGISTER_KERNEL_BUILDER(Name("TryRpc").Device(DEVICE_CPU), RpcOp); 128 129 } // namespace tensorflow 130