__init__.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. # mypy: allow-untyped-defs
  2. import warnings
  3. from collections.abc import Iterator
  4. from contextlib import contextmanager
  5. from typing import Any
  6. import torch._C
  7. # These are imported so users can access them from the `torch.jit` module
  8. from torch._jit_internal import (
  9. _Await,
  10. _drop,
  11. _IgnoreContextManager,
  12. _isinstance,
  13. _overload,
  14. _overload_method,
  15. export,
  16. Final,
  17. Future,
  18. ignore,
  19. is_scripting,
  20. unused,
  21. )
  22. from torch.jit._async import fork, wait
  23. from torch.jit._await import _awaitable, _awaitable_nowait, _awaitable_wait
  24. from torch.jit._decomposition_utils import _register_decomposition
  25. from torch.jit._freeze import freeze, optimize_for_inference, run_frozen_optimizations
  26. from torch.jit._fuser import (
  27. fuser,
  28. last_executed_optimized_graph,
  29. optimized_execution,
  30. set_fusion_strategy,
  31. )
  32. from torch.jit._ir_utils import _InsertPoint
  33. from torch.jit._script import (
  34. _ScriptProfile,
  35. _unwrap_optional,
  36. Attribute,
  37. CompilationUnit,
  38. interface,
  39. RecursiveScriptClass,
  40. RecursiveScriptModule,
  41. script,
  42. script_method,
  43. ScriptFunction,
  44. ScriptModule,
  45. ScriptWarning,
  46. )
  47. from torch.jit._serialization import (
  48. jit_module_from_flatbuffer,
  49. load,
  50. save,
  51. save_jit_module_to_flatbuffer,
  52. )
  53. from torch.jit._trace import (
  54. _flatten,
  55. _get_trace_graph,
  56. _script_if_tracing,
  57. _unique_state_dict,
  58. is_tracing,
  59. ONNXTracedModule,
  60. TopLevelTracedModule,
  61. trace,
  62. trace_module,
  63. TracedModule,
  64. TracerWarning,
  65. TracingCheckError,
  66. )
  67. from torch.utils import set_module
  68. __all__ = [
  69. "Attribute",
  70. "CompilationUnit",
  71. "Error",
  72. "Future",
  73. "ScriptFunction",
  74. "ScriptModule",
  75. "annotate",
  76. "enable_onednn_fusion",
  77. "export",
  78. "export_opnames",
  79. "fork",
  80. "freeze",
  81. "interface",
  82. "ignore",
  83. "isinstance",
  84. "load",
  85. "onednn_fusion_enabled",
  86. "optimize_for_inference",
  87. "save",
  88. "script",
  89. "script_if_tracing",
  90. "set_fusion_strategy",
  91. "strict_fusion",
  92. "trace",
  93. "trace_module",
  94. "unused",
  95. "wait",
  96. ]
  97. # For backwards compatibility
  98. _fork = fork
  99. _wait = wait
  100. _set_fusion_strategy = set_fusion_strategy
  101. def export_opnames(m):
  102. r"""
  103. Generate new bytecode for a Script module.
  104. Returns what the op list would be for a Script Module based off the current code base.
  105. If you have a LiteScriptModule and want to get the currently present
  106. list of ops call _export_operator_list instead.
  107. """
  108. return torch._C._export_opnames(m._c)
  109. # torch.jit.Error
  110. Error = torch._C.JITException
  111. set_module(Error, "torch.jit")
  112. # This is not perfect but works in common cases
  113. Error.__name__ = "Error"
  114. Error.__qualname__ = "Error"
  115. # for use in python if using annotate
  116. def annotate(the_type, the_value):
  117. """Use to give type of `the_value` in TorchScript compiler.
  118. This method is a pass-through function that returns `the_value`, used to hint TorchScript
  119. compiler the type of `the_value`. It is a no-op when running outside of TorchScript.
  120. Though TorchScript can infer correct type for most Python expressions, there are some cases where
  121. type inference can be wrong, including:
  122. - Empty containers like `[]` and `{}`, which TorchScript assumes to be container of `Tensor`
  123. - Optional types like `Optional[T]` but assigned a valid value of type `T`, TorchScript would assume
  124. it is type `T` rather than `Optional[T]`
  125. Note that `annotate()` does not help in `__init__` method of `torch.nn.Module` subclasses because it
  126. is executed in eager mode. To annotate types of `torch.nn.Module` attributes,
  127. use :meth:`~torch.jit.Attribute` instead.
  128. Example:
  129. .. testcode::
  130. import torch
  131. from typing import Dict
  132. @torch.jit.script
  133. def fn():
  134. # Telling TorchScript that this empty dictionary is a (str -> int) dictionary
  135. # instead of default dictionary type of (str -> Tensor).
  136. d = torch.jit.annotate(Dict[str, int], {})
  137. # Without `torch.jit.annotate` above, following statement would fail because of
  138. # type mismatch.
  139. d["name"] = 20
  140. .. testcleanup::
  141. del fn
  142. Args:
  143. the_type: Python type that should be passed to TorchScript compiler as type hint for `the_value`
  144. the_value: Value or expression to hint type for.
  145. Returns:
  146. `the_value` is passed back as return value.
  147. """
  148. return the_value
  149. def script_if_tracing(fn):
  150. """
  151. Compiles ``fn`` when it is first called during tracing.
  152. ``torch.jit.script`` has a non-negligible start up time when it is first called due to
  153. lazy-initializations of many compiler builtins. Therefore you should not use
  154. it in library code. However, you may want to have parts of your library work
  155. in tracing even if they use control flow. In these cases, you should use
  156. ``@torch.jit.script_if_tracing`` to substitute for
  157. ``torch.jit.script``.
  158. Args:
  159. fn: A function to compile.
  160. Returns:
  161. If called during tracing, a :class:`ScriptFunction` created by `torch.jit.script` is returned.
  162. Otherwise, the original function `fn` is returned.
  163. """
  164. return _script_if_tracing(fn)
  165. # for torch.jit.isinstance
  166. def isinstance(obj, target_type):
  167. """
  168. Provide container type refinement in TorchScript.
  169. It can refine parameterized containers of the List, Dict, Tuple, and Optional types. E.g. ``List[str]``,
  170. ``Dict[str, List[torch.Tensor]]``, ``Optional[Tuple[int,str,int]]``. It can also
  171. refine basic types such as bools and ints that are available in TorchScript.
  172. Args:
  173. obj: object to refine the type of
  174. target_type: type to try to refine obj to
  175. Returns:
  176. ``bool``: True if obj was successfully refined to the type of target_type,
  177. False otherwise with no new type refinement
  178. Example (using ``torch.jit.isinstance`` for type refinement):
  179. .. testcode::
  180. import torch
  181. from typing import Any, Dict, List
  182. class MyModule(torch.nn.Module):
  183. def __init__(self) -> None:
  184. super().__init__()
  185. def forward(self, input: Any): # note the Any type
  186. if torch.jit.isinstance(input, List[torch.Tensor]):
  187. for t in input:
  188. y = t.clamp(0, 0.5)
  189. elif torch.jit.isinstance(input, Dict[str, str]):
  190. for val in input.values():
  191. print(val)
  192. m = torch.jit.script(MyModule())
  193. x = [torch.rand(3,3), torch.rand(4,3)]
  194. m(x)
  195. y = {"key1":"val1","key2":"val2"}
  196. m(y)
  197. """
  198. return _isinstance(obj, target_type)
  199. class strict_fusion:
  200. """
  201. Give errors if not all nodes have been fused in inference, or symbolically differentiated in training.
  202. Example:
  203. Forcing fusion of additions.
  204. .. code-block:: python
  205. @torch.jit.script
  206. def foo(x):
  207. with torch.jit.strict_fusion():
  208. return x + x + x
  209. """
  210. def __init__(self) -> None:
  211. if not torch._jit_internal.is_scripting():
  212. warnings.warn("Only works in script mode")
  213. def __enter__(self):
  214. pass
  215. def __exit__(self, type: Any, value: Any, tb: Any) -> None:
  216. pass
  217. # Context manager for globally hiding source ranges when printing graphs.
  218. # Note that these functions are exposed to Python as static members of the
  219. # Graph class, so mypy checks need to be skipped.
  220. @contextmanager
  221. def _hide_source_ranges() -> Iterator[None]:
  222. old_enable_source_ranges = torch._C.Graph.global_print_source_ranges # type: ignore[attr-defined]
  223. try:
  224. torch._C.Graph.set_global_print_source_ranges(False) # type: ignore[attr-defined]
  225. yield
  226. finally:
  227. torch._C.Graph.set_global_print_source_ranges(old_enable_source_ranges) # type: ignore[attr-defined]
  228. def enable_onednn_fusion(enabled: bool):
  229. """Enable or disables onednn JIT fusion based on the parameter `enabled`."""
  230. torch._C._jit_set_llga_enabled(enabled)
  231. def onednn_fusion_enabled():
  232. """Return whether onednn JIT fusion is enabled."""
  233. return torch._C._jit_llga_enabled()
  234. del Any
  235. if not torch._C._jit_init():
  236. raise RuntimeError("JIT initialization failed")