| 218 | } |
| 219 | |
| 220 | void InferenceContext::Merge() { |
| 221 | std::unordered_set<ValueId> ready_tensors; |
| 222 | for (const auto& input_id : input_ids_) { |
| 223 | ready_tensors.insert(input_id); |
| 224 | } |
| 225 | for (int i = 0; i < nodes_.size(); ++i) { |
| 226 | auto& node = nodes_[i]; |
| 227 | for (const auto& out_id : node.outputs) { |
| 228 | ready_tensors.insert(out_id); |
| 229 | } |
| 230 | if (node.outputs.size() != 1) { |
| 231 | continue; |
| 232 | } |
| 233 | std::vector<int> next_nodes; |
| 234 | for (int j = i + 1; j < nodes_.size(); ++j) { |
| 235 | for (int k = 0; k < nodes_[j].inputs.size(); ++k) { |
| 236 | if (nodes_[j].inputs[k] == node.outputs[0]) { |
| 237 | next_nodes.push_back(j); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | if (next_nodes.size() != 1) { |
| 242 | continue; |
| 243 | } |
| 244 | auto& linkable_node = nodes_[next_nodes[0]]; |
| 245 | auto* elementwise = |
| 246 | dynamic_cast<ElementwiseOperation*>(linkable_node.operations[0].get()); |
| 247 | if (!elementwise || linkable_node.outputs.size() != 1 || |
| 248 | !IsReady(ready_tensors, linkable_node)) { |
| 249 | continue; |
| 250 | } |
| 251 | MergeCLNodes(&linkable_node, &node); |
| 252 | nodes_.erase(nodes_.begin() + next_nodes[0]); |
| 253 | i -= 1; |
| 254 | } |
| 255 | for (auto& node : nodes_) { |
| 256 | for (int j = 1; j < node.operations.size(); ++j) { |
| 257 | auto* elementwise = |
| 258 | dynamic_cast<ElementwiseOperation*>(node.operations[j].get()); |
| 259 | node.operations[0]->AddOperation(elementwise); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | Status InferenceContext::AllocateMemory(const GraphFloat32& graph, |
| 265 | const CLDevice& device, |
no test coverage detected