__init__.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. # mypy: allow-untyped-defs
  2. from __future__ import annotations
  3. __all__ = [
  4. # Modules
  5. "errors",
  6. "ops",
  7. # Public functions
  8. "export",
  9. "is_in_onnx_export",
  10. # Base error
  11. "OnnxExporterError",
  12. "ONNXProgram",
  13. ]
  14. from typing import Any, Callable, TYPE_CHECKING
  15. import torch
  16. from torch._C import _onnx as _C_onnx
  17. from torch._C._onnx import ( # Deprecated members that are excluded from __all__
  18. OperatorExportTypes as OperatorExportTypes,
  19. TensorProtoDataType as TensorProtoDataType,
  20. TrainingMode as TrainingMode,
  21. )
  22. from . import errors, ops
  23. from ._internal.exporter._onnx_program import ONNXProgram
  24. from ._internal.torchscript_exporter import ( # Deprecated members that are excluded from __all__
  25. symbolic_helper,
  26. symbolic_opset10,
  27. symbolic_opset9,
  28. utils,
  29. )
  30. from ._internal.torchscript_exporter._type_utils import (
  31. JitScalarType, # Deprecated members that are excluded from __all__
  32. )
  33. from ._internal.torchscript_exporter.utils import ( # Deprecated members that are excluded from __all__
  34. register_custom_op_symbolic,
  35. select_model_mode_for_export,
  36. unregister_custom_op_symbolic,
  37. )
  38. from .errors import OnnxExporterError
  39. if TYPE_CHECKING:
  40. import os
  41. from collections.abc import Collection, Mapping, Sequence
  42. # Set namespace for exposed private names
  43. ONNXProgram.__module__ = "torch.onnx"
  44. OnnxExporterError.__module__ = "torch.onnx"
  45. # TODO(justinchuby): Remove these two properties
  46. producer_name = "pytorch"
  47. producer_version = _C_onnx.PRODUCER_VERSION
  48. def export(
  49. model: torch.nn.Module
  50. | torch.export.ExportedProgram
  51. | torch.jit.ScriptModule
  52. | torch.jit.ScriptFunction,
  53. args: tuple[Any, ...] = (),
  54. f: str | os.PathLike | None = None,
  55. *,
  56. kwargs: dict[str, Any] | None = None,
  57. verbose: bool | None = None,
  58. input_names: Sequence[str] | None = None,
  59. output_names: Sequence[str] | None = None,
  60. opset_version: int | None = None,
  61. dynamo: bool = True,
  62. # Dynamo only options
  63. external_data: bool = True,
  64. dynamic_shapes: dict[str, Any] | tuple[Any, ...] | list[Any] | None = None,
  65. custom_translation_table: dict[Callable, Callable | Sequence[Callable]]
  66. | None = None,
  67. report: bool = False,
  68. optimize: bool = True,
  69. verify: bool = False,
  70. profile: bool = False,
  71. dump_exported_program: bool = False,
  72. artifacts_dir: str | os.PathLike = ".",
  73. fallback: bool = False,
  74. # BC options
  75. export_params: bool = True,
  76. keep_initializers_as_inputs: bool = False,
  77. dynamic_axes: Mapping[str, Mapping[int, str]]
  78. | Mapping[str, Sequence[int]]
  79. | None = None,
  80. # Deprecated options
  81. training: _C_onnx.TrainingMode = _C_onnx.TrainingMode.EVAL,
  82. operator_export_type: _C_onnx.OperatorExportTypes = _C_onnx.OperatorExportTypes.ONNX,
  83. do_constant_folding: bool = True,
  84. custom_opsets: Mapping[str, int] | None = None,
  85. export_modules_as_functions: bool | Collection[type[torch.nn.Module]] = False,
  86. autograd_inlining: bool = True,
  87. ) -> ONNXProgram | None:
  88. r"""Exports a model into ONNX format.
  89. Setting ``dynamo=True`` enables the new ONNX export logic
  90. which is based on :class:`torch.export.ExportedProgram` and a more modern
  91. set of translation logic. This is the recommended and default way to export models
  92. to ONNX.
  93. When ``dynamo=True``:
  94. The exporter tries the following strategies to get an ExportedProgram for conversion to ONNX.
  95. #. If the model is already an ExportedProgram, it will be used as-is.
  96. #. Use :func:`torch.export.export` and set ``strict=False``.
  97. #. Use :func:`torch.export.export` and set ``strict=True``.
  98. Args:
  99. model: The model to be exported.
  100. args: Example positional inputs. Any non-Tensor arguments will be hard-coded into the
  101. exported model; any Tensor arguments will become inputs of the exported model,
  102. in the order they occur in the tuple.
  103. f: Path to the output ONNX model file. E.g. "model.onnx". This argument is kept for
  104. backward compatibility. It is recommended to leave unspecified (None)
  105. and use the returned :class:`torch.onnx.ONNXProgram` to serialize the model
  106. to a file instead.
  107. kwargs: Optional example keyword inputs.
  108. verbose: Whether to enable verbose logging.
  109. input_names: names to assign to the input nodes of the graph, in order.
  110. output_names: names to assign to the output nodes of the graph, in order.
  111. opset_version: The version of the
  112. `default (ai.onnx) opset <https://github.com/onnx/onnx/blob/master/docs/Operators.md>`_
  113. to target. You should set ``opset_version`` according to the supported opset versions
  114. of the runtime backend or compiler you want to run the exported model with.
  115. Leave as default (``None``) to use the recommended version, or refer to
  116. the ONNX operators documentation for more information.
  117. dynamo: Whether to export the model with ``torch.export`` ExportedProgram instead of TorchScript.
  118. external_data: Whether to save the model weights as an external data file.
  119. This is required for models with large weights that exceed the ONNX file size limit (2GB).
  120. When False, the weights are saved in the ONNX file with the model architecture.
  121. dynamic_shapes: A dictionary or a tuple of dynamic shapes for the model inputs. Refer to
  122. :func:`torch.export.export` for more details. This is only used (and preferred) when dynamo is True.
  123. Note that dynamic_shapes is designed to be used when the model is exported with dynamo=True, while
  124. dynamic_axes is used when dynamo=False.
  125. custom_translation_table: A dictionary of custom decompositions for operators in the model.
  126. The dictionary should have the callable target in the fx Node as the key (e.g. ``torch.ops.aten.stft.default``),
  127. and the value should be a function that builds that graph using ONNX Script. This option
  128. is only valid when dynamo is True.
  129. report: Whether to generate a markdown report for the export process. This option
  130. is only valid when dynamo is True.
  131. optimize: Whether to optimize the exported model. This option
  132. is only valid when dynamo is True. Default is True.
  133. verify: Whether to verify the exported model using ONNX Runtime. This option
  134. is only valid when dynamo is True.
  135. profile: Whether to profile the export process. This option
  136. is only valid when dynamo is True.
  137. dump_exported_program: Whether to dump the :class:`torch.export.ExportedProgram` to a file.
  138. This is useful for debugging the exporter. This option is only valid when dynamo is True.
  139. artifacts_dir: The directory to save the debugging artifacts like the report and the serialized
  140. exported program. This option is only valid when dynamo is True.
  141. fallback: Whether to fallback to the TorchScript exporter if the dynamo exporter fails.
  142. This option is only valid when dynamo is True. When fallback is enabled, It is
  143. recommended to set dynamic_axes even when dynamic_shapes is provided.
  144. export_params: **When ``f`` is specified**: If false, parameters (weights) will not be exported.
  145. You can also leave it unspecified and use the returned :class:`torch.onnx.ONNXProgram`
  146. to control how initializers are treated when serializing the model.
  147. keep_initializers_as_inputs: **When ``f`` is specified**: If True, all the
  148. initializers (typically corresponding to model weights) in the
  149. exported graph will also be added as inputs to the graph. If False,
  150. then initializers are not added as inputs to the graph, and only
  151. the user inputs are added as inputs.
  152. Set this to True if you intend to supply model weights at runtime.
  153. Set it to False if the weights are static to allow for better optimizations
  154. (e.g. constant folding) by backends/runtimes.
  155. You can also leave it unspecified and use the returned :class:`torch.onnx.ONNXProgram`
  156. to control how initializers are treated when serializing the model.
  157. dynamic_axes:
  158. Prefer specifying ``dynamic_shapes`` when ``dynamo=True`` and when ``fallback``
  159. is not enabled.
  160. By default the exported model will have the shapes of all input and output tensors
  161. set to exactly match those given in ``args``. To specify axes of tensors as
  162. dynamic (i.e. known only at run-time), set ``dynamic_axes`` to a dict with schema:
  163. * KEY (str): an input or output name. Each name must also be provided in ``input_names`` or
  164. ``output_names``.
  165. * VALUE (dict or list): If a dict, keys are axis indices and values are axis names. If a
  166. list, each element is an axis index.
  167. For example::
  168. class SumModule(torch.nn.Module):
  169. def forward(self, x):
  170. return torch.sum(x, dim=1)
  171. torch.onnx.export(
  172. SumModule(),
  173. (torch.ones(2, 2),),
  174. "onnx.pb",
  175. input_names=["x"],
  176. output_names=["sum"],
  177. )
  178. Produces::
  179. input {
  180. name: "x"
  181. ...
  182. shape {
  183. dim {
  184. dim_value: 2 # axis 0
  185. }
  186. dim {
  187. dim_value: 2 # axis 1
  188. ...
  189. output {
  190. name: "sum"
  191. ...
  192. shape {
  193. dim {
  194. dim_value: 2 # axis 0
  195. ...
  196. While::
  197. torch.onnx.export(
  198. SumModule(),
  199. (torch.ones(2, 2),),
  200. "onnx.pb",
  201. input_names=["x"],
  202. output_names=["sum"],
  203. dynamic_axes={
  204. # dict value: manually named axes
  205. "x": {0: "my_custom_axis_name"},
  206. # list value: automatic names
  207. "sum": [0],
  208. },
  209. )
  210. Produces::
  211. input {
  212. name: "x"
  213. ...
  214. shape {
  215. dim {
  216. dim_param: "my_custom_axis_name" # axis 0
  217. }
  218. dim {
  219. dim_value: 2 # axis 1
  220. ...
  221. output {
  222. name: "sum"
  223. ...
  224. shape {
  225. dim {
  226. dim_param: "sum_dynamic_axes_1" # axis 0
  227. ...
  228. training: Deprecated option. Instead, set the training mode of the model before exporting.
  229. operator_export_type: Deprecated option. Only ONNX is supported.
  230. do_constant_folding: Deprecated option.
  231. custom_opsets: Deprecated option.
  232. export_modules_as_functions: Deprecated option.
  233. autograd_inlining: Deprecated option.
  234. Returns:
  235. :class:`torch.onnx.ONNXProgram` if dynamo is True, otherwise None.
  236. .. versionchanged:: 2.6
  237. *training* is now deprecated. Instead, set the training mode of the model before exporting.
  238. *operator_export_type* is now deprecated. Only ONNX is supported.
  239. *do_constant_folding* is now deprecated. It is always enabled.
  240. *export_modules_as_functions* is now deprecated.
  241. *autograd_inlining* is now deprecated.
  242. .. versionchanged:: 2.7
  243. *optimize* is now True by default.
  244. .. versionchanged:: 2.9
  245. *dynamo* is now True by default.
  246. """
  247. if dynamo is True or isinstance(model, torch.export.ExportedProgram):
  248. from torch.onnx._internal.exporter import _compat
  249. if isinstance(args, torch.Tensor):
  250. args = (args,)
  251. # Prepare legacy export parameters for potential fallback
  252. legacy_export_kwargs = {
  253. "training": training,
  254. "operator_export_type": operator_export_type,
  255. "do_constant_folding": do_constant_folding,
  256. "custom_opsets": custom_opsets,
  257. "export_modules_as_functions": export_modules_as_functions,
  258. "autograd_inlining": autograd_inlining,
  259. }
  260. return _compat.export_compat(
  261. model,
  262. args,
  263. f,
  264. kwargs=kwargs,
  265. export_params=export_params,
  266. verbose=verbose,
  267. input_names=input_names,
  268. output_names=output_names,
  269. opset_version=opset_version,
  270. custom_translation_table=custom_translation_table,
  271. dynamic_axes=dynamic_axes,
  272. keep_initializers_as_inputs=keep_initializers_as_inputs,
  273. external_data=external_data,
  274. dynamic_shapes=dynamic_shapes,
  275. report=report,
  276. optimize=optimize,
  277. verify=verify,
  278. profile=profile,
  279. dump_exported_program=dump_exported_program,
  280. artifacts_dir=artifacts_dir,
  281. fallback=fallback,
  282. legacy_export_kwargs=legacy_export_kwargs,
  283. )
  284. else:
  285. import warnings
  286. from ._internal.torchscript_exporter.utils import export
  287. warnings.warn(
  288. "You are using the legacy TorchScript-based ONNX export. Starting in PyTorch 2.9, "
  289. "the new torch.export-based ONNX exporter will be the default. To switch now, set "
  290. "dynamo=True in torch.onnx.export. This new exporter supports features like exporting "
  291. "LLMs with DynamicCache. We encourage you to try it and share feedback to help improve "
  292. "the experience. Learn more about the new export logic: "
  293. "https://pytorch.org/docs/stable/onnx_dynamo.html. For exporting control flow: "
  294. "https://pytorch.org/tutorials/beginner/onnx/export_control_flow_model_to_onnx_tutorial.html.",
  295. category=DeprecationWarning,
  296. stacklevel=2,
  297. )
  298. if dynamic_shapes:
  299. raise ValueError(
  300. "The exporter only supports dynamic shapes "
  301. "through parameter dynamic_axes when dynamo=False."
  302. )
  303. export(
  304. model,
  305. args,
  306. f, # type: ignore[arg-type]
  307. kwargs=kwargs,
  308. export_params=export_params,
  309. verbose=verbose is True,
  310. input_names=input_names,
  311. output_names=output_names,
  312. opset_version=opset_version,
  313. dynamic_axes=dynamic_axes,
  314. keep_initializers_as_inputs=keep_initializers_as_inputs,
  315. training=training,
  316. operator_export_type=operator_export_type,
  317. do_constant_folding=do_constant_folding,
  318. custom_opsets=custom_opsets,
  319. export_modules_as_functions=export_modules_as_functions,
  320. autograd_inlining=autograd_inlining,
  321. )
  322. return None
  323. def is_in_onnx_export() -> bool:
  324. """Returns whether it is in the middle of ONNX export."""
  325. from torch.onnx._internal.exporter import _flags
  326. from torch.onnx._internal.torchscript_exporter._globals import GLOBALS
  327. return GLOBALS.in_onnx_export or _flags._is_onnx_exporting