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 } // namespace internal_tensor 45 46 template <typename ShapeT, DataType Type> 47 struct Tensor { 48 using ShapeType = ShapeT; 49 50 constexpr static DataType kType = Type; 51 52 using TensorStorageType = typename internal_tensor::StorageType<Type>::value; 53 54 // Opaque id of a tensor. 55 int64_t id = -1; 56 57 ShapeType shape; 58 59 TensorStorageType data; 60 }; 61 62 // TensorRef is a reference to another tensor. If an object should never hold 63 // tensor data, then TensorRef should be used instead. 64 template <typename ShapeT> 65 struct TensorRef { 66 using ShapeType = ShapeT; 67 68 DataType type = DataType::UNKNOWN; 69 70 ShapeT shape; 71 72 // Opaque reference to a tensor. Upstream component is responsible for 73 // resolving this reference into an actual tensor. 74 int64_t ref = -1; 75 76 // Specifies if the tensor should be a variable input tensor that must be an 77 // output as well as an input to the graph. 78 bool is_variable_input = false; 79 }; 80 81 template <typename ShapeT, DataType Type> 82 constexpr DataType Tensor<ShapeT, Type>::kType; 83 84 template <typename ShapeT, DataType Type> 85 Tensor<ShapeT, Type> MakeZeroTensor(const ShapeT& shape) { 86 Tensor<ShapeT, Type> tensor; 87 tensor.shape = shape; 88 tensor.data = typename Tensor<ShapeT, Type>::TensorStorageType( 89 shape.DimensionsProduct(), 0); 90 return tensor; 91 } 92 93 using TensorFloat32 = Tensor<BHWC, DataType::FLOAT32>; 94 using Tensor5DFloat32 = Tensor<BHWDC, DataType::FLOAT32>; 95 96 } // namespace gpu 97 } // namespace tflite 98 99 #endif // TENSORFLOW_LITE_DELEGATES_GPU_COMMON_TENSOR_H_ 100