1 /* Copyright 2020 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_CC_EXPERIMENTAL_BASE_PUBLIC_TENSORHANDLE_H_
17 #define TENSORFLOW_CC_EXPERIMENTAL_BASE_PUBLIC_TENSORHANDLE_H_
18
19 #include <memory>
20 #include <vector>
21
22 #include "tensorflow/c/eager/c_api.h"
23 #include "tensorflow/c/eager/c_api_experimental.h"
24 #include "tensorflow/cc/experimental/base/public/runtime.h"
25 #include "tensorflow/cc/experimental/base/public/status.h"
26 #include "tensorflow/cc/experimental/base/public/tensor.h"
27
28 namespace tensorflow {
29 namespace experimental {
30 namespace cc {
31
32 // An opaque representation of a tensor computed/managed by the Tensorflow
33 // runtime (tensorflow:cc::Runtime). Unlike a tensor, a Tensorhandle may refer
34 // to tensors placed in memory of different devices or remote address spaces.
35 // Note that tensorflow::cc::Runtime MUST outlive all TensorHandles created
36 // from it.
37 class TensorHandle {
38 public:
39 // Unwraps a Tensor from the given TensorHandle. If an error occurred,
40 // status->ok() will be false, and the returned Tensor must not be used.
41 Tensor Resolve(Status* status);
42
43 // Constructs a TensorHandle from a Tensor. If an error occurred,
44 // status->ok() will be false, and the returned TensorHandle must not be used.
45 static TensorHandle FromTensor(const Tensor& tensor, const Runtime& runtime,
46 Status* status);
47
48 // TensorHandle is movable, and not copyable
49 TensorHandle(TensorHandle&&) = default;
50 TensorHandle& operator=(TensorHandle&&) = default;
51
52 private:
53 // Wraps a TFE_TensorHandle. Takes ownership of handle.
TensorHandle(TFE_TensorHandle * handle)54 explicit TensorHandle(TFE_TensorHandle* handle) : handle_(handle) {}
55
56 // TensorHandle is not copyable
57 TensorHandle(const TensorHandle&) = delete;
58 TensorHandle& operator=(const TensorHandle&) = delete;
59
60 // Returns the underlying TFE_TensorHandle that this object wraps.
61 // This object retains ownership of the pointer.
GetTFETensorHandle()62 TFE_TensorHandle* GetTFETensorHandle() const { return handle_.get(); }
63
64 // Deletes the currently wrapped TFE_TensorHandle, and swaps it with handle,
65 // and takes ownership of handle.
Reset(TFE_TensorHandle * handle)66 void Reset(TFE_TensorHandle* handle) { handle_.reset(handle); }
67
68 struct TFETensorHandleDeleter {
operatorTFETensorHandleDeleter69 void operator()(TFE_TensorHandle* p) const { TFE_DeleteTensorHandle(p); }
70 };
71 std::unique_ptr<TFE_TensorHandle, TFETensorHandleDeleter> handle_;
72 };
73
Resolve(Status * status)74 inline Tensor TensorHandle::Resolve(Status* status) {
75 TF_Tensor* tensor =
76 TFE_TensorHandleResolve(handle_.get(), status->GetTFStatus());
77 if (!status->ok()) {
78 return Tensor(nullptr);
79 }
80 return Tensor(tensor);
81 }
82
FromTensor(const Tensor & tensor,const Runtime & runtime,Status * status)83 inline TensorHandle TensorHandle::FromTensor(const Tensor& tensor,
84 const Runtime& runtime,
85 Status* status) {
86 TFE_TensorHandle* tensor_handle = TFE_NewTensorHandleFromTensor(
87 runtime.GetTFEContext(), tensor.GetTFTensor(), status->GetTFStatus());
88 if (!status->ok()) {
89 return TensorHandle(nullptr);
90 }
91 return TensorHandle(tensor_handle);
92 }
93
94 } // namespace cc
95 } // namespace experimental
96 } // namespace tensorflow
97
98 #endif // TENSORFLOW_CC_EXPERIMENTAL_BASE_PUBLIC_TENSORHANDLE_H_
99