Traverse the graph module and apply node_op to each node.
(
gm: torch.fx.GraphModule, node_op: Callable[[torch.fx.Node], None]
)
| 134 | |
| 135 | |
| 136 | def bfs_trace_with_node_process( |
| 137 | gm: torch.fx.GraphModule, node_op: Callable[[torch.fx.Node], None] |
| 138 | ) -> None: |
| 139 | """Traverse the graph module and apply node_op to each node.""" |
| 140 | |
| 141 | assert isinstance(gm, torch.fx.GraphModule), f"Expected GraphModule, got {type(gm)}" |
| 142 | |
| 143 | queue = [gm] |
| 144 | while queue: |
| 145 | current_graph_module = queue.pop(0) |
| 146 | for node in current_graph_module.graph.nodes: |
| 147 | node_op(node) |
| 148 | |
| 149 | control_flow_submodules = [ |
| 150 | submodule |
| 151 | for _, submodule, _ in get_control_flow_submodules(current_graph_module) |
| 152 | ] |
| 153 | queue.extend(control_flow_submodules) |
no test coverage detected