1 /* Copyright 2016 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_RPC_GRPC_WORKER_SERVICE_IMPL_H_ 17 #define TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_RPC_GRPC_WORKER_SERVICE_IMPL_H_ 18 19 #include "grpcpp/impl/codegen/async_stream.h" 20 #include "grpcpp/impl/codegen/async_unary_call.h" 21 #include "grpcpp/impl/codegen/proto_utils.h" 22 #include "grpcpp/impl/codegen/rpc_method.h" 23 #include "grpcpp/impl/codegen/service_type.h" 24 #include "grpcpp/impl/codegen/status.h" 25 #include "grpcpp/impl/codegen/stub_options.h" 26 #include "grpcpp/impl/codegen/sync_stream.h" 27 #include "grpcpp/support/byte_buffer.h" 28 29 #include "tensorflow/core/distributed_runtime/rpc/grpc_util.h" 30 #include "tensorflow/core/distributed_runtime/tensor_coding.h" 31 #include "tensorflow/core/protobuf/worker.pb.h" 32 33 namespace grpc { 34 35 // Support parsing/unparsing of tensorflow::TensorResponse. 36 // Wire-format is identical to RecvTensorResponse. 37 // This is specializing an existing template, so it's okay to do this in a 38 // namespace that we don't own. 39 template <> 40 class SerializationTraits<tensorflow::TensorResponse> { 41 public: Serialize(const tensorflow::TensorResponse & msg,ByteBuffer * bp,bool * own_buffer)42 static Status Serialize(const tensorflow::TensorResponse& msg, ByteBuffer* bp, 43 bool* own_buffer) { 44 LOG(FATAL) << "TODO(sanjay,jeff): Implement"; 45 return Status(); 46 } Deserialize(ByteBuffer * buffer,tensorflow::TensorResponse * msg)47 static Status Deserialize(ByteBuffer* buffer, 48 tensorflow::TensorResponse* msg) { 49 if (buffer == nullptr) { 50 return Status(StatusCode::INTERNAL, "No payload"); 51 } 52 Status result = Status::OK; 53 if (result.ok()) { 54 ::tensorflow::GrpcByteSource source(buffer); 55 auto s = msg->ParseFrom(&source); 56 if (!s.ok()) { 57 result = Status(StatusCode::INTERNAL, 58 ::tensorflow::strings::StrCat( 59 "TensorResponse parse error", s.ToString())); 60 } 61 } 62 buffer->Clear(); 63 return result; 64 } 65 }; 66 67 } // namespace grpc 68 69 namespace tensorflow { 70 71 // Names of worker methods. 72 enum class GrpcWorkerMethod { 73 kGetStatus, 74 kCreateWorkerSession, 75 kDeleteWorkerSession, 76 kRegisterGraph, 77 kDeregisterGraph, 78 kRunGraph, 79 kCleanupGraph, 80 kCleanupAll, 81 kRecvTensor, 82 kRecvBuf, 83 kLogging, 84 kTracing, 85 kCompleteGroup, 86 kCompleteInstance, 87 kGetStepSequence, 88 }; 89 90 static const int kGrpcNumWorkerMethods = 91 static_cast<int>(GrpcWorkerMethod::kGetStepSequence) + 1; 92 93 const char* GrpcWorkerMethodName(GrpcWorkerMethod id); 94 95 namespace grpc { 96 97 // Implementation of `tensorflow.WorkerService`, based on the 98 // definition in "//tensorflow/core/protobuf/worker_service.proto", 99 // and the gRPC generated stub and service classes. 100 // See the proto file for the definition of methods and messages. 101 class WorkerService final { 102 public: 103 class AsyncService : public ::grpc::Service { 104 public: 105 AsyncService(); 106 virtual ~AsyncService(); 107 108 // Make RequestAsyncUnary public for grpc_call.h 109 using ::grpc::Service::RequestAsyncUnary; 110 }; 111 }; 112 113 } // namespace grpc 114 115 } // namespace tensorflow 116 117 #endif // TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_RPC_GRPC_WORKER_SERVICE_IMPL_H_ 118