Update the graph signature's user_input/user_outputs.
(
old_signature: ExportGraphSignature,
new_gm: torch.fx.GraphModule,
)
| 163 | |
| 164 | |
| 165 | def _get_updated_graph_signature( |
| 166 | old_signature: ExportGraphSignature, |
| 167 | new_gm: torch.fx.GraphModule, |
| 168 | ) -> ExportGraphSignature: |
| 169 | """ |
| 170 | Update the graph signature's user_input/user_outputs. |
| 171 | """ |
| 172 | new_input_specs = [] |
| 173 | i = 0 |
| 174 | for node in new_gm.graph.nodes: |
| 175 | if node.op != "placeholder": |
| 176 | continue |
| 177 | |
| 178 | assert i < len( |
| 179 | old_signature.input_specs |
| 180 | ), "Number of inputs changed after transformation" |
| 181 | old_input_spec = old_signature.input_specs[i] |
| 182 | arg = ( |
| 183 | old_input_spec.arg |
| 184 | if isinstance(old_input_spec.arg, ConstantArgument) |
| 185 | # pyre-fixme[20]: Argument `class_fqn` expected. |
| 186 | else type(old_input_spec.arg)(node.name) |
| 187 | ) |
| 188 | new_input_specs.append( |
| 189 | InputSpec( |
| 190 | old_input_spec.kind, |
| 191 | arg, |
| 192 | old_input_spec.target, |
| 193 | persistent=old_input_spec.persistent, |
| 194 | ) |
| 195 | ) |
| 196 | i += 1 |
| 197 | |
| 198 | output_node = new_gm.graph.output_node() |
| 199 | assert output_node.op == "output" |
| 200 | |
| 201 | new_output_specs = [] |
| 202 | for i, node in enumerate(output_node.args[0]): |
| 203 | assert i < len( |
| 204 | old_signature.output_specs |
| 205 | ), "Number of outputs changed after transformation" |
| 206 | old_output_spec = old_signature.output_specs[i] |
| 207 | arg = ( |
| 208 | old_output_spec.arg |
| 209 | if isinstance(old_output_spec.arg, ConstantArgument) |
| 210 | # pyre-fixme[20]: Argument `class_fqn` expected. |
| 211 | else type(old_output_spec.arg)(node.name) |
| 212 | ) |
| 213 | new_output_specs.append( |
| 214 | OutputSpec(old_output_spec.kind, arg, old_output_spec.target) |
| 215 | ) |
| 216 | |
| 217 | new_signature = ExportGraphSignature( |
| 218 | input_specs=new_input_specs, output_specs=new_output_specs |
| 219 | ) |
| 220 | return new_signature |
| 221 | |
| 222 |
no test coverage detected