xref: /aosp_15_r20/external/tensorflow/tensorflow/core/common_runtime/eager/copy_to_device_node.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 #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 #include "tensorflow/core/profiler/lib/scoped_memory_debug_annotation.h"
24 
25 namespace tensorflow {
26 
27 class CopyToDeviceNode : public EagerNode {
28  public:
CopyToDeviceNode(TensorHandle * src,TensorHandle * dst,Device * dstd,const EagerContext & ctx,bool async,bool mirror)29   CopyToDeviceNode(TensorHandle* src, TensorHandle* dst, Device* dstd,
30                    const EagerContext& ctx, bool async, bool mirror)
31       : EagerNode(),
32         src_(src),
33         dst_(dst),
34         dstd_(dstd),
35         ctx_(ctx),
36         async_(async),
37         mirror_(mirror) {
38     if (async_) {
39       src_->Ref();
40       dst_->Ref();
41     }
42   }
43 
~CopyToDeviceNode()44   ~CopyToDeviceNode() override {
45     if (async_) {
46       src_->Unref();
47       dst_->Unref();
48     }
49   }
50 
Run()51   Status Run() override {
52     tensorflow::Tensor tensor;
53     profiler::ScopedMemoryDebugAnnotation op_annotation(
54         "eager::CopyToDeviceNode", "dynamic", tensor.dtype(),
55         [&tensor]() { return tensor.shape().DebugString(); });
56     TF_RETURN_IF_ERROR(src_->CopyToDevice(ctx_, dstd_, &tensor));
57     if (!async_ && mirror_) {
58       Status s = dst_->AddLocalMirror(std::move(tensor), dstd_);
59       // If a mirror was added since we called HasLocalMirror then just return
60       // and ignore the error.
61       if (s.ok() || (s.code() == error::Code::ALREADY_EXISTS)) {
62         return OkStatus();
63       }
64       return s;
65     } else {
66       return dst_->SetTensor(std::move(tensor), dstd_);
67     }
68   }
69 
Abort(Status status)70   void Abort(Status status) override { dst_->Poison(status, dstd_); }
71 
DebugString()72   string DebugString() const override {
73     string out = "[CopyToDeviceNode]";
74     strings::StrAppend(&out, " src_tensor: ", src_->DebugString());
75     strings::StrAppend(&out, ", dst_tensor: ", dst_->DebugString());
76     strings::StrAppend(&out, ", dst_device: ", dstd_ ? dstd_->name() : "[]");
77     return out;
78   }
79 
dst()80   TensorHandle* dst() { return dst_; }
81 
82  private:
83   TensorHandle* src_;
84   TensorHandle* dst_;
85   Device* dstd_;
86   const EagerContext& ctx_;
87   bool async_;
88   bool mirror_;
89 };
90 
91 }  // namespace tensorflow
92 
93 #endif  // TENSORFLOW_CORE_COMMON_RUNTIME_EAGER_COPY_TO_DEVICE_NODE_H_
94