ibuffer_allocator.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Copyright 2022 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_MICRO_IBUFFER_ALLOCATOR_H_
  13. #define TENSORFLOW_LITE_MICRO_IBUFFER_ALLOCATOR_H_
  14. #include <cstddef>
  15. #include <cstdint>
  16. #include "tensorflow/lite/c/c_api_types.h"
  17. namespace tflite {
  18. // Interface classes that the TFLM framework relies on to get buffers it needs.
  19. // There are two types of buffers that the TFLM framework requires: persistent
  20. // and non-persistent. Persistent buffers, once allocated, are never freed by
  21. // the TFLM framework. Non-persist buffers can be allocated and deallocated by
  22. // the TFLM framework. This file defines two interfaces classes that TFLM
  23. // framework will rely on to manage these buffers.
  24. // Interface class for managing persistent buffers.
  25. class IPersistentBufferAllocator {
  26. public:
  27. IPersistentBufferAllocator() {}
  28. virtual ~IPersistentBufferAllocator() {}
  29. // Allocates persistent memory. The persistent buffer is never freed.
  30. virtual uint8_t* AllocatePersistentBuffer(size_t size, size_t alignment) = 0;
  31. // Returns the size of all persistent allocations in bytes.
  32. virtual size_t GetPersistentUsedBytes() const = 0;
  33. };
  34. // Interface class for managing non-persistent buffers.
  35. // The default non-persistent buffers are temp buffers that are not resizable.
  36. // Support of at least one resizable buffer is required.
  37. class INonPersistentBufferAllocator {
  38. public:
  39. INonPersistentBufferAllocator() {}
  40. virtual ~INonPersistentBufferAllocator() {}
  41. // Allocates a temporary buffer. This buffer is not resizable.
  42. virtual uint8_t* AllocateTemp(size_t size, size_t alignment) = 0;
  43. // Signals that a temporary buffer is no longer needed.
  44. virtual void DeallocateTemp(uint8_t* buf) = 0;
  45. // Returns true if all temporary buffers are already deallocated.
  46. virtual bool IsAllTempDeallocated() = 0;
  47. // Signals that all temporary allocations can be reclaimed. TFLM calls this
  48. // API when it knows that all temporary buffers that it requested has been
  49. // deallocated. The goal of API is to facilitate implementations of
  50. // INonPersistentBufferAllocator can reuse buffer with some reasonable
  51. // complexity.
  52. virtual TfLiteStatus ResetTempAllocations() = 0;
  53. // Returns a buffer that is resizable viable ResizeBuffer().
  54. virtual uint8_t* AllocateResizableBuffer(size_t size, size_t alignment) = 0;
  55. // Resizes a buffer that is previously returned by the
  56. // AllocateResizableBuffer.
  57. virtual TfLiteStatus ResizeBuffer(uint8_t* resizable_buf, size_t size,
  58. size_t alignment) = 0;
  59. // Frees up the memory occupied by the resizable buffer.
  60. virtual TfLiteStatus DeallocateResizableBuffer(uint8_t* resizable_buf) = 0;
  61. // Returns a pointer pointing to the start of the overlay memory, which is
  62. // used for activation tensors and scratch buffers by kernels at Invoke stage.
  63. virtual uint8_t* GetOverlayMemoryAddress() const = 0;
  64. // Reserves the size of the overlay memory. This overlay is reserved for the
  65. // kernels at Invoke stage. This is referred to as the overlay because before
  66. // Invoket state, the same memory can be used for temp buffers. The layout of
  67. // the memory is planned by the memory planner separately at Invoke stage.
  68. virtual TfLiteStatus ReserveNonPersistentOverlayMemory(size_t size,
  69. size_t alignment) = 0;
  70. // Returns the size of non-persistent buffer in use.
  71. virtual size_t GetNonPersistentUsedBytes() const = 0;
  72. // Returns the number of bytes available with a given alignment. This number
  73. // takes in account any temporary allocations.
  74. virtual size_t GetAvailableMemory(size_t alignment) const = 0;
  75. };
  76. } // namespace tflite
  77. #endif // TENSORFLOW_LITE_MICRO_IBUFFER_ALLOCATOR_H_