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_TENSOR_HANDLE_INTERFACE_H_ 16 #define TENSORFLOW_C_EAGER_TENSOR_HANDLE_INTERFACE_H_ 17 18 #include "tensorflow/c/c_api.h" 19 #include "tensorflow/c/eager/c_api.h" 20 #include "tensorflow/c/tf_datatype.h" 21 #include "tensorflow/core/common_runtime/eager/tensor_handle.h" 22 23 // Abstract interface to a TensorHandle. 24 // 25 // A TensorHandle is management class around a Tensor which may track additional 26 // metadata and synchronization. 27 // 28 // This allows us to hide concrete implementations of TensorHandle from header 29 // files. The interface lists the common functionality that must be provided by 30 // any concrete implementation. However, in cases where the true concrete class 31 // is needed a static_cast can be applied. 32 class AbstractTensorHandleInterface { 33 public: ~AbstractTensorHandleInterface()34 virtual ~AbstractTensorHandleInterface() {} 35 36 // Check if the handle is in a valid initialized state. 37 virtual bool IsValid(tensorflow::Status* status) const = 0; 38 // Returns tensor dtype. 39 virtual TF_DataType DataType() const = 0; 40 // Returns number of dimensions. 41 virtual int NumDims(tensorflow::Status* status) const = 0; 42 // Returns number of elements across all dimensions. 43 virtual int64_t NumElements(tensorflow::Status* status) const = 0; 44 // Returns size of specified dimension 45 virtual int64_t Dim(int dim_index, tensorflow::Status* status) const = 0; 46 47 // Returns the device which created the handle. 48 virtual const char* DeviceName(tensorflow::Status* status) const = 0; 49 // Returns the device where the tensor was placed. 50 virtual const char* BackingDeviceName(tensorflow::Status* status) const = 0; 51 // Returns a tensor for the handle. If tensor is remote, it will be copied. 52 virtual TF_Tensor* Resolve(tensorflow::Status* status) = 0; 53 // Returns debug information about the tensor. 54 virtual TFE_TensorDebugInfo* TensorDebugInfo(tensorflow::Status* status) = 0; 55 56 // Return a copy of the handle. 57 virtual AbstractTensorHandleInterface* Copy() = 0; 58 }; 59 60 namespace tensorflow { 61 62 class TensorHandleInterface : public AbstractTensorHandleInterface { 63 public: TensorHandleInterface(TensorHandle * h)64 explicit TensorHandleInterface(TensorHandle* h) : handle_(h) {} 65 ~TensorHandleInterface() override; 66 67 bool IsValid(Status* status) const override; 68 TF_DataType DataType() const override; 69 int NumDims(Status* status) const override; 70 int64_t NumElements(Status* status) const override; 71 int64_t Dim(int dim_index, Status* status) const override; 72 73 const char* DeviceName(Status* status) const override; 74 const char* BackingDeviceName(Status* status) const override; 75 TF_Tensor* Resolve(Status* status) override; 76 TFE_TensorDebugInfo* TensorDebugInfo(Status* status) override; 77 78 AbstractTensorHandleInterface* Copy() override; 79 80 // TODO(gjn): This is not a very generic interface, but is needed for specific 81 // use cases. Handle()82 TensorHandle* Handle() { return handle_; } 83 84 private: 85 TensorHandle* handle_; 86 }; 87 88 } // namespace tensorflow 89 90 #endif // TENSORFLOW_C_EAGER_TENSOR_HANDLE_INTERFACE_H_ 91