
    :id                     N   d dl Z d dlmZmZ d dlmZmZ d dlmZm	Z	 d dl
mZ d dlmZmZ d dlmZ er
d dlZd dlmZ g d	Ze j         G d
 d                      Ze j         G d d                      Ze j         G d d                      Ze j         G d d                      Ze j         G d d                      Ze j         G d d                      Ze j         G d d                      Ze	eeeeeeef         Z G d de          Ze j         G d d                      Z G d de          Z e j         G d d                      Z!e j         G d  d!                      Z"e j         G d" d#                      Z#d$ Z$d%efd&Z%d'd(d)d*d+e&e'         d%d#fd,Z(dS )-    N)
CollectionMapping)autoEnum)TYPE_CHECKINGUnionFakeScriptObject)get_opaque_type_nameis_opaque_type)is_fake)GraphSignature)ConstantArgumentCustomObjArgumentExportBackwardSignatureExportGraphSignature	InputKind	InputSpec
OutputKind
OutputSpecSymIntArgumentSymFloatArgumentSymBoolArgumentTensorArgumentc                       e Zd ZU eed<   dS )r   nameN__name__
__module____qualname__str__annotations__     O/var/www/icac/venv/lib/python3.11/site-packages/torch/export/graph_signature.pyr   r                
IIIIIr$   r   c                       e Zd ZU eed<   dS )TokenArgumentr   Nr   r#   r$   r%   r(   r(   %   r&   r$   r(   c                       e Zd ZU eed<   dS )r   r   Nr   r#   r$   r%   r   r   *   r&   r$   r   c                       e Zd ZU eed<   dS )r   r   Nr   r#   r$   r%   r   r   /   r&   r$   r   c                       e Zd ZU eed<   dS )r   r   Nr   r#   r$   r%   r   r   4   r&   r$   r   c                   8    e Zd ZU eed<   eed<   dZedz  ed<   dS )r   r   	class_fqnNfake_val)r   r   r    r!   r"   r.   r
   r#   r$   r%   r   r   9   s:         
IIINNN(,H%,,,,,r$   r   c                   <    e Zd ZU eed<   eez  ez  ez  dz  ed<   dS )r   r   Nvalue)r   r   r    r!   r"   intfloatboolr#   r$   r%   r   r   @   s:         
III;#d******r$   r   c                       e Zd Z e            Z e            Z e            Z e            Z e            Z e            Z	dS )r   N)
r   r   r    r   
USER_INPUT	PARAMETERBUFFERCONSTANT_TENSOR
CUSTOM_OBJTOKENr#   r$   r%   r   r   Q   sP        JITVVFdffOJDFFEEEr$   r   c                   T    e Zd ZU eed<   eed<   edz  ed<   dZedz  ed<   d Z	d Z
dS )r   kindargNtarget
persistentc           
         | j         t          j        k    r| j        
J d            t	          | j        t          t          t          t          t          t          t          f          sJ dt          | j                               d S )Nz,Failed to specify persistent flag on BUFFER.zgot )r<   r   r7   r?   
isinstancer=   r   r   r   r   r   r   r(   typeselfs    r%   __post_init__zInputSpec.__post_init__a   s    9	(((?..> /.. H  !
 
 	# 	# #$tx..""	# 	# 	# 	# 	#r$   c                     | j         dn
d| j          d}| j        dn	d| j         }t          | j        j                   dt          | j        j                   | | S )N 	 target=''z persistent=: )r>   r?   r!   r=   r   r<   )rD   r>   r?   s      r%   __str__zInputSpec.__str__s   sq    {*0JDK0J0J0J?2RR8Xt8X8X
dhm$$QQDIN(;(;QVQZQQQr$   )r   r   r    r   r"   ArgumentSpecr!   r?   r3   rE   rK   r#   r$   r%   r   r   Z   sr         
OOO	$J"Jt"""# # #$R R R R Rr$   r   c                       e Zd Z e            Z e            Z e            Z e            Z e            Z e            Z	 e            Z
 e            ZdS )r   N)r   r   r    r   USER_OUTPUTLOSS_OUTPUTBUFFER_MUTATIONPARAMETER_MUTATIONGRADIENT_TO_PARAMETERGRADIENT_TO_USER_INPUTUSER_INPUT_MUTATIONr:   r#   r$   r%   r   r   y   sj        $&&K$&&KdffO DFF!TVV$&&DFFEEEr$   r   c                   @    e Zd ZU eed<   eed<   edz  ed<   d Zd ZdS )r   r<   r=   Nr>   c           
          t          | j        t          t          t          t
          t          t          t          f          sJ | j                    d S N)	rA   r=   r   r   r   r   r   r(   r   rC   s    r%   rE   zOutputSpec.__post_init__   sX    H  !
 
 	 	 8	 	 	 	 	r$   c                     | j         dn
d| j          d}t          | j        j                   dt          | j        j                   | S )NrG   rH   rI   rJ   )r>   r!   r=   r   r<   )rD   r>   s     r%   rK   zOutputSpec.__str__   sQ    {*0JDK0J0J0Jdhm$$EEDIN(;(;EVEEEr$   )	r   r   r    r   r"   rL   r!   rE   rK   r#   r$   r%   r   r      sa         
	$J  F F F F Fr$   r   c                   N    e Zd ZU eeef         ed<   eeef         ed<   eed<   dS )r   gradients_to_parametersgradients_to_user_inputsloss_outputN)r   r   r    dictr!   r"   r#   r$   r%   r   r      sF         !#s(^+++"38n,,,r$   r   c                      e Zd ZU dZee         ed<   ee         ed<   ede	e
         fd            Zede	e
         fd            Zede	e
         fd            Zede	e
         fd            Zede	e
         fd	            Zede	eez  ez  d
z  e
z           fd            Zede	eez  ez  d
z  e
z           fd            Zedee
e
f         fd            Zedee
e
f         fd            Zedee
e
f         fd            Zedee
e
f         fd            Zedee
e
f         fd            Zedee
e
f         fd            Zedee
e
f         fd            Zeded
z  fd            Zedeee
f         d
z  fd            Zede	e
         fd            Z ede	e
         fd            Z!ddZ"de
de
fdZ#d dZ$d Z%d
S )!r   a.  
    :class:`ExportGraphSignature` models the input/output signature of Export Graph,
    which is a fx.Graph with stronger invariants guarantees.

    Export Graph is functional and does not access "states" like parameters
    or buffers within the graph via ``getattr`` nodes. Instead, :func:`export`
    guarantees that parameters, buffers, and constant tensors are lifted out of
    the graph as inputs.  Similarly, any mutations to buffers are not included
    in the graph either, instead the updated values of mutated buffers are
    modeled as additional outputs of Export Graph.

    The ordering of all inputs and outputs are::

        Inputs = [*parameters_buffers_constant_tensors, *flattened_user_inputs]
        Outputs = [*mutated_inputs, *flattened_user_outputs]

    e.g. If following module is exported::

        class CustomModule(nn.Module):
            def __init__(self) -> None:
                super(CustomModule, self).__init__()

                # Define a parameter
                self.my_parameter = nn.Parameter(torch.tensor(2.0))

                # Define two buffers
                self.register_buffer("my_buffer1", torch.tensor(3.0))
                self.register_buffer("my_buffer2", torch.tensor(4.0))

            def forward(self, x1, x2):
                # Use the parameter, buffers, and both inputs in the forward method
                output = (
                    x1 + self.my_parameter
                ) * self.my_buffer1 + x2 * self.my_buffer2

                # Mutate one of the buffers (e.g., increment it by 1)
                self.my_buffer2.add_(1.0)  # In-place addition

                return output


        mod = CustomModule()
        ep = torch.export.export(mod, (torch.tensor(1.0), torch.tensor(2.0)))

    Resulting Graph is non-functional::

        graph():
            %p_my_parameter : [num_users=1] = placeholder[target=p_my_parameter]
            %b_my_buffer1 : [num_users=1] = placeholder[target=b_my_buffer1]
            %b_my_buffer2 : [num_users=2] = placeholder[target=b_my_buffer2]
            %x1 : [num_users=1] = placeholder[target=x1]
            %x2 : [num_users=1] = placeholder[target=x2]
            %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%x1, %p_my_parameter), kwargs = {})
            %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%add, %b_my_buffer1), kwargs = {})
            %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%x2, %b_my_buffer2), kwargs = {})
            %add_1 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mul, %mul_1), kwargs = {})
            %add_ : [num_users=0] = call_function[target=torch.ops.aten.add_.Tensor](args = (%b_my_buffer2, 1.0), kwargs = {})
            return (add_1,)

    Resulting ExportGraphSignature of the non-functional Graph would be::

        # inputs
        p_my_parameter: PARAMETER target='my_parameter'
        b_my_buffer1: BUFFER target='my_buffer1' persistent=True
        b_my_buffer2: BUFFER target='my_buffer2' persistent=True
        x1: USER_INPUT
        x2: USER_INPUT

        # outputs
        add_1: USER_OUTPUT

    To get a functional Graph, you can use :func:`run_decompositions`::

        mod = CustomModule()
        ep = torch.export.export(mod, (torch.tensor(1.0), torch.tensor(2.0)))
        ep = ep.run_decompositions()

    Resulting Graph is functional::

        graph():
            %p_my_parameter : [num_users=1] = placeholder[target=p_my_parameter]
            %b_my_buffer1 : [num_users=1] = placeholder[target=b_my_buffer1]
            %b_my_buffer2 : [num_users=2] = placeholder[target=b_my_buffer2]
            %x1 : [num_users=1] = placeholder[target=x1]
            %x2 : [num_users=1] = placeholder[target=x2]
            %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%x1, %p_my_parameter), kwargs = {})
            %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%add, %b_my_buffer1), kwargs = {})
            %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%x2, %b_my_buffer2), kwargs = {})
            %add_1 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mul, %mul_1), kwargs = {})
            %add_2 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%b_my_buffer2, 1.0), kwargs = {})
            return (add_2, add_1)

    Resulting ExportGraphSignature of the functional Graph would be::

        # inputs
        p_my_parameter: PARAMETER target='my_parameter'
        b_my_buffer1: BUFFER target='my_buffer1' persistent=True
        b_my_buffer2: BUFFER target='my_buffer2' persistent=True
        x1: USER_INPUT
        x2: USER_INPUT

        # outputs
        add_2: BUFFER_MUTATION target='my_buffer2'
        add_1: USER_OUTPUT

    input_specsoutput_specsreturnc                 >    t          d | j        D                       S )Nc              3      K   | ]:}|j         t          j        k    t          |j        t
                    1|j        V  ;d S rW   )r<   r   r6   rA   r>   r!   .0ss     r%   	<genexpr>z2ExportGraphSignature.parameters.<locals>.<genexpr>  sT       
 
v,,,!(C(( - H,,,,
 
r$   tupler_   rC   s    r%   
parameterszExportGraphSignature.parameters  3     
 
%
 
 
 
 
 	
r$   c                 >    t          d | j        D                       S )Nc              3      K   | ]:}|j         t          j        k    t          |j        t
                    1|j        V  ;d S rW   )r<   r   r7   rA   r>   r!   rd   s     r%   rg   z/ExportGraphSignature.buffers.<locals>.<genexpr>!  sT       
 
v)))!(C(( * H))))
 
r$   rh   rC   s    r%   bufferszExportGraphSignature.buffers  rk   r$   c                 >    t          d | j        D                       S )Nc              3      K   | ]C}|j         t          j        k    r,|j        d u  t	          |j        t                    :|j        V  DdS )FN)r<   r   r7   r?   rA   r>   r!   rd   s     r%   rg   z>ExportGraphSignature.non_persistent_buffers.<locals>.<genexpr>*  sc       
 
v)))|u$$!(C(( % H %$$$	
 
r$   rh   rC   s    r%   non_persistent_buffersz+ExportGraphSignature.non_persistent_buffers(  s3     
 
%
 
 
 
 
 	
r$   c                 >    t          d | j        D                       S )Nc              3      K   | ]:}|j         t          j        k    t          |j        t
                    1|j        V  ;d S rW   )r<   r   r8   rA   r>   r!   rd   s     r%   rg   z?ExportGraphSignature.lifted_tensor_constants.<locals>.<genexpr>5  sT       
 
v222!(C(( 3 H2222
 
r$   rh   rC   s    r%   lifted_tensor_constantsz,ExportGraphSignature.lifted_tensor_constants3  rk   r$   c                 >    t          d | j        D                       S )Nc              3      K   | ]:}|j         t          j        k    t          |j        t
                    1|j        V  ;d S rW   )r<   r   r9   rA   r>   r!   rd   s     r%   rg   z:ExportGraphSignature.lifted_custom_objs.<locals>.<genexpr>>  sT       
 
v---!(C(( . H----
 
r$   rh   rC   s    r%   lifted_custom_objsz'ExportGraphSignature.lifted_custom_objs<  rk   r$   Nc           	         g }| j         D ]}|j        t          j        k    rt	          |j        t          t          t          t          t          f          r |                    |j        j                   kt	          |j        t                    r |                    |j        j                   t          |j         d          t!          |          S )Nz is not a valid user inputs)r_   r<   r   r5   rA   r=   r   r   r   r   r   appendr   r   r0   RuntimeErrorri   )rD   user_inputsrf   s      r%   r{   z ExportGraphSignature.user_inputsF  s    =?! 	J 	JAv---""$#%	 	 J ""15:....AE#344 J""15;////"ae#H#H#HIII[!!!r$   c                 &   g }| j         D ]}|j        t          j        t          j        fvr"t          |j        t          t          t          t          f          r |                    |j        j                   ot          |j        t                    r |                    |j        j                   t          |j        t                    r |                    |j        j                   t!          |j         d          t#          |          S )Nz is not a valid user output)r`   r<   r   rN   rO   rA   r=   r   r   r   r   ry   r   r   r0   r   rz   ri   )rD   user_outputsrf   s      r%   r}   z!ExportGraphSignature.user_outputs`  s   >@" 	J 	JAv&&   1A?S  
J ##AEJ////AE#344 J##AEK0000AE#455 J##AEJ////"ae#H#H#HIII\"""r$   c                 >    t          d | j        D                       S )Nc              3      K   | ]`}|j         t          j        k    t          |j        t
                    1t          |j        t                    K|j        j        |j        fV  ad S rW   )	r<   r   r6   rA   r=   r   r>   r!   r   rd   s     r%   rg   z<ExportGraphSignature.inputs_to_parameters.<locals>.<genexpr>{  sq       
 
v,,,15.11 -18S)) - UZ",,,,
 
r$   _immutable_dictr_   rC   s    r%   inputs_to_parametersz)ExportGraphSignature.inputs_to_parametersy  3     
 
%
 
 
 
 
 	
r$   c                 >    t          d | j        D                       S )Nc              3      K   | ]`}|j         t          j        k    t          |j        t
                    1t          |j        t                    K|j        j        |j        fV  ad S rW   )	r<   r   r7   rA   r=   r   r>   r!   r   rd   s     r%   rg   z9ExportGraphSignature.inputs_to_buffers.<locals>.<genexpr>  sq       
 
v)))15.11 *18S)) * UZ"))))
 
r$   r   rC   s    r%   inputs_to_buffersz&ExportGraphSignature.inputs_to_buffers  r   r$   c                 >    t          d | j        D                       S )Nc              3      K   | ]`}|j         t          j        k    t          |j        t
                    1t          |j        t                    K|j        j        |j        fV  ad S rW   )	r<   r   rP   rA   r=   r   r>   r!   r   rd   s     r%   rg   z9ExportGraphSignature.buffers_to_mutate.<locals>.<genexpr>  sq       
 
v33315.11 418S)) 4 UZ"3333
 
r$   r   r`   rC   s    r%   buffers_to_mutatez&ExportGraphSignature.buffers_to_mutate  3     
 
&
 
 
 
 
 	
r$   c                 >    t          d | j        D                       S )Nc              3      K   | ]`}|j         t          j        k    t          |j        t
                    1t          |j        t                    K|j        j        |j        fV  ad S rW   )	r<   r   rQ   rA   r=   r   r>   r!   r   rd   s     r%   rg   z<ExportGraphSignature.parameters_to_mutate.<locals>.<genexpr>  sq       
 
v66615.11 718S)) 7 UZ"6666
 
r$   r   rC   s    r%   parameters_to_mutatez)ExportGraphSignature.parameters_to_mutate  r   r$   c                 >    t          d | j        D                       S )Nc              3      K   | ]`}|j         t          j        k    t          |j        t
                    1t          |j        t                    K|j        j        |j        fV  ad S rW   )	r<   r   rT   rA   r=   r   r>   r!   r   rd   s     r%   rg   z=ExportGraphSignature.user_inputs_to_mutate.<locals>.<genexpr>  sq       
 
v77715.11 818S)) 8 UZ"7777
 
r$   r   rC   s    r%   user_inputs_to_mutatez*ExportGraphSignature.user_inputs_to_mutate  r   r$   c                 >    t          d | j        D                       S )Nc              3      K   | ]`}|j         t          j        k    t          |j        t
                    1t          |j        t                    K|j        j        |j        fV  ad S rW   )	r<   r   r8   rA   r=   r   r>   r!   r   rd   s     r%   rg   zIExportGraphSignature.inputs_to_lifted_tensor_constants.<locals>.<genexpr>  sq       
 
v22215.11 318S)) 3 UZ"2222
 
r$   r   rC   s    r%   !inputs_to_lifted_tensor_constantsz6ExportGraphSignature.inputs_to_lifted_tensor_constants  r   r$   c                 >    t          d | j        D                       S )Nc              3      K   | ]`}|j         t          j        k    t          |j        t
                    1t          |j        t                    K|j        j        |j        fV  ad S rW   )	r<   r   r9   rA   r=   r   r>   r!   r   rd   s     r%   rg   zDExportGraphSignature.inputs_to_lifted_custom_objs.<locals>.<genexpr>  sr       
 
v---15"344 .18S)) . UZ"----
 
r$   r   rC   s    r%   inputs_to_lifted_custom_objsz1ExportGraphSignature.inputs_to_lifted_custom_objs  r   r$   c                 \   d }i }i }| j         D ]}|j        t          j        k    r-|J t	          |j        t                    sJ |j        j        }E|j        t          j        k    rMt	          |j	        t                    sJ t	          |j        t                    sJ |j	        ||j        j        <   |j        t          j        k    rLt	          |j	        t                    sJ t	          |j        t                    sJ |j	        ||j        j        <   
|d S t          |||          S )N)r\   rZ   r[   )r`   r<   r   rO   rA   r=   r   r   rR   r>   r!   rS   r   )rD   r\   rZ   r[   specs        r%   backward_signaturez'ExportGraphSignature.backward_signature  s7   2435 % 	F 	FDyJ222"***!$(N;;;;;"hmj>>>!$+s33333!$(N;;;;;9='66j???!$+s33333!$(N;;;;;:>+(74&#$;%=
 
 
 	
r$   c                     d S rW   r#   rC   s    r%   assertion_dep_tokenz(ExportGraphSignature.assertion_dep_token  s    tr$   c                     g }| j         D ]R}|j        t          j        k    r;t	          |j        t                    sJ |                    |j        j                   St          |          S rW   )
r_   r<   r   r:   rA   r=   r(   ry   r   ri   )rD   input_tokensrf   s      r%   r   z!ExportGraphSignature.input_tokens  sg    ! 	0 	0Av((!!%77777##AEJ///\"""r$   c                     g }| j         D ]R}|j        t          j        k    r;t	          |j        t                    sJ |                    |j        j                   St          |          S rW   )
r`   r<   r   r:   rA   r=   r(   ry   r   ri   )rD   output_tokensrf   s      r%   r   z"ExportGraphSignature.output_tokens  sh    " 	1 	1Av)))!!%77777$$QUZ000]###r$   c                     | j         }|d S t          |          dk    sJ t          t          |                                                    }t          | j                  t          | j                  z   |k    sJ d S )N   )r   lennextiterkeysr}   r   )rD   r   assertion_dep_token_indexs      r%   rE   z"ExportGraphSignature.__post_init__  s    "6&F&''1,,,,$(.A.F.F.H.H)I)I$J$J!!""S)?%@%@@() ) ) ) ) )r$   oldnewc                    t          |t                    sJ t          |t                    sJ t          t          t          t
          t          t          f}| j        D ]3}t          |j	        |          r|j	        j
        |k    r||j	        _
        4| j        D ]3}t          |j	        |          r|j	        j
        |k    r||j	        _
        4dS )zR
        Replace all uses of the old name with new name in the signature.
        N)rA   r!   r   r   r   r   r   r(   r`   r=   r   r_   )rD   r   r   	arg_typesois         r%   replace_all_usesz%ExportGraphSignature.replace_all_uses  s     #s######s#####
	 " 	% 	%A!%++ %5:$$!$AEJ! 	% 	%A!%++ %5:$$!$AEJ	% 	%r$   Fc                       fd}|S )Nc                     |j         dk    r                    | j        |           r(| j         dk    r                    | j        |           d S d S d S )Noutputplaceholder)opr   r   )r   r   userreplace_inputsrD   s      r%   _z0ExportGraphSignature.get_replace_hook.<locals>._  sj    w(""%%ch444 5#&M"9"9%%ch444445 5"9"9r$   r#   )rD   r   r   s   `` r%   get_replace_hookz%ExportGraphSignature.get_replace_hook  s)    	5 	5 	5 	5 	5 	5 r$   c                     d                     d | j        D                       }d                     d | j        D                       }d| d| dS )N
c              3   4   K   | ]}t          |          V  d S rW   r!   rd   s     r%   rg   z/ExportGraphSignature.__str__.<locals>.<genexpr>%  s(      AA1AAAAAAAr$   c              3   4   K   | ]}t          |          V  d S rW   r   rd   s     r%   rg   z/ExportGraphSignature.__str__.<locals>.<genexpr>&  s(       C CAQ C C C C C Cr$   z

# inputs
z

# outputs
)joinr_   r`   )rD   r_   r`   s      r%   rK   zExportGraphSignature.__str__$  sa    iiAA0@AAAAAyy C C1B C C CCCJkJJ,JJJJr$   )ra   N)F)&r   r   r    __doc__listr   r"   r   propertyr   r!   rj   rn   rq   rt   rw   r1   r2   r3   r{   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   rE   r   r   rK   r#   r$   r%   r   r      s        i iV i   z"""" 
JsO 
 
 
 X
 
C 
 
 
 X
 

3 
 
 
 X
 
C 
 
 
 X
 
JsO 
 
 
 X
 "Zed(:T(AC(GH " " " X"2 #jut);d)BS)HI # # # X#0 
gc3h&7 
 
 
 X
 
738#4 
 
 
 X
 
738#4 
 
 
 X
 
gc3h&7 
 
 
 X
 
wsCx'8 
 
 
 X
 
7383D 
 
 
 X
 
gc3h.? 
 
 
 X
 
$;d$B 
 
 
 X
< WS#X%6%=    X #jo # # # X# $z# $ $ $ X$	
 	
 	
 	
%C %c % % % %.   K K K K Kr$   r   c                 >    ddl m}  |t          |                     S )z
    Creates a mapping where items cannot be added, deleted, or updated.
    NOTE: The immutability is shallow (like tuple is an immutable collection).
    r   )MappingProxyType)typesr   r]   )itemsr   s     r%   r   r   *  s-    
 '&&&&&DKK(((r$   ra   c           
         ddl m}m}m}m} ddlm} t          | t          t          t          t          d           t          f          rt          d|           S d| j        v sJ |  d            | j        d         }| j        |v rt!          | j                  S t#          |          rt%          | j                  S t          ||          rt'          | j                  S t          ||          rt)          | j                  S t          ||          rt+          | j                  S t          ||          r:t-          | j        |                                                                	          S t          ||          rt-          | j        |j        |
          S t5          t          |                    r1t-          | j        t7          t          |                    |
          S t          |t          t          t          t          t          d           f          rt          | j        |          S t9          dt          |           d          )Nr   )ScriptObjectSymBoolSymFloatSymIntr	   rG   )r   r0   valz8 is not a constant or a node with a 'val' metadata field)r   )r   r-   )r   r-   r.   z*Encountered an unsupported object of type z0 while writing the metadata for exported program)torchr   r   r   r   "torch._library.fake_class_registryr
   rA   r1   r3   r2   rB   r!   r   metar   r(   r   r   r   r   r   r   _typequalified_namescript_class_namer   r   AssertionError)nodetoken_namesr   r   r   r   r
   r   s           r%   _make_argument_specr   4  st   ============CCCCCC$dE4::s;<< 5Rt4444DIIII  )E
CyK$),,,,	 
49----	C	 	  
49----	C	"	" 
TY////	C	!	! 
DI....	C	&	& 
 di399;;;U;U;W;WXXXX	C)	*	* 
 c&;c
 
 
 	
 
S			"	" 

 &:499&E&EPS
 
 
 	
 
C#tS%d<	=	= 
TYc::::?c ? ? ?
 
 	
r$   graph_signaturer   gmztorch.fx.GraphModulerq   c           
        	
 ddl m} | j        d u}t          | j                  | j        | j        t          | j                  | j        	| j	        | j
        |r| j        j        ni 
|r| j        j        ni |r| j        j        nd | j        | j        fd|j        j        D             }fd|                    t'          t)          t+          |j        j                                      j                  D             }dt.          dt0          ffddt2          d	t.          dt4          f	
fd
fd|D             }fdt7          |          D             }t9          ||          S )Nr   )_pytreec                 F    g | ]}|j         d k    t          |          S )r   )r   r   )re   r   r   s     r%   
<listcomp>z6_convert_to_export_graph_signature.<locals>.<listcomp>  s:       7m## 	D,//###r$   c                 0    g | ]}t          |          S r#   )r   )re   r   r   s     r%   r   z6_convert_to_export_graph_signature.<locals>.<listcomp>  s3        	D-00  r$   inpra   c                    t          | t                    rt          t          j        | d           S t          | t
                    st          t          j        | d           S | j        }|v rt          t          j        | d           S |v r"t          t          j        | |                   S |v r+t          t          j	        | |         |         v          S t          d|           )Nr<   r=   r>   )r<   r=   r>   r?   zUnknown tensor input kind: )rA   r(   r   r   r:   r   r5   r   r6   r7   r   )r   r   r   r   rq   r{   s     r%   to_input_specz9_convert_to_export_graph_signature.<locals>.to_input_spec  s   c=)) 	I)/s4HHHH#~.. 	N)"6CMMMMx;)"6CMMMM)))(+D1   
 &&&%(.-d3;QQ	    !!Et!E!EFFFr$   idxr   c                    t          |t                    rt          t          j        |d           S t          |t
                    st          t          j        |d           S |j        }| t                    t                    z   t          	          z   t                    z   k     r|v r"t          t          j	        ||                   S |v r"t          t          j
        ||                   S |	v r"t          t          j        |	|                   S t          d|           |
v rt          t          j        |d           S |v r"t          t          j        ||                   S |v r"t          t          j        ||                   S |k    rt          t          j        |d           S t          d|           )Nr   zUnknown tensor mutation kind: zUnknown tensor output kind: )rA   r(   r   r   r:   r   rN   r   r   rP   rQ   rT   r   rR   rS   rO   )r   r   r   buffer_mutationsgrad_paramsgrad_user_inputsr\   r   parameter_mutationsuser_input_mutationsr}   s      r%   to_output_specz:_convert_to_export_graph_signature.<locals>.to_output_spec  s1   a'' 	I:#34HHHH!^,, 	O:#9qNNNNv%&&-@)A)AAC E
 E
 
   '''!#3+D1   
 ,,,!#6.t4   
 ---!#7/5    %%Ld%L%LMMM|##!z'=1TRRRR$$!#9&t,   
 )))!#:+D1   
 $$!z'=1TRRRR %%JD%J%JKKKr$   c                 &    g | ]} |          S r#   r#   )re   r   r   s     r%   r   z6_convert_to_export_graph_signature.<locals>.<listcomp>  s#    888#==%%888r$   c                 .    g | ]\  }} ||          S r#   r#   )re   r   r   r   s      r%   r   z6_convert_to_export_graph_signature.<locals>.<listcomp>  s)    LLLvsANN3**LLLr$   )r_   r`   )torch.utilsr   r   setr{   r   r   r}   r   r   r   gradients_to_parameterr[   r\   r   r   graphnodestree_leavesr   r   reversedargsrL   r   r1   r   	enumerater   )r   r   rq   pytreeis_jointinputsoutputsr_   r`   r   r   r   r   r   r   r\   r   r   r   r   r   r{   r}   s     `      @@@@@@@@@@@@@@r%   "_convert_to_export_graph_signaturer   ]  sg   
 .-----1=H o122K*?'9344L&8)>*@ 	*AA  	*CC  	*66 
 #/L#1M   HN  F
   &&tD"(.1I1I,J,J'K'K'PQQ  G
G< GI G G G G G G G G G22LC 2LL 2LZ 2L 2L 2L 2L 2L 2L 2L 2L 2L 2L 2L 2L 2Lh 9888888KLLLL79K9KLLLLKlSSSSr$   ))dataclassescollections.abcr   r   enumr   r   typingr   r   r   r
   torch._library.opaque_objectr   r   torch._subclasses.fake_tensorr   r   &torch._functorch._aot_autograd.schemasr   __all__	dataclassr   r(   r   r   r   r   r   rL   r   r   r   r   r   r   r   r   r   r!   r   r#   r$   r%   <module>r     sT       / / / / / / / /         ' ' ' ' ' ' ' ' ? ? ? ? ? ? M M M M M M M M 1 1 1 1 1 1  FLLLEEEEEE                                            - - - - - - - - + + + + + + + +
         R R R R R R R R<        F F F F F F F F0         BK BK BK BK BK BK BK BKJ) ) )&
l &
 &
 &
 &
R|T%|T|T  H|T 	|T |T |T |T |T |Tr$   