diff --git a/tflite_micro_compiler/src/Api.cc b/tflite_micro_compiler/src/Api.cc index 7502f2b6..c4c0f507 100644 --- a/tflite_micro_compiler/src/Api.cc +++ b/tflite_micro_compiler/src/Api.cc @@ -25,8 +25,19 @@ std::string TFLMC_Compiler::getTensorName(int tensorIndex, int sg) const { return compiler_->getTensorName(tensorIndex, sg); } -// Returns tensor arena size +// Returns tensor arena size (persistent + non-persistent) size_t TFLMC_Compiler::getTensorArenaSize() const { return compiler_->getTensorArenaSize(); } + +// Returns non-persistent arena size +size_t TFLMC_Compiler::getNonPersistentArenaSize() const { + return compiler_->getNonPersistentArenaSize(); +} + +// Returns persistent arena size +size_t TFLMC_Compiler::getPersistentArenaSize() const { + return compiler_->getPersistentArenaSize(); +} + } // namespace tflmc \ No newline at end of file diff --git a/tflite_micro_compiler/src/Api.h b/tflite_micro_compiler/src/Api.h index c7de6d20..de1f2ee8 100644 --- a/tflite_micro_compiler/src/Api.h +++ b/tflite_micro_compiler/src/Api.h @@ -27,6 +27,12 @@ class TFLMC_Compiler { // Returns tensor arena size size_t getTensorArenaSize() const; + // Returns non-persistent arena size + size_t getNonPersistentArenaSize() const; + + // Returns persistent arena size + size_t getPersistentArenaSize() const; + private: Compiler *compiler_; }; diff --git a/tflite_micro_compiler/src/Compiler.cc b/tflite_micro_compiler/src/Compiler.cc index 7c05301d..2064a1b5 100644 --- a/tflite_micro_compiler/src/Compiler.cc +++ b/tflite_micro_compiler/src/Compiler.cc @@ -180,6 +180,7 @@ bool tflmc::Compiler::init(const void *modelData) { arena_buf_.resize(SUFFICIENT_ARENA_SIZE); g_arena_size = SUFFICIENT_ARENA_SIZE; + // TODO: This looks unused? std::vector arena_buf(g_arena_size); g_arenaPtr = arena_buf_.data(); @@ -348,13 +349,15 @@ bool tflmc::Compiler::init(const void *modelData) { } } - // This includes: + // Non-persistent arena consists of: // - Tensors // - Scratch buffers + nonPersistentArenaSize_ = ramTensorBufferSize; + + // Persistent arena consists of: // - Persistent buffers // - Variable tensors (which are allocated as persistent buffers separately) - arenaBufferSize_ = - ramTensorBufferSize + totalRuntimeAllocSize + varTensorsSize; + persistentArenaSize_ = totalRuntimeAllocSize + varTensorsSize; if (debugPrint_) { interpreter_->allocator_.memory_planner()->PrintMemoryPlan(); @@ -545,14 +548,29 @@ namespace xcore { wr << R"( constexpr int kTensorArenaSize = )" - << arenaBufferSize_ << R"(; -#ifndef SHARED_TENSOR_ARENA + << persistentArenaSize_ + nonPersistentArenaSize_ << R"(; + +#ifdef EXTERN_TENSOR_ARENA + +#ifdef SPLIT_PERSISTENT_TENSOR_ARENA +constexpr int kPersistentTensorArenaSize = )" + << persistentArenaSize_ << R"(; +extern uint8_t persistent_tensor_arena[]; +#endif // SPLIT_PERSISTENT_TENSOR_ARENA + +extern uint8_t tensor_arena[]; + +#else + +#ifdef SPLIT_PERSISTENT_TENSOR_ARENA +#error "Split persistent/non-persistent tensor arenas require externally defined tensor arenas (EXTERN_TENSOR_ARENA must be defined) " +#endif + namespace { uint8_t tensor_arena[kTensorArenaSize] ALIGN(8); } -#else -extern uint8_t tensor_arena[]; -#endif + +#endif // EXTERN_TENSOR_ARENA namespace { template struct TfArray { @@ -899,6 +917,7 @@ static TfLiteStatus RequestScratchBufferInArena(struct TfLiteContext *context, s static void *GetScratchBuffer(struct TfLiteContext *context, int buffer_idx) { + // Scratch buffers are allocated in the non-persistent tensor arena return tensor_arena + scratch_buffer_offsets[buffer_idx]; } @@ -1044,7 +1063,12 @@ TfLiteStatus )" << prefix_ << R"(init(void *flash_data) { // Clear and initialize scratch_buffer_idx = 0; + +#ifdef SPLIT_PERSISTENT_TENSOR_ARENA + persistentBufferPtr = persistent_tensor_arena + kPersistentTensorArenaSize; +#else persistentBufferPtr = tensor_arena + kTensorArenaSize; +#endif // Set flash data in xcore context config xc_config.flash_data = flash_data; @@ -1348,14 +1372,35 @@ void tflmc::Compiler::writeHeader(std::ostream &out) { #include "tensorflow/lite/c/common.h" -#ifdef SHARED_TENSOR_ARENA - #ifndef LARGEST_TENSOR_ARENA_SIZE - #define LARGEST_TENSOR_ARENA_SIZE )" + - std::to_string(arenaBufferSize_) + R"( - #elif LARGEST_TENSOR_ARENA_SIZE < )" + - std::to_string(arenaBufferSize_) + R"( - #define LARGEST_TENSOR_ARENA_SIZE )" + - std::to_string(arenaBufferSize_) + R"( +#ifdef EXTERN_TENSOR_ARENA + #ifdef SPLIT_PERSISTENT_TENSOR_ARENA + #ifndef LARGEST_TENSOR_ARENA_SIZE + #define LARGEST_TENSOR_ARENA_SIZE )" + + std::to_string(nonPersistentArenaSize_) + R"( + #elif LARGEST_TENSOR_ARENA_SIZE < )" + + std::to_string(nonPersistentArenaSize_) + R"( + #define LARGEST_TENSOR_ARENA_SIZE )" + + std::to_string(nonPersistentArenaSize_) + R"( + #endif + + #ifndef LARGEST_PERSISTENT_TENSOR_ARENA_SIZE + #define LARGEST_PERSISTENT_TENSOR_ARENA_SIZE )" + + std::to_string(persistentArenaSize_) + R"( + #elif LARGEST_PERSISTENT_TENSOR_ARENA_SIZE < )" + + std::to_string(persistentArenaSize_) + R"( + #define LARGEST_PERSISTENT_TENSOR_ARENA_SIZE )" + + std::to_string(persistentArenaSize_) + R"( + #endif + + #else + #ifndef LARGEST_TENSOR_ARENA_SIZE + #define LARGEST_TENSOR_ARENA_SIZE )" + + std::to_string(persistentArenaSize_ + nonPersistentArenaSize_) + R"( + #elif LARGEST_TENSOR_ARENA_SIZE < )" + + std::to_string(persistentArenaSize_ + nonPersistentArenaSize_) + R"( + #define LARGEST_TENSOR_ARENA_SIZE )" + + std::to_string(persistentArenaSize_ + nonPersistentArenaSize_) + R"( + #endif #endif #endif diff --git a/tflite_micro_compiler/src/Compiler.h b/tflite_micro_compiler/src/Compiler.h index 56b82357..d44664d3 100644 --- a/tflite_micro_compiler/src/Compiler.h +++ b/tflite_micro_compiler/src/Compiler.h @@ -49,7 +49,13 @@ class Compiler { std::string getTensorName(int tensorIndex, int sg) const; // Returns tensor arena size - size_t getTensorArenaSize() const { return arenaBufferSize_; } + size_t getTensorArenaSize() const { return persistentArenaSize_ + nonPersistentArenaSize_; } + + // Returns persistent arena size + size_t getPersistentArenaSize() const { return persistentArenaSize_; } + + // Returns non-persistent arena size + size_t getNonPersistentArenaSize() const { return nonPersistentArenaSize_; } private: bool init(const void *modelData); @@ -105,7 +111,8 @@ class Compiler { std::unique_ptr interpreter_; MemMap memMap_; - size_t arenaBufferSize_ = 0; + size_t persistentArenaSize_ = 0; + size_t nonPersistentArenaSize_ = 0; size_t varTensors_count = 0; // Vector of vector is for subgraphs std::vector> tensors_;