| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include "edge-impulse-sdk/tensorflow/lite/micro/fake_micro_context.h" |
|
|
| #include "edge-impulse-sdk/tensorflow/lite/kernels/internal/compatibility.h" |
| #include "edge-impulse-sdk/tensorflow/lite/micro/single_arena_buffer_allocator.h" |
| #include "edge-impulse-sdk/tensorflow/lite/micro/micro_allocator.h" |
| #include "edge-impulse-sdk/tensorflow/lite/micro/micro_arena_constants.h" |
| #include "edge-impulse-sdk/tensorflow/lite/micro/micro_log.h" |
|
|
| namespace tflite { |
|
|
| |
| constexpr int FakeMicroContext::kNumScratchBuffers_; |
|
|
| namespace { |
| |
| |
| static constexpr int KDummyTensorArenaSize = 256; |
| static uint8_t dummy_tensor_arena[KDummyTensorArenaSize]; |
| } |
|
|
| FakeMicroContext::FakeMicroContext(TfLiteTensor* tensors, |
| SingleArenaBufferAllocator* allocator, |
| MicroGraph* micro_graph) |
| : MicroContext( |
| MicroAllocator::Create(dummy_tensor_arena, KDummyTensorArenaSize), |
| nullptr, micro_graph), |
| tensors_(tensors), |
| allocator_(allocator) {} |
|
|
| TfLiteTensor* FakeMicroContext::AllocateTempTfLiteTensor(int tensor_index) { |
| allocated_tensor_count_++; |
| return &tensors_[tensor_index]; |
| } |
|
|
| void FakeMicroContext::DeallocateTempTfLiteTensor(TfLiteTensor* tensor) { |
| allocated_tensor_count_--; |
| } |
|
|
| bool FakeMicroContext::IsAllTempTfLiteTensorDeallocated() { |
| return !allocated_tensor_count_; |
| } |
|
|
| TfLiteEvalTensor* FakeMicroContext::GetEvalTensor(int tensor_index) { |
| TfLiteEvalTensor* eval_tensor = |
| reinterpret_cast<TfLiteEvalTensor*>(allocator_->AllocateTemp( |
| sizeof(TfLiteEvalTensor), alignof(TfLiteEvalTensor))); |
| TFLITE_DCHECK(eval_tensor != nullptr); |
|
|
| |
| |
| eval_tensor->data = tensors_[tensor_index].data; |
| eval_tensor->dims = tensors_[tensor_index].dims; |
| eval_tensor->type = tensors_[tensor_index].type; |
| return eval_tensor; |
| } |
|
|
| void* FakeMicroContext::AllocatePersistentBuffer(size_t bytes) { |
| |
| |
| |
| |
| return allocator_->AllocatePersistentBuffer(bytes, |
| MicroArenaBufferAlignment()); |
| } |
|
|
| TfLiteStatus FakeMicroContext::RequestScratchBufferInArena(size_t bytes, |
| int* buffer_index) { |
| TFLITE_DCHECK(buffer_index != nullptr); |
|
|
| if (scratch_buffer_count_ == kNumScratchBuffers_) { |
| MicroPrintf("Exceeded the maximum number of scratch tensors allowed (%d).", |
| kNumScratchBuffers_); |
| return kTfLiteError; |
| } |
|
|
| |
| |
| |
| scratch_buffers_[scratch_buffer_count_] = |
| allocator_->AllocatePersistentBuffer(bytes, MicroArenaBufferAlignment()); |
| TFLITE_DCHECK(scratch_buffers_[scratch_buffer_count_] != nullptr); |
|
|
| *buffer_index = scratch_buffer_count_++; |
| return kTfLiteOk; |
| } |
|
|
| void* FakeMicroContext::GetScratchBuffer(int buffer_index) { |
| TFLITE_DCHECK(scratch_buffer_count_ <= kNumScratchBuffers_); |
| if (buffer_index >= scratch_buffer_count_) { |
| return nullptr; |
| } |
| return scratch_buffers_[buffer_index]; |
| } |
|
|
| } |
|
|