• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }  // namespace internal_tensor
70 
71 template <typename ShapeT, DataType Type>
72 struct Tensor {
73   using ShapeType = ShapeT;
74 
75   constexpr static DataType kType = Type;
76 
77   using TensorStorageType = typename internal_tensor::StorageType<Type>::value;
78 
79   // Opaque id of a tensor.
80   int64_t id = -1;
81 
82   ShapeType shape;
83 
84   TensorStorageType data;
85 };
86 
87 // TensorRef is a reference to another tensor. If an object should never hold
88 // tensor data, then TensorRef should be used instead.
89 template <typename ShapeT>
90 struct TensorRef {
91   using ShapeType = ShapeT;
92 
93   DataType type = DataType::UNKNOWN;
94 
95   ShapeT shape;
96 
97   // Opaque reference to a tensor. Upstream component is responsible for
98   // resolving this reference into an actual tensor.
99   int64_t ref = -1;
100 
101   // Specifies if the tensor should be a variable input tensor that must be an
102   // output as well as an input to the graph.
103   bool is_variable_input = false;
104 };
105 
106 template <typename ShapeT, DataType Type>
107 constexpr DataType Tensor<ShapeT, Type>::kType;
108 
109 template <typename ShapeT, DataType Type>
110 Tensor<ShapeT, Type> MakeZeroTensor(const ShapeT& shape) {
111   Tensor<ShapeT, Type> tensor;
112   tensor.shape = shape;
113   tensor.data = typename Tensor<ShapeT, Type>::TensorStorageType(
114       shape.DimensionsProduct(), 0);
115   return tensor;
116 }
117 
118 using TensorFloat32 = Tensor<BHWC, DataType::FLOAT32>;
119 using Tensor5DFloat32 = Tensor<BHWDC, DataType::FLOAT32>;
120 
121 }  // namespace gpu
122 }  // namespace tflite
123 
124 #endif  // TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TENSOR_H_
125