|
| 1 | +# Copyright 2025 Arm Limited and/or its affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import json |
| 9 | + |
| 10 | +from dataclasses import asdict, dataclass |
| 11 | +from typing import Any |
| 12 | + |
| 13 | +import serializer.tosa_serializer as ts # type: ignore |
| 14 | +import torch |
| 15 | + |
| 16 | +from torch.fx.traceback import NodeSource |
| 17 | + |
| 18 | + |
| 19 | +@dataclass |
| 20 | +class TosaDebugSchema: |
| 21 | + node_name: str |
| 22 | + operator_name: str |
| 23 | + operator_id: int |
| 24 | + |
| 25 | + |
| 26 | +@dataclass |
| 27 | +class ATenDebugSchema: |
| 28 | + node_name: str |
| 29 | + operator_name: str |
| 30 | + |
| 31 | + @staticmethod |
| 32 | + def from_node(node: torch.fx.Node) -> ATenDebugSchema: |
| 33 | + # node.target is Union[Callable[..., Any], str], so we need to access this correctly depending on the type |
| 34 | + if callable(node.target): |
| 35 | + operator_name = node.target.__name__ |
| 36 | + else: |
| 37 | + operator_name = node.target |
| 38 | + |
| 39 | + return ATenDebugSchema(node_name=node.name, operator_name=operator_name) |
| 40 | + |
| 41 | + |
| 42 | +@dataclass |
| 43 | +class TorchDebugSchema: |
| 44 | + stack_trace: list[str] |
| 45 | + node_trace: list[dict[str, Any]] | str |
| 46 | + nn_module_stack: dict[str, Any] | str |
| 47 | + torch_fn: tuple[str, str] | str |
| 48 | + |
| 49 | + @staticmethod |
| 50 | + def serialize_node_trace(node_trace: list[NodeSource]) -> list[dict[str, Any]]: |
| 51 | + """Flatten the from_node dictionary to remove nesting.""" |
| 52 | + flattened = [] |
| 53 | + node_stack = [] |
| 54 | + |
| 55 | + for n in node_trace: |
| 56 | + node_stack.append((n, -1)) |
| 57 | + |
| 58 | + while len(node_stack) > 0: |
| 59 | + node, parent_id = node_stack.pop() |
| 60 | + flattened.append( |
| 61 | + { |
| 62 | + "name": node.name, |
| 63 | + "target": node.target, |
| 64 | + "graph_id": node.graph_id, |
| 65 | + "pass_name": node.pass_name, |
| 66 | + "action": node._get_action_string(), |
| 67 | + "parent_graph_id": parent_id, |
| 68 | + } |
| 69 | + ) |
| 70 | + |
| 71 | + for n in node.from_node: |
| 72 | + node_stack.append((n, node.graph_id)) |
| 73 | + |
| 74 | + return flattened |
| 75 | + |
| 76 | + @staticmethod |
| 77 | + def from_node(node: torch.fx.Node) -> TorchDebugSchema: |
| 78 | + node_trace: str | list[dict[str, Any]] = "No node trace available." |
| 79 | + |
| 80 | + if "from_node" in node.meta: |
| 81 | + # Flatten the node_trace dictionary, so there is no nesting |
| 82 | + node_trace = TorchDebugSchema.serialize_node_trace(node.meta["from_node"]) |
| 83 | + |
| 84 | + return TorchDebugSchema( |
| 85 | + stack_trace=node.meta.get("stack_trace", "No stack trace available").split( |
| 86 | + "\n" |
| 87 | + ), |
| 88 | + node_trace=node_trace, |
| 89 | + nn_module_stack=node.meta.get( |
| 90 | + "nn_module_stack", "No module stack trace available" |
| 91 | + ), |
| 92 | + torch_fn=node.meta.get("torch_fn", "No torch_fn available"), |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +@dataclass |
| 97 | +class DebugSchema: |
| 98 | + event_id: int |
| 99 | + aten_info: ATenDebugSchema |
| 100 | + tosa_info: TosaDebugSchema |
| 101 | + torch_info: TorchDebugSchema |
| 102 | + |
| 103 | + |
| 104 | +class DebugHook: |
| 105 | + def __init__(self) -> None: |
| 106 | + self._debug_events: list[DebugSchema] = [] |
| 107 | + self.__op_id_to_name = {} |
| 108 | + |
| 109 | + # Build up a mapping from TOSA 1.0 operator IDs to their names |
| 110 | + for name, val in vars(ts.Op).items(): |
| 111 | + self.__op_id_to_name[val] = name |
| 112 | + |
| 113 | + def add(self, node: torch.fx.Node, tosa_op: Any, tosa_op_id: int) -> None: |
| 114 | + tosa_debug_info = TosaDebugSchema( |
| 115 | + node_name=str(tosa_op), |
| 116 | + operator_name=self.__op_id_to_name[tosa_op_id], |
| 117 | + operator_id=tosa_op_id, |
| 118 | + ) |
| 119 | + |
| 120 | + aten_debug_info = ATenDebugSchema.from_node(node) |
| 121 | + torch_debug_info = TorchDebugSchema.from_node(node) |
| 122 | + |
| 123 | + self._debug_events.append( |
| 124 | + DebugSchema( |
| 125 | + event_id=len(self._debug_events), |
| 126 | + aten_info=aten_debug_info, |
| 127 | + tosa_info=tosa_debug_info, |
| 128 | + torch_info=torch_debug_info, |
| 129 | + ) |
| 130 | + ) |
| 131 | + |
| 132 | + def serialize(self) -> str: |
| 133 | + return json.dumps([asdict(event) for event in self._debug_events], indent=4) |
0 commit comments