• 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 #ifndef TENSORFLOW_CORE_COMMON_RUNTIME_EAGER_COPY_TO_DEVICE_NODE_H_
16 #define TENSORFLOW_CORE_COMMON_RUNTIME_EAGER_COPY_TO_DEVICE_NODE_H_
17 
18 #include "tensorflow/core/common_runtime/device.h"
19 #include "tensorflow/core/common_runtime/eager/eager_executor.h"
20 #include "tensorflow/core/common_runtime/eager/tensor_handle.h"
21 #include "tensorflow/core/framework/tensor.h"
22 #include "tensorflow/core/lib/core/status.h"
23 
24 namespace tensorflow {
25 
26 class CopyToDeviceNode : public EagerNode {
27  public:
CopyToDeviceNode(TensorHandle * src,Device * dstd,EagerContext * ctx)28   CopyToDeviceNode(TensorHandle* src, Device* dstd, EagerContext* ctx)
29       : EagerNode(ctx->NextId()),
30         src_(src),
31         dstd_(dstd),
32         ctx_(ctx),
33         dst_(new TensorHandle(id, dstd_, dstd_, nullptr, src->dtype, ctx)) {
34     src_->Ref();
35     dst_->Ref();
36   }
37 
~CopyToDeviceNode()38   ~CopyToDeviceNode() override {
39     src_->Unref();
40     dst_->Unref();
41   }
42 
Run()43   Status Run() override {
44     TensorHandle* temp = nullptr;
45     TF_RETURN_IF_ERROR(src_->CopyToDevice(ctx_, dstd_, &temp));
46     const Tensor* tensor = nullptr;
47     Status status = temp->Tensor(&tensor);
48     // `temp` is a ready handle. So the following call should return OK.
49     TF_DCHECK_OK(status) << status.error_message();
50     DCHECK(tensor);
51     dst_->SetTensor(*tensor);
52     temp->Unref();
53     return Status::OK();
54   }
55 
dst()56   TensorHandle* dst() { return dst_; }
57 
58  private:
59   TensorHandle* src_;
60   Device* dstd_;
61   EagerContext* ctx_;
62   TensorHandle* dst_;
63 };
64 
65 }  // namespace tensorflow
66 
67 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_EAGER_COPY_TO_DEVICE_NODE_H_
68