micro_resource_variable.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Copyright 2021 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 TFLITE_MICRO_TENSORFLOW_LITE_MICRO_MICRO_RESOURCE_H_
  13. #define TFLITE_MICRO_TENSORFLOW_LITE_MICRO_MICRO_RESOURCE_H_
  14. #include <cstdint>
  15. #include "tensorflow/lite/c/common.h"
  16. #include "tensorflow/lite/micro/micro_allocator.h"
  17. namespace tflite {
  18. class MicroResourceVariables {
  19. public:
  20. // Create
  21. static MicroResourceVariables* Create(MicroAllocator* allocator,
  22. int num_variables);
  23. // Creates a resource variable if none is available for the given container
  24. // and shared name pair. Returns the resource ID corresponding to the
  25. // container and shared name pair. If allocation fails, the returned resource
  26. // ID will be negative. The the container and shared_name must outlive this
  27. // class.
  28. int CreateIdIfNoneFound(const char* container, const char* shared_name);
  29. // Read the resource buffer associated with the given ID into the given
  30. // tensor.
  31. TfLiteStatus Read(int id, const TfLiteEvalTensor* tensor);
  32. // Allocates the resource buffer if none has been allocated, based on the
  33. // length of the input tensor. Copies input tensor contents to the resource
  34. // buffer.
  35. TfLiteStatus Allocate(int id, TfLiteContext* context,
  36. const TfLiteTensor* tensor);
  37. // Copies input tensor contents to the resource buffer.
  38. // AllocateResourceVariable with a TFLite tensor must have been called first
  39. // in order to allocate the resource buffer.
  40. TfLiteStatus Assign(int id, const TfLiteEvalTensor* tensor);
  41. // Zeros out all resource buffers.
  42. TfLiteStatus ResetAll();
  43. private:
  44. int FindId(const char* container, const char* shared_name);
  45. // Micro resource contains the mapping between resource container/name strings
  46. // and resouce IDs. Each resource ID corresponds to a resource buffer pointer.
  47. // The resouce ID is created during the VAR_HANDLE operator preparation stage.
  48. // The resource buffer pointer is created during ASSIGN_VARIABLE preparation
  49. // stage based on the size of the TFLiteTensor being assigned.
  50. struct MicroResourceVariable {
  51. const char* container;
  52. const char* shared_name;
  53. void* resource_buffer;
  54. // This is only for verifying read size.
  55. size_t bytes;
  56. };
  57. MicroResourceVariables(MicroResourceVariable* variables,
  58. int max_variable_count)
  59. : resource_variables_(variables),
  60. max_variable_count_(max_variable_count),
  61. num_resource_variables_(0) {}
  62. MicroResourceVariable* resource_variables_;
  63. int max_variable_count_;
  64. int num_resource_variables_;
  65. };
  66. } // namespace tflite
  67. #endif // TFLITE_MICRO_TENSORFLOW_LITE_MICRO_MICRO_RESOURCE_H_