case.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # mypy: allow-untyped-defs
  2. import inspect
  3. import re
  4. import string
  5. from dataclasses import dataclass, field
  6. from enum import Enum
  7. from typing import Any, Dict, List, Optional, Set, Tuple, Union
  8. from types import ModuleType
  9. import torch
  10. _TAGS: Dict[str, Dict[str, Any]] = {
  11. "torch": {
  12. "cond": {},
  13. "dynamic-shape": {},
  14. "escape-hatch": {},
  15. "map": {},
  16. "dynamic-value": {},
  17. "operator": {},
  18. "mutation": {},
  19. },
  20. "python": {
  21. "assert": {},
  22. "builtin": {},
  23. "closure": {},
  24. "context-manager": {},
  25. "control-flow": {},
  26. "data-structure": {},
  27. "standard-library": {},
  28. "object-model": {},
  29. },
  30. }
  31. class SupportLevel(Enum):
  32. """
  33. Indicates at what stage the feature
  34. used in the example is handled in export.
  35. """
  36. SUPPORTED = 1
  37. NOT_SUPPORTED_YET = 0
  38. class ExportArgs:
  39. __slots__ = ("args", "kwargs")
  40. def __init__(self, *args, **kwargs):
  41. self.args = args
  42. self.kwargs = kwargs
  43. InputsType = Union[Tuple[Any, ...], ExportArgs]
  44. def check_inputs_type(x):
  45. if not isinstance(x, (ExportArgs, tuple)):
  46. raise ValueError(
  47. f"Expecting inputs type to be either a tuple, or ExportArgs, got: {type(x)}"
  48. )
  49. def _validate_tag(tag: str):
  50. parts = tag.split(".")
  51. t = _TAGS
  52. for part in parts:
  53. assert set(part) <= set(
  54. string.ascii_lowercase + "-"
  55. ), f"Tag contains invalid characters: {part}"
  56. if part in t:
  57. t = t[part]
  58. else:
  59. raise ValueError(f"Tag {tag} is not found in registered tags.")
  60. @dataclass(frozen=True)
  61. class ExportCase:
  62. example_inputs: InputsType
  63. description: str # A description of the use case.
  64. model: torch.nn.Module
  65. name: str
  66. extra_inputs: Optional[InputsType] = None # For testing graph generalization.
  67. # Tags associated with the use case. (e.g dynamic-shape, escape-hatch)
  68. tags: Set[str] = field(default_factory=set)
  69. support_level: SupportLevel = SupportLevel.SUPPORTED
  70. dynamic_shapes: Optional[Dict[str, Any]] = None
  71. def __post_init__(self):
  72. check_inputs_type(self.example_inputs)
  73. if self.extra_inputs is not None:
  74. check_inputs_type(self.extra_inputs)
  75. for tag in self.tags:
  76. _validate_tag(tag)
  77. if not isinstance(self.description, str) or len(self.description) == 0:
  78. raise ValueError(f'Invalid description: "{self.description}"')
  79. _EXAMPLE_CASES: Dict[str, ExportCase] = {}
  80. _MODULES: Set[ModuleType] = set()
  81. _EXAMPLE_CONFLICT_CASES: Dict[str, List[ExportCase]] = {}
  82. _EXAMPLE_REWRITE_CASES: Dict[str, List[ExportCase]] = {}
  83. def register_db_case(case: ExportCase) -> None:
  84. """
  85. Registers a user provided ExportCase into example bank.
  86. """
  87. if case.name in _EXAMPLE_CASES:
  88. if case.name not in _EXAMPLE_CONFLICT_CASES:
  89. _EXAMPLE_CONFLICT_CASES[case.name] = [_EXAMPLE_CASES[case.name]]
  90. _EXAMPLE_CONFLICT_CASES[case.name].append(case)
  91. return
  92. _EXAMPLE_CASES[case.name] = case
  93. def to_snake_case(name):
  94. name = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name)
  95. return re.sub("([a-z0-9])([A-Z])", r"\1_\2", name).lower()
  96. def _make_export_case(m, name, configs):
  97. if not issubclass(m, torch.nn.Module):
  98. raise TypeError("Export case class should be a torch.nn.Module.")
  99. m = m()
  100. if "description" not in configs:
  101. # Fallback to docstring if description is missing.
  102. assert (
  103. m.__doc__ is not None
  104. ), f"Could not find description or docstring for export case: {m}"
  105. configs = {**configs, "description": m.__doc__}
  106. return ExportCase(**{**configs, "model": m, "name": name})
  107. def export_case(**kwargs):
  108. """
  109. Decorator for registering a user provided case into example bank.
  110. """
  111. def wrapper(m):
  112. configs = kwargs
  113. module = inspect.getmodule(m)
  114. if module in _MODULES:
  115. raise RuntimeError("export_case should only be used once per example file.")
  116. assert module is not None
  117. _MODULES.add(module)
  118. normalized_name = to_snake_case(m.__name__)
  119. module_name = module.__name__.split(".")[-1]
  120. if module_name != normalized_name:
  121. raise RuntimeError(
  122. f'Module name "{module.__name__}" is inconsistent with exported program '
  123. + f'name "{m.__name__}". Please rename the module to "{normalized_name}".'
  124. )
  125. case = _make_export_case(m, module_name, configs)
  126. register_db_case(case)
  127. return case
  128. return wrapper
  129. def export_rewrite_case(**kwargs):
  130. def wrapper(m):
  131. configs = kwargs
  132. parent = configs.pop("parent")
  133. assert isinstance(parent, ExportCase)
  134. key = parent.name
  135. if key not in _EXAMPLE_REWRITE_CASES:
  136. _EXAMPLE_REWRITE_CASES[key] = []
  137. configs["example_inputs"] = parent.example_inputs
  138. case = _make_export_case(m, to_snake_case(m.__name__), configs)
  139. _EXAMPLE_REWRITE_CASES[key].append(case)
  140. return case
  141. return wrapper
  142. def normalize_inputs(x: InputsType) -> ExportArgs:
  143. if isinstance(x, tuple):
  144. return ExportArgs(*x)
  145. assert isinstance(x, ExportArgs)
  146. return x