xref: /aosp_15_r20/external/tensorflow/tensorflow/c/eager/abstract_tensor_handle.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 
25 namespace tensorflow {
26 
27 // Abstract interface to a Tensor handle in either tracing or immediate
28 // execution mode.
29 class AbstractTensorHandle : public core::RefCounted {
30  protected:
31   enum AbstractTensorHandleKind { kGraph, kMlir, kEager, kTfrt, kCustomDevice };
AbstractTensorHandle(AbstractTensorHandleKind kind)32   explicit AbstractTensorHandle(AbstractTensorHandleKind kind) : kind_(kind) {}
~AbstractTensorHandle()33   ~AbstractTensorHandle() override {}
34 
35  public:
36   // Returns tensor dtype.
37   virtual tensorflow::DataType DataType() const = 0;
38 
39   // Returns the status of the tensor handle. If it is a tfrt::TensorHandle,
40   // the tensor handle can be an error and return non-OK status.
41   virtual tensorflow::Status TensorHandleStatus() const;
42 
43   // Returns tensor shape. If tensor has unknown rank, shape remains untouched.
44   virtual tensorflow::Status Shape(
45       tensorflow::PartialTensorShape* shape) const = 0;
46 
47   // The default debug string includes a shape and dtype. Implementations are
48   // free to override it with something more informative.
49   virtual std::string DebugString() const;
50 
getKind()51   AbstractTensorHandleKind getKind() const { return kind_; }
52 
53  private:
54   const AbstractTensorHandleKind kind_;
55 };
56 
57 namespace internal {
58 struct AbstractTensorHandleDeleter {
operatorAbstractTensorHandleDeleter59   void operator()(AbstractTensorHandle* p) const {
60     if (p != nullptr) {
61       p->Unref();
62     }
63   }
64 };
65 }  // namespace internal
66 
67 // TODO(b/185908092): Make AbstractTensorHandlePtr an IntrusivePtr.
68 using AbstractTensorHandlePtr =
69     std::unique_ptr<AbstractTensorHandle,
70                     internal::AbstractTensorHandleDeleter>;
71 
72 }  // namespace tensorflow
73 
74 #endif  // TENSORFLOW_C_EAGER_ABSTRACT_TENSOR_HANDLE_H_
75