
    Ph9                        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             Ze j                   G d d             Ze j                   G d	 d
             Z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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Ze j                   G d  d!             Ze j                   G d" d#             Zy)$    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<   y)r   nameN__name__
__module____qualname__str__annotations__     gC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/export/graph_signature.pyr   r          
Ir   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eedf   ed<   y)r   Nvalue)r   r   r   r   intfloatboolr   r   r   r    r   r      s    eT4'((r   r   c                   D    e Zd Z e       Z e       Z e       Z e       Zy)r   N)r   r   r   r   
USER_INPUT	PARAMETERBUFFERCONSTANT_TENSORr   r   r    r   r   %   s    JIVFfOr   r   c                   8    e Zd ZU eed<   eed<   ee   ed<   d Zy)r   kindargtargetc                 R    t        | j                  t        t        t        f      sJ y N
isinstancer/   r   r   r   selfs    r    __post_init__zInputSpec.__post_init__2       $((^^EU$VWWWr   N)	r   r   r   r   r   ArgumentSpecr	   r   r7   r   r   r    r   r   ,   s    
O	SMX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_OUTPUTLOSS_OUTPUTBUFFER_MUTATIONGRADIENT_TO_PARAMETERGRADIENT_TO_USER_INPUTUSER_INPUT_MUTATIONr   r   r    r   r   6   s/    &K&KfO F!V&r   r   c                   8    e Zd ZU eed<   eed<   ee   ed<   d Zy)r   r.   r/   r0   c                 R    t        | j                  t        t        t        f      sJ y r2   r3   r5   s    r    r7   zOutputSpec.__post_init__E   r8   r   N)	r   r   r   r   r   r9   r	   r   r7   r   r   r    r   r   ?   s    
	SMXr   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                     dt         dt        f fd}dt        dt         dt        ffd}|	D cg c]
  } ||       }}t	        |
      D cg c]  \  }} |||       }}}||fS c c}w c c}}w )NirN   c                 \   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                  | |         S t        d|       )Nr.   r/   r0   zUnknown tensor input kind: )	r4   r   r   r   r)   r   r*   r+   AssertionError)rP   r   rE   rD   rC   s     r    to_input_specz$_sig_to_specs.<locals>.to_input_specW   s    !^,)"6"6AdKKvv;)"6"6AdKK))((+D1 
 &&%%15Ft5L  !#>tf!EFFr   idxoc                 z   t        |t              st        t        j                  |d       S |j
                  }| t              t              z   k  rT|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|       )NrR   zUnknown tensor mutation kind: zUnknown tensor output kind: )r4   r   r   r   r;   r   lenr=   r@   rS   r>   r?   r<   )	rU   rV   r   rG   rI   rJ   rK   rH   rF   s	      r    to_output_specz%_sig_to_specs.<locals>.to_output_specj   sU   !^,:#9#9qNNvv%&-A)BBB''!#33+D1 
 --!#77/5  %'EdV%LMM|#!z'='=1TRR$!#99&t, 
 ))!#::+D1 
 $!z'='=1TRR %'CD6%JKKr   )r9   r   r%   r   	enumerate)rC   rD   rE   rF   rG   rH   rI   rJ   rK   rL   rM   rT   rY   rP   input_specsrU   rV   output_specss   `````````         r    _sig_to_specsr]   I   s    G G) G&'LC 'LL 'LZ 'L 'LR .44V=#VK49B79KL9KvsAN3*9KLL$$ 5Ls   A8A=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_inputsrK   N)r   r   r   r   r   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
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   fd       Zedeeee
f      fd       ZddZde
de
fdZy)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)
            ]
        )
    r[   r\   rN   c                     | j                   D cg c]E  }|j                  t        j                  k(  r&t	        |j
                  t              r|j
                  G c}S c c}w r2   )r[   r.   r   r*   r4   r0   r   r6   ss     r    
parameterszExportGraphSignature.parameters   sS    
 %%
%vv,,,!((C( HH%
 	
 
   A
Ac                     | j                   D cg c]E  }|j                  t        j                  k(  r&t	        |j
                  t              r|j
                  G c}S c c}w r2   )r[   r.   r   r+   r4   r0   r   rc   s     r    bufferszExportGraphSignature.buffers   sS    
 %%
%vv)))!((C( HH%
 	
 
rf   c                     | j                   D cg c]E  }|j                  t        j                  k(  r&t	        |j
                  t              r|j
                  G c}S c c}w r2   )r[   r.   r   r,   r4   r0   r   rc   s     r    lifted_tensor_constantsz,ExportGraphSignature.lifted_tensor_constants  sS    
 %%
%vv222!((C( HH%
 	
 
rf   c                 :    t        d | j                  D              S )Nc              3      K   | ]Q  }|j                   t        j                  k(  r2t        |j                  t
              r|j                  j                   S y wr2   )r.   r   r)   r4   r/   r   r   .0rd   s     r    	<genexpr>z3ExportGraphSignature.user_inputs.<locals>.<genexpr>  s?      
%vv---*QUUN2S EEJJ%   AA)tupler[   r5   s    r    rC   z ExportGraphSignature.user_inputs  s"     
%%
 
 	
r   c                 :    t        d | j                  D              S )Nc              3      K   | ]Q  }|j                   t        j                  k(  r2t        |j                  t
              r|j                  j                   S y wr2   )r.   r   r;   r4   r/   r   r   rm   s     r    ro   z4ExportGraphSignature.user_outputs.<locals>.<genexpr>  s?      
&vv///Jquun4U EEJJ&rp   )rq   r\   r5   s    r    rF   z!ExportGraphSignature.user_outputs  s"     
&&
 
 	
r   c                     | j                   D ci c]t  }|j                  t        j                  k(  rUt	        |j
                  t              r;t	        |j                  t              r!|j
                  j                  |j                  v c}S c c}w r2   )
r[   r.   r   r*   r4   r/   r   r0   r   r   rc   s     r    rD   z)ExportGraphSignature.inputs_to_parameters  sn     %%
%vv,,,155.1188S)	 EEJJ %
 	
 
   A9Bc                     | j                   D ci c]t  }|j                  t        j                  k(  rUt	        |j
                  t              r;t	        |j                  t              r!|j
                  j                  |j                  v c}S c c}w r2   )
r[   r.   r   r+   r4   r/   r   r0   r   r   rc   s     r    rE   z&ExportGraphSignature.inputs_to_buffers+  sn     %%
%vv)))155.1188S)	 EEJJ %
 	
 
ru   c                     | j                   D ci c]t  }|j                  t        j                  k(  rUt	        |j
                  t              r;t	        |j                  t              r!|j
                  j                  |j                  v c}S c c}w r2   )
r\   r.   r   r=   r4   r/   r   r0   r   r   rc   s     r    buffers_to_mutatez&ExportGraphSignature.buffers_to_mutate7  sn     &&
&vv333155.1188S)	 EEJJ &
 	
 
ru   c                     | j                   D ci c]t  }|j                  t        j                  k(  rUt	        |j
                  t              r;t	        |j                  t              r!|j
                  j                  |j                  v c}S c c}w r2   )
r\   r.   r   r@   r4   r/   r   r0   r   r   rc   s     r    user_inputs_to_mutatez*ExportGraphSignature.user_inputs_to_mutateA  sn     &&
&vv777155.1188S)	 EEJJ &
 	
 
ru   c                     | j                   D ci c]t  }|j                  t        j                  k(  rUt	        |j
                  t              r;t	        |j                  t              r!|j
                  j                  |j                  v c}S c c}w r2   )
r[   r.   r   r,   r4   r/   r   r0   r   r   rc   s     r    !inputs_to_lifted_tensor_constantsz6ExportGraphSignature.inputs_to_lifted_tensor_constantsL  sn     %%
%vv222155.1188S)	 EEJJ %
 	
 
ru   c                    d }i }i }| j                   D ]I  }|j                  t        j                  k(  r7|J t	        |j
                  t              sJ |j
                  j                  }X|j                  t        j                  k(  r\t	        |j                  t              sJ t	        |j
                  t              sJ |j                  ||j
                  j                  <   |j                  t        j                  k(  st	        |j                  t              sJ t	        |j
                  t              sJ |j                  ||j
                  j                  <   L |y t        |||      S )N)rK   r_   r`   )r\   r.   r   r<   r4   r/   r   r   r>   r0   r   r?   r   )r6   rK   r_   r`   specs        r    backward_signaturez'ExportGraphSignature.backward_signatureV  s%   2435 %%DyyJ222"***!$((N;;;"hhmmj>>>!$++s333!$((N;;;9='6j???!$++s333!$((N;;;:>++(7 & &#$;%=
 	
r   c                      y r2   r   r5   s    r    assertion_dep_tokenz(ExportGraphSignature.assertion_dep_tokenu  s    r   Nc                     | j                   }|y t        |      dk(  sJ t        t        |j	                                     }t        | j
                        t        | j                        z   |k(  sJ y )N   )r   rX   nextiterkeysrF   rx   )r6   r   assertion_dep_token_indexs      r    r7   z"ExportGraphSignature.__post_init__y  su    "66&&'1,,,$(.A.F.F.H)I$J!!!"S)?)?%@@()	
)r   oldnewc                     t        |t              sJ t        |t              sJ | j                  D ]H  }t        |j                  t              s|j                  j
                  |k(  s8||j                  _        J y)zR
        Replace all uses of the old name with new name in the signature.
        N)r4   r   r\   r/   r   r   )r6   r   r   rV   s       r    replace_all_usesz%ExportGraphSignature.replace_all_uses  s]     #s####s###""A!%%055::$!$AEEJ #r   )rN   N)r   r   r   __doc__r   r   r   r   propertyr   r   re   rh   rj   rC   rF   r   rD   rE   rx   rz   r|   r	   r   r   r%   r   r7   r   r   r   r    r   r      s   DL i z"" 
JsO 
 
 
C 
 
 
C 
 
 
Z_ 
 
 
jo 
 
 
gc3h&7 
 
 
738#4 
 
 
738#4 
 
 
wsCx'8 
 
 
7383D 
 
 
H-D$E 
 
< Xgc3h.?%@  	
	%C 	%c 	%r   r   )dataclassesenumr   r   typingr   r   r   r   r	   r
   r   r   __all__	dataclassr   r   r   r9   r   r   r   r   r   r]   r   r   r   r   r    <module>r      s     O O O
       ) ) ) ^^5EEF  X X X! ! X X XL%SL% "#s(+L% sCx(	L%
 c(L% c3h'L% "#s(+L% c"L% c3h'L% #L% L% ,L% 4	?D,,-L%^    m% m% m%r   