• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_EAGER_CLIENT_H_
17 #define TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_EAGER_EAGER_CLIENT_H_
18 
19 #include "tensorflow/core/distributed_runtime/call_options.h"
20 #include "tensorflow/core/lib/core/refcount.h"
21 #include "tensorflow/core/lib/core/status.h"
22 #include "tensorflow/core/platform/env.h"
23 #include "tensorflow/core/protobuf/eager_service.pb.h"
24 
25 namespace tensorflow {
26 namespace eager {
27 
28 // This is a base class that can be implemented by a variety of
29 // transports (e.g. gRPC which for each of the client methods makes an RPC).
30 class EagerClient : public core::RefCounted {
31  public:
~EagerClient()32   ~EagerClient() override {}
33 #define CLIENT_METHOD(method)                                \
34   virtual void method##Async(const method##Request* request, \
35                              method##Response* response,     \
36                              StatusCallback done) = 0;
37 
38   CLIENT_METHOD(CreateContext);
39   CLIENT_METHOD(UpdateContext);
40   CLIENT_METHOD(WaitQueueDone);
41   CLIENT_METHOD(KeepAlive);
42   CLIENT_METHOD(CloseContext);
43 
44 #undef CLIENT_METHOD
45 
46 #define CLIENT_CANCELABLE_METHOD(method)                      \
47   virtual void method##Async(                                 \
48       CallOptions* call_opts, const method##Request* request, \
49       method##Response* response, StatusCallback done) = 0;
50 
51   CLIENT_CANCELABLE_METHOD(Enqueue);
52   CLIENT_CANCELABLE_METHOD(RunComponentFunction);
53 
54 #undef CLIENT_CANCELABLE_METHOD
55 
56   // Feeds `request` into the request stream of EagerService::StreamingEnqueue.
57   // `response` will be filled with the response for this `request`. The
58   // 1-to-1 correspondence between requests and responses is a property
59   // of the current service implementation. When the response is received,
60   // `done` is invoked with the current status of the StreamingEnqueue call.
61   // The status can contain an error because of an earlier request in the
62   // current streaming call.
63   // The client initiates a streaming call the first time StreamingEnqueueAsync
64   // is invoked and keeps it open until some error condition.
65   // Similarly to the methods above, the request can be deleted as soon as
66   // StreamingEnqueueAsync returns.
67   virtual void StreamingEnqueueAsync(CallOptions* call_opts,
68                                      const EnqueueRequest* request,
69                                      EnqueueResponse* response,
70                                      StatusCallback done) = 0;
71 
72   virtual bool allow_multiple_pending_requests() const = 0;
73 };
74 
75 // Simple wrapper class that can be used to retrieve EagerClients.
76 class EagerClientCache {
77  public:
~EagerClientCache()78   virtual ~EagerClientCache() {}
79 
80   // If the `target` exists, assign the EagerClient pointer to `client` and
81   // increment the refcount of the client. The reference ownership is
82   // transferred to the caller, and the unref should automatically happen when
83   // destructing the RefCountPtr object from the caller's side.
84   virtual Status GetClient(const string& target,
85                            core::RefCountPtr<EagerClient>* client) = 0;
86 };
87 
88 }  // namespace eager
89 }  // namespace tensorflow
90 
91 #endif  // TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_EAGER_EAGER_CLIENT_H_
92