Register a scalar constant as a named buffer on the graph module and return a get_attr node referencing it. Used in edge dialect op decomposition passes where raw scalar arguments are not accepted by QNN op builders which need the inputs to be graph nodes.
(
graph: torch.fx.Graph,
graph_module: torch.fx.GraphModule,
attr_name: str,
value,
source_node: torch.fx.Node,
)
| 300 | |
| 301 | |
| 302 | def get_const_node( |
| 303 | graph: torch.fx.Graph, |
| 304 | graph_module: torch.fx.GraphModule, |
| 305 | attr_name: str, |
| 306 | value, |
| 307 | source_node: torch.fx.Node, |
| 308 | ) -> torch.fx.Node: |
| 309 | """ |
| 310 | Register a scalar constant as a named buffer on the graph module and return a get_attr node referencing it. |
| 311 | Used in edge dialect op decomposition passes where raw scalar arguments are not accepted by QNN op builders which need the inputs to be graph nodes. |
| 312 | """ |
| 313 | dtype = source_node.meta["val"].dtype |
| 314 | tensor = torch.tensor(value, dtype=dtype) |
| 315 | graph_module.register_buffer(attr_name, tensor) |
| 316 | |
| 317 | fake_mode = source_node.meta["val"].fake_mode |
| 318 | with graph.inserting_before(next(iter(graph.nodes))): |
| 319 | const_node = graph.get_attr(attr_name) |
| 320 | const_node.meta["val"] = fake_mode.from_tensor(tensor) |
| 321 | return const_node |
| 322 | |
| 323 | |
| 324 | def create_node(graph, target, args, meta, callback=None): |
no test coverage detected