micro_resource_variable.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /* Copyright 2020 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/micro_resource_variable.h"
  13. #include <cstring>
  14. #include "tensorflow/lite/c/common.h"
  15. #include "tensorflow/lite/kernels/internal/compatibility.h"
  16. #include "tensorflow/lite/micro/memory_helpers.h"
  17. #include "tensorflow/lite/micro/micro_error_reporter.h"
  18. #include "tensorflow/lite/micro/micro_utils.h"
  19. namespace tflite {
  20. namespace {} // namespace
  21. MicroResourceVariables* MicroResourceVariables::Create(
  22. MicroAllocator* allocator, int max_num_variables) {
  23. TFLITE_DCHECK(allocator != nullptr);
  24. uint8_t* allocator_buffer = static_cast<uint8_t*>(
  25. allocator->AllocatePersistentBuffer(sizeof(MicroResourceVariables)));
  26. MicroResourceVariable* variable_array =
  27. static_cast<MicroResourceVariable*>(allocator->AllocatePersistentBuffer(
  28. sizeof(MicroResourceVariable) * max_num_variables));
  29. MicroResourceVariables* variables = new (allocator_buffer)
  30. MicroResourceVariables(variable_array, max_num_variables);
  31. return variables;
  32. }
  33. int MicroResourceVariables::CreateIdIfNoneFound(const char* container,
  34. const char* shared_name) {
  35. int resource_id = FindId(container, shared_name);
  36. if (resource_id >= 0) {
  37. return resource_id;
  38. }
  39. // no existing variable found for the given container and shared name pair.
  40. if (num_resource_variables_ >= max_variable_count_) {
  41. MicroPrintf(
  42. "Failed to allocate resource variable. Maximum resource variable count "
  43. "(%d) "
  44. "reached.",
  45. max_variable_count_);
  46. return -1;
  47. }
  48. resource_id = num_resource_variables_++;
  49. resource_variables_[resource_id].container = container;
  50. resource_variables_[resource_id].shared_name = shared_name;
  51. resource_variables_[resource_id].resource_buffer = nullptr;
  52. resource_variables_[resource_id].bytes = 0;
  53. return resource_id;
  54. }
  55. TfLiteStatus MicroResourceVariables::Read(int id,
  56. const TfLiteEvalTensor* tensor) {
  57. if (id < 0 || id >= num_resource_variables_) {
  58. MicroPrintf("Attempting to read non-existent resource variable %d", id);
  59. return kTfLiteError;
  60. }
  61. MicroResourceVariable variable = resource_variables_[id];
  62. TFLITE_DCHECK(EvalTensorBytes(tensor) == variable.bytes);
  63. TFLITE_DCHECK(variable.resource_buffer != nullptr);
  64. memcpy(tensor->data.raw, variable.resource_buffer, variable.bytes);
  65. return kTfLiteOk;
  66. }
  67. TfLiteStatus MicroResourceVariables::Allocate(int id, TfLiteContext* context,
  68. const TfLiteTensor* tensor) {
  69. if (id < 0 || id >= num_resource_variables_) {
  70. MicroPrintf("Attempting to read non-existent resource variable %d", id);
  71. return kTfLiteError;
  72. }
  73. MicroResourceVariable& variable = resource_variables_[id];
  74. if (variable.resource_buffer == nullptr) {
  75. variable.bytes = tensor->bytes;
  76. variable.resource_buffer =
  77. context->AllocatePersistentBuffer(context, tensor->bytes);
  78. if (variable.resource_buffer == nullptr) {
  79. MicroPrintf("Failed to allocate resource buffer.");
  80. return kTfLiteError;
  81. }
  82. // Zero out resource buffers by deafult. Buffers can be initialized to
  83. // nonzero values using ASSIGN_VARIABLE.
  84. memset(variable.resource_buffer, 0, variable.bytes);
  85. }
  86. return kTfLiteOk;
  87. }
  88. TfLiteStatus MicroResourceVariables::Assign(int id,
  89. const TfLiteEvalTensor* tensor) {
  90. if (id < 0 || id >= num_resource_variables_) {
  91. MicroPrintf("Attempting to read non-existent resource variable %d", id);
  92. return kTfLiteError;
  93. }
  94. MicroResourceVariable variable = resource_variables_[id];
  95. if (variable.resource_buffer == nullptr) {
  96. MicroPrintf(
  97. "Attempting to assign from a TfLiteEvalTensor before the resource "
  98. "buffer has been allocated. Make sure to call AssignResourceVariable "
  99. "with a TfLiteTensor first.");
  100. return kTfLiteError;
  101. }
  102. TFLITE_DCHECK(EvalTensorBytes(tensor) == variable.bytes);
  103. memcpy(variable.resource_buffer, tensor->data.raw, variable.bytes);
  104. return kTfLiteOk;
  105. }
  106. TfLiteStatus MicroResourceVariables::ResetAll() {
  107. for (int i = 0; i < num_resource_variables_; i++) {
  108. MicroResourceVariable variable = resource_variables_[i];
  109. memset(variable.resource_buffer, 0, variable.bytes);
  110. }
  111. return kTfLiteOk;
  112. }
  113. int MicroResourceVariables::FindId(const char* container,
  114. const char* shared_name) {
  115. for (int i = 0; i < num_resource_variables_; i++) {
  116. // Some TFLite flatbuffers contain null container names to save space.
  117. if ((container == nullptr ||
  118. !strcmp(container, resource_variables_[i].container)) &&
  119. !strcmp(shared_name, resource_variables_[i].shared_name)) {
  120. return i;
  121. }
  122. }
  123. return -1;
  124. }
  125. } // namespace tflite