|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +# pyre-unsafe |
| 8 | + |
| 9 | +import torch |
| 10 | +from torch.export.exported_program import ( |
| 11 | + ConstantArgument, |
| 12 | + ExportGraphSignature, |
| 13 | + InputSpec, |
| 14 | + OutputSpec, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +def _get_updated_range_constraints(gm): |
| 19 | + def get_shape_env(gm): |
| 20 | + vals = [ |
| 21 | + node.meta["val"] |
| 22 | + for node in gm.graph.nodes |
| 23 | + if node.meta.get("val", None) is not None |
| 24 | + ] |
| 25 | + from torch._guards import detect_fake_mode # type: ignore[21] |
| 26 | + |
| 27 | + fake_mode = detect_fake_mode(vals) |
| 28 | + if fake_mode is not None: |
| 29 | + return fake_mode.shape_env |
| 30 | + for v in vals: |
| 31 | + if isinstance(v, torch.SymInt): |
| 32 | + return v.node.shape_env |
| 33 | + |
| 34 | + shape_env = get_shape_env(gm) |
| 35 | + if shape_env is None: |
| 36 | + return {} |
| 37 | + range_constraints = { |
| 38 | + shape_env.replacements.get(k, k): v for k, v in shape_env.var_to_range.items() |
| 39 | + } |
| 40 | + # Only when we have an unbacked symint, and it's used as constructor inputs, |
| 41 | + # runtime_var_to_range will make a difference compated to var_to_range. |
| 42 | + # e.g. [2, oo) -> [0, oo) |
| 43 | + for k, v in shape_env.var_to_range.items(): |
| 44 | + if k not in shape_env.replacements: |
| 45 | + range_constraints[k] = v |
| 46 | + return range_constraints |
| 47 | + |
| 48 | + |
| 49 | +def _get_updated_graph_signature( |
| 50 | + old_signature: ExportGraphSignature, |
| 51 | + new_gm: torch.fx.GraphModule, |
| 52 | +) -> ExportGraphSignature: |
| 53 | + """ |
| 54 | + Update the graph signature's user_input/user_outputs. |
| 55 | + """ |
| 56 | + new_input_specs = [] |
| 57 | + i = 0 |
| 58 | + for node in new_gm.graph.nodes: |
| 59 | + if node.op != "placeholder": |
| 60 | + continue |
| 61 | + |
| 62 | + assert i < len( |
| 63 | + old_signature.input_specs |
| 64 | + ), "Number of inputs changed after transformation" |
| 65 | + old_input_spec = old_signature.input_specs[i] |
| 66 | + arg = ( |
| 67 | + old_input_spec.arg |
| 68 | + if isinstance(old_input_spec.arg, ConstantArgument) |
| 69 | + # pyre-fixme[20]: Argument `class_fqn` expected. |
| 70 | + else type(old_input_spec.arg)(node.name) |
| 71 | + ) |
| 72 | + new_input_specs.append( |
| 73 | + InputSpec( |
| 74 | + old_input_spec.kind, |
| 75 | + arg, |
| 76 | + old_input_spec.target, |
| 77 | + persistent=old_input_spec.persistent, |
| 78 | + ) |
| 79 | + ) |
| 80 | + i += 1 |
| 81 | + |
| 82 | + output_node = new_gm.graph.output_node() |
| 83 | + assert output_node.op == "output" |
| 84 | + |
| 85 | + new_output_specs = [] |
| 86 | + for i, node in enumerate(output_node.args[0]): |
| 87 | + assert i < len( |
| 88 | + old_signature.output_specs |
| 89 | + ), "Number of outputs changed after transformation" |
| 90 | + old_output_spec = old_signature.output_specs[i] |
| 91 | + arg = ( |
| 92 | + old_output_spec.arg |
| 93 | + if isinstance(old_output_spec.arg, ConstantArgument) |
| 94 | + # pyre-fixme[20]: Argument `class_fqn` expected. |
| 95 | + else type(old_output_spec.arg)(node.name) |
| 96 | + ) |
| 97 | + new_output_specs.append( |
| 98 | + OutputSpec(old_output_spec.kind, arg, old_output_spec.target) |
| 99 | + ) |
| 100 | + |
| 101 | + new_signature = ExportGraphSignature( |
| 102 | + input_specs=new_input_specs, output_specs=new_output_specs |
| 103 | + ) |
| 104 | + return new_signature |
0 commit comments