memory_helpers.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /* Copyright 2019 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. #include "tensorflow/lite/micro/memory_helpers.h"
  13. #include <cstddef>
  14. #include <cstdint>
  15. #include "flatbuffers/flatbuffers.h" // from @flatbuffers
  16. #include "tensorflow/lite/c/common.h"
  17. #include "tensorflow/lite/core/api/error_reporter.h"
  18. #include "tensorflow/lite/core/api/flatbuffer_conversions.h"
  19. #include "tensorflow/lite/kernels/internal/tensor_ctypes.h"
  20. #include "tensorflow/lite/schema/schema_generated.h"
  21. namespace tflite {
  22. uint8_t* AlignPointerUp(uint8_t* data, size_t alignment) {
  23. std::uintptr_t data_as_uintptr_t = reinterpret_cast<std::uintptr_t>(data);
  24. uint8_t* aligned_result = reinterpret_cast<uint8_t*>(
  25. ((data_as_uintptr_t + (alignment - 1)) / alignment) * alignment);
  26. return aligned_result;
  27. }
  28. uint8_t* AlignPointerDown(uint8_t* data, size_t alignment) {
  29. std::uintptr_t data_as_uintptr_t = reinterpret_cast<std::uintptr_t>(data);
  30. uint8_t* aligned_result =
  31. reinterpret_cast<uint8_t*>((data_as_uintptr_t / alignment) * alignment);
  32. return aligned_result;
  33. }
  34. size_t AlignSizeUp(size_t size, size_t alignment) {
  35. size_t aligned_size = (((size + (alignment - 1)) / alignment) * alignment);
  36. return aligned_size;
  37. }
  38. TfLiteStatus TfLiteTypeSizeOf(TfLiteType type, size_t* size) {
  39. switch (type) {
  40. case kTfLiteFloat32:
  41. *size = sizeof(float);
  42. break;
  43. case kTfLiteInt16:
  44. *size = sizeof(int16_t);
  45. break;
  46. case kTfLiteInt32:
  47. *size = sizeof(int32_t);
  48. break;
  49. case kTfLiteUInt8:
  50. *size = sizeof(uint8_t);
  51. break;
  52. case kTfLiteInt8:
  53. *size = sizeof(int8_t);
  54. break;
  55. case kTfLiteInt64:
  56. *size = sizeof(int64_t);
  57. break;
  58. case kTfLiteBool:
  59. *size = sizeof(bool);
  60. break;
  61. case kTfLiteComplex64:
  62. *size = sizeof(float) * 2;
  63. break;
  64. case kTfLiteComplex128:
  65. *size = sizeof(double) * 2;
  66. break;
  67. default:
  68. return kTfLiteError;
  69. }
  70. return kTfLiteOk;
  71. }
  72. TfLiteStatus BytesRequiredForTensor(const tflite::Tensor& flatbuffer_tensor,
  73. size_t* bytes, size_t* type_size,
  74. ErrorReporter* error_reporter) {
  75. int element_count = 1;
  76. // If flatbuffer_tensor.shape == nullptr, then flatbuffer_tensor is a scalar
  77. // so has 1 element.
  78. if (flatbuffer_tensor.shape() != nullptr) {
  79. for (size_t n = 0; n < flatbuffer_tensor.shape()->Length(); ++n) {
  80. element_count *= flatbuffer_tensor.shape()->Get(n);
  81. }
  82. }
  83. TfLiteType tf_lite_type;
  84. TF_LITE_ENSURE_STATUS(ConvertTensorType(flatbuffer_tensor.type(),
  85. &tf_lite_type, error_reporter));
  86. TF_LITE_ENSURE_STATUS(TfLiteTypeSizeOf(tf_lite_type, type_size));
  87. *bytes = element_count * (*type_size);
  88. return kTfLiteOk;
  89. }
  90. TfLiteStatus TfLiteEvalTensorByteLength(const TfLiteEvalTensor* eval_tensor,
  91. size_t* out_bytes) {
  92. TFLITE_DCHECK(out_bytes != nullptr);
  93. int element_count = 1;
  94. // If eval_tensor->dims == nullptr, then tensor is a scalar so has 1 element.
  95. if (eval_tensor->dims != nullptr) {
  96. for (int n = 0; n < eval_tensor->dims->size; ++n) {
  97. element_count *= eval_tensor->dims->data[n];
  98. }
  99. }
  100. size_t type_size;
  101. TF_LITE_ENSURE_STATUS(TfLiteTypeSizeOf(eval_tensor->type, &type_size));
  102. *out_bytes = element_count * type_size;
  103. return kTfLiteOk;
  104. }
  105. TfLiteStatus AllocateOutputDimensionsFromInput(TfLiteContext* context,
  106. const TfLiteTensor* input1,
  107. const TfLiteTensor* input2,
  108. TfLiteTensor* output) {
  109. const TfLiteTensor* input = nullptr;
  110. TF_LITE_ENSURE(context, input1->dims != nullptr);
  111. TF_LITE_ENSURE(context, input2->dims != nullptr);
  112. TF_LITE_ENSURE(context, output->dims->size == 0);
  113. input = input1->dims->size > input2->dims->size ? input1 : input2;
  114. TF_LITE_ENSURE(context, output->type == input->type);
  115. size_t size = 0;
  116. TfLiteTypeSizeOf(input->type, &size);
  117. const int dimensions_count = tflite::GetTensorShape(input).DimensionsCount();
  118. for (int i = 0; i < dimensions_count; i++) {
  119. size *= input->dims->data[i];
  120. }
  121. output->bytes = size;
  122. output->dims =
  123. reinterpret_cast<TfLiteIntArray*>(context->AllocatePersistentBuffer(
  124. context, TfLiteIntArrayGetSizeInBytes(size)));
  125. output->dims->size = input->dims->size;
  126. for (int i = 0; i < dimensions_count; i++) {
  127. output->dims->data[i] = input->dims->data[i];
  128. }
  129. return kTfLiteOk;
  130. }
  131. } // namespace tflite