| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include "edge-impulse-sdk/tensorflow/lite/micro/kernels/circular_buffer.h" |
|
|
| #include "edge-impulse-sdk/tensorflow/lite/c/builtin_op_data.h" |
| #include "edge-impulse-sdk/tensorflow/lite/c/common.h" |
| #include "edge-impulse-sdk/tensorflow/lite/kernels/internal/compatibility.h" |
| #include "edge-impulse-sdk/tensorflow/lite/kernels/internal/quantization_util.h" |
| #include "edge-impulse-sdk/tensorflow/lite/kernels/internal/tensor_ctypes.h" |
| #include "edge-impulse-sdk/tensorflow/lite/kernels/kernel_util.h" |
| #include "edge-impulse-sdk/tensorflow/lite/kernels/op_macros.h" |
| #include "edge-impulse-sdk/tensorflow/lite/micro/flatbuffer_utils.h" |
| #include "edge-impulse-sdk/tensorflow/lite/micro/kernels/kernel_util.h" |
| #include "edge-impulse-sdk/tensorflow/lite/micro/micro_log.h" |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| namespace tflite { |
|
|
| void* CircularBufferInit(TfLiteContext* context, const char* buffer, |
| size_t length) { |
| TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); |
| OpDataCircularBuffer* op_data = static_cast<OpDataCircularBuffer*>( |
| context->AllocatePersistentBuffer(context, sizeof(OpDataCircularBuffer))); |
|
|
| if (buffer != nullptr && length > 0) { |
| const uint8_t* buffer_t = reinterpret_cast<const uint8_t*>(buffer); |
| tflite::FlexbufferWrapper wrapper(buffer_t, length); |
| op_data->cycles_max = wrapper.ElementAsInt32(kCircularBufferCyclesMaxIndex); |
| } else { |
| op_data->cycles_max = 0; |
| } |
|
|
| return op_data; |
| } |
|
|
| |
| |
| |
| void EvalInt8(const int8_t* input, int num_slots, int depth, int8_t* output) { |
| memmove(output, &output[depth], (num_slots - 1) * depth); |
| memcpy(&output[(num_slots - 1) * depth], input, depth); |
| } |
|
|
| TfLiteStatus CircularBufferEval(TfLiteContext* context, TfLiteNode* node) { |
| const TfLiteEvalTensor* input = |
| tflite::micro::GetEvalInput(context, node, kCircularBufferInputTensor); |
| TfLiteEvalTensor* output = |
| tflite::micro::GetEvalOutput(context, node, kCircularBufferOutputTensor); |
|
|
| TFLITE_DCHECK(node->user_data != nullptr); |
| OpDataCircularBuffer* data = |
| reinterpret_cast<OpDataCircularBuffer*>(node->user_data); |
|
|
| int num_slots = output->dims->data[1]; |
| int depth = output->dims->data[2] * output->dims->data[3]; |
|
|
| if (input->type == kTfLiteInt8) { |
| EvalInt8(tflite::micro::GetTensorData<int8_t>(input), num_slots, depth, |
| tflite::micro::GetTensorData<int8_t>(output)); |
| } else { |
| MicroPrintf("Type %s (%d) not supported.", |
| TfLiteTypeGetName(input->type), input->type); |
| return kTfLiteError; |
| } |
|
|
| if (--data->cycles_until_run != 0) { |
| |
| |
| |
| return static_cast<TfLiteStatus>(kTfLiteAbort); |
| } |
|
|
| data->cycles_until_run = data->cycles_max; |
|
|
| return kTfLiteOk; |
| } |
|
|
| TfLiteRegistration* Register_CIRCULAR_BUFFER() { |
| static TfLiteRegistration r = tflite::micro::RegisterOp( |
| CircularBufferInit, CircularBufferPrepare, CircularBufferEval); |
| return &r; |
| } |
|
|
| } |
|
|