
    sg4X                       d dl mZ d dlmZmZmZmZmZmZm	Z	 d dl
Z
d dlmZ d dlmZ e	rd dlZe G d de             Z G d d      Ze G d	 d
e             Z G d d      Zd(dZd(dZ	 	 	 	 	 	 	 	 d)dZ G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Z  G d  d!e      Z! G d" d#e      Z" G d$ d%e      Z# G d& d'e      Z$y)*    )annotations)AnyCallableMappingProtocolruntime_checkableSequenceTYPE_CHECKINGN)_pytreec                  *    e Zd ZdZ	 d	 	 	 	 	 	 	 ddZy)InputAdaptStepa  A protocol that defines a step in the input adapting process.

    The input adapting process is a sequence of steps that are applied to the
    PyTorch model inputs to transform them into the inputs format expected by the
    exported ONNX model. Each step takes the PyTorch model inputs as arguments and
    returns the transformed inputs.

    This serves as a base formalized construct for the transformation done to model
    input signature by any individual component in the exporter.
    Nc                     y N self
model_argsmodel_kwargsmodels       R/var/www/html/venv/lib/python3.12/site-packages/torch/onnx/_internal/io_adapter.pyapplyzInputAdaptStep.apply&   s    
 36    r   r   Sequence[Any]r   zMapping[str, Any]r   @torch.nn.Module | Callable | torch_export.ExportedProgram | Nonereturnz'tuple[Sequence[Any], Mapping[str, Any]]__name__
__module____qualname____doc__r   r   r   r   r   r      s:    	 SW	6!6 (6 P	6
 
16r   r   c                  6    e Zd ZdZdddZd	dZdd	 	 	 d
dZy)InputAdapterzRA class that adapts the PyTorch model inputs to exported ONNX model inputs format.Nc                    |xs g | _         y r   _stepsr   stepss     r   __init__zInputAdapter.__init__1       krr   c                :    | j                   j                  |       y)zfAppends a step to the input adapt steps.

        Args:
            step: The step to append.
        Nr&   appendr   steps     r   append_stepzInputAdapter.append_step4        	4 r   r   c               f    |}|}| j                   D ]  }|j                  |||      \  }} |rJ |S )aK  Converts the PyTorch model inputs to exported ONNX model inputs format.

        Args:
            model_args: The PyTorch model inputs.
            model: The PyTorch model.
            model_kwargs: The PyTorch model keyword inputs.
        Returns:
            A sequence of tensors converted from PyTorch model inputs.
        r2   r&   r   )r   r   r   r   argskwargsr/   s          r   r   zInputAdapter.apply<   sH     )$0KK 	AD::dF%:@LD&	Azr   r   )r(   zlist[InputAdaptStep] | None)r/   r   r   None)r   r   r   zFSequence[int | float | bool | str | torch.Tensor | torch.dtype | None]r   r   r    r!   r)   r0   r   r   r   r   r#   r#   .   s1    \"! SW P
 
Pr   r#   c                  &    e Zd ZdZ	 d	 	 	 	 	 ddZy)OutputAdaptStepa  A protocol that defines a step in the output adapting process.

    The output adapting process is a sequence of steps that are applied to the
    PyTorch model outputs to transform them into the outputs format produced by the
    exported ONNX model. Each step takes the PyTorch model outputs as arguments and
    returns the transformed outputs.

    This serves as a base formalized construct for the transformation done to model
    output signature by any individual component in the exporter.
    Nc                     y r   r   r   model_outputsr   s      r   r   zOutputAdaptStep.apply`   s     r   r   r=   r   r   r   r   r   r   r   r   r   r:   r:   S   s0    	 SW P 
	r   r:   c                  8    e Zd ZdZdddZddZ	 d	 	 	 	 	 d	dZy)
OutputAdapterzTA class that adapts the PyTorch model outputs to exported ONNX model outputs format.Nc                    |xs g | _         y r   r%   r'   s     r   r)   zOutputAdapter.__init__j   r*   r   c                :    | j                   j                  |       y)zhAppends a step to the output format steps.

        Args:
            step: The step to append.
        Nr,   r.   s     r   r0   zOutputAdapter.append_stepm   r1   r   c                N    | j                   D ]  }|j                  ||      } |S )a  Converts the PyTorch model outputs to exported ONNX model outputs format.

        Args:
            model_outputs: The PyTorch model outputs.
            model: The PyTorch model.

        Returns:
            PyTorch model outputs in exported ONNX model outputs format.
        r2   r4   )r   r=   r   r/   s       r   r   zOutputAdapter.applyu   s1     KK 	CD JJ}EJBM	Cr   r   )r(   zlist[OutputAdaptStep] | None)r/   r:   r   r7   )r=   r   r   r   r   z1Sequence[torch.Tensor | int | float | bool | str]r8   r   r   r   r@   r@   g   s8    ^"! SW P 
;	r   r@   c           
         | j                   t        k(  rt        n| j                   }t        j                  || j
                  t        t        t        | j                                    S r   )	typetuplelistpytreeTreeSpeccontextmap_replace_tuple_with_listchildren_specs)spec_types     r   rL   rL      sG    II&DDIIE??t||T#&>@S@S"TU r   c                h    | j                   t        k(  r| j                  dk(  r| j                  d   S | S )N   r   )rE   rG   num_childrenrM   )rN   s    r   &_open_top_level_list_if_single_elementrS      s1    yyDT..!3""1%%Kr   c                      fd fd fd fdg}t        d |D              st        | d  d d      y	)
aZ  Assert the two `TreeSpec` objects are identical.

    Args:
        spec1: The first `TreeSpec` object.
        spec2: The second `TreeSpec` object.
        error_message: The error message to raise if the two `TreeSpec` objects are not
            identical.

    Raises:
        ValueError: If the two `TreeSpec` objects are not identical.
    c                      k(  S r   r   spec1spec2s   r   <lambda>z/_assert_identical_pytree_spec.<locals>.<lambda>   s     r   c                 2    t               t              k(  S r   )rL   rV   s   r   rY   z/_assert_identical_pytree_spec.<locals>.<lambda>   s    (/3KE3RR r   c                      t               k(  S r   rS   rV   s   r   rY   z/_assert_identical_pytree_spec.<locals>.<lambda>   s    6u=F r   c                       t              k(  S r   r\   rV   s   r   rY   z/_assert_identical_pytree_spec.<locals>.<lambda>   s    ?FF r   c              3  *   K   | ]  } |         y wr   r   ).0checks     r   	<genexpr>z0_assert_identical_pytree_spec.<locals>.<genexpr>   s     75uw7s   z
Expect z	.
Actual .N)any
ValueError)rW   rX   error_messagepass_if_any_checkss   ``  r   _assert_identical_pytree_specrg      sM      	RFF8 7$677M?)E7*UG1MNN 8r   c                  2    e Zd ZdZddZ	 d	 	 	 	 	 	 	 ddZy)BindInputStepz0Bind the input arguments to the model signature.c                    || _         y r   )_model_signature)r   model_signatures     r   r)   zBindInputStep.__init__   s
     /r   Nc                     | j                   j                  |i |}|j                          |j                  rt	        d      d|j
                  fS )a&  Bind the input arguments to the model signature.

        We hope the input kwargs will be mapped to bound.args after binding.
        If not, we will raise an error.

        Args:
            model_args: The model args.
            model_kwargs: The model kwargs.
            model: The PyTorch model.

        Returns:
            A tuple of the model args and kwargs. args is always empty.

        Raises:
            ValueError: If there are keyword-only arguments left after binding args and
                kwargs to model signature.
        z)Keyword-only arguments are not supported.r   )rk   bindapply_defaultsr6   rd   	arguments)r   r   r   r   bounds        r   r   zBindInputStep.apply   sQ    . +%%**JG,G
 <<HII5??""r   )rl   zinspect.Signaturer   r   r   r   r    r!   r)   r   r   r   r   ri   ri      s=    :0 SW	#!# (# P	#
 
1#r   ri   c                  *    e Zd ZdZ	 d	 	 	 	 	 	 	 ddZy)MergeKwargsIntoArgsInputStepz+Merge the input kwargs into the input args.Nc                P    t        |      t        |j                               z   i fS )a  Merge the input kwargs into the input args.

        Args:
            model_args: The model args.
            model_kwargs: The model kwargs.
            model: The PyTorch model.

        Returns:
            A tuple of the model args and kwargs. kwargs is always empty.
        )rF   valuesr   s       r   r   z"MergeKwargsIntoArgsInputStep.apply   s'      Z 5)<)<)>#??CCr   r   r   r   r   r   r   rt   rt      s=    5 SW	D!D (D P	D
 
1Dr   rt   c                  2    e Zd ZdZddZ	 d	 	 	 	 	 	 	 ddZy))LiftParametersAndBuffersIntoArgsInputStepzBAppend parameters and buffers to model's positional argument list.c                    || _         y r   inputs)r   r{   s     r   r)   z2LiftParametersAndBuffersIntoArgsInputStep.__init__   s	    r   Nc                (    g || j                   |fS )a  Append model's parameters and buffers into its input.

        Args:
            model_args: The model args.
            model_kwargs: The model kwargs.
            model: The PyTorch model.

        Returns:
            A tuple of the model args + appended inputs and kwargs.
        rz   r   s       r   r   z/LiftParametersAndBuffersIntoArgsInputStep.apply   s      +*dkk*L88r   )r{   ztuple[torch.Tensor, ...]r   r7   r   r   rr   r   r   r   rx   rx      s=    L SW	9!9 (9 P	9
 
19r   rx   c                  *    e Zd ZdZ	 d	 	 	 	 	 	 	 ddZy)+ConvertComplexToRealRepresentationInputStep8  Convert complex dtype tensors to real representation tensors.

    ONNX does not support complex dtype tensors. Thus, we convert complex dtype tensors
    to real representation tensors (i.e., float dtype tensors with an extra dimension
    representing the real and imaginary parts of the complex number).

    Nc                *    t        d |D              |fS )Convert complex tensors to float tensors.

        Args:
            model_args: The model args.
            model_kwargs: The model kwargs.
            model: The PyTorch model.

        Returns:
            A tuple of the model args and kwargs.
        c              3     K   | ]S  }t        |t        j                        r3|j                         r#t        j                  |j                               n| U y wr   
isinstancetorchTensor
is_complexview_as_realresolve_conjr_   args     r   ra   zDConvertComplexToRealRepresentationInputStep.apply.<locals>.<genexpr>%  sO        c5<<0S^^5E ""3#3#3#56s   AArF   r   s       r   r   z1ConvertComplexToRealRepresentationInputStep.apply  s*    "   &	  
 	
r   r   r   r   r   r   r   r~   r~     s:     SW	
!
 (
 P	

 
1
r   r~   c                  *    e Zd ZdZ	 d	 	 	 	 	 	 	 ddZy)RemoveNoneInputStepzRemove `None` from arguments.

    This adapt step assumes ``model_kwargs`` is empty. It also assumes ``model_args``
    is flattened, i.e. it does not check `None` inside nested collections.
    Nc                2    |rJ t        d |D              i fS )a<  Remove `None` from arguments.

        Args:
            model_args: The model args.
            model_kwargs: The model kwargs.
            model: The PyTorch model.

        Returns:
            A tuple of the model args and kwargs.

        Raises:
            ValueError: If `model_kwargs` is not empty.
        c              3  &   K   | ]	  }||  y wr   r   r   s     r   ra   z,RemoveNoneInputStep.apply.<locals>.<genexpr>J  s     BS#/SBs   r   r   s       r   r   zRemoveNoneInputStep.apply6  s#    &  BJBBBFFr   r   r   r   r   r   r   r   r   /  s?     SW	G!G (G P	G
 
1Gr   r   c                  *    e Zd ZdZ	 d	 	 	 	 	 	 	 ddZy)RemoveNonTensorInputStepaN  Remove the non-tensor input arguments.

    Dynamo does not support non-tensor input arguments (https://github.com/pytorch/pytorch/issues/99534).

    Specifically, it does put the input into graph with an empty node, but consumed by no ones.
    The concrete value is embedded into the graph as a constant arg of a target node. Meta
    suggests in this case that one should rewrite the model code to make it tensor if the
    input value is supposed to change at runtime. We might need to further investigate
    the feasibility of that suggestion.

    For example,

        def func(x, b=1.0):
            y = x + b
            z = y.relu()
            return (y, z)

        x = torch.randn(1, 1, 2, dtype=torch.float32)
        gm_fun, _ = dynamo.export(func, x, b=8.0, aten_graph=True, tracing_mode="real")

        # class GraphModule(torch.nn.Module):
        #     def forward(self, x, b):
        #         arg0: f32[1, 1, 2], arg1, = fx_pytree.tree_flatten_spec(([x, b], {}), self._in_spec)
        #         # File: path/to/pytorch/test_constant_input.py:5, code: y = x + b
        #         add_tensor: f32[1, 1, 2] = torch.ops.aten.add.Tensor(arg0, 8.0);  arg0 = None

        #         # File: path/to/pytorch/test_constant_input.py:6, code: z = y.relu()
        #         relu_default: f32[1, 1, 2] = torch.ops.aten.relu.default(add_tensor)
        #         return pytree.tree_unflatten([add_tensor, relu_default], self._out_spec)

    Empty torch.fx.Node input leading to a mismatched number of input with PyTorch, as
    it's ignored in ONNX graph. Thus, we delete the useless input here.

    Nc                2    |rJ t        d |D              i fS )a>  Remove Constant from arguments.

        Args:
            model_args: The model args.
            model_kwargs: The model kwargs.
            model: The PyTorch model.

        Returns:
            A tuple of the model args and kwargs.

        Raises:
            ValueError: If `model_kwargs` is not empty.
        c              3  `   K   | ]&  }t        |t        t        t        t        f      s| ( y wr   )r   intfloatboolstrr   s     r   ra   z1RemoveNonTensorInputStep.apply.<locals>.<genexpr>  s+      !#UD#'>? s   ,.r   r   s       r   r   zRemoveNonTensorInputStep.applyq  s1    &   % 
 
 	
r   r   r   r   r   r   r   r   r   M  s;    !N SW	
!
 (
 P	

 
1
r   r   c                  :    e Zd ZU dZdZded<   	 d	 	 	 	 	 	 	 ddZy)+FlattenInputWithTreeSpecValidationInputStepaW  Flatten nested collection types and return a flat list of elements.

    ONNX can't represent collection types (e.g., dictionary, tuple of tuple of tensor,
    etc).

    This class stores the `SpecTree` output produced when `adapt` was called the first
    time. It then validates the `SpecTree` output produced from later `adapt` calls.
    Npytree.TreeSpec | None_specc                    t        j                  ||f      \  }}| j                  || _        |i fS t        | j                  |d       |i fS )ay  Flatten the model args and kwargs and validate the `SpecTree` output.

        Args:
            model_args: The model args.
            model_kwargs: The model kwargs.
            model: The PyTorch model.

        Returns:
            A tuple of the flattened model args and kwargs. The kwargs is empty, because
            they are flattened and merged into the args.

        Raises:
            ValueError: If the `SpecTree` output produced from the current `model_outputs`
                is not identical to the `SpecTree` output produced from the first
                `model_outputs` that was passed to this method.
        z=Model inputs incompatible with the format that was exported. re   rH   tree_flattenr   rg   )r   r   r   r   flattened_argsrN   s         r   r   z1FlattenInputWithTreeSpecValidationInputStep.apply  s`    ,  &22J3MN::DJ r!! *

]
 r!!r   r   r   r   r   r    r!   r   __annotations__r   r   r   r   r   r     sG     %)E!( SW	"!" (" P	"
 
1"r   r   c                  &    e Zd ZdZ	 d	 	 	 	 	 ddZy)FlattenOutputStepa  Flatten nested collection types and return a flat list of elements.

    ONNX can't represent collection types (e.g., dictionary, tuple of tuple of tensor,
    etc).

    NOTE: Ideally we would want to use ``FlattenOutputWithTreeSpecValidationOutputStep``, such
    that `SpecTree` can be validate for new model outputs. However, this is not possible
    currently because we never have access to real PyTorch model outputs during export.
    Only traced outputs may be available, but they are not an accurate reflection of the
    original PyTorch model outputs format as they are typically in their own unique format,
    depending on the tracing strategy.
    Nc                ,    t        j                  |      S )zFlatten the model outputs.

        Args:
            model_outputs: The model outputs to flatten.
            model: The PyTorch model.

        Returns:
            A tuple of the flattened model outputs.
        )rH   tree_leavesr<   s      r   r   zFlattenOutputStep.apply  s     !!-00r   r   r=   r   r   r   r   r   r   r   r   r   r   r     s0      SW11 P1 
	1r   r   c                  &    e Zd ZdZ	 d	 	 	 	 	 ddZy),ConvertComplexToRealRepresentationOutputStepr   Nc                    |D cg c]V  }t        |t        j                        r8t        j                  |      r#t        j                  |j                               n|X c}S c c}w )zConvert float tensors to complex tensors.

        Args:
            model_output: The model output.
            model: The PyTorch model.

        Returns:
            A tuple of the model output.
        r   )r   r=   r   outputs       r   r   z2ConvertComplexToRealRepresentationOutputStep.apply  s_    $ (	
  &%,,/E4D4DV4L v2245
 	
 
s   AA#r   r>   r   r   r   r   r   r     s0     SW

 P
 
	
r   r   c                  6    e Zd ZU dZdZded<   	 d	 	 	 	 	 ddZy)-FlattenOutputWithTreeSpecValidationOutputStepzSame as ``FlattenOutputStep``, with additional `TreeSpec` validation.

    This class stores the `SpecTree` output produced when `adapt` was called the first
    time. It then validates the `SpecTree` output produced from later `adapt` calls.
    Nr   r   c                    t        j                  |      \  }}| j                  	|| _        |S t        | j                  |d       |S )a   Flatten the model outputs and validate the `SpecTree` output.

        Args:
            model_outputs: The model outputs to flatten.
            model: The PyTorch model.

        Returns:
            flattened_outputs: The flattened model outputs.

        Raises:
            ValueError: If the `SpecTree` output produced from the current `model_outputs`
                is not identical to the `SpecTree` output produced from the first
                `model_outputs` that was passed to this method.
        z>Model outputs incompatible with the format that was exported. r   r   )r   r=   r   flattened_outputsrN   s        r   r   z3FlattenOutputWithTreeSpecValidationOutputStep.apply  sR    & #)"5"5m"D4::DJ !  *

^
 ! r   r   r   r   r   r   r   r   r     s=     %)E!(
 SW!! P! 
	!r   r   c                  *    e Zd ZdZ	 d	 	 	 	 	 	 	 ddZy)0PrependParamsBuffersConstantAotAutogradInputStepaE  Prepend model parameters, buffers and constants to the user input.

    :func:`torch.export.export` lifts model parameters, buffers and constants as model input, thus, they
    must be added to the user input before the model is executed.

    Args:
        model: The PyTorch model with embedded parameters and buffers.
    Nc                   t        fdj                  j                  D              }t        j                  j                        }g }j                  j
                  D ]C  }||v r|j                  j                  |          &|j                  j                  |          E t        fdj                  j                  D              }g ||||}	|rt               j                  |	|      S |	i fS )r   c              3  <   K   | ]  }j                   |     y wr   )
state_dictr_   namer   s     r   ra   zIPrependParamsBuffersConstantAotAutogradInputStep.apply.<locals>.<genexpr>=  s$      
 T"
   c              3  <   K   | ]  }j                   |     y wr   )	constants)r_   fqnr   s     r   ra   zIPrependParamsBuffersConstantAotAutogradInputStep.apply.<locals>.<genexpr>H  s"      )
 OOC )
r   r2   )rF   graph_signature
parameterssetnon_persistent_buffersbuffersr-   r   r   lifted_tensor_constantsrt   r   )
r   r   r   r   ordered_paramsr   ordered_buffersr   ordered_constant_tensorsupdated_argss
      `      r   r   z6PrependParamsBuffersConstantAotAutogradInputStep.apply-  s      
--88
 
 "%U%:%:%Q%Q!R))11 	?D--&&ut'<=&&u'7'7'=>		?
 $) )
,,DD)
 $
 


 &
 	
 /177l% 8   Rr   r   r   r   r   r   r   r   r   #  s:     SW	, !,  (,  P	, 
 
1, r   r   c                  &    e Zd ZdZ	 d	 	 	 	 	 ddZy),PrependParamsAndBuffersAotAutogradOutputStepa  Prepend model's mutated buffers to the user output.

    :func:`torch.export.export` lifts model's mutated buffers as outputs, thus, they
    must be added to the user output after the model is executed.

    Args:
        model: The PyTorch model with mutated buffers.
    Nc                    t        t        j                        sJ d       t        fdj                  j
                  j                         D              }g ||}|S )a  Flatten the model outputs and validate the `SpecTree` output.

        Args:
            model_outputs: The model outputs to flatten.
            model: The PyTorch model.

        Returns:
            flattened_outputs: The flattened model outputs.
        z,'model' must be torch_export.ExportedProgramc              3  v   K   | ]0  }|j                   v rj                   |   nj                  |    2 y wr   )r   r   r   s     r   ra   zEPrependParamsAndBuffersAotAutogradOutputStep.apply.<locals>.<genexpr>x  sG       
  u''' T"&' 
s   69)r   torch_exportExportedProgramrF   r   buffers_to_mutaterv   )r   r=   r   r   updated_outputss     `  r   r   z2PrependParamsAndBuffersAotAutogradOutputStep.applyf  sq     <//
 	:9	: 
    
 --??FFH	 
 
 =O<m<r   r   r   r   r   r   r   r   r   \  s0     SW P 
	r   r   )rN   pytree.TreeSpecr   r   )rW   r   rX   r   re   r   r   r7   )%
__future__r   typingr   r   r   r   r   r	   r
   r   torch.exportexportr   torch.utilsr   rH   inspectr   r#   r:   r@   rL   rS   rg   ri   rt   rx   r~   r   r   r   r   r   r   r   r   r   r   r   <module>r      sD   "    # ) 
 6X 6 6(" "J h  & HOO#2OCFO	O8%#N %#PD> D,9 92!
. !
HG. G<?
~ ?
D+". +"\1 1>
? 
>%!O %!P6 ~ 6 r%? %r   