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_ABSTRACT_TENSOR_HANDLE_H_ 16 #define TENSORFLOW_C_EAGER_ABSTRACT_TENSOR_HANDLE_H_ 17 18 #include <memory> 19 20 #include "tensorflow/core/framework/tensor_shape.h" 21 #include "tensorflow/core/framework/types.pb.h" 22 #include "tensorflow/core/platform/refcount.h" 23 #include "tensorflow/core/platform/status.h" 24 namespace tensorflow { 25 26 // Abstract interface to a Tensor handle in either tracing or immediate 27 // execution mode. 28 class AbstractTensorHandle : public core::RefCounted { 29 protected: 30 enum AbstractTensorHandleKind { kGraph, kMlir, kEager, kTfrt, kCustomDevice }; AbstractTensorHandle(AbstractTensorHandleKind kind)31 explicit AbstractTensorHandle(AbstractTensorHandleKind kind) : kind_(kind) {} ~AbstractTensorHandle()32 virtual ~AbstractTensorHandle() {} 33 34 public: 35 // Returns tensor dtype. 36 virtual tensorflow::DataType DataType() const = 0; 37 // Returns tensor shape. If tensor has unknown rank, shape remains untouched. 38 virtual tensorflow::Status Shape( 39 tensorflow::PartialTensorShape* shape) const = 0; 40 41 // The default debug string includes a shape and dtype. Implementations are 42 // free to override it with something more informative. 43 virtual std::string DebugString() const; 44 getKind()45 AbstractTensorHandleKind getKind() const { return kind_; } 46 47 private: 48 const AbstractTensorHandleKind kind_; 49 }; 50 51 namespace internal { 52 struct AbstractTensorHandleDeleter { operatorAbstractTensorHandleDeleter53 void operator()(AbstractTensorHandle* p) const { 54 if (p != nullptr) { 55 p->Unref(); 56 } 57 } 58 }; 59 } // namespace internal 60 61 using AbstractTensorHandlePtr = 62 std::unique_ptr<AbstractTensorHandle, 63 internal::AbstractTensorHandleDeleter>; 64 65 } // namespace tensorflow 66 67 #endif // TENSORFLOW_C_EAGER_ABSTRACT_TENSOR_HANDLE_H_ 68