call_once.cc 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright 2021 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. #include <stddef.h>
  13. #include <cstring>
  14. #include "tensorflow/lite/c/builtin_op_data.h"
  15. #include "tensorflow/lite/c/common.h"
  16. #include "tensorflow/lite/kernels/internal/compatibility.h"
  17. #include "tensorflow/lite/kernels/kernel_util.h"
  18. #include "tensorflow/lite/micro/kernels/kernel_util.h"
  19. #include "tensorflow/lite/micro/memory_helpers.h"
  20. #include "tensorflow/lite/micro/micro_context.h"
  21. #include "tensorflow/lite/micro/micro_graph.h"
  22. #include "tensorflow/lite/schema/schema_generated.h"
  23. namespace tflite {
  24. namespace {
  25. struct OpData {
  26. int init_subgraph_index;
  27. bool has_run;
  28. };
  29. void* Init(TfLiteContext* context, const char* buffer, size_t length) {
  30. TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr);
  31. return context->AllocatePersistentBuffer(context, sizeof(OpData));
  32. }
  33. TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) {
  34. OpData* op_data = reinterpret_cast<OpData*>(node->user_data);
  35. const auto* params =
  36. reinterpret_cast<const TfLiteCallOnceParams*>(node->builtin_data);
  37. op_data->init_subgraph_index = params->init_subgraph_index;
  38. op_data->has_run = false;
  39. TF_LITE_ENSURE(context, NumInputs(node) == 0);
  40. TF_LITE_ENSURE(context, NumOutputs(node) == 0);
  41. tflite::MicroContext* micro_context = tflite::GetMicroContext(context);
  42. MicroGraph& graph_info = micro_context->graph();
  43. TF_LITE_ENSURE(context,
  44. op_data->init_subgraph_index < graph_info.NumSubgraphs());
  45. return kTfLiteOk;
  46. }
  47. TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
  48. OpData* op_data = reinterpret_cast<OpData*>(node->user_data);
  49. // Call once only runs one time then is a no-op for every subsequent call.
  50. if (op_data->has_run) {
  51. return kTfLiteOk;
  52. }
  53. tflite::MicroContext* micro_context = tflite::GetMicroContext(context);
  54. MicroGraph& graph_info = micro_context->graph();
  55. TF_LITE_ENSURE_OK(context,
  56. graph_info.InvokeSubgraph(op_data->init_subgraph_index));
  57. op_data->has_run = true;
  58. return kTfLiteOk;
  59. }
  60. } // namespace.
  61. TfLiteRegistration Register_CALL_ONCE() {
  62. return {/*init=*/Init,
  63. /*free=*/nullptr,
  64. /*prepare=*/Prepare,
  65. /*invoke=*/Eval,
  66. /*profiling_string=*/nullptr,
  67. /*builtin_code=*/0,
  68. /*custom_name=*/nullptr,
  69. /*version=*/0};
  70. }
  71. } // namespace tflite