o
    ߗiX                     @   s  d dl Z d dlmZmZ d dlmZmZmZmZm	Z	m
Z
mZmZ d dlmZ er2d dlZd dlmZ g dZe jG dd dZe jG d	d
 d
Ze jG dd dZe jG dd dZe jG dd dZe jG dd dZe jG dd dZeeeeeeeef ZG dd deZe jG dd dZG dd deZe jG dd dZe jG dd dZ e jG dd  d Z!d!d" Z"d#efd$d%Z#d&d'd(d)d*e
e$ d#d fd+d,Z%dS )-    N)autoEnum)
CollectionDictListMappingOptionalSetTYPE_CHECKINGUnionFakeScriptObject)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__ r#   r#   Z/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/torch/export/graph_signature.pyr         
 r   c                   @   r   )TokenArgumentr   Nr   r#   r#   r#   r$   r&   "   r%   r&   c                   @   r   )r   r   Nr   r#   r#   r#   r$   r   '   r%   r   c                   @   r   )r   r   Nr   r#   r#   r#   r$   r   ,   r%   r   c                   @   r   )r   r   Nr   r#   r#   r#   r$   r   1   r%   r   c                   @   s.   e Zd ZU eed< eed< dZee ed< dS )r   r   	class_fqnNfake_val)r   r   r    r!   r"   r(   r   r   r#   r#   r#   r$   r   6   s   
 r   c                   @   s,   e Zd ZU eed< eeeeedf ed< dS )r   r   Nvalue)	r   r   r    r!   r"   r   intfloatboolr#   r#   r#   r$   r   =   s   
 r   c                   @   s0   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   N   s    
r   c                   @   sB   e Zd ZU eed< eed< ee ed< dZee	 ed< dd Z
dS )r   kindargtargetN
persistentc              	   C   sP   | j tjkr| jd us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 )r3   r   r/   r6   
isinstancer4   r   r   r   r   r   r   r&   typeselfr#   r#   r$   __post_init__^   s"   zInputSpec.__post_init__)r   r   r    r   r"   ArgumentSpecr   r!   r6   r,   r;   r#   r#   r#   r$   r   W   s   
 r   c                   @   s6   e Zd 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GRADIENT_TO_PARAMETERGRADIENT_TO_USER_INPUTUSER_INPUT_MUTATIONr2   r#   r#   r#   r$   r   q   s    
r   c                   @   s2   e Zd ZU eed< eed< ee ed< dd ZdS )r   r3   r4   r5   c              	   C   s(   t | jtttttttfsJ | jd S N)	r7   r4   r   r   r   r   r   r&   r   r9   r#   r#   r$   r;      s   zOutputSpec.__post_init__N)	r   r   r    r   r"   r<   r   r!   r;   r#   r#   r#   r$   r   {   s
   
 r   c                   @   s6   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    r   r!   r"   r#   r#   r#   r$   r      s   
 r   c                	   @   s  e Zd ZU dZee ed< ee ed< ede	e
 fddZede	e
 fddZede	e
 fd	d
Zede	e
 fddZede	e
 fddZede	eeeede
f  fddZede	eeeede
f  fddZedee
e
f fddZedee
e
f fddZedee
e
f fddZedee
e
f fddZedee
e
f fddZedee
e
f fddZedee fd d!Zedeeee
f  fd"d#Z ede	e
 fd$d%Z!ede	e
 fd&d'Z"d1d(d)Z#d*e
d+e
fd,d-Z$d2d/d0Z%dS )3r   a  
    :class:`ExportGraphSignature` models the input/output signature of Export Graph,
    which is a fx.Graph with stronger invariants gurantees.

    Export Graph is functional and does not access "states" like parameters
    or buffers within the graph via ``getattr`` nodes. Instead, :func:`export`
    gurantees 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

    Resulting Graph would be::

        graph():
            %arg0_1 := placeholder[target=arg0_1]
            %arg1_1 := placeholder[target=arg1_1]
            %arg2_1 := placeholder[target=arg2_1]
            %arg3_1 := placeholder[target=arg3_1]
            %arg4_1 := placeholder[target=arg4_1]
            %add_tensor := call_function[target=torch.ops.aten.add.Tensor](args = (%arg3_1, %arg0_1), kwargs = {})
            %mul_tensor := call_function[target=torch.ops.aten.mul.Tensor](args = (%add_tensor, %arg1_1), kwargs = {})
            %mul_tensor_1 := call_function[target=torch.ops.aten.mul.Tensor](args = (%arg4_1, %arg2_1), kwargs = {})
            %add_tensor_1 := call_function[target=torch.ops.aten.add.Tensor](args = (%mul_tensor, %mul_tensor_1), kwargs = {})
            %add_tensor_2 := call_function[target=torch.ops.aten.add.Tensor](args = (%arg2_1, 1.0), kwargs = {})
            return (add_tensor_2, add_tensor_1)

    Resulting ExportGraphSignature would be::

        ExportGraphSignature(
            input_specs=[
                InputSpec(kind=<InputKind.PARAMETER: 2>, arg=TensorArgument(name='arg0_1'), target='my_parameter'),
                InputSpec(kind=<InputKind.BUFFER: 3>, arg=TensorArgument(name='arg1_1'), target='my_buffer1'),
                InputSpec(kind=<InputKind.BUFFER: 3>, arg=TensorArgument(name='arg2_1'), target='my_buffer2'),
                InputSpec(kind=<InputKind.USER_INPUT: 1>, arg=TensorArgument(name='arg3_1'), target=None),
                InputSpec(kind=<InputKind.USER_INPUT: 1>, arg=TensorArgument(name='arg4_1'), target=None)
            ],
            output_specs=[
                OutputSpec(kind=<OutputKind.BUFFER_MUTATION: 3>, arg=TensorArgument(name='add_2'), target='my_buffer2'),
                OutputSpec(kind=<OutputKind.USER_OUTPUT: 1>, arg=TensorArgument(name='add_1'), target=None)
            ]
        )
    input_specsoutput_specsreturnc                 C      t dd | jD S )Nc                 s   .    | ]}|j tjkrt|jtr|jV  qd S rC   )r3   r   r.   r7   r5   r!   .0sr#   r#   r$   	<genexpr>       
z2ExportGraphSignature.parameters.<locals>.<genexpr>tuplerG   r9   r#   r#   r$   
parameters      zExportGraphSignature.parametersc                 C   rJ   )Nc                 s   rK   rC   )r3   r   r/   r7   r5   r!   rL   r#   r#   r$   rO      rP   z/ExportGraphSignature.buffers.<locals>.<genexpr>rQ   r9   r#   r#   r$   buffers   rT   zExportGraphSignature.buffersc                 C   rJ   )Nc                 s   s8    | ]}|j tjkr|jd u rt|jtr|jV  qdS )FN)r3   r   r/   r6   r7   r5   r!   rL   r#   r#   r$   rO      s    

z>ExportGraphSignature.non_persistent_buffers.<locals>.<genexpr>rQ   r9   r#   r#   r$   non_persistent_buffers   rT   z+ExportGraphSignature.non_persistent_buffersc                 C   rJ   )Nc                 s   rK   rC   )r3   r   r0   r7   r5   r!   rL   r#   r#   r$   rO     rP   z?ExportGraphSignature.lifted_tensor_constants.<locals>.<genexpr>rQ   r9   r#   r#   r$   lifted_tensor_constants  rT   z,ExportGraphSignature.lifted_tensor_constantsc                 C   rJ   )Nc                 s   rK   rC   )r3   r   r1   r7   r5   r!   rL   r#   r#   r$   rO     rP   z:ExportGraphSignature.lifted_custom_objs.<locals>.<genexpr>rQ   r9   r#   r#   r$   lifted_custom_objs
  rT   z'ExportGraphSignature.lifted_custom_objsNc                 C   sv   g }| j D ]1}|jtjkrqt|jtttt	t
fr!||jj qt|jtr/||jj qt|j dt|S )Nz is not a valid user inputs)rG   r3   r   r-   r7   r4   r   r   r   r   r   appendr   r   r)   RuntimeErrorrR   )r:   user_inputsrN   r#   r#   r$   r[     s$   

z ExportGraphSignature.user_inputsc                 C   s   g }| j D ]A}|jtjtjfvrqt|jttt	t
fr#||jj qt|jtr1||jj qt|jtr?||jj qt|j dt|S )Nz is not a valid user output)rH   r3   r   r=   r>   r7   r4   r   r   r   r   rY   r   r   r)   r   rZ   rR   )r:   user_outputsrN   r#   r#   r$   r\   .  s$   

z!ExportGraphSignature.user_outputsc                 C   rJ   )Nc                 s   B    | ]}|j tjkrt|jtrt|jtr|jj|jfV  qd S rC   )	r3   r   r.   r7   r4   r   r5   r!   r   rL   r#   r#   r$   rO   I      

z<ExportGraphSignature.inputs_to_parameters.<locals>.<genexpr>_immutable_dictrG   r9   r#   r#   r$   inputs_to_parametersG  rT   z)ExportGraphSignature.inputs_to_parametersc                 C   rJ   )Nc                 s   r]   rC   )	r3   r   r/   r7   r4   r   r5   r!   r   rL   r#   r#   r$   rO   U  r^   z9ExportGraphSignature.inputs_to_buffers.<locals>.<genexpr>r_   r9   r#   r#   r$   inputs_to_buffersS  rT   z&ExportGraphSignature.inputs_to_buffersc                 C   rJ   )Nc                 s   r]   rC   )	r3   r   r?   r7   r4   r   r5   r!   r   rL   r#   r#   r$   rO   a  r^   z9ExportGraphSignature.buffers_to_mutate.<locals>.<genexpr>r`   rH   r9   r#   r#   r$   buffers_to_mutate_  rT   z&ExportGraphSignature.buffers_to_mutatec                 C   rJ   )Nc                 s   r]   rC   )	r3   r   rB   r7   r4   r   r5   r!   r   rL   r#   r#   r$   rO   k  r^   z=ExportGraphSignature.user_inputs_to_mutate.<locals>.<genexpr>rc   r9   r#   r#   r$   user_inputs_to_mutatei  rT   z*ExportGraphSignature.user_inputs_to_mutatec                 C   rJ   )Nc                 s   r]   rC   )	r3   r   r0   r7   r4   r   r5   r!   r   rL   r#   r#   r$   rO   v  r^   zIExportGraphSignature.inputs_to_lifted_tensor_constants.<locals>.<genexpr>r_   r9   r#   r#   r$   !inputs_to_lifted_tensor_constantst  rT   z6ExportGraphSignature.inputs_to_lifted_tensor_constantsc                 C   rJ   )Nc                 s   r]   rC   )	r3   r   r1   r7   r4   r   r5   r!   r   rL   r#   r#   r$   rO     r^   zDExportGraphSignature.inputs_to_lifted_custom_objs.<locals>.<genexpr>r_   r9   r#   r#   r$   inputs_to_lifted_custom_objs~  rT   z1ExportGraphSignature.inputs_to_lifted_custom_objsc                 C   s   d }i }i }| j D ]V}|jtjkr$|d u sJ t|jtsJ |jj}q	|jtjkrBt|j	t
s2J t|jts:J |j	||jj< q	|jtjkr_t|j	t
sPJ t|jtsXJ |j	||jj< q	|d u rfd S t|||dS )N)rF   rD   rE   )rH   r3   r   r>   r7   r4   r   r   r@   r5   r!   rA   r   )r:   rF   rD   rE   specr#   r#   r$   backward_signature  s0   

z'ExportGraphSignature.backward_signaturec                 C   s   d S rC   r#   r9   r#   r#   r$   assertion_dep_token  s   z(ExportGraphSignature.assertion_dep_tokenc                 C   B   g }| j D ]}|jtjkrt|jtsJ ||jj qt	|S rC   )
rG   r3   r   r2   r7   r4   r&   rY   r   rR   )r:   input_tokensrN   r#   r#   r$   rl        
z!ExportGraphSignature.input_tokensc                 C   rk   rC   )
rH   r3   r   r2   r7   r4   r&   rY   r   rR   )r:   output_tokensrN   r#   r#   r$   rn     rm   z"ExportGraphSignature.output_tokensc                 C   sR   | j }|d u r	d S t|dksJ tt| }t| jt| j |ks'J d S )N   )rj   lennextiterkeysr\   rd   )r:   rj   assertion_dep_token_indexr#   r#   r$   r;     s   z"ExportGraphSignature.__post_init__oldnewc                 C   s   t |tsJ t |tsJ ttttttf}| jD ]}t |j	|r+|j	j
|kr+||j	_
q| jD ]}t |j	|rA|j	j
|krA||j	_
q/dS )zR
        Replace all uses of the old name with new name in the signature.
        N)r7   r!   r   r   r   r   r   r&   rH   r4   r   rG   )r:   ru   rv   	arg_typesoir#   r#   r$   replace_all_uses  s(   

z%ExportGraphSignature.replace_all_usesFc                    s    fdd}|S )Nc                    s@   |j dkr| j|  r| j dkr| j| d S d S d S )Noutputplaceholder)oprz   r   )ru   rv   userreplace_inputsr:   r#   r$   _  s
   
z0ExportGraphSignature.get_replace_hook.<locals>._r#   )r:   r   r   r#   r   r$   get_replace_hook  s   z%ExportGraphSignature.get_replace_hook)rI   N)F)&r   r   r    __doc__r   r   r"   r   propertyr   r!   rS   rU   rV   rW   rX   r   r*   r+   r,   r[   r\   r   ra   rb   rd   re   rf   rg   r   r   ri   rj   rl   rn   r;   rz   r   r#   r#   r#   r$   r      sR   
 F	
	""	
		
r   c                 C   s   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   dict)itemsr   r#   r#   r$   r`     s   r`   rI   c           	      C   sZ  ddl m}m}m}m} ddlm} ddlm} t	| t
tttd tfr*td| dS d| jv s6J |  d| jd }| j|v rFt| jd	S t	||rQt| jd	S t	||r\t| jd	S t	||rgt| jd	S t	||rrt| jd	S t	||rt| j|  d
S t	||rt| j|j|dS t	|t
ttttd frt| j|dS tdt| d)Nr   )ScriptObjectSymBoolSymFloatSymIntr   )
FakeTensor )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   torch._subclasses.fake_tensorr   r7   r*   r,   r+   r8   r!   r   metar   r&   r   r   r   r   r   _typequalified_namescript_class_nameAssertionError)	nodetoken_namesr   r   r   r   r   r   r   r#   r#   r$   _make_argument_spec  s<   








r   graph_signaturer   gmztorch.fx.GraphModulerV   c           	         s4  ddl m} | jd u}t| j| j| jt| j| j | j	|r'| jj
ni |r/| jjni |r7| jjnd | j| jfdd|jjD }fdd|ttt|jjjD }dtdtffdd		d
tdtdtf fdd
	fdd|D }
fddt|D }t||dS )Nr   )_pytreec                    s    g | ]}|j d krt| qS )r|   )r}   r   rM   r   )rl   r#   r$   
<listcomp>/  s
    
z6_convert_to_export_graph_signature.<locals>.<listcomp>c                    s   g | ]}t | qS r#   )r   r   )rn   r#   r$   r   4  s    inprI   c                    s   t | trttj| d dS t | tsttj| d dS | j}|v r)ttj| d dS |v r7ttj| | dS | v rJttj	|  |  | vdS t
d| )Nr3   r4   r5   )r3   r4   r5   r6   zUnknown tensor input kind: )r7   r&   r   r   r2   r   r-   r   r.   r/   r   )r   r   )rb   ra   rV   r[   r#   r$   to_input_spec9  s*   


z9_convert_to_export_graph_signature.<locals>.to_input_specidxrx   c                    s  t |trttj|d dS t |tsttj|d dS |j}| t t t k rN| v r9ttj	| | dS |v rGttj
|| dS td| |v rZttj|d dS |v rhttj|| dS |v rvttj|| dS |krttj|d dS td| )Nr   zUnknown tensor mutation kind: zUnknown tensor output kind: )r7   r&   r   r   r2   r   r=   r   rp   r?   rB   r   r@   rA   r>   )r   rx   r   )buffer_mutationsgrad_paramsgrad_user_inputsrF   rn   user_input_mutationsr\   r#   r$   to_output_specR  sH   

z:_convert_to_export_graph_signature.<locals>.to_output_specc                    s   g | ]} |qS r#   r#   )rM   r   )r   r#   r$   r   ~  s    c                    s   g | ]	\}} ||qS r#   r#   )rM   r   rx   )r   r#   r$   r     s    )rG   rH   )torch.utilsr   ri   setr[   ra   rb   r\   rd   re   gradients_to_parameterrE   rF   rl   rn   graphnodestree_leavesrq   rr   reversedargsr<   r   r*   r   	enumerater   )	r   r   rV   pytreeis_jointinputsoutputsrG   rH   r#   )r   r   r   rl   rb   ra   rF   rV   rn   r   r   r   r[   r\   r$   "_convert_to_export_graph_signature  s0   




&,r   )&dataclassesenumr   r   typingr   r   r   r   r   r	   r
   r   r   r   r   &torch._functorch._aot_autograd.schemasr   __all__	dataclassr   r&   r   r   r   r   r   r<   r   r   r   r   r   r   r`   r   r!   r   r#   r#   r#   r$   <module>   sn   (	
  S
&