xref: /aosp_15_r20/external/tensorflow/tensorflow/core/distributed_runtime/eager/eager_client.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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/platform/refcount.h"
21 #include "tensorflow/core/platform/status.h"
22 #include "tensorflow/core/protobuf/eager_service.pb.h"
23 
24 namespace tensorflow {
25 namespace eager {
26 
27 // This is a base class that can be implemented by a variety of
28 // transports (e.g. gRPC which for each of the client methods makes an RPC).
29 class EagerClient : public core::RefCounted {
30  public:
~EagerClient()31   ~EagerClient() override {}
32 #define CLIENT_METHOD(method)                                \
33   virtual void method##Async(const method##Request* request, \
34                              method##Response* response,     \
35                              StatusCallback done) = 0;
36 
37   CLIENT_METHOD(CreateContext);
38   CLIENT_METHOD(UpdateContext);
39   CLIENT_METHOD(WaitQueueDone);
40   CLIENT_METHOD(KeepAlive);
41   CLIENT_METHOD(CloseContext);
42 
43 #undef CLIENT_METHOD
44 
45 #define CLIENT_CANCELABLE_METHOD(method)                      \
46   virtual void method##Async(                                 \
47       CallOptions* call_opts, const method##Request* request, \
48       method##Response* response, StatusCallback done) = 0;
49 
50   CLIENT_CANCELABLE_METHOD(Enqueue);
51   CLIENT_CANCELABLE_METHOD(RunComponentFunction);
52 
53 #undef CLIENT_CANCELABLE_METHOD
54 
55   // Feeds `request` into the request stream of EagerService::StreamingEnqueue.
56   // `response` will be filled with the response for this `request`. The
57   // 1-to-1 correspondence between requests and responses is a property
58   // of the current service implementation. When the response is received,
59   // `done` is invoked with the current status of the StreamingEnqueue call.
60   // The status can contain an error because of an earlier request in the
61   // current streaming call.
62   // The client initiates a streaming call the first time StreamingEnqueueAsync
63   // is invoked and keeps it open until some error condition.
64   // Similarly to the methods above, the request can be deleted as soon as
65   // StreamingEnqueueAsync returns.
66   virtual void StreamingEnqueueAsync(bool enable_streaming_enqueue,
67                                      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