if.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 then_subgraph_index;
  27. int else_subgraph_index;
  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 TfLiteIfParams*>(node->builtin_data);
  37. op_data->then_subgraph_index = params->then_subgraph_index;
  38. op_data->else_subgraph_index = params->else_subgraph_index;
  39. TF_LITE_ENSURE(context, node->inputs->size > 0);
  40. // The first input is the condition.
  41. tflite::MicroContext* micro_context = tflite::GetMicroContext(context);
  42. TfLiteTensor* cond = micro_context->AllocateTempInputTensor(node, 0);
  43. TF_LITE_ENSURE(context, cond != nullptr);
  44. TF_LITE_ENSURE_EQ(context, cond->type, kTfLiteBool);
  45. TF_LITE_ENSURE_EQ(context, NumElements(cond), 1);
  46. micro_context->DeallocateTempTfLiteTensor(cond);
  47. // The first input of the node is the condition. The rest of inputs are
  48. // passed to the branch subgraphs. Therefore, the number of subgraph inputs
  49. // will be the number of node inputs - 1.
  50. size_t num_inputs = node->inputs->size - 1;
  51. size_t num_outputs = node->outputs->size;
  52. MicroGraph& graph_info = micro_context->graph();
  53. TF_LITE_ENSURE(context,
  54. op_data->then_subgraph_index < graph_info.NumSubgraphs());
  55. TF_LITE_ENSURE(context,
  56. op_data->else_subgraph_index < graph_info.NumSubgraphs());
  57. TF_LITE_ENSURE_EQ(context, num_inputs,
  58. graph_info.NumSubgraphInputs(op_data->then_subgraph_index));
  59. TF_LITE_ENSURE_EQ(
  60. context, num_outputs,
  61. graph_info.NumSubgraphOutputs(op_data->then_subgraph_index));
  62. return kTfLiteOk;
  63. }
  64. TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
  65. const OpData* op_data = reinterpret_cast<OpData*>(node->user_data);
  66. tflite::MicroContext* micro_context = tflite::GetMicroContext(context);
  67. TfLiteTensor* cond = micro_context->AllocateTempInputTensor(node, 0);
  68. TF_LITE_ENSURE(context, cond != nullptr);
  69. bool cond_value = cond->data.b[0];
  70. micro_context->DeallocateTempTfLiteTensor(cond);
  71. MicroGraph* graph_info = &micro_context->graph();
  72. // Currently we copy the input / output between the subgraphs.
  73. int active_branch_subgraph_index =
  74. cond_value ? op_data->then_subgraph_index : op_data->else_subgraph_index;
  75. TF_LITE_ENSURE_OK(context,
  76. tflite::micro::CopyOpInputsToSubgraphInputs(
  77. context, node, graph_info, active_branch_subgraph_index,
  78. /*first_tensor_idx=*/1));
  79. TF_LITE_ENSURE_OK(context,
  80. graph_info->InvokeSubgraph(active_branch_subgraph_index));
  81. TF_LITE_ENSURE_OK(
  82. context, tflite::micro::CopySubgraphOutputsToOpOutputs(
  83. context, node, graph_info, active_branch_subgraph_index));
  84. return kTfLiteOk;
  85. }
  86. } // namespace.
  87. TfLiteRegistration Register_IF() {
  88. return tflite::micro::RegisterOp(Init, Prepare, Eval);
  89. }
  90. } // namespace tflite