o
    H&i9                     @   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 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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de
e deeef deeef de
e deeef deeef deeef deeef de	e dee dee deee ee f fdd Ze jG d!d" d"Ze jG d#d$ d$ZdS )%    N)autoEnum)
CollectionDictListMappingOptionalSetTupleUnion)	ConstantArgumentExportBackwardSignatureExportGraphSignature	InputKind	InputSpec
OutputKind
OutputSpecSymIntArgumentTensorArgumentc                   @      e Zd ZU eed< dS )r   nameN__name__
__module____qualname__str__annotations__ r   r   GC:\wamp64\www\opt\env\Lib\site-packages\torch/export/graph_signature.pyr         
 r   c                   @   r   )r   r   Nr   r   r   r   r   r      r   r   c                   @   s"   e Zd ZU eeeedf ed< dS )r   Nvalue)r   r   r   r   intfloatboolr   r   r   r   r   r      s   
 r   c                   @   s$   e Zd Ze Ze Ze Ze ZdS )r   N)r   r   r   r   
USER_INPUT	PARAMETERBUFFERCONSTANT_TENSORr   r   r   r   r   %   s
    
r   c                   @   2   e Zd ZU eed< eed< ee ed< dd ZdS )r   kindargtargetc                 C      t | jtttfsJ d S N
isinstancer*   r   r   r   selfr   r   r   __post_init__2      zInputSpec.__post_init__N)	r   r   r   r   r   ArgumentSpecr   r   r2   r   r   r   r   r   ,   
   
 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_OUTPUTLOSS_OUTPUTBUFFER_MUTATIONGRADIENT_TO_PARAMETERGRADIENT_TO_USER_INPUTUSER_INPUT_MUTATIONr   r   r   r   r   6   s    
r   c                   @   r(   )r   r)   r*   r+   c                 C   r,   r-   r.   r0   r   r   r   r2   E   r3   zOutputSpec.__post_init__N)	r   r   r   r   r   r4   r   r   r2   r   r   r   r   r   ?   r5   r   user_inputsinputs_to_parametersinputs_to_buffersuser_outputsbuffer_mutationsuser_input_mutationsgrad_paramsgrad_user_inputsloss_outputinputsoutputsreturnc                    sn   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 }||fS )NirG   c                    s~   t | tsttj| d dS | j}|v rttj| d dS |v r*ttj| | dS | v r8ttj|  | dS td| )Nr)   r*   r+   zUnknown tensor input kind: )	r/   r   r   r   r$   r   r%   r&   AssertionError)rH   r   )r>   r=   r<   r   r   to_input_specW   s    
z$_sig_to_specs.<locals>.to_input_specidxoc                    s   t |tsttj|d dS |j}| t t k r=| v r(ttj| | dS |v r6ttj|| dS t	d| |v rIttj|d dS |v rWttj
|| dS |v rettj|| dS |krqttj|d dS t	d| )NrI   zUnknown tensor mutation kind: zUnknown tensor output kind: )r/   r   r   r   r6   r   lenr8   r;   rJ   r9   r:   r7   )rL   rM   r   )r@   rB   rC   rD   rA   r?   r   r   to_output_specj   sD   
z%_sig_to_specs.<locals>.to_output_specc                    s   g | ]} |qS r   r   ).0rH   )rK   r   r   
<listcomp>   s    z!_sig_to_specs.<locals>.<listcomp>c                    s   g | ]	\}} ||qS r   r   )rP   rL   rM   )rO   r   r   rQ      s    )r4   r   r!   r   	enumerate)r<   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   input_specsoutput_specsr   )r@   rB   rC   r>   r=   rD   rK   rO   rA   r<   r?   r   _sig_to_specsI   s
   $)rU   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_inputsrD   N)r   r   r   r   r   r   r   r   r   r   r      s   
 r   c                   @   sj  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
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d$ddZd e
d!e
fd"d#ZdS )%r   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):
                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)
            ]
        )
    rS   rT   rG   c                 C      dd | j D S )Nc                 S   *   g | ]}|j tjkrt|jtr|jqS r   )r)   r   r%   r/   r+   r   rP   sr   r   r   rQ          
z3ExportGraphSignature.parameters.<locals>.<listcomp>rS   r0   r   r   r   
parameters      zExportGraphSignature.parametersc                 C   rX   )Nc                 S   rY   r   )r)   r   r&   r/   r+   r   rZ   r   r   r   rQ      r\   z0ExportGraphSignature.buffers.<locals>.<listcomp>r]   r0   r   r   r   buffers   r_   zExportGraphSignature.buffersc                 C   rX   )Nc                 S   rY   r   )r)   r   r'   r/   r+   r   rZ   r   r   r   rQ     r\   z@ExportGraphSignature.lifted_tensor_constants.<locals>.<listcomp>r]   r0   r   r   r   lifted_tensor_constants  r_   z,ExportGraphSignature.lifted_tensor_constantsc                 C      t dd | jD S )Nc                 s   0    | ]}|j tjkrt|jtr|jjV  qd S r-   )r)   r   r$   r/   r*   r   r   rZ   r   r   r   	<genexpr>      z3ExportGraphSignature.user_inputs.<locals>.<genexpr>)tuplerS   r0   r   r   r   r<        z ExportGraphSignature.user_inputsc                 C   rb   )Nc                 s   rc   r-   )r)   r   r6   r/   r*   r   r   rZ   r   r   r   rd     re   z4ExportGraphSignature.user_outputs.<locals>.<genexpr>)rf   rT   r0   r   r   r   r?     rg   z!ExportGraphSignature.user_outputsc                 C   rX   )Nc                 S   <   i | ]}|j tjkrt|jtrt|jtr|jj|jqS r   )	r)   r   r%   r/   r*   r   r+   r   r   rZ   r   r   r   
<dictcomp>!      


z=ExportGraphSignature.inputs_to_parameters.<locals>.<dictcomp>r]   r0   r   r   r   r=        z)ExportGraphSignature.inputs_to_parametersc                 C   rX   )Nc                 S   rh   r   )	r)   r   r&   r/   r*   r   r+   r   r   rZ   r   r   r   ri   -  rj   z:ExportGraphSignature.inputs_to_buffers.<locals>.<dictcomp>r]   r0   r   r   r   r>   +  rk   z&ExportGraphSignature.inputs_to_buffersc                 C   rX   )Nc                 S   rh   r   )	r)   r   r8   r/   r*   r   r+   r   r   rZ   r   r   r   ri   9  rj   z:ExportGraphSignature.buffers_to_mutate.<locals>.<dictcomp>rT   r0   r   r   r   buffers_to_mutate7  rk   z&ExportGraphSignature.buffers_to_mutatec                 C   rX   )Nc                 S   rh   r   )	r)   r   r;   r/   r*   r   r+   r   r   rZ   r   r   r   ri   C  rj   z>ExportGraphSignature.user_inputs_to_mutate.<locals>.<dictcomp>rl   r0   r   r   r   user_inputs_to_mutateA  rk   z*ExportGraphSignature.user_inputs_to_mutatec                 C   rX   )Nc                 S   rh   r   )	r)   r   r'   r/   r*   r   r+   r   r   rZ   r   r   r   ri   N  rj   zJExportGraphSignature.inputs_to_lifted_tensor_constants.<locals>.<dictcomp>r]   r0   r   r   r   !inputs_to_lifted_tensor_constantsL  rk   z6ExportGraphSignature.inputs_to_lifted_tensor_constantsc                 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)rD   rV   rW   )rT   r)   r   r7   r/   r*   r   r   r9   r+   r   r:   r   )r1   rD   rV   rW   specr   r   r   backward_signatureV  s0   

z'ExportGraphSignature.backward_signaturec                 C   s   d S r-   r   r0   r   r   r   assertion_dep_tokenu  s   z(ExportGraphSignature.assertion_dep_tokenNc                 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   )rr   rN   nextiterkeysr?   rm   )r1   rr   Zassertion_dep_token_indexr   r   r   r2   y  s   z"ExportGraphSignature.__post_init__oldnewc                 C   sL   t |tsJ t |tsJ | jD ]}t |jtr#|jj|kr#||j_qdS )zR
        Replace all uses of the old name with new name in the signature.
        N)r/   r   rT   r*   r   r   )r1   rw   rx   rM   r   r   r   replace_all_uses  s   
z%ExportGraphSignature.replace_all_uses)rG   N)r   r   r   __doc__r   r   r   r   propertyr   r   r^   r`   ra   r<   r?   r   r=   r>   rm   rn   ro   r   r   rq   r!   rr   r2   ry   r   r   r   r   r      s<   
 F


		
	
r   )dataclassesenumr   r   typingr   r   r   r   r   r	   r
   r   __all__	dataclassr   r   r   r4   r   r   r   r   r   rU   r   r   r   r   r   r   <module>   s\    (			





	

O