Exports the module and returns the serialized program data.
(
module_class: Type[nn.Module],
external_constants: bool = False,
)
| 315 | |
| 316 | |
| 317 | def export_module_to_program( |
| 318 | module_class: Type[nn.Module], |
| 319 | external_constants: bool = False, |
| 320 | ) -> ExecutorchProgramManager: |
| 321 | """Exports the module and returns the serialized program data.""" |
| 322 | torch.manual_seed(0) |
| 323 | # Look for an optional @staticmethod that defines custom trace params. |
| 324 | export_kwargs: Dict[str, Any] = {} |
| 325 | if hasattr(module_class, "get_export_kwargs"): |
| 326 | # pyre-ignore[16]: pyre doesn't know about get_export_kwargs. |
| 327 | export_kwargs = module_class.get_export_kwargs() |
| 328 | export_joint = False |
| 329 | export_state_names = False |
| 330 | share_mutable_buffers = False |
| 331 | if hasattr(module_class, "export_joint"): |
| 332 | # pyre-ignore[16]: pyre just cant figure it out |
| 333 | export_joint = module_class.export_joint() |
| 334 | if hasattr(module_class, "export_state_names"): |
| 335 | # pyre-ignore[16]: pyre just cant figure it out |
| 336 | export_state_names = module_class.export_state_names() |
| 337 | if hasattr(module_class, "get_method_names_to_export"): |
| 338 | # pyre-ignore[16]: pyre just cant figure it out |
| 339 | methods = module_class.get_method_names_to_export() |
| 340 | else: |
| 341 | methods = ["forward"] |
| 342 | if hasattr(module_class, "share_mutable_buffers"): |
| 343 | # pyre-ignore[16]: pyre just cant figure it out |
| 344 | share_mutable_buffers = module_class.share_mutable_buffers() |
| 345 | module = ExportedModule.export( |
| 346 | module_class, |
| 347 | methods, |
| 348 | export_joint_graph=export_joint, |
| 349 | external_constants=external_constants, |
| 350 | export_state_names=export_state_names, |
| 351 | share_mutable_buffers=share_mutable_buffers, |
| 352 | **export_kwargs, |
| 353 | ) |
| 354 | return module.executorch_program |
| 355 | |
| 356 | |
| 357 | def main() -> None: |
no test coverage detected