| 25 | namespace grappler { |
| 26 | |
| 27 | void CanonicalizeNode(NodeDef* node) { |
| 28 | if (node->input_size() < 2) return; |
| 29 | // Partition control and regular inputs. |
| 30 | int index = 0; |
| 31 | for (; index < node->input_size(); ++index) { |
| 32 | if (IsControlInput(node->input(index))) { |
| 33 | break; |
| 34 | } |
| 35 | } |
| 36 | auto* input = node->mutable_input(); |
| 37 | // Maybe sort regular inputs. |
| 38 | if (IsCommutative(*node) && index > 0) { |
| 39 | std::sort(input->begin(), input->begin() + index); |
| 40 | } |
| 41 | // Sort and dedup control inputs. |
| 42 | if (index < node->input_size()) { |
| 43 | std::sort(input->begin() + index, input->end()); |
| 44 | input->erase(std::unique(input->begin() + index, input->end()), |
| 45 | input->end()); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void CanonicalizeGraph(GraphDef* graph) { |
| 50 | for (int i = 0; i < graph->node_size(); ++i) { |