
    Phv                       U d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	m
Z
 d dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ d dlmZ d dlZd dlZd dlm Z! d dl"m#c m$Z% d dl&m'Z' d dl(m)Z)m*Z* d dl+m,Z, d d	l-m.Z.m/Z/m0Z0m1Z2 erd dl3Z3d dl4Z4d dl5Z5d d
l6m0Z7 d dl-m8Z8 n	 d dl-m8Z8 dZ:de;d<   	 dZ<	 dZ=	 dZ>	  ej~                  e@      ZAe,j                  ZBej                   G d d             ZD G d d      ZE G d d      ZF G d deF      ZGej                  d        ZIe G d de             ZJ G d d      ZK G d d       ZL G d! d"      ZM G d# d$      ZN G d% d&ej                        ZP G d' d(      ZQ G d) d*eR      ZS G d+ d,eR      ZT G d- d.eR      ZUe)j                  d4d/       ZWe)j                  dd0	 	 	 	 	 d5d1       ZX	 	 	 	 	 	 	 	 d6d2ZYg d3ZZy# e9$ r Y 8w xY w)7    )annotationsN)defaultdict)AnyCallableDictFinalListMappingOptionalProtocolruntime_checkableSequenceSetTupleTYPE_CHECKINGTypeVarUnion)Selffake_tensor)	_beartype
io_adapter)infra)decomposition_tablepatcherregistrationserialization)r   )diagnostics   z
Final[int]_DEFAULT_OPSET_VERSIONz)https://github.com/pytorch/pytorch/issueszreport_dynamo_export.sarifl        c                  ,    e Zd ZU dZded<   	 dZded<   y)ONNXFakeContextaV  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`.
    zfake_tensor.FakeTensorMode	fake_modeNz'Optional[Tuple[Union[str, io.BytesIO]]]state_dict_paths)__name__
__module____qualname____doc____annotations__r$        hC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/onnx/_internal/exporter.pyr"   r"   ^   s!     *)X@D=DJr+   r"   c                  @   e Zd ZdZddZedd       Z	 	 ddZej                  	 	 	 	 	 	 dd       Z
ej                  	 	 d	 	 	 	 	 	 	 	 	 	 	 dd       Zej                  	 d	 	 	 	 	 	 	 dd       Zej                  	 d	 	 	 	 	 	 	 dd	       Zej                  dd
       Zy)OnnxRegistrya  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.

    c                    t        t              | _        ddlm}m} t        | _        t        j                  d| j                   d       | j                  |j                         y)zInitializes the registryr   )opsr   z7torch.onnx.dynamo_export only implements opset version ze for now. If you need to use a different opset version, please register them with register_custom_op.N)r   list	_registry"onnxscript.function_libs.torch_libr0   r   r    _opset_versionwarningswarn _initiate_registry_from_torchlibdefault_registry)selfr0   r   s      r,   __init__zOnnxRegistry.__init__w   s_      		
 5EdFYFYEZ [U U	
 	--l.K.KLr+   c                    | j                   S )zThe ONNX opset version the exporter should target. Defaults to the latest
        supported ONNX opset version: 18. The default version will increment over time as
        ONNX continues to evolve.)r4   r9   s    r,   opset_versionzOnnxRegistry.opset_version   s     """r+   c                    |j                          D ]  \  }}t        j                  j                  |      }|j                  D ];  }t        j
                  ||j                         dd      }| j                  ||       = |j                  D ];  }t        j
                  ||j                         dd      }| j                  ||       =  y)zPopulates the registry with ATen functions from torchlib.

        Args:
            torchlib_registry: The torchlib registry to use for populating the registry.
        Fonnx_functionop_full_name	is_custom
is_complexTN)	itemsr   OpNamefrom_qualified_name	overloadsONNXFunctionqualified_name	_registercomplex)r9   torchlib_registry	aten_nameaten_overloads_funcinternal_name_instanceoverload_funcsymbolic_functioncomplex_funcs           r,   r7   z-OnnxRegistry._initiate_registry_from_torchlib   s     /F.?.E.E.G*I*%1%8%8%L%LY%W"!4!>!>$0$=$="/!7!F!F!H#$	%! 57HI "? !4 ; ;$0$=$=".!7!F!F!H##	%! 57HI !< /Hr+   c                @    | j                   |   j                  |       y)zRegisters a ONNXFunction to an operator.

        Args:
            internal_qualified_name: The qualified name of the operator to register: OpName.
            symbolic_function: The ONNXFunction to register.
        N)r2   append)r9   internal_qualified_namerQ   s      r,   rJ   zOnnxRegistry._register   s     	./667HIr+   Nc                    t         j                  j                  |||      }t        j                  ||j	                         d|      }| j                  ||       y)aO  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'.
        	namespaceop_nameoverloadTr?   N)r   rE   from_name_partsrH   rI   rJ   )r9   functionrX   rY   rZ   rC   rO   rQ   s           r,   register_opzOnnxRegistry.register_op   sb    , ".!4!4!D!D8 "E "
 )55"/>>@!	
 	-/@Ar+   c                |    t         j                  j                  |||      }| j                  j	                  |      S )an  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.
        rW   )r   rE   r[   r2   get)r9   rX   rY   rZ   rO   s        r,   get_op_functionszOnnxRegistry.get_op_functions   s@    $ ".!4!4!D!D8 "E "
 ~~!!"899r+   c                2    | j                  |||      }|duS )a  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.
        rW   N)r`   )r9   rX   rY   rZ   	functionss        r,   is_registered_opzOnnxRegistry.is_registered_op   s-     ))8 * 
	 $$r+   c                x    | j                   j                         D ch c]  }|j                          c}S c c}w )z1Returns the set of all registered function names.)r2   keysrI   )r9   op_name_classs     r,   _all_registered_opsz OnnxRegistry._all_registered_ops  s>     AE@S@S@U
@U}M((*@U
 	
 
s   7returnNone)ri   int)rL   ztorchlib_registry.Registry)rU   zregistration.OpNamerQ   zregistration.ONNXFunctionri   rj   )NF)r\   zAUnion['onnxscript.OnnxFunction', 'onnxscript.TracedOnnxFunction']rX   strrY   rl   rZ   Optional[str]rC   boolri   rj   N)rX   rl   rY   rl   rZ   rm   ri   z)Optional[List[registration.ONNXFunction]])rX   rl   rY   rl   rZ   rm   ri   rn   )ri   zSet[str])r%   r&   r'   r(   r:   propertyr=   r7   r   beartyperJ   r]   r`   rc   rg   r*   r+   r,   r.   r.   n   sd   M4 # #J!;J8 J!4J 5J 
	J J  #' BSB B 	B
  B B 
B B@ FJ::'*:6C:	2: :, FJ%%'*%6C%	% %& 
 
r+   r.   c                      e Zd ZU dZdZded<   	 dZded<   	 ded<   	 dZded	<   	 dZd
ed<   	 e	j                  dddddd	 	 	 	 	 	 	 	 	 dd       Zy)ExportOptionsa  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.
        op_level_debug: Whether to export the model with op-level debug information
        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.
    NOptional[bool]dynamic_shapesop_level_debugDiagnosticOptionsdiagnostic_optionsOptional[ONNXFakeContext]fake_contextOptional[OnnxRegistry]onnx_registry)ru   rv   rz   r|   rx   c               b    || _         || _        || _        || _        |xs
 t	               | _        y ro   )ru   rv   rz   r|   rw   rx   )r9   ru   rv   rz   r|   rx   s         r,   r:   zExportOptions.__init__:  s5     -,(*"4"K8I8Kr+   )
ru   rt   rv   rt   rz   ry   r|   r{   rx   zOptional[DiagnosticOptions])r%   r&   r'   r(   ru   r)   rv   rz   r|   r   rq   r:   r*   r+   r,   rs   rs     s     &*NN) &*NN)Z))2.2L+25,0M)0N *.)-2604:>L 'L '	L
 0L .L 8L Lr+   rs   c                      e Zd ZU dZded<   ded<   ded<   ded<   d	ed
<   ded<   	 ded<   	 ded<   	 ded<   	 ej                  	 d	 	 	 dd       Zy)ResolvedExportOptionszConsolidates :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.
    rn   ru   rv   rw   rx   r"   rz   r.   r|   z%Dict[torch._ops.OpOverload, Callable]r   zFtorch.onnx._internal.fx.onnxfunction_dispatcher.OnnxFunctionDispatcheronnxfunction_dispatcherFXGraphExtractor	fx_tracerdiagnostics.DiagnosticContextdiagnostic_contextNc                   ddl m}m}m} t	        |t
              r|j                  | _        |j                  | _        |j                  | _        |j                  | _	        t	        |t        j                        rWt	        |j                  |j                        s7d}t        |      }t        t        j!                  ||j"                        |      |j                  | _        |j$                  | _        |j&                  | _        |j(                  | _        |j"                  | _        y t+        d      }t,        j.                  dd       }	 |	|j                  d      | _         |	|j                  t1                     | _        t	        |t        j                        r|j                         | _        n|j3                         | _         |	|j                  d       | _	         |j4                  dt6        j8                  | j                        | _         |	|j$                  t;                     | _        t)        j<                  | j$                        | _        ddl m}
  |	|j                  d      | _        |
j?                  | j$                  | j"                        | _        tA        |      D ]+  }|jC                  d	      rtE        | |      r"J d
| d        y )Nr   )r   dynamo_graph_extractortorch_export_graph_extractorzP'model' of type 'ExportedProgram' is only supported with 'TorchExport' FX TracerTc                2    | | S t        |      r |       S |S ro   )callable)valuefallbacks     r,   resolvez/ResolvedExportOptions.__init__.<locals>.resolve  s"    $ LH%#:%r+   Fztorch.onnx.dynamo_export)r   _zUnresolved option '')r   zOptional[T]r   zUnion[T, Callable[[], T]]ri   r   )#torch.onnx._internal.fxr   r   r   
isinstancer   ru   rv   rx   rz   torch_exportExportedProgramr   TorchExportInvalidExportOptionsErrorONNXProgram_from_failurer   r|   r   r   r   r   rq   rw   DynamoExportDiagnosticContexttorch__version__r.   (create_onnx_friendly_decomposition_tableOnnxFunctionDispatcherdir
startswithhasattr)r9   optionsmodelr   r   r   messageer   r   r   keys               r,   r:   zResolvedExportOptions.__init__f  st   	
 	
 g45")"8"8D")"8"8D&-&@&@D# ' 4 4D%!=!=>z!!#?#K#KH m-g6/--a1K1KLg  %..DN!(!6!6D+2+J+JD('.'B'BD$&-&@&@D#A     #*'*@*@%"HD&-**,=,?'D# %!=!=>!=!I!I!K!7!D!D!F '(<(<d CD&Ck&C&C*!!'''D# "))>)>!OD#LL&& $ H")'*@*@%"HD'>>&&++ ( 7|~~c*"4-K1DSE/KK- $r+   ro   )r   z-Union[ExportOptions, 'ResolvedExportOptions']r   HOptional[Union[torch.nn.Module, Callable, torch_export.ExportedProgram]])r%   r&   r'   r(   r)   r   rq   r:   r*   r+   r,   r   r   K  s     ))!! ?>LccPT55:  [_NL>NL XNL NLr+   r   c               #  n  K   ddl m}  ddlm}  | j                  t
        j                  j                           |dd            }t        j                         }t        |      }|5  |5  | ddd       ddd       t        |j                        |_        y# 1 sw Y   ,xY w# 1 sw Y   0xY ww)	a^  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
    exporting large models without the actual memory footprint needed for executing it.

    It is highly recommended to enable fake mode when exporting models that
    are too large to fit into memory.

    Returns:
        A :class:`ONNXFakeContext` object that must be passed to :func:`dynamo_export`
        through the :attr:`ExportOptions.fake_context` argument.

    Example::

        # xdoctest: +REQUIRES(env:TORCH_DOCTEST_ONNX)
        >>> import torch
        >>> import torch.onnx
        >>> class MyModel(torch.nn.Module):  # Dummy model
        ...     def __init__(self) -> None:
        ...         super().__init__()
        ...         self.linear = torch.nn.Linear(2, 2)
        ...     def forward(self, x):
        ...         out = self.linear(x)
        ...         return out
        >>> with torch.onnx.enable_fake_mode() as fake_context:
        ...     my_nn_module = MyModel()
        ...     arg1 = torch.randn(2, 2, 2)  # positional input 1
        >>> export_options = torch.onnx.ExportOptions(fake_context=fake_context)
        >>> onnx_program = torch.onnx.dynamo_export(
        ...     my_nn_module,
        ...     arg1,
        ...     export_options=export_options
        ... )
        >>> # Saving model WITHOUT initializers
        >>> onnx_program.save("my_model_without_initializers.onnx")
        >>> # Saving model WITH initializers
        >>> onnx_program.save("my_model_with_initializers.onnx", model_state_dict=MyModel().state_dict())

    .. warning::
        This API is experimental and is *NOT* backward-compatible.

    r   r   )ShapeEnvF)allow_scalar_outputsallow_dynamic_output_shape_ops)allow_non_fake_inputs	shape_env)r#   N)torch._subclassesr   %torch.fx.experimental.symbolic_shapesr   FakeTensorModer   _guardsdetect_fake_moder   ONNXTorchPatcherr"   tuplepathsr$   )r   r   r#   patcher_contextrz   s        r,   enable_fake_moder     s     d .> +**"'--"@"@"BB!&u
I ..0O"Y7L	O $$)%L! $Os6   A(B5*B)-B2B):#B5B&	"B))B2.B5c                  $    e Zd ZdZ	 	 	 	 	 	 ddZy)ONNXProgramSerializerzProtocol for serializing an ONNX graph into a specific format (e.g. Protobuf).
    Note that this is an advanced usage scenario.c                     y)aa  Protocol method that must be implemented for serialization.

        Args:
            onnx_program: Represents the in-memory exported ONNX model
            destination: A binary IO stream or pre-allocated buffer into which
                the serialized model should be written.

        Example:

            A simple serializer that writes the exported :py:obj:`onnx.ModelProto` in Protobuf
            format to ``destination``:

            ::

                # xdoctest: +REQUIRES(env:TORCH_DOCTEST_ONNX)
                >>> import io
                >>> import torch
                >>> import torch.onnx
                >>> class MyModel(torch.nn.Module):  # Dummy model
                ...     def __init__(self) -> None:
                ...         super().__init__()
                ...         self.linear = torch.nn.Linear(2, 2)
                ...     def forward(self, x):
                ...         out = self.linear(x)
                ...         return out
                >>> class ProtobufONNXProgramSerializer:
                ...     def serialize(
                ...         self, onnx_program: torch.onnx.ONNXProgram, destination: io.BufferedIOBase
                ...     ) -> None:
                ...         destination.write(onnx_program.model_proto.SerializeToString())
                >>> model = MyModel()
                >>> arg1 = torch.randn(2, 2, 2)  # positional input 1
                >>> torch.onnx.dynamo_export(model, arg1).save(
                ...     destination="exported_model.onnx",
                ...     serializer=ProtobufONNXProgramSerializer(),
                ... )
        Nr*   )r9   onnx_programdestinations      r,   	serializezONNXProgramSerializer.serialize  s    P 	r+   Nr   r   r   zio.BufferedIOBaseri   rj   )r%   r&   r'   r(   r   r*   r+   r,   r   r     s$    5('(6G(	(r+   r   c                  B    e Zd ZdZej
                  	 	 	 	 	 	 dd       Zy)ProtobufONNXProgramSerializerz"Serializes ONNX graph as Protobuf.c                    dd l }t        |j                  |j                        st	        d      |j                  |j                  j                                y )Nr   z1onnx_program.ModelProto is not an onnx.ModelProto)onnxr   model_proto
ModelProto
ValueErrorwriteSerializeToStringr9   r   r   r   s       r,   r   z'ProtobufONNXProgramSerializer.serialize5  sD     	,22DOODPQQ,22DDFGr+   Nr   )r%   r&   r'   r(   r   rq   r   r*   r+   r,   r   r   2  s:    ,H'H6GH	H Hr+   r   c                  V    e Zd ZU dZded<   ddZej                  	 	 	 	 	 	 dd       Zy)	"LargeProtobufONNXProgramSerializerzSerializes ONNX graph as Protobuf.

    Fallback to serializing as Protobuf with external data for models larger than 2GB.
    z
Final[str]_destination_pathc                    || _         y ro   )r   )r9   destination_paths     r,   r:   z+LargeProtobufONNXProgramSerializer.__init__H  s
    !1r+   c                    ddl }|j                  j                         t        k  r( |j                  |j                  | j
                         y |j                  |j                  | j
                  dd       y)zQ`destination` is ignored. The model is saved to `self._destination_path` instead.r   NT)save_as_external_dataall_tensors_to_one_file)r   r   ByteSize_PROTOBUF_SIZE_MAX_LIMIT
save_modelr   r   s       r,   r   z,LargeProtobufONNXProgramSerializer.serializeK  sc    
 	##,,.1IIDOOL44d6L6LM DOO((&&&*(,	r+   N)r   rl   r   )	r%   r&   r'   r(   r)   r:   r   rq   r   r*   r+   r,   r   r   @  sF    
 "!2 '6G	 r+   r   c                  |    e Zd ZU dZdZded<   	 dZded<   	 dZded<   	 ej                  dddd		 	 	 	 	 dd
       Z
y)ONNXRuntimeOptionsaA  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.
    N0Optional[Sequence['onnxruntime.SessionOptions']]session_options:Optional[Sequence[Union[str, Tuple[str, Dict[Any, Any]]]]]execution_providers"Optional[Sequence[Dict[Any, Any]]]execution_provider_optionsr   r   r   c               .    || _         || _        || _        y ro   r   )r9   r   r   r   s       r,   r:   zONNXRuntimeOptions.__init__s  s      /#6 *D'r+   )r   r   r   r   r   r   )r%   r&   r'   r(   r   r)   r   r   r   rq   r:   r*   r+   r,   r   r   _  s     IMOEL' 	    JEI BI2 MQ IME JE
	E %GE Er+   r   c                     e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   ded<   ded<   ded<   ej                  ddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d"d       Zddd	 	 	 	 	 	 	 	 	 d#dZe	d$d       Z
e	d%d       Ze	d&d       Ze	d'd       Zej                  dd	 	 	 d(d       Zej                  	 d)	 	 	 	 	 d*d       Zej                  ddd	 	 	 	 	 	 	 d+d       Zej                  d,d        Ze	 	 	 	 	 	 d-d!       Zy).r   a  An in-memory representation of a PyTorch model that has been exported to ONNX.

    Args:
        model_proto: The exported ONNX model as an :py:obj:`onnx.ModelProto`.
        input_adapter: The input adapter used to convert PyTorch inputs into ONNX inputs.
        output_adapter: The output adapter used to convert PyTorch outputs into ONNX outputs.
        diagnostic_context: Context object for the SARIF diagnostic system responsible for logging errors and metadata.
        fake_context: The fake context used for symbolic tracing.
        export_exception: The exception that occurred during export, if any.
        model_signature: The model signature for the exported ONNX graph.
    zFinal[onnx.ModelProto]_model_protozFinal[io_adapter.InputAdapter]_input_adapterzFinal[io_adapter.OutputAdapter]_output_adapterz$Final[diagnostics.DiagnosticContext]_diagnostic_contextz Final[Optional[ONNXFakeContext]]_fake_contextzFinal[Optional[Exception]]_export_exceptionz2Final[Optional[torch.export.ExportGraphSignature]]_model_signaturezOFinal[Optional[Union[torch.nn.Module, Callable, torch_export.ExportedProgram]]]_model_torchN)rz   export_exceptionmodel_signaturemodel_torchc               t    || _         || _        || _        || _        || _        || _        || _        || _        y ro   )r   r   r   r   r   r   r   r   )	r9   r   input_adapteroutput_adapterr   rz   r   r   r   s	            r,   r:   zONNXProgram.__init__  sD     ( /'+-#5 )!1r+   )model_with_state_dictr   c                  ddl }|xs | j                  } | j                  |d|i|}|xs
 t               }|j                  xs  |j
                         }| j                  j                         } |j                  ||      }	t        |	j                         |      D 
ci c]"  \  }
}|
j                  |j                  d      $ }}
}|	j                  d|      S c c}}
w )a  Runs the ONNX model using ONNX Runtime

        Args:
            args: The positional inputs to the model.
            kwargs: The keyword inputs to the model.
            model_with_state_dict: The PyTorch model to fetch state from.
                Required when :func:`enable_fake_mode` is used to extract real initializers as needed by the ONNX graph.
            options: The options to use for running the model with ONNX Runtime.

        Returns:
            The model output as computed by ONNX Runtime
        r   Nr   )	providersT)force)onnxruntimer   adapt_torch_inputs_to_onnxr   r   get_available_providersr   r   InferenceSessionzip
get_inputsnamenumpyrun)r9   r   r   argskwargsr   
onnx_inputr   
onnx_modelort_sessionkvonnxruntime_inputs                r,   __call__zONNXProgram.__call__  s    * 	 !6 J9J9J4T44
)>
BH

 1/1//X3V;3V3V3X	%%779
2k22:S K224jA
A1 FFAGG$G''A 	 

 t%677
s   !'Cc                J    | j                   | j                   | j                  S )z8The exported ONNX model as an :py:obj:`onnx.ModelProto`.)r   r   r<   s    r,   r   zONNXProgram.model_proto  s(     !!-(((   r+   c                    | j                   S )a  The model signature for the exported ONNX graph.

        This information is relevant because ONNX specification often differs from PyTorch's, resulting
        in a ONNX graph with input and output schema different from the actual PyTorch model implementation.
        By using the model signature, the users can understand the inputs and outputs differences
        and properly execute the model in ONNX Runtime.

        NOTE: Model signature is only available when the ONNX graph was exported from a
        :class:`torch.export.ExportedProgram` object.

        NOTE: Any transformation done to the model that changes the model signature must be accompanied
        by updates to this model signature as well through :class:`InputAdaptStep` and/or :class:`OutputAdaptStep`.

        Example:

            The following model produces different sets of inputs and outputs.
            The first 4 inputs are model parameters (namely conv1.weight, conv2.weight, fc1.weight, fc2.weight),
            and the next 2 inputs are registered buffers (namely my_buffer2, my_buffer1) and finally
            the last 2 inputs are user inputs (namely x and b).
            The first output is a buffer mutation (namely my_buffer2) and the last output is the actual model output.

            >>> class CustomModule(torch.nn.Module):
            ...     def __init__(self):
            ...         super().__init__()
            ...         self.my_parameter = torch.nn.Parameter(torch.tensor(2.0))
            ...         self.register_buffer("my_buffer1", torch.tensor(3.0))
            ...         self.register_buffer("my_buffer2", torch.tensor(4.0))
            ...         self.conv1 = torch.nn.Conv2d(1, 32, 3, 1, bias=False)
            ...         self.conv2 = torch.nn.Conv2d(32, 64, 3, 1, bias=False)
            ...         self.fc1 = torch.nn.Linear(9216, 128, bias=False)
            ...         self.fc2 = torch.nn.Linear(128, 10, bias=False)
            ...     def forward(self, x, b):
            ...         tensor_x = self.conv1(x)
            ...         tensor_x = torch.nn.functional.sigmoid(tensor_x)
            ...         tensor_x = self.conv2(tensor_x)
            ...         tensor_x = torch.nn.functional.sigmoid(tensor_x)
            ...         tensor_x = torch.nn.functional.max_pool2d(tensor_x, 2)
            ...         tensor_x = torch.flatten(tensor_x, 1)
            ...         tensor_x = self.fc1(tensor_x)
            ...         tensor_x = torch.nn.functional.sigmoid(tensor_x)
            ...         tensor_x = self.fc2(tensor_x)
            ...         output = torch.nn.functional.log_softmax(tensor_x, dim=1)
            ...         (
            ...         self.my_buffer2.add_(1.0) + self.my_buffer1
            ...         )  # Mutate buffer through in-place addition
            ...         return output
            >>> inputs = (torch.rand((64, 1, 28, 28), dtype=torch.float32), torch.randn(3))
            >>> exported_program = torch.export.export(CustomModule(), args=inputs)
            >>> onnx_program = torch.onnx.dynamo_export(exported_program, *inputs)
            >>> print(onnx_program.model_signature)
            ExportGraphSignature(
                input_specs=[
                    InputSpec(kind=<InputKind.PARAMETER: 2>, arg=TensorArgument(name='arg0_1'), target='conv1.weight'),
                    InputSpec(kind=<InputKind.PARAMETER: 2>, arg=TensorArgument(name='arg1_1'), target='conv2.weight'),
                    InputSpec(kind=<InputKind.PARAMETER: 2>, arg=TensorArgument(name='arg2_1'), target='fc1.weight'),
                    InputSpec(kind=<InputKind.PARAMETER: 2>, arg=TensorArgument(name='arg3_1'), target='fc2.weight'),
                    InputSpec(kind=<InputKind.BUFFER: 3>, arg=TensorArgument(name='arg4_1'), target='my_buffer2'),
                    InputSpec(kind=<InputKind.BUFFER: 3>, arg=TensorArgument(name='arg5_1'), target='my_buffer1'),
                    InputSpec(kind=<InputKind.USER_INPUT: 1>, arg=TensorArgument(name='l_x_'), target=None),
                    InputSpec(kind=<InputKind.USER_INPUT: 1>, arg=TensorArgument(name='arg1'), target=None)
                ],
                output_specs=[
                    OutputSpec(kind=<OutputKind.BUFFER_MUTATION: 3>, arg=TensorArgument(name='add'), target='my_buffer2'),
                    OutputSpec(kind=<OutputKind.USER_OUTPUT: 1>, arg=TensorArgument(name='_log_softmax'), target=None)
                ]
            )
        )r   r<   s    r,   r   zONNXProgram.model_signature  s    L $$$r+   c                    | j                   S )z2The diagnostic context associated with the export.)r   r<   s    r,   r   zONNXProgram.diagnostic_context+  s     '''r+   c                    | j                   S )z,The fake context associated with the export.)r   r<   s    r,   rz   zONNXProgram.fake_context1  s     !!!r+   )r   c               p    |xs | j                   }|J d        | j                  j                  |d|i|S )a	  Converts the PyTorch model inputs to exported ONNX model inputs format.

        Due to design differences, input/output format between PyTorch model and exported
        ONNX model are often not the same. E.g., None is allowed for PyTorch model, but are
        not supported by ONNX. Nested constructs of tensors are allowed for PyTorch model,
        but only flattened tensors are supported by ONNX, etc.

        The actual adapting steps are associated with each individual export. It
        depends on the PyTorch model, the particular set of model_args and model_kwargs
        used for the export, and export options.

        This method replays the adapting steps recorded during export.

        Args:
            model_args: The PyTorch model inputs.
            model_with_state_dict: The PyTorch model to get extra state from.
                If not specified, the model used during export is used.
                Required when :func:`enable_fake_mode` is used to extract real initializers as needed by the ONNX graph.
            model_kwargs: The PyTorch model keyword inputs.

        Returns:
            A sequence of tensors converted from PyTorch model inputs.

        Example::

            # xdoctest: +REQUIRES(env:TORCH_DOCTEST_ONNX)
            >>> import torch
            >>> import torch.onnx
            >>> from typing import Dict, Tuple
            >>> def func_nested_input(
            ...     x_dict: Dict[str, torch.Tensor],
            ...     y_tuple: Tuple[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]
            ... ):
            ...     if "a" in x_dict:
            ...         x = x_dict["a"]
            ...     elif "b" in x_dict:
            ...         x = x_dict["b"]
            ...     else:
            ...         x = torch.randn(3)
            ...
            ...     y1, (y2, y3) = y_tuple
            ...
            ...     return x + y1 + y2 + y3
            >>> x_dict = {"a": torch.tensor(1.)}
            >>> y_tuple = (torch.tensor(2.), (torch.tensor(3.), torch.tensor(4.)))
            >>> onnx_program = torch.onnx.dynamo_export(func_nested_input, x_dict, y_tuple)
            >>> print(x_dict, y_tuple)
            {'a': tensor(1.)} (tensor(2.), (tensor(3.), tensor(4.)))
            >>> print(onnx_program.adapt_torch_inputs_to_onnx(x_dict, y_tuple, model_with_state_dict=func_nested_input))
            (tensor(1.), tensor(2.), tensor(3.), tensor(4.))

        .. warning::
            This API is experimental and is *NOT* backward-compatible.

        (model_with_state_dict must be specified.r   )r   r   apply)r9   r   
model_argsmodel_kwargss       r,   r   z&ONNXProgram.adapt_torch_inputs_to_onnx7  sZ    B !6 J9J9J!-	65	6-(t""((
4
8D
 	
r+   c                n    |xs | j                   }|J d       | j                  j                  ||      S )aR  Converts the PyTorch model outputs to exported ONNX model outputs format.

        Due to design differences, input/output format between PyTorch model and exported
        ONNX model are often not the same. E.g., None is allowed for PyTorch model, but are
        not supported by ONNX. Nested constructs of tensors are allowed for PyTorch model,
        but only flattened tensors are supported by ONNX, etc.

        The actual adapting steps are associated with each individual export. It
        depends on the PyTorch model, the particular set of model_args and model_kwargs
        used for the export, and export options.

        This method replays the adapting steps recorded during export.

        Args:
            model: The PyTorch model to get extra state from.
            model_outputs: The PyTorch model outputs.
            model_with_state_dict: The PyTorch model to get extra state from.
                If not specified, the model used during export is used.
                Required when :func:`enable_fake_mode` is used to extract real initializers as needed by the ONNX graph.

        Returns:
            PyTorch model outputs in exported ONNX model outputs format.

        Example::

            # xdoctest: +REQUIRES(env:TORCH_DOCTEST_ONNX)
            >>> import torch
            >>> import torch.onnx
            >>> def func_returning_tuples(x, y, z):
            ...     x = x + y
            ...     y = y + z
            ...     z = x + y
            ...     return (x, (y, z))
            >>> x = torch.tensor(1.)
            >>> y = torch.tensor(2.)
            >>> z = torch.tensor(3.)
            >>> onnx_program = torch.onnx.dynamo_export(func_returning_tuples, x, y, z)
            >>> pt_output = func_returning_tuples(x, y, z)
            >>> print(pt_output)
            (tensor(3.), (tensor(5.), tensor(8.)))
            >>> print(onnx_program.adapt_torch_outputs_to_onnx(pt_output, model_with_state_dict=func_returning_tuples))
            [tensor(3.), tensor(5.), tensor(8.)]

        .. warning::
            This API is experimental and is *NOT* backward-compatible.

        r  r   )r   r   r  )r9   model_outputsr   s      r,   adapt_torch_outputs_to_onnxz'ONNXProgram.adapt_torch_outputs_to_onnx  sI    p !6 J9J9J!-	65	6-##))-?T)UUr+   )model_state_dict
serializerc                  |&t        |t              rt        |      }n
t               }g }|t        |t              rMt        j                         }t        j                  ||       |j                  d       |j                  |       nt        |t              df |j                  |       n| j                  r| j                  j                  r| j                  j                  D ]i  }||v r	 t        j                  |      }t        j                         }t        j                  ||       |j                  d       |j                  |       k |rt        |t              st        d      t         j"                  j%                  |      \  }	}
|
}|
j%                  d      d   dz   }t'        j(                  |	||t+        |      | j,                         yt        |t              r(t/        |d      5 }|j1                  | |       ddd       y	 |j1                  | |       y# t        $ r Y Iw xY w# 1 sw Y   yxY w# t2        $ r}t3        d      |d}~ww xY w)	a  Saves the in-memory ONNX model to ``destination`` using specified ``serializer``.

        Args:
            destination: The destination to save the ONNX model. It can be either a string or a file-like object.
                When used with ``model_state_dict``, it must be a string with a full path to the destination.
                In that case, besides saving the ONNX model, a folder with "_initializers" suffix (without extension)
                will be created to store the each initializer of the ONNX model in a separate file. For example, if the
                destination is "/path/model.onnx", the initializers will be saved in "/path/model_initializers/" folder.
            model_state_dict: The state_dict of the PyTorch model containing all weights on it.
                It can be either a dict as returned by :meth:`model.state_dict`, or a string with a file name.
                Required when :func:`enable_fake_mode` is used but real initializers are needed on the ONNX graph.
                It can be either a string with the path to a checkpoint or a dictionary with the actual model state.

            serializer: The serializer to use. If not specified, the model will be serialized as Protobuf.
        Nr   zRmodel_state_dict must be a path to the model's state_dict or the actual state_dictzP`destination` must be a string with a path when `model_state_dict` is specified.._initializerswbz'destination' should be provided as a path-like string when saving a model larger than 2GB. External tensor data will be saved alongside the model on disk.)r   rl   r   r   dictioBytesIOr   saveseekrT   r   r$   loadFileNotFoundErrorRuntimeErrorospathsplitfx_serializationsave_model_with_external_datar   r   openr   r   )r9   r   r  r  _model_state_dict_filesmodel_state_dict_filer  extra_state_dictextra_state_dict_filer   destination_filenameonnx_model_locationonnx_initializer_locationfexcs                  r,   r  zONNXProgram.save  s:   0 +s+?L
:<
 AC'*D1(*

%

+-BC%**1-'../DE$cgh (../?@D$6$6$G$G**;;22',zz$'7$,.JJL)JJ/1FG)..q1+223HI < #k3/"f  68WW]];5O22"6$**3/2_D & :: #)-.   +s++t,((q1 -,(({;9 ) 0 -,
 " $Z s7   /A!H#3H3H? #	H0/H03H<?	IIIc                    |j                  d      s%d| }t        j                  |       t        |      | j                  j                  |       y)aL  Saves the export diagnostics as a SARIF log to the specified destination path.

        Args:
            destination: The destination to save the diagnostics SARIF log.
                It must have a `.sarif` extension.

        Raises:
            ValueError: If the destination path does not end with `.sarif` extension.
        z.sarifz0'destination' must have a .sarif extension, got N)endswithlogfatalr   r   dump)r9   r   r   s      r,   save_diagnosticszONNXProgram.save_diagnostics  sI     ##H-HVGIIgW%%$$[1r+   c                    ddl }t         |j                         t        j                         t        j
                         ||      S )aZ  
        Creates an instance of :class:`ONNXProgram` when the export process encounters a failure.

        In case of a failed export, this method is used to encapsulate the exception
        and associated diagnostic context within an :class:`ONNXProgram` instance for
        easier handling and debugging.

        Args:
            export_exception: The exception raised during the export process.
            diagnostic_context: The context associated with diagnostics during export.

        Returns:
            An instance of :class:`ONNXProgram` representing the failed ONNX program.
        r   N)r   )r   r   r   r   InputAdapterOutputAdapter)clsr   r   r   s       r,   r   zONNXProgram._from_failure*  s@    , 	 DOO##%$$&-
 	
r+   )r   onnx.ModelProtor   zio_adapter.InputAdapterr   zio_adapter.OutputAdapterr   r   rz   ry   r   zOptional[Exception]r   +Optional[torch.export.ExportGraphSignature]r   r   )
r   r   r   r   r   zOptional[ONNXRuntimeOptions]r   r   ri   r   )ri   r6  )ri   r7  )ri   r   )ri   ry   )r   r   ri   /Sequence[Union[torch.Tensor, int, float, bool]]ro   )r  r   r   r   ri   r8  )r   zUnion[str, io.BufferedIOBase]r  z$Optional[Union[Dict[str, Any], str]]r  zOptional[ONNXProgramSerializer]ri   rj   )r   rl   ri   rj   )r   	Exceptionr   r   ri   r   )r%   r&   r'   r(   r)   r   rq   r:   r  rp   r   r   r   rz   r   r  r  r1  classmethodr   r*   r+   r,   r   r     s[   
 )(2244==3311HH   3704GK 2$2 /2 1	2
 :2 02 .2 E2
2 28 04'8'8 
'8 .'8 '8 
'8R ! ! E% E%N ( (
 " "
  F
 
F
 
9F
 F
P  ;V;V 
;V 
9;V ;Vz 
 BF6:W2W ?	W
 4W 
W Wr 2 2" 
#
 :
 
	
 
r+   r   c                       e Zd ZdZd fdZej                  	 	 	 	 	 	 	 	 	 	 dd       Zej                  	 	 	 	 	 	 	 	 dd       Z xZ	S )r   zAbstract 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.c                    t         |           t        j                         | _        t        j
                         | _        y ro   )superr:   r   r3  r   r4  r   )r9   	__class__s    r,   r:   zFXGraphExtractor.__init__Q  s0    6@6M6M6O8B8P8P8Rr+   c                     y)aH  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.
        Nr*   )r9   r   r   r	  r
  s        r,   generate_fxzFXGraphExtractor.generate_fxV  s    " 	r+   c                     y)ab  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.
        Nr*   )r9   r   original_model	fx_modulefx_module_argss        r,   pre_export_passesz"FXGraphExtractor.pre_export_passesj  s     	r+   rh   )
r   r   r    Union[torch.nn.Module, Callable]r	  Sequence[Any]r
  Mapping[str, Any]ri   torch.fx.GraphModuler   r   rB  rF  rC  rI  rD  rG  )
r%   r&   r'   r(   r:   abcabstractmethodr@  rE  __classcell__r>  s   @r,   r   r   L  s    QS
 	& 0 "	
 ( 
 & 	& 9 (	
 & r+   r   c                  P    e Zd Zej                  	 	 	 	 	 	 	 	 dd       ZddZd Zy)Exporterc                    || _         | j                   J || _        || _        || _        ddlm} t        | j                   j                  |j                        s| j                          y y )Nr   )fx_symbolic_graph_extractor)
r   r   r	  r
  r   rR  r   r   FXSymbolicTracer_assert_fake_tensor_mode)r9   r   r   r	  r
  rR  s         r,   r:   zExporter.__init__}  sh     ||'''
$( 	HLL""$?$P$P
 ))+
r+   c                P   | j                   j                  5  | j                   j                  j                  | j                   | j                  | j
                  | j                        }ddlm} |j                  | j                   j                        }|j                  || j                   j                  | j                   j                        }| j                   j                  Ui }|j                  j                         D ]/  \  }}t!        |t"        j$                  j&                        r+|||<   1 ||_        |j)                  | j                   j*                  j,                        }t"        j.                  j1                  || j                   j                  j2                  | j                   j                  j4                  | j                   j                  | j                   j                  t7        | j                  dd       | j                        cd d d        S # 1 sw Y   y xY w)Nr   )fx_onnx_interpreter)r   )fx_graph_moduler   rv   graph_signature)rz   r   r   )r   r   r   r@  r   r	  r
  r   rV  FxOnnxInterpreterr   r   rv   rz   initializersrD   r   r   _subclasses
FakeTensorto_model_protor|   r=   r   r   r   r   getattr)	r9   graph_modulerV  fx_interpreteronnxscript_graphinitializers_with_real_tensorsinitializer_nameinitializerr   s	            r,   exportzExporter.export  s   \\,,<<11==djj$//4;L;LL D0BB#'<<#B#B C N  .11 ,(,(L(L#||::  2   ||((4JL. &2288:$%k53D3D3O3OPKV67GH ; 1O - *88**88J ::))&&44&&55//!\\66 'JJ 14! !JJ * 
K -,,s   DH-C%HH%c                2   t        j                  d | j                  | j                  f      }d}t	        | j
                  t        j                  j                        rIt        j                  d | j
                  j                         | j
                  j                         f      }|s|r!| j                  j                  st        d      t        j                  d | j                  | j                  f      }d}t	        | j
                  t        j                  j                        rIt        j                  d | j
                  j                         | j
                  j                         f      }|s|r"| j                  j                  rt        d      yy)	zAAsserts that the model and its input do not contain fake tensors.c                J    t        | t        j                  j                        S ro   r   r   r[  r\  xs    r,   <lambda>z3Exporter._assert_fake_tensor_mode.<locals>.<lambda>  s    jE$5$5$@$@Ar+   Fc                J    t        | t        j                  j                        S ro   rh  ri  s    r,   rk  z3Exporter._assert_fake_tensor_mode.<locals>.<lambda>  s    *Q(9(9(D(DEr+   zJCannot export a model with fake inputs/weights without enabling fake mode.c                    t        | t        j                        xr% t        | t        j                  j                         S ro   r   r   Tensorr[  r\  ri  s    r,   rk  z3Exporter._assert_fake_tensor_mode.<locals>.<lambda>  s4    jELL1 @q%"3"3">">??@r+   c                    t        | t        j                        xr% t        | t        j                  j                         S ro   rn  ri  s    r,   rk  z3Exporter._assert_fake_tensor_mode.<locals>.<lambda>  s4    *Q5 D"1e&7&7&B&BCCDr+   zICannot export a model with non fake inputs/weights and enabled fake mode.N)pytreetree_anyr	  r
  r   r   r   nnModule
parametersbuffersr   rz   r  )r9   has_any_fake_tensorhas_any_fake_param_or_bufferhas_any_non_fake_tensors has_any_non_fake_param_or_buffers        r,   rT  z!Exporter._assert_fake_tensor_mode  sM    %ooA__d//0
 (-$djj%((//2+1??E&&($***<*<*>?,(
  #?,,++\  $*??@__d//0$
 
 ,1(djj%((//2/5D&&($***<*<*>?0, %(Hll''[  ( )Ir+   N)r   r   r   >Union[torch.nn.Module, Callable, torch_export.ExportedProgram]r	  rG  r
  rH  )ri   r   )r%   r&   r'   r   rq   r:   re  rT  r*   r+   r,   rP  rP  |  sL    ,&, N, "	,
 (, ,.0d&r+   rP  c                  $     e Zd ZdZd fdZ xZS )UnsatisfiedDependencyErrorz<Raised when an ONNX exporter dependency cannot be satisfied.c                2    t         |   |       || _        y ro   )r=  r:   package_name)r9   r  r   r>  s      r,   r:   z#UnsatisfiedDependencyError.__init__  s    !(r+   )r  rl   r   rl   )r%   r&   r'   r(   r:   rM  rN  s   @r,   r}  r}    s    F) )r+   r}  c                  0     e Zd ZU dZded<   d fdZ xZS )OnnxExporterErrora  Raised when an ONNX exporter error occurs.

    This exception is thrown when there's an error during the ONNX export process.
    It encapsulates the :class:`ONNXProgram` object generated until the failure, allowing
    access to the partial export results and associated metadata.
    zFinal[ONNXProgram]r   c                2    t         |   |       || _        y)z
        Initializes the OnnxExporterError with the given ONNX program and message.

        Args:
            onnx_program (ONNXProgram): The partial results of the ONNX export.
            message (str): The error message to be displayed.
        N)r=  r:   r   )r9   r   r   r>  s      r,   r:   zOnnxExporterError.__init__  s     	!(r+   )r   r   r   rl   )r%   r&   r'   r(   r)   r:   rM  rN  s   @r,   r  r    s     %$	) 	)r+   r  c                      e Zd ZdZy)r   zKRaised when user specified an invalid value for the :class:`ExportOptions`.N)r%   r&   r'   r(   r*   r+   r,   r   r     s    Ur+   r   c                   | j                   j                  dd}dfd}	 dd l}|j                  j                         k  r |d      	 dd l}t        |j                  j                  df   |j                  j                        s |d      y # t        $ r} |d|      |d }~ww xY w# t        $ r} |d|      |d }~ww xY w)	Nc                Z    d|  d|  d}t         j                  ||       t        | |      S )NzPlease install the `z'` package (e.g. `python -m pip install z`).)exc_infor.  r/  r}  )r  r  r   s      r,   missing_packagez-_assert_dependencies.<locals>.missing_package  s>    "<. 1,,8>> 	 			'H	-),@@r+   c                ^    d|  d d|  d}t         j                  |       t        | |      S )NzThe installed `z4` does not support the specified ONNX opset version z. Install a newer `z,` package or specify an older opset version.r  )r  r   r=   s     r,   missing_opsetz+_assert_dependencies.<locals>.missing_opset   sF    l^ ,$o%8 G./ 	
 			'),@@r+   r   r   
onnxscript )r  rl   r  zlogging._ExcInfoType)r  rl   )r|   r=   r   ImportErrordefsonnx_opset_versionr  r   
onnx_opset
all_opsetsvaluesOpset)export_optionsr  r  r   r   r  r=   s         @r,   _assert_dependenciesr    s    "00>>MAA0 yy##%5F##6 (("m)<= L))	  0fa(a/0  6lA.A56s/   B B. 	B+
B&&B+.	C7
CC)r  c                 | t        |t              r|nt        ||       }nt        t               |       }t        |       	 t	        || ||      j                         S # t        $ r]}t        }|j                  j                  |       d| dt         }t        t        j                  ||j                        |      |d}~ww xY w)aQ  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.}
        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"
              }
            }
          }
        }
    Nr  )r   r   r	  r
  z@Failed to export the model to ONNX. Generating SARIF report at 'z'. 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/). Please report a bug on PyTorch Github: )r   r   rs   r  rP  re  r9  %_DEFAULT_FAILED_EXPORT_SARIF_LOG_PATHr   r0  _PYTORCH_GITHUB_ISSUES_URLr  r   r   )r   r  r	  r
  resolved_export_optionsr   sarif_report_pathr   s           r,   dynamo_exportr  >  s    T ! .*?@ &~UC 	  #8u"U01+!%	

 &(	  A22778IJNO`Na b6 7Q5Q	S 	  %%a)@)S)ST
 	s   A! !	C*ACCc           	        ddl m}m} | j                  } |j	                  ||| j
                  | j                  | j                  d u      j                  | } |j                  ||| j                  | j                  d u      j                  | } |j                  ||      j                  | }|j                  ||      j                         }|j                  ||| j                        j                  t        j                   j"                         t%        |t&        j(                  j*                        r!|j-                  |||      j                         }|j/                  ||      j                         }| j0                  j2                  j5                  t7        j8                                | j0                  j2                  j5                  t7        j:                                | j0                  j2                  j5                  t7        j<                                | j0                  j>                  j5                  t7        j@                                | j0                  j>                  j5                  t7        jB                                |S )Nr   )analysispasses)enable_dynamic_axesallow_fake_constant)"r   r  r  r   	Decomposer   ru   rz   r   FunctionalizeRemoveInputMutationInsertTypePromotionUnsupportedFxNodesAnalysisr   analyzer   levelsERRORr   r   rs  rt  RestoreParameterAndBufferNames
Modularizer   r   append_stepr   RemoveNoneInputStepRemoveNonTensorInputStep+ConvertComplexToRealRepresentationInputStepr   FlattenOutputStep,ConvertComplexToRealRepresentationOutputStep)r   rB  rC  rD  r  r  r   modules           r,   common_pre_export_passesr    s+    9 33
V###22#00<   
c>F
V!!#22#00<	 " 
 
c>F HV''(:FCGGXF ''(:FCGGIF''FG$C$Cgell  !.%((//266

#% 	 16:>>@F ##//
0N0N0PQ ##//
0S0S0UV
 ##//>>@ $$001M1M1OP $$00??A Mr+   )rw   rs   r   r   r   r   r  r.   r}  r  r   )r  r   )r   r{  r  zOptional[ExportOptions]ri   r   rJ  )[
__future__r   rK  
contextlibdataclassesr  loggingr  r5   collectionsr   typingr   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   typing_extensionsr   r   
torch._opstorch.exportre  r   torch.utils._pytreeutils_pytreerq  r   r   torch.onnx._internalr   r    torch.onnx._internal.diagnosticsr   r   r   r   r   r   r   r   r   r  r3   rL   r   r  r    r)   r  r  r   	getLoggerr%   r.  rw   	dataclassr"   r.   rs   r   contextmanagerr   r   r   r   r   r   ABCr   rP  r  r}  r  r   rq   r  r  r  __all__r*   r+   r,   <module>r     sy      	  	  #    $ #   # $ $ ) 6 2   4 	8
 &( 
 '9 I  0(D % E1 2 g! ++  K K Kg
 g
T0L 0LfjLM jLZ F FR ,H , ,^H H > E  EFG
 G
T-sww -`q qh) )) ),	 	 &* &*R 
 /3	hIh ,	h h hVJ"J4J $J "	JZe-  s   %G G$#G$