(
model: torch.fx.GraphModule,
skip_stack_trace: Optional[bool] = False,
enable_module_hierarchy: bool = False,
)
| 214 | # Given an FX GraphModule, parse it into an OperatorGraph |
| 215 | @staticmethod |
| 216 | def gen_operator_graph( |
| 217 | model: torch.fx.GraphModule, |
| 218 | skip_stack_trace: Optional[bool] = False, |
| 219 | enable_module_hierarchy: bool = False, |
| 220 | ) -> FXOperatorGraph: |
| 221 | graph: torch.fx.Graph = model.graph |
| 222 | |
| 223 | nodes = {} |
| 224 | input_nodes = {} |
| 225 | output_nodes = {} |
| 226 | out_variant_output_nodes = set() |
| 227 | module_mapping = defaultdict(list) |
| 228 | |
| 229 | const_count = 0 |
| 230 | for fx_node in graph.nodes: |
| 231 | if ( |
| 232 | fx_node.target == exir.memory.alloc |
| 233 | or fx_node.target == operator.getitem |
| 234 | ): |
| 235 | continue |
| 236 | op = fx_node.op |
| 237 | name = FXOperatorGraph._get_node_name(fx_node) |
| 238 | target = fx_node.target |
| 239 | args = fx_node.args |
| 240 | kwargs = fx_node.kwargs |
| 241 | metadata = FXOperatorGraph._extract_metadata(fx_node.meta, skip_stack_trace) |
| 242 | output_shapes = FXOperatorGraph._extract_output_shapes( |
| 243 | fx_node.meta.get("val") |
| 244 | ) |
| 245 | dtype = FXOperatorGraph._extract_output_dtype(fx_node.meta.get("val")) or "" |
| 246 | |
| 247 | assert ( |
| 248 | op != "call_module" |
| 249 | ), f"Module call not yet supported in edge model graph [.toEdge()]: {name}, {str(target)}" |
| 250 | assert ( |
| 251 | op != "call_method" |
| 252 | ), f"Call Method not yet supported in edge model graph [.toEdge()]: {name}, {str(target)}" |
| 253 | |
| 254 | # Input |
| 255 | if op == "placeholder": |
| 256 | node = ValueNode( |
| 257 | name, |
| 258 | output_shapes=output_shapes, |
| 259 | metadata=metadata, |
| 260 | dtype=str(dtype), |
| 261 | ) # val is default arg |
| 262 | input_nodes[name] = node |
| 263 | # Constants |
| 264 | elif op == "get_attr": |
| 265 | node = ValueNode( |
| 266 | name, |
| 267 | output_shapes=output_shapes, |
| 268 | metadata=metadata, |
| 269 | dtype=str(dtype), |
| 270 | ) |
| 271 | # Output |
| 272 | elif op == "output": |
| 273 | assert len(args) == 1 |
no test coverage detected