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