tools.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # mypy: allow-untyped-defs
  2. import logging
  3. import warnings
  4. from collections.abc import Iterable
  5. from typing import Any, Optional
  6. import torch
  7. import torch.export
  8. import torch.export._trace
  9. from torch._utils_internal import log_export_usage
  10. log = logging.getLogger(__name__)
  11. __all__ = ["report_exportability"]
  12. def _generate_inputs_for_submodules(
  13. model: torch.nn.Module,
  14. target_submodules: Iterable[str],
  15. args: tuple[Any, ...],
  16. kwargs: Optional[dict[str, Any]] = None,
  17. ) -> dict[str, tuple[Any, Any]]:
  18. """
  19. Generate inputs for targeting submdoules in the given model. Note that if two submodules refer to the same obj, this
  20. function doesn't work.
  21. Args:
  22. model: root model.
  23. inputs: inputs to the root model.
  24. target_submodules: submodules that we want to generate inputs for.
  25. Returns:
  26. A dict that maps from submodule name to its inputs.
  27. """
  28. kwargs = kwargs or {}
  29. handles = []
  30. results = {}
  31. submodule_to_names = {mod: name for name, mod in model.named_modules()}
  32. def pre_forward(module, module_args, module_kwargs):
  33. results[submodule_to_names[module]] = (module_args, module_kwargs)
  34. try:
  35. for name, mod in model.named_modules():
  36. if name in target_submodules:
  37. handles.append(
  38. mod.register_forward_pre_hook(pre_forward, with_kwargs=True)
  39. )
  40. model(*args, **kwargs)
  41. except Exception as e:
  42. warnings.warn(
  43. f"Failed to generate submodule inputs because of the following error:\n{e}"
  44. )
  45. finally:
  46. for h in handles:
  47. h.remove()
  48. return results
  49. def report_exportability(
  50. mod: torch.nn.Module,
  51. args: tuple[Any, ...],
  52. kwargs: Optional[dict[str, Any]] = None,
  53. *,
  54. strict: bool = True,
  55. pre_dispatch: bool = False,
  56. ) -> dict[str, Optional[Exception]]:
  57. """
  58. Report exportability issues for a module in one-shot.
  59. Args:
  60. mod: root module.
  61. args: args to the root module.
  62. kwargs: kwargs to the root module.
  63. Returns:
  64. A dict that maps from submodule name to the exception that was raised when trying to export it.
  65. `None` means the module is exportable without issue.
  66. Sample output:
  67. {
  68. '': UnsupportedOperatorException(func=<OpOverload(op='testlib.op_missing_meta', overload='default')>),
  69. 'submod_1': UnsupportedOperatorException(func=<OpOverload(op='testlib.op_missing_meta', overload='default')>),
  70. 'submod_2': None
  71. }
  72. """
  73. log_export_usage(event="export.report_exportability")
  74. kwargs = kwargs or {}
  75. all_submod_names = [name for name, _ in mod.named_modules() if name != ""]
  76. submod_inputs = _generate_inputs_for_submodules(mod, all_submod_names, args, kwargs)
  77. tried_module_types = set()
  78. report: dict[str, Optional[Exception]] = {}
  79. def try_export(module, module_name, args, kwargs):
  80. nonlocal submod_inputs, report, strict, pre_dispatch, tried_module_types
  81. if type(module) in tried_module_types:
  82. return
  83. tried_module_types.add(type(module))
  84. if args is not None or kwargs is not None:
  85. try:
  86. torch.export._trace._export(
  87. module,
  88. args,
  89. kwargs,
  90. strict=strict,
  91. pre_dispatch=pre_dispatch,
  92. )
  93. report[module_name] = None
  94. log.info("Successfully exported `%s`", module_name)
  95. return
  96. except Exception as e:
  97. short_msg = repr(e).split("\n")[0]
  98. log.warning(
  99. "Failed exporting `%s` with exception: %s", module_name, short_msg
  100. )
  101. report[module_name] = e
  102. for name, submod in module.named_children():
  103. sub_module_name = name if module_name == "" else f"{module_name}.{name}"
  104. submod_args, submod_kwargs = submod_inputs.get(
  105. sub_module_name, (None, None)
  106. )
  107. try_export(submod, sub_module_name, submod_args, submod_kwargs)
  108. return
  109. try_export(mod, "", args, kwargs)
  110. unique_issues = set()
  111. for exception in report.values():
  112. if exception is not None:
  113. key = repr(exception).split("\\n")[0]
  114. unique_issues.add(key)
  115. log.warning("Found %d export issues:", len(unique_issues))
  116. for issue in unique_issues:
  117. log.warning(issue)
  118. return report