Insert a cursor at the node location in the fx.Graph. e.g: # graph(): # %x : [#users=1] = placeholder[target=x] # %param : [#users=1] = get_attr[target=param] # %add : [#users=1] = call_function[target=operator.add](args = (%x, %param), kwargs = {}) # --> %linear :
(graph: torch.fx.Graph, finding_node: torch.fx.Node)
| 318 | |
| 319 | |
| 320 | def add_cursor_to_graph(graph: torch.fx.Graph, finding_node: torch.fx.Node) -> str: |
| 321 | """ |
| 322 | Insert a cursor at the node location in the fx.Graph. |
| 323 | e.g: |
| 324 | # graph(): |
| 325 | # %x : [#users=1] = placeholder[target=x] |
| 326 | # %param : [#users=1] = get_attr[target=param] |
| 327 | # %add : [#users=1] = call_function[target=operator.add](args = (%x, %param), kwargs = {}) |
| 328 | # --> %linear : [#users=1] = call_module[target=linear](args = (%add,), kwargs = {}) |
| 329 | # %clamp : [#users=1] = call_method[target=clamp](args = (%linear,), kwargs = {min: 0.0, max: 1.0}) |
| 330 | # return clamp |
| 331 | |
| 332 | This is mostly used for error reporting |
| 333 | """ |
| 334 | |
| 335 | new_graph = copy.deepcopy(graph) |
| 336 | |
| 337 | found_at = -1 |
| 338 | for ix, node in enumerate(graph.nodes): |
| 339 | if node == finding_node: |
| 340 | found_at = ix |
| 341 | |
| 342 | # This is heavily based on __str__ method of fx.Graph |
| 343 | def _format_graph(graph: torch.fx.Graph, offending_node_idx: int) -> str: |
| 344 | s = "graph():" |
| 345 | for ix, node in enumerate(graph.nodes): |
| 346 | node_str = node.format_node() |
| 347 | if node_str: |
| 348 | if ix != offending_node_idx: |
| 349 | s += "\n " + node_str |
| 350 | else: |
| 351 | s += "\n--> " + node_str |
| 352 | return s |
| 353 | |
| 354 | return _format_graph(new_graph, found_at) |
| 355 | |
| 356 | |
| 357 | def _stacktrace_to_framelist(stacktrace: str) -> FrameList: |