Create a graph module with a single op. Args: placeholders: Placeholders to be used as inputs to the GraphModule. op: The op to be inserted. args: The args to be passed to the op. kwargs: The kwargs to be passed to the op. Returns: A graph module wit
(
placeholders: Sequence[Union[torch.Tensor, FakeTensor]],
op: Target,
args: Sequence[Argument],
kwargs: Optional[dict[str, Argument]] = None,
)
| 114 | |
| 115 | |
| 116 | def single_op_builder( |
| 117 | placeholders: Sequence[Union[torch.Tensor, FakeTensor]], |
| 118 | op: Target, |
| 119 | args: Sequence[Argument], |
| 120 | kwargs: Optional[dict[str, Argument]] = None, |
| 121 | ) -> torch.fx.GraphModule: |
| 122 | """Create a graph module with a single op. |
| 123 | |
| 124 | Args: |
| 125 | placeholders: Placeholders to be used as inputs to the GraphModule. |
| 126 | op: The op to be inserted. |
| 127 | args: The args to be passed to the op. |
| 128 | kwargs: The kwargs to be passed to the op. |
| 129 | |
| 130 | Returns: |
| 131 | A graph module with a single op |
| 132 | """ |
| 133 | builder = GraphBuilder() |
| 134 | op_to_placeholder_dict = { |
| 135 | p: builder.placeholder(f"p_{i}", p) for i, p in enumerate(placeholders) |
| 136 | } |
| 137 | proxy_args, proxy_kwargs = pytree.tree_map_only( |
| 138 | (torch.Tensor, FakeTensor), lambda x: op_to_placeholder_dict[x], (args, kwargs) |
| 139 | ) |
| 140 | node = builder.call_operator(op, proxy_args, proxy_kwargs) |
| 141 | builder.output([node]) |
| 142 | return builder.get_graph_module() |