1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867
|
# mypy: allow-untyped-defs
from __future__ import annotations
__all__ = [
"DiagnosticOptions",
"ExportOptions",
"ONNXRuntimeOptions",
"InvalidExportOptionsError",
"OnnxRegistry",
"UnsatisfiedDependencyError",
"dynamo_export",
"enable_fake_mode",
]
import abc
import contextlib
import dataclasses
import logging
import warnings
from collections import defaultdict
from typing import Any, Callable, Mapping, Sequence, TYPE_CHECKING, TypeVar
import torch
import torch._ops
import torch.utils._pytree as pytree
from torch.onnx import errors
from torch.onnx._internal import io_adapter
from torch.onnx._internal._lazy_import import onnxscript_apis, onnxscript_ir as ir
from torch.onnx._internal.diagnostics import infra
from torch.onnx._internal.exporter import _onnx_program
from torch.onnx._internal.fx import (
decomposition_table,
patcher as patcher,
registration,
)
# We can only import onnx from this module in a type-checking context to ensure that
# 'import torch.onnx' continues to work without having 'onnx' installed. We fully
# 'import onnx' inside of dynamo_export (by way of _assert_dependencies).
if TYPE_CHECKING:
import io
import onnxruntime
import onnxscript
from torch._subclasses import fake_tensor
from torch.onnx._internal.fx import diagnostics
_PYTORCH_GITHUB_ISSUES_URL = "https://github.com/pytorch/pytorch/issues"
"""The URL to the PyTorch GitHub issues page."""
_DEFAULT_FAILED_EXPORT_SARIF_LOG_PATH = "report_dynamo_export.sarif"
"""The default path to write the SARIF log to if the export fails."""
log = logging.getLogger(__name__)
DiagnosticOptions = infra.DiagnosticOptions
@dataclasses.dataclass
class ONNXFakeContext:
"""A dataclass used to store context for model export using FakeTensor.
This dataclass stores the FakeTensorMode instance used to convert
real tensors and model parameters into fake tensors. This :attr:`ONNXFakeContext.fake_mode` is
reused internally during tracing of a :class:`torch.nn.Module` into a FX :class:`GraphModule`.
"""
fake_mode: fake_tensor.FakeTensorMode
"""The fake tensor mode used for tracing model using fake tensors and parameters."""
state_dict_paths: tuple[str | io.BytesIO | dict[str, Any]] | None = None
"""List of paths of files that contain the model :meth:`state_dict`"""
class OnnxRegistry:
"""Registry for ONNX functions.
The registry maintains a mapping from qualified names to symbolic functions under a
fixed opset version. It supports registering custom onnx-script functions and for
dispatcher to dispatch calls to the appropriate function.
"""
def __init__(self) -> None:
"""Initializes the registry"""
# NOTE: _registry is the registry maps OpNameto a list of ONNXFunctions. It is important
# not to directly modify this variable. Instead, access to it should be done through
# the public methods: register_custom_op, get_ops, and is_registered_op.
self._registry: dict[registration.OpName, list[registration.ONNXFunction]] = (
defaultdict(list)
)
self._opset_version = onnxscript_apis.torchlib_opset_version()
warnings.warn(
f"torch.onnx.dynamo_export only implements opset version {self._opset_version} for now. If you need to use a "
"different opset version, please register them with register_custom_op."
)
self._initiate_registry_from_torchlib()
@property
def opset_version(self) -> int:
"""The ONNX opset version the exporter should target."""
return self._opset_version
def _initiate_registry_from_torchlib(self) -> None:
"""Populates the registry with ATen functions from torchlib.
Args:
torchlib_registry: The torchlib registry to use for populating the registry.
"""
for meta in onnxscript_apis.get_torchlib_ops():
internal_name_instance = registration.OpName.from_qualified_name(
meta.qualified_name
)
symbolic_function = registration.ONNXFunction(
onnx_function=meta.function, # type: ignore[arg-type]
op_full_name=internal_name_instance.qualified_name(),
is_custom=False,
is_complex=meta.is_complex,
)
self._register(internal_name_instance, symbolic_function)
def _register(
self,
internal_qualified_name: registration.OpName,
symbolic_function: registration.ONNXFunction,
) -> None:
"""Registers a ONNXFunction to an operator.
Args:
internal_qualified_name: The qualified name of the operator to register: OpName.
symbolic_function: The ONNXFunction to register.
"""
self._registry[internal_qualified_name].append(symbolic_function)
def register_op(
self,
function: onnxscript.OnnxFunction | onnxscript.TracedOnnxFunction,
namespace: str,
op_name: str,
overload: str | None = None,
is_complex: bool = False,
) -> None:
"""Registers a custom operator: torch.ops.<namespace>.<op_name>.<overload>.
Args:
function: The onnx-sctip function to register.
namespace: The namespace of the operator to register.
op_name: The name of the operator to register.
overload: The overload of the operator to register. If it's default overload,
leave it to None.
is_complex: Whether the function is a function that handles complex valued inputs.
Raises:
ValueError: If the name is not in the form of 'namespace::op'.
"""
internal_name_instance = registration.OpName.from_name_parts(
namespace=namespace, op_name=op_name, overload=overload
)
symbolic_function = registration.ONNXFunction(
onnx_function=function,
op_full_name=internal_name_instance.qualified_name(),
is_custom=True,
is_complex=is_complex,
)
self._register(internal_name_instance, symbolic_function)
def get_op_functions(
self, namespace: str, op_name: str, overload: str | None = None
) -> list[registration.ONNXFunction] | None:
"""Returns a list of ONNXFunctions for the given op: torch.ops.<namespace>.<op_name>.<overload>.
The list is ordered by the time of registration. The custom operators should be
in the second half of the list.
Args:
namespace: The namespace of the operator to get.
op_name: The name of the operator to get.
overload: The overload of the operator to get. If it's default overload,
leave it to None.
Returns:
A list of ONNXFunctions corresponding to the given name, or None if
the name is not in the registry.
"""
internal_name_instance = registration.OpName.from_name_parts(
namespace=namespace, op_name=op_name, overload=overload
)
return self._registry.get(internal_name_instance)
def is_registered_op(
self, namespace: str, op_name: str, overload: str | None = None
) -> bool:
"""Returns whether the given op is registered: torch.ops.<namespace>.<op_name>.<overload>.
Args:
namespace: The namespace of the operator to check.
op_name: The name of the operator to check.
overload: The overload of the operator to check. If it's default overload,
leave it to None.
Returns:
True if the given op is registered, otherwise False.
"""
functions = self.get_op_functions(
namespace=namespace, op_name=op_name, overload=overload
)
return functions is not None
def _all_registered_ops(self) -> set[str]:
"""Returns the set of all registered function names."""
return {
op_name_class.qualified_name() for op_name_class in self._registry.keys()
}
class ExportOptions:
"""Options to influence the TorchDynamo ONNX exporter.
Attributes:
dynamic_shapes: Shape information hint for input/output tensors.
When ``None``, the exporter determines the most compatible setting.
When ``True``, all input shapes are considered dynamic.
When ``False``, all input shapes are considered static.
diagnostic_options: The diagnostic options for the exporter.
fake_context: The fake context used for symbolic tracing.
onnx_registry: The ONNX registry used to register ATen operators to ONNX functions.
"""
dynamic_shapes: bool | None = None
"""Shape information hint for input/output tensors.
- ``None``: the exporter determines the most compatible setting.
- ``True``: all input shapes are considered dynamic.
- ``False``: all input shapes are considered static.
"""
diagnostic_options: DiagnosticOptions
"""The diagnostic options for the exporter."""
fake_context: ONNXFakeContext | None = None
"""The fake context used for symbolic tracing."""
onnx_registry: OnnxRegistry | None = None
"""The ONNX registry used to register ATen operators to ONNX functions."""
def __init__(
self,
*,
dynamic_shapes: bool | None = None,
fake_context: ONNXFakeContext | None = None,
onnx_registry: OnnxRegistry | None = None,
diagnostic_options: DiagnosticOptions | None = None,
):
self.dynamic_shapes = dynamic_shapes
self.fake_context = fake_context
self.onnx_registry = onnx_registry
self.diagnostic_options = diagnostic_options or DiagnosticOptions()
class ResolvedExportOptions(ExportOptions):
"""Consolidates :class:`ExportOptions` with default values.
All unspecified options from :class:`ExportOptions` are assigned a default value.
This is an internal class and its API may be changed at any time without notice.
"""
# Public attributes MUST be redefined below without ``Optional[]`` from ``ExportOptions``
dynamic_shapes: bool
diagnostic_options: DiagnosticOptions
fake_context: ONNXFakeContext
onnx_registry: OnnxRegistry
# Private only attributes
decomposition_table: dict[torch._ops.OpOverload, Callable]
"""A dictionary that maps operators to their decomposition functions."""
onnxfunction_dispatcher: (
torch.onnx._internal.fx.onnxfunction_dispatcher.OnnxFunctionDispatcher
)
"""The ONNX dispatcher used to dispatch ATen operators to ONNX functions."""
fx_tracer: FXGraphExtractor
"""The FXGraphExtractor instance used to extract the FX graph from the model."""
diagnostic_context: diagnostics.DiagnosticContext
"""The diagnostics context for the export. Responsible for recording diagnostics,
logging diagnostics, and generating the SARIF log."""
def __init__(
self,
options: ExportOptions | ResolvedExportOptions,
model: torch.nn.Module | Callable | None = None, # type: ignore[name-defined]
):
from torch.onnx._internal.fx import ( # TODO: Prevent circular dep
diagnostics,
dynamo_graph_extractor,
)
if isinstance(options, ResolvedExportOptions):
self.dynamic_shapes = options.dynamic_shapes
self.diagnostic_options = options.diagnostic_options
self.fake_context = options.fake_context
self.fx_tracer = options.fx_tracer
self.onnx_registry = options.onnx_registry
self.onnxfunction_dispatcher = options.onnxfunction_dispatcher
self.decomposition_table = options.decomposition_table
self.diagnostic_context = options.diagnostic_context
else:
T = TypeVar("T")
def resolve(value: T | None, fallback: T | Callable[[], T]) -> T:
if value is not None:
return value
if callable(fallback):
return fallback()
return fallback
self.dynamic_shapes = resolve(options.dynamic_shapes, False)
self.diagnostic_options = resolve(
options.diagnostic_options, DiagnosticOptions()
)
self.fx_tracer = dynamo_graph_extractor.DynamoExport()
self.fake_context = resolve(options.fake_context, None) # type: ignore[arg-type]
self.diagnostic_context = diagnostics.DiagnosticContext(
"torch.onnx.dynamo_export",
torch.__version__,
self.diagnostic_options,
)
self.onnx_registry = resolve(options.onnx_registry, OnnxRegistry())
self.decomposition_table = (
decomposition_table.create_onnx_friendly_decomposition_table( # type: ignore[assignment]
self.onnx_registry
)
)
from torch.onnx._internal.fx import onnxfunction_dispatcher
self.onnxfunction_dispatcher = (
onnxfunction_dispatcher.OnnxFunctionDispatcher(
self.onnx_registry,
self.diagnostic_context,
)
)
for key in dir(options):
if not key.startswith("_"): # skip private attributes
assert hasattr(self, key), f"Unresolved option '{key}'"
@contextlib.contextmanager
def enable_fake_mode():
"""Enable fake mode for the duration of the context.
Internally it instantiates a :class:`torch._subclasses.fake_tensor.FakeTensorMode` context manager
that converts user input and model parameters into :class:`torch._subclasses.fake_tensor.FakeTensor`.
A :class:`torch._subclasses.fake_tensor.FakeTensor`
is a :class:`torch.Tensor` with the ability to run PyTorch code without having to
actually do computation through tensors allocated on a ``meta`` device. Because
there is no actual data being allocated on the device, this API allows for
initializing and exporting large models without the actual memory footprint needed for executing it.
It is highly recommended to initialize the model in fake mode when exporting models that
are too large to fit into memory.
Example::
# xdoctest: +REQUIRES(env:TORCH_DOCTEST_ONNX)
>>> import torch
>>> class MyModel(torch.nn.Module): # Model with a parameter
... def __init__(self) -> None:
... super().__init__()
... self.weight = torch.nn.Parameter(torch.tensor(42.0))
... def forward(self, x):
... return self.weight + x
>>> with torch.onnx.enable_fake_mode():
... # When initialized in fake mode, the model's parameters are fake tensors
... # They do not take up memory so we can initialize large models
... my_nn_module = MyModel()
... arg1 = torch.randn(2, 2, 2)
>>> onnx_program = torch.onnx.export(my_nn_module, (arg1,), dynamo=True)
>>> # Saving model WITHOUT initializers (only the architecture)
>>> onnx_program.save(
... "my_model_without_initializers.onnx",
... include_initializers=False,
... keep_initializers_as_inputs=True,
... )
>>> # Saving model WITH initializers after applying concrete weights
>>> onnx_program.apply_weights({"weight": torch.tensor(42.0)})
>>> onnx_program.save("my_model_with_initializers.onnx")
.. warning::
This API is experimental and is *NOT* backward-compatible.
"""
from torch._subclasses import fake_tensor
from torch.fx.experimental.symbolic_shapes import ShapeEnv
# This overrides the internal `FakeTensorMode` instance created by `torch._dynamo.export`[1].
# It is a good idea to keep them in sync (constructor args) to maintain the same default behavior
# [1] `torch/_dynamo/output_graph.py::InstructionTranslator::OutputGraph.__init__`
# Mixed fake/real tensors are only allowed when `torch.onnx.dynamo_export` is not called within `FakeTensorMode`
# This is needed because models can create new parameters during `forward(self, *args, **kwargs)` run
fake_mode = fake_tensor.FakeTensorMode(
allow_non_fake_inputs=not torch._guards.detect_fake_mode(),
shape_env=ShapeEnv(
allow_scalar_outputs=False, allow_dynamic_output_shape_ops=False
),
)
# The patcher is needed for when user calls `fake_model.load_state_dict(...)` within fake mode
patcher_context = patcher.ONNXTorchPatcher()
fake_context = ONNXFakeContext(fake_mode=fake_mode)
with fake_mode, patcher_context:
yield fake_context
fake_context.state_dict_paths = tuple(
patcher_context.paths,
) # type: ignore[assignment]
class ONNXRuntimeOptions:
"""Options to influence the execution of the ONNX model through ONNX Runtime.
Attributes:
session_options: ONNX Runtime session options.
execution_providers: ONNX Runtime execution providers to use during model execution.
execution_provider_options: ONNX Runtime execution provider options.
"""
session_options: Sequence[onnxruntime.SessionOptions] | None = None
"""ONNX Runtime session options."""
execution_providers: Sequence[str | tuple[str, dict[Any, Any]]] | None = None
"""ONNX Runtime execution providers to use during model execution."""
execution_provider_options: Sequence[dict[Any, Any]] | None = None
"""ONNX Runtime execution provider options."""
def __init__(
self,
*,
session_options: Sequence[onnxruntime.SessionOptions] | None = None,
execution_providers: Sequence[str | tuple[str, dict[Any, Any]]] | None = None,
execution_provider_options: Sequence[dict[Any, Any]] | None = None,
):
self.session_options = session_options
self.execution_providers = execution_providers
self.execution_provider_options = execution_provider_options
class FXGraphExtractor(abc.ABC):
"""Abstract interface for FX graph extractor engines.
This class isolates FX extraction logic from the rest of the export logic.
That allows a single ONNX exporter that can leverage different FX graphs."""
def __init__(self) -> None:
super().__init__()
self.input_adapter: io_adapter.InputAdapter = io_adapter.InputAdapter()
self.output_adapter: io_adapter.OutputAdapter = io_adapter.OutputAdapter()
@abc.abstractmethod
def generate_fx(
self,
options: ResolvedExportOptions,
model: torch.nn.Module | Callable,
model_args: Sequence[Any],
model_kwargs: Mapping[str, Any],
) -> torch.fx.GraphModule:
"""Analyzes user ``model`` and generates a FX graph.
Args:
options: The export options.
model: The user model.
model_args: The model's positional input arguments.
model_kwargs: The model's keyword input arguments.
Returns:
The generated FX Graph.
"""
...
# TODO: Design the passes API
@abc.abstractmethod
def pre_export_passes(
self,
options: ResolvedExportOptions,
original_model: torch.nn.Module | Callable,
fx_module: torch.fx.GraphModule,
fx_module_args: Sequence[Any],
):
"""Applies pre-export passes to the FX graph.
Pre-export passes are FX-to-FX graph transformations that make the graph
more palatable for the FX-to-ONNX conversion.
For example, it can be used to flatten model input/output, add explicit
casts to the graph, replace/decompose operators, functionalize the graph, etc.
"""
...
class Exporter:
def __init__(
self,
options: ResolvedExportOptions,
model: torch.nn.Module | Callable,
model_args: Sequence[Any],
model_kwargs: Mapping[str, Any],
):
self.options = options
assert self.options is not None
self.model = model
self.model_args = model_args
self.model_kwargs = model_kwargs
# TODO: https://github.com/pytorch/pytorch/issues/107714
# NOTE: FXSymbolicTracer would fail in this assert, as it does not use `enable_fake_mode`
from torch.onnx._internal.fx import fx_symbolic_graph_extractor
if not isinstance(
self.options.fx_tracer, fx_symbolic_graph_extractor.FXSymbolicTracer
):
self._assert_fake_tensor_mode()
def export(self) -> _onnx_program.ONNXProgram:
from torch.export._trace import ( # TODO: Prevent circular dependency
DEFAULT_EXPORT_DYNAMO_CONFIG,
)
# TODO: Defer `import onnxscript` out of `import torch` path
# https://github.com/pytorch/pytorch/issues/103764
from torch.onnx._internal.fx import decomposition_skip
with self.options.diagnostic_context, decomposition_skip.enable_decomposition_skips(
self.options
), torch._dynamo.config.patch(dataclasses.asdict(DEFAULT_EXPORT_DYNAMO_CONFIG)):
graph_module = self.options.fx_tracer.generate_fx(
self.options, self.model, self.model_args, self.model_kwargs
)
# TODO: Defer `import onnxscript` out of `import torch` path
# https://github.com/pytorch/pytorch/issues/103764
from torch.onnx._internal.fx import fx_onnx_interpreter
fx_interpreter = fx_onnx_interpreter.FxOnnxInterpreter(
diagnostic_context=self.options.diagnostic_context
)
onnxscript_graph = fx_interpreter.run(
fx_graph_module=graph_module,
onnxfunction_dispatcher=self.options.onnxfunction_dispatcher,
)
# NOTE: Filter out the initializers with fake tensors when it's fake_mode exporting.
# Otherwise, the ONNX exporter will fail: RuntimeError: basic_string::_M_construct null
# not valid.
# Concrete data is expected to be filled for those initializers later during `ONNXProgram.save`.
if self.options.fake_context is not None:
initializers_with_real_tensors: dict[str, torch.Tensor] = {}
for (
initializer_name,
initializer,
) in onnxscript_graph.initializers.items():
if not isinstance(initializer, torch._subclasses.FakeTensor):
initializers_with_real_tensors[initializer_name] = initializer
onnxscript_graph.initializers = initializers_with_real_tensors
# Export TorchScript graph to ONNX ModelProto.
onnx_model = onnxscript_graph.to_model_proto(
self.options.onnx_registry.opset_version,
)
ir_model = ir.serde.deserialize_model(onnx_model)
try:
ir_model = onnxscript_apis.optimize(ir_model)
except Exception as e:
warnings.warn(
"ONNXScript optimizer failed. Skipping optimization. "
"\n\nPLEASE REPORT A BUG AT https://github.com/microsoft/onnxscript/issues "
f"\n\nDetail:\n{e}"
)
return _onnx_program.ONNXProgram(ir_model, None)
def _assert_fake_tensor_mode(self):
"""Asserts that the model and its input do not contain fake tensors."""
# Case 1: Model with fake inputs/weights and without enabling fake mode
has_any_fake_tensor = pytree.tree_any(
lambda x: isinstance(x, torch._subclasses.FakeTensor),
(self.model_args, self.model_kwargs),
)
has_any_fake_param_or_buffer = False
if isinstance(self.model, torch.nn.Module):
has_any_fake_param_or_buffer = pytree.tree_any(
lambda x: isinstance(x, torch._subclasses.FakeTensor),
(self.model.parameters(), self.model.buffers()),
)
if (
has_any_fake_tensor or has_any_fake_param_or_buffer
) and not self.options.fake_context:
raise RuntimeError(
"Cannot export a model with fake inputs/weights without enabling fake mode.",
)
# Case 2: Model with non fake inputs/weights and enabled fake mode
has_any_non_fake_tensors = pytree.tree_any(
lambda x: isinstance(x, torch.Tensor)
and not isinstance(x, torch._subclasses.FakeTensor),
(self.model_args, self.model_kwargs),
)
has_any_non_fake_param_or_buffer = False
if isinstance(self.model, torch.nn.Module):
has_any_non_fake_param_or_buffer = pytree.tree_any(
lambda x: isinstance(x, torch.Tensor)
and not isinstance(x, torch._subclasses.FakeTensor),
(self.model.parameters(), self.model.buffers()),
)
if (
has_any_non_fake_tensors or has_any_non_fake_param_or_buffer
) and self.options.fake_context:
raise RuntimeError(
"Cannot export a model with non fake inputs/weights and enabled fake mode.",
)
class UnsatisfiedDependencyError(RuntimeError):
"""Raised when an ONNX exporter dependency cannot be satisfied."""
def __init__(self, package_name: str, message: str):
super().__init__(message)
self.package_name = package_name
class InvalidExportOptionsError(RuntimeError):
"""Raised when user specified an invalid value for the :class:`ExportOptions`."""
def _assert_dependencies(export_options: ResolvedExportOptions):
opset_version = export_options.onnx_registry.opset_version
def missing_package(package_name: str, exc_info: logging._ExcInfoType):
message = (
f"Please install the `{package_name}` package "
f"(e.g. `python -m pip install {package_name}`)."
)
log.fatal(message, exc_info=exc_info)
return UnsatisfiedDependencyError(package_name, message)
def missing_opset(package_name: str):
message = (
f"The installed `{package_name}` does not support the specified ONNX opset "
f"version {opset_version}. Install a newer `{package_name}` package or "
f"specify an older opset version."
)
log.fatal(message)
return UnsatisfiedDependencyError(package_name, message)
try:
import onnx
except ImportError as e:
raise missing_package("onnx", e) from e
if onnx.defs.onnx_opset_version() < opset_version:
raise missing_opset("onnx")
try:
# PyTorch runs lintrunner in CI without onnxscript installed
import onnxscript # type: ignore[import]
except ImportError as e:
raise missing_package("onnxscript", e) from e
if not isinstance(
onnxscript.onnx_opset.all_opsets[("", opset_version)],
onnxscript.values.Opset,
):
raise missing_opset("onnxscript")
def dynamo_export(
model: torch.nn.Module | Callable,
/,
*model_args,
export_options: ExportOptions | None = None,
**model_kwargs,
) -> _onnx_program.ONNXProgram:
"""Export a torch.nn.Module to an ONNX graph.
Args:
model: The PyTorch model to be exported to ONNX.
model_args: Positional inputs to ``model``.
model_kwargs: Keyword inputs to ``model``.
export_options: Options to influence the export to ONNX.
Returns:
An in-memory representation of the exported ONNX model.
**Example 1 - Simplest export**
::
class MyModel(torch.nn.Module):
def __init__(self) -> None:
super().__init__()
self.linear = torch.nn.Linear(2, 2)
def forward(self, x, bias=None):
out = self.linear(x)
out = out + bias
return out
model = MyModel()
kwargs = {"bias": 3.0}
args = (torch.randn(2, 2, 2),)
onnx_program = torch.onnx.dynamo_export(model, *args, **kwargs).save(
"my_simple_model.onnx"
)
**Example 2 - Exporting with dynamic shapes**
::
# The previous model can be exported with dynamic shapes
export_options = torch.onnx.ExportOptions(dynamic_shapes=True)
onnx_program = torch.onnx.dynamo_export(
model, *args, **kwargs, export_options=export_options
)
onnx_program.save("my_dynamic_model.onnx")
By printing input dynamic dimensions we can see the input shape is no longer (2,2,2)
::
>>> print(onnx_program.model_proto.graph.input[0])
name: "arg0"
type {
tensor_type {
elem_type: 1
shape {
dim {
dim_param: "arg0_dim_0"
}
dim {
dim_param: "arg0_dim_1"
}
dim {
dim_param: "arg0_dim_2"
}
}
}
}
"""
if export_options is not None:
resolved_export_options = (
export_options
if isinstance(export_options, ResolvedExportOptions)
else ResolvedExportOptions(export_options, model=model)
)
else:
resolved_export_options = ResolvedExportOptions(ExportOptions(), model=model)
_assert_dependencies(resolved_export_options)
try:
from torch._dynamo import config as _dynamo_config
with _dynamo_config.patch(do_not_emit_runtime_asserts=True):
return Exporter(
options=resolved_export_options,
model=model,
model_args=model_args,
model_kwargs=model_kwargs,
).export()
except Exception as e:
sarif_report_path = _DEFAULT_FAILED_EXPORT_SARIF_LOG_PATH
resolved_export_options.diagnostic_context.dump(sarif_report_path)
message = (
f"Failed to export the model to ONNX. Generating SARIF report at '{sarif_report_path}'. "
"SARIF is a standard format for the output of static analysis tools. "
"SARIF logs can be loaded in VS Code SARIF viewer extension, "
"or SARIF web viewer (https://microsoft.github.io/sarif-web-component/). "
f"Please report a bug on PyTorch Github: {_PYTORCH_GITHUB_ISSUES_URL}"
)
raise errors.OnnxExporterError(message) from e
def common_pre_export_passes(
options: ResolvedExportOptions,
original_model: torch.nn.Module | Callable,
fx_module: torch.fx.GraphModule,
fx_module_args: Sequence[Any],
):
# TODO: Import here to prevent circular dependency
from torch.onnx._internal.fx import analysis, passes
diagnostic_context = options.diagnostic_context
# Apply decomposition table to the input graph.
module = passes.Decompose(
diagnostic_context,
fx_module,
options.decomposition_table,
enable_dynamic_axes=options.dynamic_shapes,
allow_fake_constant=options.fake_context is not None,
).run(*fx_module_args)
# ONNX does not support views and mutations.
# Functionalize to get a semantically equivalent graph without mutations.
module = passes.Functionalize(
diagnostic_context,
module,
enable_dynamic_axes=options.dynamic_shapes,
allow_fake_constant=options.fake_context is not None,
).run(*fx_module_args)
# Input mutations are detected and distilled after `Functionalize` pass.
# Remove them since ONNX inference does not need them.
module = passes.RemoveInputMutation(diagnostic_context, module).run(*fx_module_args)
# ONNX does not support concept of (implicit) type promotion.
# Insert type casts explicitly where needed.
module = passes.InsertTypePromotion(diagnostic_context, module).run()
analysis.UnsupportedFxNodesAnalysis(
diagnostic_context, module, options.onnxfunction_dispatcher
).analyze(infra.levels.ERROR)
if isinstance(original_model, torch.nn.Module):
module = passes.RestoreParameterAndBufferNames(
diagnostic_context, module, original_model
).run()
# This operation should be invoked as the last pre export pass.
# See [NOTE: Modularize pass ordering]
module = passes.Modularize(diagnostic_context, module).run()
# ONNX does not support None inputs. During graph building, all None inputs
# are removed. Here we register this step to input adapter.
options.fx_tracer.input_adapter.append_step(io_adapter.RemoveNoneInputStep())
# NOTE: temp workaround for https://github.com/pytorch/pytorch/issues/99534
# Dynamo doesn't support non-tensor inputs.
options.fx_tracer.input_adapter.append_step(io_adapter.RemoveNonTensorInputStep())
# ONNX does not support complex inputs. During graph building, all complex inputs
# are converted to real representation inputs. Here we register this step to
# input/output adapter.
options.fx_tracer.input_adapter.append_step(
io_adapter.ConvertComplexToRealRepresentationInputStep()
)
# ONNX can't represent collection types (e.g., dictionary, tuple of tuple of
# tensor, etc), we flatten the collection and register each element as output.
options.fx_tracer.output_adapter.append_step(io_adapter.FlattenOutputStep())
# Output post-processing steps should happen after `FlattenOutputStep`.
options.fx_tracer.output_adapter.append_step(
io_adapter.ConvertComplexToRealRepresentationOutputStep()
)
return module
|