28 auto *main_runtime_graph = runtime_graph;
32 const auto *options = cur_op->builtin_options_as_WhileOptions();
33 const auto body_subgraph_index = options->body_subgraph_index();
34 const auto cond_subgraph_index = options->cond_subgraph_index();
36 auto *cond_runtime_graph = runtime_module->getRuntimeGraphAt(cond_subgraph_index);
37 auto *body_runtime_graph = runtime_module->getRuntimeGraphAt(body_subgraph_index);
39 body_runtime_graph->selectOwnSubgraph();
40 const auto body_input_size = body_runtime_graph->getNumOfInputTensors();
41 const auto body_output_size = body_runtime_graph->getNumOfOutputTensors();
45 body_runtime_graph->invalidate();
46 body_runtime_graph->configure(
false);
48 cond_runtime_graph->selectOwnSubgraph();
49 const auto cond_input_size = cond_runtime_graph->getNumOfInputTensors();
50 const auto cond_output_size = cond_runtime_graph->getNumOfOutputTensors();
53 const circle::Tensor *cond_output_tensor = cond_runtime_graph->getOutputTensorByIndex(0);
55 cond_runtime_graph->invalidate();
56 cond_runtime_graph->configure(
false);
58 main_runtime_graph->selectOwnSubgraph();
63 auto *main_runtime_graph = runtime_graph;
66 const auto input_size = cur_op->inputs()->size();
68 std::vector<uint8_t *> operation_inputs_data(input_size);
69 std::vector<uint8_t *> operation_outputs_data;
71 std::vector<int32_t> input_sizes(input_size);
75 for (int32_t i = 0; i < input_size; ++i)
77 const auto op_input_index = cur_op->inputs()->operator[](i);
78 const auto op_output_index = cur_op->outputs()->operator[](i);
79 assert(op_input_index != -1);
80 assert(op_output_index != -1);
81 const auto input = main_runtime_graph->getCircleTensorByIndex(op_input_index);
82 const auto output = main_runtime_graph->getCircleTensorByIndex(op_output_index);
84 input_sizes[i] = Tensor::num_elements(input) *
size(Tensor::element_type(input));
86 auto *input_data = main_runtime_graph->getDataByTensor(input);
88 uint8_t *tensor_data =
nullptr;
91 if (input_data ==
nullptr)
93 tensor_data =
new uint8_t[input_sizes[i]];
94 input_data = main_runtime_graph->getConstDataByTensor(input);
95 assert(input_data !=
nullptr);
96 std::memcpy(tensor_data, input_data, input_sizes[i]);
100 tensor_data = input_data;
105 if (input_data ==
nullptr)
106 input_data = main_runtime_graph->getConstDataByTensor(input);
107 assert(input_data !=
nullptr);
108 tensor_data = main_runtime_graph->getDataByTensor(output);
109 assert(tensor_data !=
nullptr);
110 std::memcpy(tensor_data, input_data, input_sizes[i]);
112 assert(tensor_data !=
nullptr);
114 operation_inputs_data[i] = tensor_data;
117 const auto *options = cur_op->builtin_options_as_WhileOptions();
118 const auto body_subgraph_index = options->body_subgraph_index();
119 const auto cond_subgraph_index = options->cond_subgraph_index();
121 auto *cond_runtime_graph = runtime_module->getRuntimeGraphAt(cond_subgraph_index);
122 auto *body_runtime_graph = runtime_module->getRuntimeGraphAt(body_subgraph_index);
126 cond_runtime_graph->selectOwnSubgraph();
128 for (int32_t i = 0; i < input_size; ++i)
129 cond_runtime_graph->configureGraphInput(i, operation_inputs_data[i]);
131 cond_runtime_graph->execute();
133 bool cond_value = (cond_runtime_graph->getOutputDataByIndex(0))[0];
137 body_runtime_graph->selectOwnSubgraph();
138 for (int32_t i = 0; i < input_size; ++i)
139 body_runtime_graph->configureGraphInput(i, operation_inputs_data[i]);
141 body_runtime_graph->execute();
143 for (int32_t i = 0; i < input_size; ++i)
145 auto cur_output_body_data = body_runtime_graph->getOutputDataByIndex(i);
146 if (cur_output_body_data ==
nullptr)
148 std::memcpy(operation_inputs_data[i], cur_output_body_data, input_sizes[i]);
152 cond_runtime_graph->resetOutputTensorsData();
153 cond_runtime_graph->clearTensors();
155 body_runtime_graph->selectOwnSubgraph();
156 body_runtime_graph->resetOutputTensorsData();
157 body_runtime_graph->clearTensors();
159 main_runtime_graph->selectOwnSubgraph();
163 for (int32_t i = 0; i < input_size; ++i)
165 const auto op_input_index = cur_op->inputs()->operator[](i);
166 const auto op_output_index = cur_op->outputs()->operator[](i);
167 assert(op_input_index != -1);
168 assert(op_output_index != -1);
169 const auto input = main_runtime_graph->getCircleTensorByIndex(op_input_index);
170 const auto output = main_runtime_graph->getCircleTensorByIndex(op_output_index);
172 if (main_runtime_graph->getDataByTensor(input))
174 main_runtime_graph->makeInplaceOperation(input, output);
178 main_runtime_graph->setDataToTensor(output, operation_inputs_data[i]);