
    9jk                     *   d dl 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
 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z  ez  ez  ez  ez  ez  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'y)-    N)
CollectionMapping)autoEnum)TYPE_CHECKING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<   y)r   nameN__name__
__module____qualname__str__annotations__     \/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/export/graph_signature.pyr   r           
Ir#   r   c                       e Zd ZU eed<   y)TokenArgumentr   Nr   r"   r#   r$   r'   r'   %   r%   r#   r'   c                       e Zd ZU eed<   y)r   r   Nr   r"   r#   r$   r   r   *   r%   r#   r   c                       e Zd ZU eed<   y)r   r   Nr   r"   r#   r$   r   r   /   r%   r#   r   c                       e Zd ZU eed<   y)r   r   Nr   r"   r#   r$   r   r   4   r%   r#   r   c                   6    e Zd ZU eed<   eed<   dZedz  ed<   y)r   r   	class_fqnNfake_val)r   r   r   r    r!   r-   r	   r"   r#   r$   r   r   9   s    
IN(,H%,r#   r   c                   :    e Zd ZU eed<   eez  ez  ez  dz  ed<   y)r   r   Nvalue)r   r   r   r    r!   intfloatboolr"   r#   r$   r   r   @   s     
I;#d**r#   r   c                   `    e Zd Z e       Z e       Z e       Z e       Z e       Z e       Z	y)r   N)
r   r   r   r   
USER_INPUT	PARAMETERBUFFERCONSTANT_TENSOR
CUSTOM_OBJTOKENr"   r#   r$   r   r   Q   s,    JIVFfOJFEr#   r   c                   R    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
y)r   kindargNtarget
persistentc           
          | j                   t        j                  k(  r| j                  t	        d      t        | j                  t        t        t        t        t        t        t        f      s!t	        dt        | j                               y )Nz,Failed to specify persistent flag on BUFFER.expected valid arg type, got )r;   r   r6   r>   AssertionError
isinstancer<   r   r   r   r   r   r   r'   typeselfs    r$   __post_init__zInputSpec.__post_init__a   sv    99	(((&$%STTHH  !
 !#@dhh@P!QRR
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;   )rE   r=   r>   s      r$   __str__zInputSpec.__str__s   sp    {{*)DKK=0J??2R,tFW8X
dhhmm$%RDIINN(;'<VHZLQQr#   )r   r   r   r   r!   ArgumentSpecr    r>   r2   rF   rL   r"   r#   r$   r   r   Z   s1    
O	$J"Jt"S$Rr#   r   c                   |    e Zd Z e       Z e       Z e       Z e       Z e       Z e       Z	 e       Z
 e       Zy)r   N)r   r   r   r   USER_OUTPUTLOSS_OUTPUTBUFFER_MUTATIONPARAMETER_MUTATIONGRADIENT_TO_PARAMETERGRADIENT_TO_USER_INPUTUSER_INPUT_MUTATIONr9   r"   r#   r$   r   r   y   s>    &K&KfO F!V&FEr#   r   c                   >    e Zd ZU eed<   eed<   edz  ed<   d Zd Zy)r   r;   r<   Nr=   c           
          t        | j                  t        t        t        t
        t        t        t        f      st        d| j                         y )Nr@   )
rB   r<   r   r   r   r   r   r'   r   rA   rD   s    r$   rF   zOutputSpec.__post_init__   sI    HH  !
 !#@
!KLL
r#   c                     | j                   dnd| j                    d}t        | j                  j                         dt        | j                  j                         | S )NrH   rI   rJ   rK   )r=   r    r<   r   r;   )rE   r=   s     r$   rL   zOutputSpec.__str__   sP    {{*)DKK=0Jdhhmm$%RDIINN(;'<VHEEr#   )	r   r   r   r   r!   rM   r    rF   rL   r"   r#   r$   r   r      s$    
	$JMFr#   r   c                   @    e Zd ZU eeef   ed<   eeef   ed<   eed<   y)r   gradients_to_parametersgradients_to_user_inputsloss_outputN)r   r   r   dictr    r!   r"   r#   r$   r   r      s$    !#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  e
z  d
z     fd       Zede	eez  ez  e
z  d
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%y
) 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   | ]G  }|j                   t        j                  k(  r(t        |j                  t
              r|j                   I y wN)r;   r   r5   rB   r=   r    .0ss     r$   	<genexpr>z2ExportGraphSignature.parameters.<locals>.<genexpr>  s<      
vv,,,!((C( HH
   AAtupler_   rD   s    r$   
parameterszExportGraphSignature.parameters  "     
%%
 
 	
r#   c                 :    t        d | j                  D              S )Nc              3      K   | ]G  }|j                   t        j                  k(  r(t        |j                  t
              r|j                   I y wrd   )r;   r   r6   rB   r=   r    re   s     r$   rh   z/ExportGraphSignature.buffers.<locals>.<genexpr>"  s<      
vv)))!((C( HH
ri   rj   rD   s    r$   bufferszExportGraphSignature.buffers   rm   r#   c                 :    t        d | j                  D              S )Nc              3      K   | ]U  }|j                   t        j                  k(  r6|j                  d u r(t	        |j
                  t              r|j
                   W yw)FN)r;   r   r6   r>   rB   r=   r    re   s     r$   rh   z>ExportGraphSignature.non_persistent_buffers.<locals>.<genexpr>+  sH      
vv)))||u$!((C(	 HH
s   AArj   rD   s    r$   non_persistent_buffersz+ExportGraphSignature.non_persistent_buffers)  s"     
%%
 
 	
r#   c                 :    t        d | j                  D              S )Nc              3      K   | ]G  }|j                   t        j                  k(  r(t        |j                  t
              r|j                   I y wrd   )r;   r   r7   rB   r=   r    re   s     r$   rh   z?ExportGraphSignature.lifted_tensor_constants.<locals>.<genexpr>6  s<      
vv222!((C( HH
ri   rj   rD   s    r$   lifted_tensor_constantsz,ExportGraphSignature.lifted_tensor_constants4  rm   r#   c                 :    t        d | j                  D              S )Nc              3      K   | ]G  }|j                   t        j                  k(  r(t        |j                  t
              r|j                   I y wrd   )r;   r   r8   rB   r=   r    re   s     r$   rh   z:ExportGraphSignature.lifted_custom_objs.<locals>.<genexpr>?  s<      
vv---!((C( HH
ri   rj   rD   s    r$   lifted_custom_objsz'ExportGraphSignature.lifted_custom_objs=  rm   r#   Nc           	         g }| j                   D ]  }|j                  t        j                  k7  r!t	        |j
                  t        t        t        t        t        f      r&|j                  |j
                  j                         vt	        |j
                  t              r&|j                  |j
                  j                         t        |j
                   d       t!        |      S )Nz is not a valid user inputs)r_   r;   r   r4   rB   r<   r   r   r   r   r   appendr   r   r/   RuntimeErrorrk   )rE   user_inputsrg   s      r$   r}   z ExportGraphSignature.user_inputsG  s    =?!! 	JAvv---""$#%	 ""155::.AEE#34""155;;/"aeeW,G#HII%	J& [!!r#   c                 j   g }| j                   D ]  }|j                  t        j                  t        j                  fvr1t        |j                  t        t        t        t        f      r&|j                  |j                  j                         t        |j                  t              r&|j                  |j                  j                         t        |j                  t              r'|j                  |j                  j                         t!        |j                   d       t#        |      S )Nz is not a valid user output)r`   r;   r   rO   rP   rB   r<   r   r   r   r   r{   r   r   r/   r   r|   rk   )rE   user_outputsrg   s      r$   r   z!ExportGraphSignature.user_outputsa  s    >@"" 	JAvv&&&&  1A?S ##AEEJJ/AEE#34##AEEKK0AEE#45##AEEJJ/"aeeW,G#HII#	J$ \""r#   c                 :    t        d | j                  D              S )Nc              3     K   | ]w  }|j                   t        j                  k(  rXt        |j                  t
              r>t        |j                  t              r$|j                  j                  |j                  f y y wrd   )	r;   r   r5   rB   r<   r   r=   r    r   re   s     r$   rh   z<ExportGraphSignature.inputs_to_parameters.<locals>.<genexpr>|  sW      
vv,,,155.1188S)	 UUZZ"
   A=A?_immutable_dictr_   rD   s    r$   inputs_to_parametersz)ExportGraphSignature.inputs_to_parametersz  "     
%%
 
 	
r#   c                 :    t        d | j                  D              S )Nc              3     K   | ]w  }|j                   t        j                  k(  rXt        |j                  t
              r>t        |j                  t              r$|j                  j                  |j                  f y y wrd   )	r;   r   r6   rB   r<   r   r=   r    r   re   s     r$   rh   z9ExportGraphSignature.inputs_to_buffers.<locals>.<genexpr>  sW      
vv)))155.1188S)	 UUZZ"
r   r   rD   s    r$   inputs_to_buffersz&ExportGraphSignature.inputs_to_buffers  r   r#   c                 :    t        d | j                  D              S )Nc              3     K   | ]w  }|j                   t        j                  k(  rXt        |j                  t
              r>t        |j                  t              r$|j                  j                  |j                  f y y wrd   )	r;   r   rQ   rB   r<   r   r=   r    r   re   s     r$   rh   z9ExportGraphSignature.buffers_to_mutate.<locals>.<genexpr>  sW      
vv333155.1188S)	 UUZZ"
r   r   r`   rD   s    r$   buffers_to_mutatez&ExportGraphSignature.buffers_to_mutate  "     
&&
 
 	
r#   c                 :    t        d | j                  D              S )Nc              3     K   | ]w  }|j                   t        j                  k(  rXt        |j                  t
              r>t        |j                  t              r$|j                  j                  |j                  f y y wrd   )	r;   r   rR   rB   r<   r   r=   r    r   re   s     r$   rh   z<ExportGraphSignature.parameters_to_mutate.<locals>.<genexpr>  sW      
vv666155.1188S)	 UUZZ"
r   r   rD   s    r$   parameters_to_mutatez)ExportGraphSignature.parameters_to_mutate  r   r#   c                 :    t        d | j                  D              S )Nc              3     K   | ]w  }|j                   t        j                  k(  rXt        |j                  t
              r>t        |j                  t              r$|j                  j                  |j                  f y y wrd   )	r;   r   rU   rB   r<   r   r=   r    r   re   s     r$   rh   z=ExportGraphSignature.user_inputs_to_mutate.<locals>.<genexpr>  sW      
vv777155.1188S)	 UUZZ"
r   r   rD   s    r$   user_inputs_to_mutatez*ExportGraphSignature.user_inputs_to_mutate  r   r#   c                 :    t        d | j                  D              S )Nc              3     K   | ]w  }|j                   t        j                  k(  rXt        |j                  t
              r>t        |j                  t              r$|j                  j                  |j                  f y y wrd   )	r;   r   r7   rB   r<   r   r=   r    r   re   s     r$   rh   zIExportGraphSignature.inputs_to_lifted_tensor_constants.<locals>.<genexpr>  sW      
vv222155.1188S)	 UUZZ"
r   r   rD   s    r$   !inputs_to_lifted_tensor_constantsz6ExportGraphSignature.inputs_to_lifted_tensor_constants  r   r#   c                 :    t        d | j                  D              S )Nc              3     K   | ]w  }|j                   t        j                  k(  rXt        |j                  t
              r>t        |j                  t              r$|j                  j                  |j                  f y y wrd   )	r;   r   r8   rB   r<   r   r=   r    r   re   s     r$   rh   zDExportGraphSignature.inputs_to_lifted_custom_objs.<locals>.<genexpr>  sX      
vv---155"34188S)	 UUZZ"
r   r   rD   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_|t	        d      t        |j                  t              s!t	        dt        |j                               |j                  j                  }|j                  t        j                  k(  rt        |j                  t              s!t	        dt        |j                               t        |j                  t              s!t	        dt        |j                               |j                  ||j                  j                  <   8|j                  t        j                  k(  sWt        |j                  t              s!t	        dt        |j                               t        |j                  t              s!t	        dt        |j                               |j                  ||j                  j                  <    |y t        |||      S )Nz multiple LOSS_OUTPUT specs foundz-expected TensorArgument for LOSS_OUTPUT, got z3expected str target for GRADIENT_TO_PARAMETER, got z7expected TensorArgument for GRADIENT_TO_PARAMETER, got z4expected str target for GRADIENT_TO_USER_INPUT, got z8expected TensorArgument for GRADIENT_TO_USER_INPUT, got )r\   rZ   r[   )r`   r;   r   rP   rA   rB   r<   r   rC   r   rS   r=   r    rT   r   )rE   r\   rZ   r[   specs        r$   backward_signaturez'ExportGraphSignature.backward_signature  s   2435 %% 	FDyyJ222*()KLL!$((N;(GTXXGWX  #hhmmj>>>!$++s3(MdSWS^S^N_M`a  "$((N;(QRVW[W_W_R`Qab  :>'6j???!$++s3(NtTXT_T_O`Nab  "$((N;(RSWX\X`X`SaRbc  ;?++(79	F< &#$;%=
 	
r#   c                      y rd   r"   rD   s    r$   assertion_dep_tokenz(ExportGraphSignature.assertion_dep_token  s    r#   c                 :   g }| j                   D ]  }|j                  t        j                  k(  s!t	        |j
                  t              s!t        dt        |j
                               |j                  |j
                  j                          t        |      S Nz+expected TokenArgument for TOKEN kind, got )r_   r;   r   r9   rB   r<   r'   rA   rC   r{   r   rk   )rE   input_tokensrg   s      r$   r   z!ExportGraphSignature.input_tokens  s~    !! 	0Avv(!!%%7(Ed155k]S  ##AEEJJ/	0 \""r#   c                 :   g }| j                   D ]  }|j                  t        j                  k(  s!t	        |j
                  t              s!t        dt        |j
                               |j                  |j
                  j                          t        |      S r   )r`   r;   r   r9   rB   r<   r'   rA   rC   r{   r   rk   )rE   output_tokensrg   s      r$   r   z"ExportGraphSignature.output_tokens  s    "" 	1Avv)))!!%%7(Ed155k]S  $$QUUZZ0	1 ]##r#   c                 2   | j                   }|y t        |      dk7  rt        dt        |             t        t	        |j                                     }t        | j                        t        | j                        z   }||k7  rt        d| d|       y )N   z,expected exactly 1 assertion_dep_token, got z)expected assertion_dep_token_index to be z, got )r   lenrA   nextiterkeysr   r   )rE   r   assertion_dep_token_indexexpected_indexs       r$   rF   z"ExportGraphSignature.__post_init__  s    "66&"#q( >sCV?W>XY  %).A.F.F.H)I$J!T../#d6L6L2MM66 ;N;K6RkQlm  7r#   oldnewc                 ,   t        |t              st        dt        |             t        |t              st        dt        |             t        t
        t        t        t        t        f}| j                  D ]D  }t        |j                  |      s|j                  j                  |k(  s4||j                  _        F | j                  D ]D  }t        |j                  |      s|j                  j                  |k(  s4||j                  _        F y)zR
        Replace all uses of the old name with new name in the signature.
        zexpected old to be str, got zexpected new to be str, got N)rB   r    rA   rC   r   r   r   r   r   r'   r`   r<   r   r_   )rE   r   r   	arg_typesois         r$   replace_all_usesz%ExportGraphSignature.replace_all_uses  s     #s# #?S	{!KLL#s# #?S	{!KLL
	 "" 	%A!%%+55::$!$AEEJ	% !! 	%A!%%+55::$!$AEEJ	%r#   c                       fd}|S )Nc                     |j                   dk(  rj                  | j                  |       r-| j                   dk(  rj                  | j                  |       y y y )Noutputplaceholder)opr   r   )r   r   userreplace_inputsrE   s      r$   _z0ExportGraphSignature.get_replace_hook.<locals>._9  sL    ww("%%chh4#&&M"9%%chh4 #:~r#   r"   )rE   r   r   s   `` r$   get_replace_hookz%ExportGraphSignature.get_replace_hook8  s    	5 r#   c                     dj                  d | j                  D              }dj                  d | j                  D              }d| d| dS )N
c              3   2   K   | ]  }t        |        y wrd   r    re   s     r$   rh   z/ExportGraphSignature.__str__.<locals>.<genexpr>B  s     A1AA   c              3   2   K   | ]  }t        |        y wrd   r   re   s     r$   rh   z/ExportGraphSignature.__str__.<locals>.<genexpr>C  s      CAQ Cr   z

# inputs
z

# outputs
)joinr_   r`   )rE   r_   r`   s      r$   rL   zExportGraphSignature.__str__A  sK    iiA0@0@AAyy C1B1B CCk]/,rJJr#   )ra   N)F)&r   r   r   __doc__listr   r!   r   propertyr   r    rl   rp   rs   rv   ry   r0   r1   r2   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rF   r   r   rL   r"   r#   r$   r   r      s   iV i z"" 
JsO 
 
 
C 
 
 

3 
 
 
C 
 
 
JsO 
 
 "Zed(:S(@4(GH " "2 #jut);c)AD)HI # #0 
gc3h&7 
 
 
738#4 
 
 
738#4 
 
 
gc3h&7 
 
 
wsCx'8 
 
 
7383D 
 
 
gc3h.? 
 
 )
$;d$B )
 )
\ WS#X%6%=   	#jo 	# 	# 	$z# 	$ 	$%C %c %2Kr#   r   c                 0    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   G  s    
 'DK((r#   ra   c           
      H   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rt        |  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        ||      r3t/        | j                   |j1                         j3                         	      S t        ||      r"t/        | j                   |j4                  |
      S t7        t        |            r*t/        | j                   t9        t        |            |
      S t        |t        t        t        t        t        d       f      rt        | j                   |      S t        dt        |       d      )Nr   )ScriptObjectSymBoolSymFloatSymIntr   rH   )r   r/   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	   rB   r0   r2   r1   rC   r    r   metarA   r   r'   r   r   r   r   r   r   _typequalified_namescript_class_namer   r
   )nodetoken_namesr   r   r   r   r	   r   s           r$   _make_argument_specr   Q  s   ==C$dE4:s;<Rt44DIIfLM
 	
 ))E
CyyK$)),,	499--	C	 499--	C	"TYY//	C	!DII..	C	& dii399;;U;U;WXX	C)	* c&;&;c
 	
 
S		" &:49&EPS
 	
 
C#tS%d<	=TYYc::8c D> ?
 	
r#   graph_signaturer   gmztorch.fx.GraphModulers   c           	      2   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                  |j                   j"                  D cg c]  }|j$                  dk(  rt'        ||       }}|j)                  t+        t-        t/        |j                   j"                                    j0                        D cg c]  }t'        |       }}dt2        dt4        ffd}	dt6        dt2        dt8        ffd	}
|D cg c]
  } |	|       }}t;        |      D cg c]  \  }} |
||       }}}t=        ||
      S c c}w c c}w c c}w c c}}w )Nr   )_pytreer   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: )rB   r'   r   r   r9   r   r4   r   r5   r6   rA   )r   r   r   r   rs   r}   s     r$   to_input_specz9_convert_to_export_graph_signature.<locals>.to_input_spec  s    c=))//s4HH#~.)"6"6CMMxx;)"6"6CMM))((+D1 
 &&%%(.-d3;QQ	  !#>tf!EFFr#   idxr   c                 H   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  rw|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: )rB   r'   r   r   r9   r   rO   r   r   rQ   rR   rU   rA   rS   rT   rP   )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  s   a':#3#34HH!^,:#9#9qNNvv%&-@)AAC E
 
  ''!#33+D1 
 ,,!#66.t4 
 --!#77/5  %'EdV%LMM|#!z'='=1TRR$!#99&t, 
 ))!#::+D1 
 $!z'='=1TRR %'CD6%JKKr#   )r_   r`   )torch.utilsr   r   setr}   r   r   r   r   r   r   gradients_to_parameterr[   r\   r   r   graphnodesr   r   tree_leavesr   r   reversedargsrM   r   r0   r   	enumerater   )r   r   rs   pytreeis_jointr   r   inputsoutputsr   r   r   r_   r   r   r`   r   r   r   r   r   r\   r   r   r   r}   r   s     `             @@@@@@@@@@@r$   "_convert_to_export_graph_signaturer   {  s   
 .11=H o112K*??'99334L&88)>>*@@  	**AA   	**CC   	**66 
 #//L#11M HHNN77m# 	D,/F  &&tD"((..1I,J'K'P'PQ 	D-0G 
G< GI G G22LC 2LL 2LZ 2L 2Lh 288#=%8K89B79KLvsAN3*LLLKlSSs
d 9Ls   "H5H	H!H)(dataclassescollections.abcr   r   enumr   r   typingr   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   rM   r   r   r   r   r   r   r   r   r   r    r   r"   r#   r$   <module>r     s1    /    ? M 1 E                 - - - + + +   	
     R R R<  F F F2    ^K ^K ^KB)'
l '
T|T%|T|T  H|T 	|Tr#   