tensor.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_TENSOR_H_
  13. #define TENSORFLOW_LITE_KERNELS_INTERNAL_TENSOR_H_
  14. #include <complex>
  15. #include <vector>
  16. #include "tensorflow/lite/c/common.h"
  17. #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
  18. #include "tensorflow/lite/kernels/internal/types.h"
  19. #include "tensorflow/lite/string_util.h"
  20. namespace tflite {
  21. inline RuntimeShape GetTensorShape(std::vector<int32_t> data) {
  22. return RuntimeShape(data.size(), data.data());
  23. }
  24. // A list of tensors in a format that can be used by kernels like split and
  25. // concatenation.
  26. template <typename T>
  27. class VectorOfTensors {
  28. public:
  29. // Build with the tensors in 'tensor_list'.
  30. VectorOfTensors(const TfLiteContext& context,
  31. const TfLiteIntArray& tensor_list) {
  32. int num_tensors = tensor_list.size;
  33. all_data_.reserve(num_tensors);
  34. all_shape_.reserve(num_tensors);
  35. all_shape_ptr_.reserve(num_tensors);
  36. for (int i = 0; i < num_tensors; ++i) {
  37. TfLiteTensor* t = &context.tensors[tensor_list.data[i]];
  38. all_data_.push_back(GetTensorData<T>(t));
  39. all_shape_.push_back(GetTensorShape(t));
  40. }
  41. // Taking the pointer from inside a std::vector is only OK if the vector is
  42. // never modified, so we populate all_shape in the previous loop and then we
  43. // are free to grab iterators here.
  44. for (int i = 0; i < num_tensors; ++i) {
  45. all_shape_ptr_.push_back(&all_shape_[i]);
  46. }
  47. }
  48. // Return a pointer to the data pointers of all tensors in the list. For
  49. // example:
  50. // float* const* f = v.data();
  51. // f[0][1] is the second element of the first tensor.
  52. T* const* data() const { return all_data_.data(); }
  53. // Return a pointer the shape pointers of all tensors in the list. For
  54. // example:
  55. // const RuntimeShape* const* d = v.dims();
  56. // dims[1] are the dimensions of the second tensor in the list.
  57. const RuntimeShape* const* shapes() const { return all_shape_ptr_.data(); }
  58. private:
  59. std::vector<T*> all_data_;
  60. std::vector<RuntimeShape> all_shape_;
  61. std::vector<RuntimeShape*> all_shape_ptr_;
  62. };
  63. // A list of quantized tensors in a format that can be used by kernels like
  64. // split and concatenation.
  65. class VectorOfQuantizedTensors : public VectorOfTensors<uint8_t> {
  66. public:
  67. // Build with the tensors in 'tensor_list'.
  68. VectorOfQuantizedTensors(const TfLiteContext& context,
  69. const TfLiteIntArray& tensor_list)
  70. : VectorOfTensors<uint8_t>(context, tensor_list) {
  71. for (int i = 0; i < tensor_list.size; ++i) {
  72. TfLiteTensor* t = &context.tensors[tensor_list.data[i]];
  73. zero_point_.push_back(t->params.zero_point);
  74. scale_.push_back(t->params.scale);
  75. }
  76. }
  77. const float* scale() const { return scale_.data(); }
  78. const int32_t* zero_point() const { return zero_point_.data(); }
  79. private:
  80. std::vector<int32_t> zero_point_;
  81. std::vector<float> scale_;
  82. };
  83. // Writes randomly accessed values from `input` sequentially into `output`.
  84. template <typename T>
  85. class SequentialTensorWriter {
  86. public:
  87. SequentialTensorWriter(const TfLiteTensor* input, TfLiteTensor* output) {
  88. input_data_ = GetTensorData<T>(input);
  89. output_ptr_ = GetTensorData<T>(output);
  90. }
  91. SequentialTensorWriter(const T* input_data, T* output_data)
  92. : input_data_(input_data), output_ptr_(output_data) {}
  93. void Write(int position) { *output_ptr_++ = input_data_[position]; }
  94. void WriteN(int position, int len) {
  95. memcpy(output_ptr_, &input_data_[position], sizeof(T) * len);
  96. output_ptr_ += len;
  97. }
  98. private:
  99. const T* input_data_;
  100. T* output_ptr_;
  101. };
  102. // String ops are not yet supported on platforms w/ static memory.
  103. #ifndef TF_LITE_STATIC_MEMORY
  104. template <>
  105. class SequentialTensorWriter<string> {
  106. public:
  107. SequentialTensorWriter(const TfLiteTensor* input, TfLiteTensor* output)
  108. : input_(input), output_(output) {}
  109. ~SequentialTensorWriter() { buffer_.WriteToTensor(output_, nullptr); }
  110. void Write(int position) { this->WriteN(position, 1); }
  111. void WriteN(int position, int len) {
  112. for (int i = 0; i < len; i++) {
  113. buffer_.AddString(GetString(input_, position + i));
  114. }
  115. }
  116. private:
  117. const TfLiteTensor* input_;
  118. TfLiteTensor* output_;
  119. DynamicBuffer buffer_;
  120. };
  121. #endif // TF_LITE_STATIC_MEMORY
  122. } // namespace tflite
  123. #endif // TENSORFLOW_LITE_KERNELS_INTERNAL_TENSOR_H_