1 /* Copyright 2017 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_LITE_KERNELS_INTERNAL_TENSOR_H_
16 #define TENSORFLOW_LITE_KERNELS_INTERNAL_TENSOR_H_
17
18 #include <complex>
19 #include <vector>
20 #include "tensorflow/lite/c/c_api_internal.h"
21 #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
22 #include "tensorflow/lite/kernels/internal/types.h"
23
24 namespace tflite {
25
26 template <>
GetTensorData(TfLiteTensor * tensor)27 inline std::complex<float>* GetTensorData(TfLiteTensor* tensor) {
28 return tensor != nullptr
29 ? reinterpret_cast<std::complex<float>*>(tensor->data.c64)
30 : nullptr;
31 }
32
33 template <>
GetTensorData(const TfLiteTensor * tensor)34 inline const std::complex<float>* GetTensorData(const TfLiteTensor* tensor) {
35 return tensor != nullptr
36 ? reinterpret_cast<const std::complex<float>*>(tensor->data.c64)
37 : nullptr;
38 }
39
GetTensorShape(std::vector<int32_t> data)40 inline RuntimeShape GetTensorShape(std::vector<int32_t> data) {
41 return RuntimeShape(data.size(), data.data());
42 }
43
44 // A list of tensors in a format that can be used by kernels like split and
45 // concatenation.
46 template <typename T>
47 class VectorOfTensors {
48 public:
49 // Build with the tensors in 'tensor_list'.
VectorOfTensors(const TfLiteContext & context,const TfLiteIntArray & tensor_list)50 VectorOfTensors(const TfLiteContext& context,
51 const TfLiteIntArray& tensor_list) {
52 int num_tensors = tensor_list.size;
53
54 all_data_.reserve(num_tensors);
55 all_shape_.reserve(num_tensors);
56 all_shape_ptr_.reserve(num_tensors);
57
58 for (int i = 0; i < num_tensors; ++i) {
59 TfLiteTensor* t = &context.tensors[tensor_list.data[i]];
60 all_data_.push_back(GetTensorData<T>(t));
61 all_shape_.push_back(GetTensorShape(t));
62 }
63
64 // Taking the pointer from inside a std::vector is only OK if the vector is
65 // never modified, so we populate all_shape in the previous loop and then we
66 // are free to grab iterators here.
67 for (int i = 0; i < num_tensors; ++i) {
68 all_shape_ptr_.push_back(&all_shape_[i]);
69 }
70 }
71 // Return a pointer to the data pointers of all tensors in the list. For
72 // example:
73 // float* const* f = v.data();
74 // f[0][1] is the second element of the first tensor.
data()75 T* const* data() const { return all_data_.data(); }
76
77 // Return a pointer the shape pointers of all tensors in the list. For
78 // example:
79 // const RuntimeShape* const* d = v.dims();
80 // dims[1] are the dimensions of the second tensor in the list.
shapes()81 const RuntimeShape* const* shapes() const { return all_shape_ptr_.data(); }
82
83 private:
84 std::vector<T*> all_data_;
85 std::vector<RuntimeShape> all_shape_;
86 std::vector<RuntimeShape*> all_shape_ptr_;
87 };
88
89 // A list of quantized tensors in a format that can be used by kernels like
90 // split and concatenation.
91 class VectorOfQuantizedTensors : public VectorOfTensors<uint8> {
92 public:
93 // Build with the tensors in 'tensor_list'.
VectorOfQuantizedTensors(const TfLiteContext & context,const TfLiteIntArray & tensor_list)94 VectorOfQuantizedTensors(const TfLiteContext& context,
95 const TfLiteIntArray& tensor_list)
96 : VectorOfTensors<uint8>(context, tensor_list) {
97 for (int i = 0; i < tensor_list.size; ++i) {
98 TfLiteTensor* t = &context.tensors[tensor_list.data[i]];
99 zero_point_.push_back(t->params.zero_point);
100 scale_.push_back(t->params.scale);
101 }
102 }
103
scale()104 const float* scale() const { return scale_.data(); }
zero_point()105 const int32* zero_point() const { return zero_point_.data(); }
106
107 private:
108 std::vector<int32> zero_point_;
109 std::vector<float> scale_;
110 };
111
112 } // namespace tflite
113
114 #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_TENSOR_H_
115