Create a fake exported program. This uses fake tensors for the state dict to prevent mutation, and points to the real constants, to avoid large memory usage from copying when constants are large. Args: real_exported_program: the original exported program Returns: A n
(real_exported_program: ExportedProgram)
| 16 | |
| 17 | |
| 18 | def get_fake_program(real_exported_program: ExportedProgram) -> ExportedProgram: |
| 19 | """Create a fake exported program. This uses fake tensors for the state dict |
| 20 | to prevent mutation, and points to the real constants, to avoid large memory |
| 21 | usage from copying when constants are large. |
| 22 | |
| 23 | Args: |
| 24 | real_exported_program: the original exported program |
| 25 | Returns: |
| 26 | A new exported program, with fake tensors. |
| 27 | """ |
| 28 | fake_mode = detect_fake_mode( |
| 29 | tuple( |
| 30 | node.meta["val"] |
| 31 | for node in real_exported_program.graph.nodes |
| 32 | if node.op == "placeholder" |
| 33 | ) |
| 34 | ) |
| 35 | if fake_mode is None: |
| 36 | raise AssertionError( |
| 37 | "Could not detect fake mode for graph: ", real_exported_program.graph |
| 38 | ) |
| 39 | |
| 40 | new_state_dict: Dict[str, Union[torch.Tensor, torch.nn.Parameter]] = {} |
| 41 | |
| 42 | for key, tensor in real_exported_program.state_dict.items(): |
| 43 | fake = fake_mode.from_tensor(tensor, static_shapes=True) |
| 44 | new_state_dict[key] = fake |
| 45 | |
| 46 | gm = copy.deepcopy(real_exported_program.graph_module) |
| 47 | fake_exported_program = ExportedProgram( |
| 48 | root=gm, |
| 49 | graph=gm.graph, |
| 50 | graph_signature=copy.deepcopy(real_exported_program.graph_signature), |
| 51 | state_dict=new_state_dict, |
| 52 | range_constraints=copy.deepcopy(real_exported_program.range_constraints), |
| 53 | module_call_graph=copy.deepcopy(real_exported_program.module_call_graph), |
| 54 | constants=real_exported_program.constants, |
| 55 | verifiers=[real_exported_program.verifier], |
| 56 | ) |
| 57 | return fake_exported_program |
| 58 | |
| 59 | |
| 60 | def update_to_real_program( |