aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/distributed_runtime/eager/eager_service_impl.h
blob: b0e4aa84b9e9b84f54644c673c3bcbe4bcd55395 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_EAGER_EAGER_SERVICE_IMPL_H_
#define TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_EAGER_EAGER_SERVICE_IMPL_H_

#include <unordered_map>

#include "tensorflow/core/common_runtime/eager/context.h"
#include "tensorflow/core/common_runtime/eager/tensor_handle.h"
#include "tensorflow/core/distributed_runtime/eager/remote_tensor_handle.h"
#include "tensorflow/core/distributed_runtime/worker_env.h"
#include "tensorflow/core/lib/core/refcount.h"
#include "tensorflow/core/lib/gtl/array_slice.h"
#include "tensorflow/core/lib/strings/stringprintf.h"
#include "tensorflow/core/protobuf/eager_service.pb.h"

namespace tensorflow {
namespace eager {

// A TensorFlow Eager Worker runs ops and supports worker to worker
// Tensor transfer.
//
// See eager_service.proto for more details about each method.
// This class can be wrapped by specific classes that implement rpc transports
// over this (e.g. gRPC).
class EagerServiceImpl {
 public:
  explicit EagerServiceImpl(const WorkerEnv* env) : env_(env) {}
  virtual ~EagerServiceImpl() {
    for (auto& entry : contexts_) {
      entry.second->Unref();
    }
  }

  Status CreateContext(const CreateContextRequest* request,
                       CreateContextResponse* response);

  Status Enqueue(const EnqueueRequest* request, EnqueueResponse* response);

  Status WaitQueueDone(const WaitQueueDoneRequest* request,
                       WaitQueueDoneResponse* response);

  Status KeepAlive(const KeepAliveRequest* request,
                   KeepAliveResponse* response);

  Status CloseContext(const CloseContextRequest* request,
                      CloseContextResponse* response);

  Status RegisterFunction(const RegisterFunctionRequest* request,
                          RegisterFunctionResponse* response);

 protected:
  // This is the server-side execution context. All state regarding execution of
  // a client's ops is held in this server-side context (all generated tensors,
  // and the EagerContext).
  class ServerContext : public core::RefCounted {
   public:
    explicit ServerContext(std::unique_ptr<tensorflow::EagerContext> ctx)
        : ctx_(std::move(ctx)) {}
    ~ServerContext() {
      for (const auto& entry : tensors_) {
        entry.second->Unref();
      }
    }

    tensorflow::EagerContext* Context() const { return ctx_.get(); }

    void AddOperationOutputs(
        const gtl::ArraySlice<tensorflow::TensorHandle*>& handles,
        int64 operation_id) {
      mutex_lock l(tensors_mu_);
      for (int i = 0; i < handles.size(); i++) {
        // TODO(nareshmodi): Correctly handle operation_id not being unique.
        tensors_.emplace(RemoteTensorHandleInternal(operation_id, i),
                         handles[i]);
      }
    }

    Status GetTensorHandle(const RemoteTensorHandleInternal& remote_handle,
                           tensorflow::TensorHandle** handle) {
      mutex_lock l(tensors_mu_);
      auto iter = tensors_.find(remote_handle);
      if (iter == tensors_.end()) {
        return errors::InvalidArgument(
            "Unable to find the relevant tensor remote_handle: Op ID: ",
            remote_handle.op_id, ", Output num: ", remote_handle.output_num);
      }

      *handle = iter->second;

      return Status::OK();
    }

    Status DeleteTensorHandle(const RemoteTensorHandleInternal& remote_handle) {
      mutex_lock l(tensors_mu_);
      auto iter = tensors_.find(remote_handle);
      if (iter == tensors_.end()) {
        return errors::InvalidArgument(
            "Unable to find the relevant tensor remote_handle: Op ID: ",
            remote_handle.op_id, ", Output num: ", remote_handle.output_num);
      }

      iter->second->Unref();
      tensors_.erase(iter);

      return Status::OK();
    }

   private:
    using RemoteTensorHandleMap =
        gtl::FlatMap<RemoteTensorHandleInternal, tensorflow::TensorHandle*,
                     RemoteTensorHandleInternalHash,
                     RemoteTensorHandleInternalEquals>;

    // The context for this execution.
    std::unique_ptr<tensorflow::EagerContext> ctx_;

    mutex tensors_mu_;
    RemoteTensorHandleMap tensors_ GUARDED_BY(tensors_mu_);
  };
  // The returned ServerContext will need to be Unrefed.
  tensorflow::Status GetServerContext(uint64, ServerContext**);

 private:
  Status ExecuteOp(const Operation& operation, ServerContext* server_context,
                   QueueResponse* queue_response);
  const WorkerEnv* const env_;  // Not owned.

  mutex contexts_mu_;
  std::unordered_map<uint64, ServerContext*> contexts_ GUARDED_BY(contexts_mu_);

  TF_DISALLOW_COPY_AND_ASSIGN(EagerServiceImpl);
};

}  // namespace eager
}  // namespace tensorflow

#endif  // TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_EAGER_EAGER_SERVICE_IMPL_H_