| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_PORTABLE_TENSOR_H_ |
| #define TENSORFLOW_LITE_KERNELS_INTERNAL_PORTABLE_TENSOR_H_ |
|
|
| #include <vector> |
|
|
| #include "edge-impulse-sdk/tensorflow/lite/core/c/common.h" |
| #include "edge-impulse-sdk/tensorflow/lite/kernels/internal/tensor_ctypes.h" |
| #include "edge-impulse-sdk/tensorflow/lite/kernels/internal/types.h" |
|
|
| namespace tflite { |
|
|
| inline RuntimeShape GetTensorShape(std::vector<int32_t> data) { |
| return RuntimeShape(data.size(), data.data()); |
| } |
|
|
| |
| |
| template <typename T> |
| class VectorOfTensors { |
| public: |
| |
| VectorOfTensors(const TfLiteContext& context, |
| const TfLiteIntArray& tensor_list) { |
| int num_tensors = tensor_list.size; |
|
|
| all_data_.reserve(num_tensors); |
| all_shape_.reserve(num_tensors); |
| all_shape_ptr_.reserve(num_tensors); |
|
|
| for (int i = 0; i < num_tensors; ++i) { |
| TfLiteTensor* t = context.GetTensor(&context, tensor_list.data[i]); |
| all_data_.push_back(GetTensorData<T>(t)); |
| all_shape_.push_back(GetTensorShape(t)); |
| } |
|
|
| |
| |
| |
| for (int i = 0; i < num_tensors; ++i) { |
| all_shape_ptr_.push_back(&all_shape_[i]); |
| } |
| } |
| |
| |
| |
| |
| T* const* data() const { return all_data_.data(); } |
|
|
| |
| |
| |
| |
| const RuntimeShape* const* shapes() const { return all_shape_ptr_.data(); } |
|
|
| private: |
| std::vector<T*> all_data_; |
| std::vector<RuntimeShape> all_shape_; |
| std::vector<RuntimeShape*> all_shape_ptr_; |
| }; |
|
|
| |
| |
| class VectorOfQuantizedTensors : public VectorOfTensors<uint8_t> { |
| public: |
| |
| VectorOfQuantizedTensors(const TfLiteContext& context, |
| const TfLiteIntArray& tensor_list) |
| : VectorOfTensors<uint8_t>(context, tensor_list) { |
| for (int i = 0; i < tensor_list.size; ++i) { |
| TfLiteTensor* t = context.GetTensor(&context, tensor_list.data[i]); |
| zero_point_.push_back(t->params.zero_point); |
| scale_.push_back(t->params.scale); |
| } |
| } |
|
|
| const float* scale() const { return scale_.data(); } |
| const int32_t* zero_point() const { return zero_point_.data(); } |
|
|
| private: |
| std::vector<int32_t> zero_point_; |
| std::vector<float> scale_; |
| }; |
|
|
| |
| template <typename T> |
| class SequentialTensorWriter { |
| public: |
| SequentialTensorWriter(const TfLiteTensor* input, TfLiteTensor* output) { |
| input_data_ = GetTensorData<T>(input); |
| output_ptr_ = GetTensorData<T>(output); |
| } |
| SequentialTensorWriter(const T* input_data, T* output_data) |
| : input_data_(input_data), output_ptr_(output_data) {} |
|
|
| void Write(int position) { *output_ptr_++ = input_data_[position]; } |
| void WriteN(int position, int len) { |
| memcpy(output_ptr_, &input_data_[position], sizeof(T) * len); |
| output_ptr_ += len; |
| } |
|
|
| private: |
| const T* input_data_; |
| T* output_ptr_; |
| }; |
|
|
| } |
|
|
| #endif |
|
|