
    Vh                         U d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z
mZ ddlZddlmZ erddlmZ dZd	ed
<   	  G d dej(                        Z G d de      Z G d de      Z G d de      ZeeegZej4                  ef	 	 	 dd       Zy)a
  A context manager that disables the decomposition of certain ops during dynamo tracing.

The approach is to temporarily hijack the operator callable with PT2 custom operator.
The custom operator will not be decomposed and will show up as a single node to be exported to ONNX.

For the time being the decomposition of these ops is otherwise unavoidable.

https://github.com/pytorch/pytorch/issues/116684
https://github.com/pytorch/pytorch/issues/115883

This solution will no longer be required once the issue is resolved.
    )annotationsN)CallableTYPE_CHECKING)corenn)decompositions)Sequenceonnx_exportstr_NEW_OP_NAMESPACEc                      e Zd ZU ded<   	 ded<   	 ded<   	 ded<   	 eej                  dd              Zeej                  d               Zeej                  d	               Z	ed
        Z
ed        Zy)
DecompSkipr   op_callableonnxscript_functionr   new_op_namenew_op_schemac                     y)aA  Registers the custom operator and overrides the original operator.

        It should do the following steps in order:

        1. Register the custom operator.
        2. Override the original operator with the replacement callable.
        3. Register the ONNXScript function for exporting the custom operator.
        N )clsexport_optionss     Z/home/dcms/DCMS/lib/python3.12/site-packages/torch/onnx/_internal/fx/decomposition_skip.pyregisterzDecompSkip.register1   s     	    c                     y)z(Restores the original operator callable.Nr   r   s    r   
unregisterzDecompSkip.unregister>        	r   c                     y)z0An abstract impl (meta kernel) for the operator.Nr   r   argskwargss      r   abstractzDecompSkip.abstractD   r   r   c                ,   t          d| j                   }t        j                  j	                  || j
                         t        j                  j                  |d| j                         t        j                  j                  || j                         y)zRegisters the custom operator.z::defaultN)
r   r   torchlibrarydefiner   implreplacementregister_faker"   )r   new_op_qualnames     r   register_custom_opzDecompSkip.register_custom_opJ   sf     //r#//1BC_c.?.?@?IsG##OS\\Br   c                &     | j                   |i |S )zA replacement callable for the operator to be hijacked.

        This has the same signature and eager behavior as the original operator.
        )r   r   s      r   r)   zDecompSkip.replacementR   s     s///r   Nr   torch.onnx.ExportOptions)__name__
__module____qualname____annotations__classmethodabcabstractmethodr   r   r"   r,   r)   r   r   r   r   r   &   s    ?!!U+l	  	       C C 0 0r   r   c                      e Zd Zej                  j
                  j                  Zej                  Z
dZdZedd       Zed        Zed        Zy)UpsampleBilinear2DDecompSkipupsample_bilinear2d\(Tensor self, SymInt[]? output_size, bool align_corners, float[]? scale_factors) -> (Tensor)c                   t        t        j                  t              r.t        t        j                  j                  | j
                        s| j                          t        j                  j                  j                  t        j                  j                  _        |j                  #t        j                  j                         |_
        |j                  }|j                  | j                  t        | j
                         y N)function	namespaceop_name)hasattrr%   opsr   r
   r   r,   r9   _C_nnonnx_registryonnxOnnxRegistryregister_opr   r   r   registrys      r   r   z%UpsampleBilinear2DDecompSkip.registera   s    uyy"34GII!!3??=
 ""$+099+@+@+T+T(''/+0::+B+B+DN(!//,,'OO 	 	
r   c                V    | j                   t        j                  j                  _        y N)r   r%   rB   rC   r9   r   s    r   r   z'UpsampleBilinear2DDecompSkip.unregisterq   s    +.??(r   c                    t        j                  |j                         ||      }t        j                  |j                  d      |j                  d      g||j
                  |j                        S )Nr      dtypedevicer   upsample_compute_output_sizesizer%   emptyrO   rP   r   inputoutput_sizealign_cornersscale_factorsosizes         r   r"   z%UpsampleBilinear2DDecompSkip.abstractu   s]    ;;JJL+}
 {{ZZ]EJJqM2E2++<<
 	
r   Nr.   )r0   r1   r2   r%   rB   rC   r9   r   torchlib_nnaten_upsample_bilinear2d_vecr   r   r   r4   r   r   r"   r   r   r   r8   r8   [   se    ((,,22K%BB'KrM
 
 ; ; 
 
r   r8   c                      e Zd Zej                  j
                  j                  Zej                  Z
dZdZedd       Zed        Zed        Zy)UpsampleTrilinear3DDecompSkipupsample_trilinear3dr:   c                   t        t        j                  t              r.t        t        j                  j                  | j
                        s| j                          t        j                  j                  j                  t        j                  j                  _        |j                  #t        j                  j                         |_
        |j                  }|j                  | j                  t        | j
                         y r<   )r@   r%   rA   r   r
   r   r,   r_   rB   rC   rD   rE   rF   rG   r   rH   s      r   r   z&UpsampleTrilinear3DDecompSkip.register   s    uyy"34GII!!3??=
 ""$,1II,A,A,V,V)''/+0::+B+B+DN(!//,,'OO 	 	
r   c                V    | j                   t        j                  j                  _        y rK   )r   r%   rB   rC   r_   r   s    r   r   z(UpsampleTrilinear3DDecompSkip.unregister   s    ,/OO)r   c                
   t        j                  |j                         ||      }t        j                  |j                  d      |j                  d      |j                  d      g||j
                  |j                        S )Nr   rM      rN   rQ   rU   s         r   r"   z&UpsampleTrilinear3DDecompSkip.abstract   sg    ;;JJL+}
 {{ZZ]EJJqM5::a=A5A++<<
 	
r   Nr.   )r0   r1   r2   r%   rB   rC   r_   r   r[   aten_upsample_trilinear3d_vecr   r   r   r4   r   r   r"   r   r   r   r^   r^      se    ((,,33K%CC(KrM
 
 < < 
 
r   r^   c                      e Zd Zej                  Zej                  ZdZ	dZ
edd       Zed        Ze	 	 	 	 	 	 	 	 dd       Zy)	InstanceNormDecompSkipinstance_normz(Tensor input, Tensor? weight, Tensor? bias, Tensor? running_mean, Tensor? running_var, bool use_input_stats, float momentum, float eps, bool cudnn_enabled) -> Tensorc                   t        t        j                  t              r.t        t        j                  j                  | j
                        s| j                          t        j                  j                  j                  t        _        |j                  #t        j                  j                         |_        |j                  }|j                  | j                  t        | j
                         y r<   )r@   r%   rA   r   r
   r   r,   rg   rD   rE   rF   rG   r   rH   s      r   r   zInstanceNormDecompSkip.register   s    uyy"34GII!!3??=
 ""$#ii33AA''/+0::+B+B+DN(!//,,'OO 	 	
r   c                .    | j                   t        _        y rK   )r   r%   rg   r   s    r   r   z!InstanceNormDecompSkip.unregister   s    !oor   c
                v    t        j                  |j                         |j                  |j                        S )NrN   )r%   rT   rS   rO   rP   )
r   rV   weightbiasrunning_meanrunning_varuse_input_statsmomentumepscudnn_enableds
             r   r"   zInstanceNormDecompSkip.abstract   s,     {{JJL++<<
 	
r   Nr.   )ro   boolrp   floatrq   rt   rr   rs   )r0   r1   r2   r%   rg   r   torchlib_coreaten_instance_normr   r   r   r4   r   r   r"   r   r   r   rf   rf      s    %%K'::!K	(  
 
  . . 
 
 
 
 
 
r   rf   c              #     K   	 |D ]  }|j                  |         d |D ]  }|j                           y# |D ]  }|j                           w xY ww)a  A context manager that enables the decomposition skips.

    The original operator callables that are otherwise decomposed are replaced with custom operators.
    The ONNXScript functions for exporting the custom operators are added to the ONNX registry inside export_options.
    N)r   r   )r   skipsskips      r   enable_decomposition_skipsrz      s[      	*DMM.)	* 	DOO	E 	DOO	s   A8 AAA)r   r/   rx   zSequence[type[DecompSkip]])__doc__
__future__r   r5   
contextlibtypingr   r   &onnxscript.function_libs.torch_lib.opsr   ru   r   r[   r%   torch._decompr   collections.abcr	   r   r3   ABCr   r8   r^   rf   _DEFAULT_SKIP_LISTcontextmanagerrz   r   r   r   <module>r      s    # 
  *
  ( ( ' 3 & ,20 20j#
: #
L#
J #
L1
Z 1
j !!   );,% r   