| 127 | } |
| 128 | |
| 129 | Status Prepare(TfLiteContext* context, |
| 130 | const TfLiteDelegateParams* delegate_params) { |
| 131 | // Extract TFLite delegate execution plan from the context and convert it |
| 132 | // into FlowGraph32. |
| 133 | GraphFloat32 graph; |
| 134 | RETURN_IF_ERROR(BuildModel(context, delegate_params, &graph)); |
| 135 | |
| 136 | // Apply general transformations on the graph. |
| 137 | NullTransformationReporter reporter; |
| 138 | ModelTransformer transformer(&graph, &reporter); |
| 139 | if (!ApplyGeneralTransformations(&transformer)) { |
| 140 | return InternalError("Graph general transformations failed"); |
| 141 | } |
| 142 | |
| 143 | if (!env_) RETURN_IF_ERROR(EglEnvironment::NewEglEnvironment(&env_)); |
| 144 | |
| 145 | // TODO(impjdi): Remove code duplication. |
| 146 | auto values = graph.values(); |
| 147 | auto find_value = [&](int tensor_index) -> Value<TensorRef<BHWC>>* { |
| 148 | for (auto value : values) { |
| 149 | if (value->tensor.ref == tensor_index) return value; |
| 150 | } |
| 151 | return nullptr; |
| 152 | }; |
| 153 | tensors_.reserve(values.back()->id + 1); |
| 154 | for (auto value : values) { |
| 155 | if (tensors_.size() <= value->id) { |
| 156 | tensors_.resize(value->id + 1); |
| 157 | } |
| 158 | tensors_[value->id] = {value->tensor.shape, 0}; |
| 159 | } |
| 160 | |
| 161 | std::unordered_set<int> tflite_graph_io; |
| 162 | |
| 163 | // Prepare graph inputs. |
| 164 | // |
| 165 | // Note that graph.inputs() cannot be used directly, as the notion of |
| 166 | // graph input has a different meaning in public API and GPU-internal API. |
| 167 | { |
| 168 | inputs_.clear(); |
| 169 | inputs_.reserve(delegate_params->input_tensors->size); |
| 170 | for (int i = 0; i < delegate_params->input_tensors->size; ++i) { |
| 171 | const int tensor_index = delegate_params->input_tensors->data[i]; |
| 172 | auto* tensor = context->tensors + tensor_index; |
| 173 | if (tensor->allocation_type == TfLiteAllocationType::kTfLiteMmapRo) { |
| 174 | continue; |
| 175 | } |
| 176 | tflite_graph_io.insert(tensor_index); |
| 177 | const auto* input = find_value(tensor_index); |
| 178 | if (!input || tensor->type != TfLiteType::kTfLiteFloat32) { |
| 179 | return NotFoundError("Input tensor is not found in the graph."); |
| 180 | } |
| 181 | |
| 182 | inputs_.push_back(input->id); |
| 183 | tensor->buffer_handle = input->id; |
| 184 | tensor->delegate = &delegate_; |
| 185 | tensors_[input->id].tensor_index = tensor_index; |
| 186 |
no test coverage detected