xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/delegates/gpu/common/tensor.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2019 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_LITE_DELEGATES_GPU_COMMON_TENSOR_H_
17 #define TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TENSOR_H_
18 
19 #include <stdint.h>
20 
21 #include <vector>
22 
23 #include "tensorflow/lite/delegates/gpu/common/data_type.h"
24 #include "tensorflow/lite/delegates/gpu/common/shape.h"
25 
26 namespace tflite {
27 namespace gpu {
28 namespace internal_tensor {
29 
30 // Meta function given element type returns a type for Tensor data container.
31 template <DataType Type>
32 struct StorageType;
33 
34 template <>
35 struct StorageType<DataType::FLOAT32> {
36   using value = std::vector<float>;
37 };
38 
39 template <>
40 struct StorageType<DataType::INT32> {
41   using value = std::vector<int32_t>;
42 };
43 
44 template <>
45 struct StorageType<DataType::INT16> {
46   using value = std::vector<int16_t>;
47 };
48 
49 template <>
50 struct StorageType<DataType::INT8> {
51   using value = std::vector<int8_t>;
52 };
53 
54 template <>
55 struct StorageType<DataType::UINT32> {
56   using value = std::vector<uint32_t>;
57 };
58 
59 template <>
60 struct StorageType<DataType::UINT16> {
61   using value = std::vector<uint16_t>;
62 };
63 
64 template <>
65 struct StorageType<DataType::UINT8> {
66   using value = std::vector<uint8_t>;
67 };
68 
69 template <>
70 struct StorageType<DataType::BOOL> {
71   using value = std::vector<uint8_t>;
72 };
73 
74 }  // namespace internal_tensor
75 
76 template <typename ShapeT, DataType Type>
77 struct Tensor {
78   using ShapeType = ShapeT;
79 
80   constexpr static DataType kType = Type;
81 
82   using TensorStorageType = typename internal_tensor::StorageType<Type>::value;
83 
84   // Opaque id of a tensor.
85   int64_t id = -1;
86 
87   ShapeType shape;
88 
89   TensorStorageType data;
90 };
91 
92 // TensorRef is a reference to another tensor. If an object should never hold
93 // tensor data, then TensorRef should be used instead.
94 template <typename ShapeT>
95 struct TensorRef {
96   using ShapeType = ShapeT;
97 
98   DataType type = DataType::UNKNOWN;
99 
100   ShapeT shape;
101 
102   // Opaque reference to a tensor. Upstream component is responsible for
103   // resolving this reference into an actual tensor.
104   int64_t ref = -1;
105 
106   // Specifies if the tensor should be a variable input tensor that must be an
107   // output as well as an input to the graph.
108   bool is_variable_input = false;
109 };
110 
111 template <typename ShapeT, DataType Type>
112 constexpr DataType Tensor<ShapeT, Type>::kType;
113 
114 template <typename ShapeT, DataType Type>
115 Tensor<ShapeT, Type> MakeZeroTensor(const ShapeT& shape) {
116   Tensor<ShapeT, Type> tensor;
117   tensor.shape = shape;
118   tensor.data = typename Tensor<ShapeT, Type>::TensorStorageType(
119       shape.DimensionsProduct(), 0);
120   return tensor;
121 }
122 
123 using TensorFloat32 = Tensor<BHWC, DataType::FLOAT32>;
124 using Tensor5DFloat32 = Tensor<BHWDC, DataType::FLOAT32>;
125 
126 }  // namespace gpu
127 }  // namespace tflite
128 
129 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TENSOR_H_
130