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 #ifndef TENSORFLOW_C_EAGER_IMMEDIATE_EXECUTION_TENSOR_HANDLE_H_
16 #define TENSORFLOW_C_EAGER_IMMEDIATE_EXECUTION_TENSOR_HANDLE_H_
17 
18 #include "tensorflow/c/eager/abstract_tensor_handle.h"
19 #include "tensorflow/c/tensor_interface.h"
20 #include "tensorflow/core/framework/types.pb.h"
21 #include "tensorflow/core/platform/status.h"
22 
23 namespace tensorflow {
24 
25 // Abstract interface to a TensorHandle.
26 //
27 // A TensorHandle is management class around a Tensor which may track additional
28 // metadata and synchronization.
29 //
30 // This allows us to hide concrete implementations of TensorHandle from header
31 // files. The interface lists the common functionality that must be provided by
32 // any concrete implementation. However, in cases where the true concrete class
33 // is needed a static_cast can be applied.
34 class ImmediateExecutionTensorHandle : public AbstractTensorHandle {
35  public:
36   // Returns number of dimensions.
37   virtual Status NumDims(int* num_dims) const = 0;
38   // Returns number of elements across all dimensions.
39   virtual Status NumElements(int64* num_elements) const = 0;
40   // Returns size of specified dimension
41   virtual Status Dim(int dim_index, int64* dim) const = 0;
42 
43   // Returns the device which created the handle.
44   virtual const char* DeviceName(Status* status) const = 0;
45   // Returns the device where the tensor was placed.
46   virtual const char* BackingDeviceName(Status* status) const = 0;
47   // Returns the device type which created the handle.
48   virtual const char* DeviceType(Status* status) const = 0;
49   // Returns the device ID which created the handle.
50   virtual int DeviceId(Status* status) const = 0;
51   // Returns a tensor for the handle. If tensor is remote, it will be copied.
52   virtual AbstractTensorInterface* Resolve(Status* status) = 0;
53 
54   // Return a copy of the handle.
55   virtual ImmediateExecutionTensorHandle* Copy() = 0;
56 
57   // Release any underlying resources, including the interface object.
58   //
59   // WARNING: The destructor of this class is marked as protected to disallow
60   // clients from directly destroying this object since it may manage it's own
61   // lifetime through ref counting. Thus this must be allocated on the heap and
62   // clients MUST call Release() in order to destroy an instance of this class.
63   virtual void Release() = 0;
64 
65   // For LLVM style RTTI.
classof(const AbstractTensorHandle * ptr)66   static bool classof(const AbstractTensorHandle* ptr) {
67     return ptr->getKind() == kEager || ptr->getKind() == kTfrt;
68   }
69 
70  protected:
ImmediateExecutionTensorHandle(AbstractTensorHandleKind kind)71   explicit ImmediateExecutionTensorHandle(AbstractTensorHandleKind kind)
72       : AbstractTensorHandle(kind) {}
~ImmediateExecutionTensorHandle()73   ~ImmediateExecutionTensorHandle() override {}
74 };
75 
76 namespace internal {
77 struct ImmediateExecutionTensorHandleDeleter {
operatorImmediateExecutionTensorHandleDeleter78   void operator()(ImmediateExecutionTensorHandle* p) const {
79     if (p != nullptr) {
80       p->Release();
81     }
82   }
83 };
84 }  // namespace internal
85 
86 using ImmediateTensorHandlePtr =
87     std::unique_ptr<ImmediateExecutionTensorHandle,
88                     internal::ImmediateExecutionTensorHandleDeleter>;
89 
90 }  // namespace tensorflow
91 
92 #endif  // TENSORFLOW_C_EAGER_IMMEDIATE_EXECUTION_TENSOR_HANDLE_H_
93