| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include <stddef.h> |
|
|
| #include <cstring> |
|
|
| #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/kernel_util.h" |
| #include "edge-impulse-sdk/tensorflow/lite/micro/kernels/kernel_util.h" |
| #include "edge-impulse-sdk/tensorflow/lite/micro/memory_helpers.h" |
| #include "edge-impulse-sdk/tensorflow/lite/micro/micro_context.h" |
| #include "edge-impulse-sdk/tensorflow/lite/micro/micro_graph.h" |
| #include "edge-impulse-sdk/tensorflow/lite/schema/schema_generated.h" |
|
|
| namespace tflite { |
|
|
| namespace { |
|
|
| struct OpData { |
| int init_subgraph_index; |
| bool has_run; |
| }; |
|
|
| void* Init(TfLiteContext* context, const char* buffer, size_t length) { |
| TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); |
| return context->AllocatePersistentBuffer(context, sizeof(OpData)); |
| } |
|
|
| TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { |
| OpData* op_data = reinterpret_cast<OpData*>(node->user_data); |
| const auto* params = |
| reinterpret_cast<const TfLiteCallOnceParams*>(node->builtin_data); |
| op_data->init_subgraph_index = params->init_subgraph_index; |
| op_data->has_run = false; |
|
|
| TF_LITE_ENSURE(context, NumInputs(node) == 0); |
| TF_LITE_ENSURE(context, NumOutputs(node) == 0); |
|
|
| tflite::MicroContext* micro_context = tflite::GetMicroContext(context); |
| MicroGraph& graph_info = micro_context->graph(); |
|
|
| TF_LITE_ENSURE(context, |
| op_data->init_subgraph_index < graph_info.NumSubgraphs()); |
|
|
| return kTfLiteOk; |
| } |
|
|
| TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { |
| OpData* op_data = reinterpret_cast<OpData*>(node->user_data); |
|
|
| |
| if (op_data->has_run) { |
| return kTfLiteOk; |
| } |
|
|
| tflite::MicroContext* micro_context = tflite::GetMicroContext(context); |
| MicroGraph& graph_info = micro_context->graph(); |
|
|
| TF_LITE_ENSURE_OK(context, |
| graph_info.InvokeSubgraph(op_data->init_subgraph_index)); |
|
|
| op_data->has_run = true; |
|
|
| return kTfLiteOk; |
| } |
|
|
| } |
|
|
| TfLiteRegistration Register_CALL_ONCE() { |
| return tflite::micro::RegisterOp(Init, Prepare, Eval); |
| } |
|
|
| } |
|
|