
    VhJ              	          U d dl Z d dlmZ d dlmZ d dlmZmZmZ d dl	m
Z
 d dlmZmZmZmZmZ d dlmZ erd dlmZ d dlZd dlZd d	lmZmZmZmZ d d
lmZ d dlmZ d dl m!Z" g dZ# ed      Z$ ed      Z% ee&      Z'e&e(e&ejR                  j2                  ef   f   e*d<   e'd   Z+e'd   Z,e'd   Z-dejR                  j2                  de.fdZ/d Z0d Z1	 d#ddddeee%e$f   gee%e$f   f   fdZ2	 d$deeejR                  j2                  ef      de(de&ejR                  j2                  ef   fdZ3de&ejR                  j2                  ef   deeeef      ddfd Z4d dl5Zd dl6Zd%d!Z7de&ejR                  j2                  ef   fd"Z8y)&    N)defaultdict)Sequence)	lru_cachepartialwraps)chain)CallableOptionalTYPE_CHECKINGTypeVarUnion)	ParamSpec)CustomDecompTable)HigherOrderOperatorOperatorBase
OpOverloadOpOverloadPacket)CustomOutParamAnnotation)FunctionalTensor)_pytree)decomposition_table pre_autograd_decomposition_table
meta_tableregister_decompositionget_decompositionscore_aten_decompositions#_should_decompose_because_unsafe_op_T_Pglobal_decomposition_tablepost_autogradpre_autogradmetaopreturnc                     t        | t        j                  j                        syt        j                  j
                  | j                  v ry| t        j                  j                  j                  j                  k(  S )a  
    Returns True if the op must always decompose in export/compile tracing system

    In export, we always decompose certain CIA ops that are tagged with
    maybe_aliasing_or_mutating because we statically need to know if the op is
    mutating or not. But these CIA ops could have different behaviour in runtime.

    native_batch_norm is a prim op which has a wrong schema and it needs to be replaced
    with correct schema. But until then, we will force decompose it via this tag.
    FT)
isinstancetorch_opsr   Tagmaybe_aliasing_or_mutatingtagsopsatennative_batch_normdefault)r$   s    F/home/dcms/DCMS/lib/python3.12/site-packages/torch/_decomp/__init__.pyr   r   .   sS     b%**//0yy++rww6119999    c                    g }t        |t              r|| |<   yt        |t              r|j                  |       nBt        |t              sJ |j                         D ]  }|j                  t        ||              |D ]G  }|| v rt        d|       t        j                  j                  |j                               sC|| |<   I y)z
    This is an internal API for adding an op to the decomposition table.

    If op is OpOverload, it will be added to the registry directly.
    If op is OpOverloadPacket, all the valid op_overloads in the packet will be added to the registry.
    Nzduplicate registrations for )r'   r   r   appendr   	overloadsgetattrRuntimeErrorr(   _C_dispatch_has_kernelname)registryr$   fnr5   olop_overloads         r1   _add_op_to_registryr?   @   s     79I")*	B
	#".///,,. 	.BWR_-	. ! '("!=k]KLL 88(()9)9);<$&H[!'r2   c           
           j                   j                  d      }|s S t        |dd       t        u r]t	        j
                         }|j                  j                  t                fd       }t        |j                        D cg c]6  \  }}t	        j                  |t        j                  j                  d |      8 }}}t        d |j                  j                         D        |      }t	        j                   ||j                        |_         j                   j                         D 	ci c]  \  }}	|dk7  s||	 c}	}|_         |D ]%  }|j$                  |j                   |j&                  <   '  j(                  |_        |S  j                   j+                  t,        d       r
t                fd       }t	        j                  t        j                  j                  d |      }
t	        j
                         }t        d |j                  j                         D        |
f      }t	        j                   ||j                        |_         j                   j                         D 	ci c]  \  }}	|dk7  s||	 c}	}|_         |
j$                  |j                   |
j&                  <   |S  S c c}}w c c}	}w c c}	}w )	Nout
__origin__c                      t        fdD              }|d   d u t        fd|D              sJ  | i drd iS |iS )Nc              3   B   K   | ]  }j                  |d         y wNpop).0okwargss     r1   	<genexpr>z3_convert_out_params.<locals>._fn.<locals>.<genexpr>m   s     Fqvzz!T2Fs   r   c              3   ,   K   | ]  }|d u k(    y wrE    )rH   rI   is_nones     r1   rK   z3_convert_out_params.<locals>._fn.<locals>.<genexpr>p   s     B!T	g-Bs   rA   )tupleall)argsrJ   
out_kwargsrN   f	out_namess    ` @r1   _fnz _convert_out_params.<locals>._fnk   sT    FIFFJ mt+GBzBBBBdJfJ'$JJzJJr2   )kindr0   
annotationc              3   2   K   | ]  \  }}|d k7  s|  ywrA   NrM   rH   kvs      r1   rK   z&_convert_out_params.<locals>.<genexpr>}   s     KdaU
K   )
parametersreturn_annotationc                  @    |j                  d       } | i |d|iS )NrA   rF   )rQ   rJ   	out_kwargcustom_out_param_namerS   s      r1   rU   z _convert_out_params.<locals>._fn   s)    

#8$?Id4f4)44r2   c              3   2   K   | ]  \  }}|d k7  s|  ywrY   rM   rZ   s      r1   rK   z&_convert_out_params.<locals>.<genexpr>   s     @41aQ%ZQ@r]   )__annotations__getr6   rO   inspect	signaturer_   _fieldsr   zip__args__	ParameterKEYWORD_ONLYr   r^   items	Signature__signature__rW   r:   !_torch_decompositions_out_wrapperrG   r   )rS   out_annotationsigrU   rI   t
out_paramsparamsr[   r\   	out_paramrb   rT   s   `          @@r1   _convert_out_paramsrw   ]   s   &&**51N  ~|T2e;"))11	 
q	K 
	K I~'>'>?
 1 &&33	

 
 Kcnn&:&:&<KZX#--!33

 120A0A0G0G0IX1QRWZq!tX 	7A*+,,C'	7 120S0S-

 --112JDQ	q	5 
	5 %%!""//%	
	 "@3>>//1@9,
 $--!33
 120A0A0G0G0IX1QRWZq!tX.7.B.BINN+
Hw
  YL Ys   ;K;K		K	 KKF)typeunsafec                x     dv sJ dt         t        t        f   dt         t        t        f   f fd}|S )az  
    A decorator to register a function as a decomposition to the Python
    decomposition table.  Use it like this::

        @register_decomposition(torch.ops.aten.clamp_min)
        def clamp_min(x):
            return torch.clamp(self, min=min)

    If you are writing a new decomposition, consider contributing it
    directly to PyTorch in torch._decomp.decompositions.

    This API is experimental; we are almost certainly going to extend
    the API when we make decompositions eligible for use in transforms (e.g.,
    autograd) and not just backend tracing, where we then need to know if a
    decomposition can be used to simulate a transform.

    By default, we also will register it to the Meta key of dispatcher,
    and replace the c++ Meta implementation if there is already one.

    unsafe kwarg is for reuse of this function for registering non-function
    things
    >   r#   r"   r!   r<   r%   c                 v      }st                	t            fd}t        j                  |       |S )Nc                      t        |        y rE   )r?   )r$   r<   r;   s    r1   registerzIregister_decomposition.<locals>.decomposition_decorator.<locals>.register   s    "b1r2   )rw   r    pytree	tree_map_)r<   orig_fnr}   aten_opr;   rx   ry   s   `  r1   decomposition_decoratorz7register_decomposition.<locals>.decomposition_decorator   sC    $R(B 1$7H	2 	7+r2   )r	   r   r   )r   r;   rx   ry   r   s   ```` r1   r   r      sF    4 <<<<HRV$4 "b&9I    #"r2   aten_opsrx   c                 ~   |dv sJ t         |   }t        t              }|D ]7  }t        |t        t
        f      s||j                     j                  |       9 i }| D ][  }t        |t
              r||v r||   D ]
  }||   ||<    *t        |t        j                  j                        sO||v sT||   ||<   ] |S )a,  
    Retrieve a dictionary of decompositions corresponding to the list of
    operator overloads and overload packets passed as input.  Overload
    packets will include all decomposed overloads in the packet.  If there is
    no decomposition for a requested operator, it is silently ignored.

    This API is experimental; we are almost certainly going to give an alternate,
    more recommended formulation, where a user provides the set of operators
    they know how to implement, and we provide decompositions for everything
    not in this set.
    >   r#   r"   r!   )r    r   listr'   r   r   overloadpacketr4   r(   r)   r   )r   rx   r;   packets_to_overloadsopodecompositionsr$   r>   s           r1   r   r      s     <<<<)$/H&t, AcJ(89: !3!34;;C@A ?AN .b*+6J0J3B7 D.6{.C{+DUZZ4462>!)"N2. r2   r   c                     |D ]i  }t        |t              r4|j                         D ]   }t        ||      }| j	                  |d       " Gt        |t
              sX| j	                  |d       k y)a=  
    Given a dictionary of decompositions obtained from get_decompositions(), removes
    operators associated with a list of operator overloads and overload packets passed
    as input. If the decomposition dictionary does not contain a decomposition that is
    specified to be removed, it is silently ignored.
    N)r'   r   r5   r6   rG   r   )r   r   r$   overload_namer   s        r1   remove_decompositionsr      sk      )b*+!# .b-0""3-. J'r4()r2   c                      ddl m}   |        S )Nr   default_decompositions)torch.export.exported_programr   r   s    r1   r   r     s    D!##r2   c                     t         j                  j                  } t        g | j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  j                  | j                  j                  | j                   | j"                  | j$                  | j&                  | j(                  | j*                  | j,                  j.                  | j,                  j                  | j0                  | j2                  | j4                  | j6                  | j8                  | j:                  | j<                  | j>                  | j@                  | jB                  | jD                  | jF                  | jH                  | jJ                  | jL                  | jN                  | jP                  | jR                  | jT                  | jV                  | jX                  | jZ                  | j\                  | j^                  | j`                  | jb                  j                  | jd                  | jf                  | jh                  | jj                  | jl                  | jn                  | jp                  | jr                  | jt                  | jv                  | jx                  | jz                  | j|                  | j~                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  j                  | j                  j                  | j                  | j                  j                  | j                  j                  | j                  | j                  j                  | j                  j                  | j                  j                  | j                  j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                   | j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  j                  | j                  j                  | j                  j                  | j                  j                  | j                  j                  | j                  j                  | j                  j                   | j                  j"                  | j                  j$                  | j                  j&                  | j(                  | j*                  | j,                  | j.                  | j0                  | j2                  | j4                  | j6                  | j8                  | j:                  | j<                  | j>                  | j@                  | jB                  | jD                  | jF                  | jH                  | jJ                  | jL                  | jN                  | jH                  | jP                  | jR                  | jT                  | jV                  | jX                  | jZ                  | j\                  | j^                  j                  | j`                  | jb                  | jd                  | jf                  | jh                  | jj                  | jl                  | jn                  jp                  | jr                  | jt                  | jv                  | jx                  | jz                  | j|                  | j~                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  j                  | j                  | j                  | j                  j                  | j                  j                  | j                  j                  | j                  j                  | j                  j                  | j                  j"                  | j                  j                  | j                  j                  | j                  j                  | j                  | j                  j                  | j                  j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  j                  | j                  | j                  | j                  | j                  | j                  j                  | j                  j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                        S rE   )r(   r-   r.   r   addcdivaddcdiv_addcmuladdcmul_addraffine_grid_generator
alias_copyrP   aminmaxaranger0   startavg_pool2d_backwardbaddbmmbinary_cross_entropybinary_cross_entropy_backward binary_cross_entropy_with_logits
block_diag	bernoullipcelucelu_channel_shuffle	clamp_max	clamp_mincol2imcount_nonzerolinalg_crosscudnn_batch_normcudnn_batch_norm_backwardmiopen_batch_norm_backwarddeg2raddeg2rad_detach
diag_embeddiagonal_backwarddiagonal_copydotvdoteluelu_elu_backward_embedding_bagembedding_dense_backward
empty_like_euclidean_dist	expand_asexpand_copyeyefillfill_floor_dividefracfrac__fused_moving_avg_obs_fq_helpergelu_gelu_backwardgluglu_backward
hardshrinkhardsigmoidhardsigmoid_hardsigmoid_backward	hardswish
hardswish_hardswish_backward	hardtanh_hardtanh_backward	heaviside
heaviside_
huber_losshuber_loss_backwardim2col	index_addrA   
index_add_
index_copyindex_copy_
index_fill
int_Scalar
int_Tensorint_Scalar_outint_Tensor_outindex_fill_isinisneginfisposinfl1_loss_lazy_clone_test_parallel_materializeleaky_relu_leaky_relu_backwardlerplerp_linspace	logaddexp
logaddexp2logitlogit_logit_backwardlog_sigmoid_backwardlog_sigmoid_forward_log_softmax_backward_datalogspace	logsumexpmasked_fillmasked_fill_max_unpool2dmax_unpool3dmishmish_mse_lossmse_loss_backwardmulti_margin_lossmultilabel_margin_loss_forwardmvmvlgamma	mvlgamma_nansum
nan_to_numnan_to_num_narrownative_batch_norm_backwardnative_dropout_backwardnative_group_norm_backwardnative_layer_norm_backward	new_emptynew_fullnew_ones	new_zerosnll_loss2d_forwardnll_loss2d_backwardnll_loss_backwardnll_loss_forwardnormScalarOpt_dtypeScalarScalarOpt_dim_dtypeScalarOpt_dim	dtype_outnames_dtype_out	names_outScalarOpt_dtype_out
Scalar_outones	ones_likepixel_shufflepixel_unshuffle_prelu_kernel_prelu_kernel_backward_reshape_aliasrad2degrad2deg_reflection_pad1dreflection_pad1d_backwardreflection_pad2dreflection_pad2d_backwardreflection_pad3dreflection_pad3d_backwardreplication_pad1dreplication_pad2dreplication_pad3drenormrenorm_	resize_asrollrot90rrelu_with_noiserrelu_with_noise_rsub_safe_softmax+_scaled_dot_product_flash_attention_for_cpuselect_backwardselect_scattersgnsgn_sigmoid_backwardsilusilu_silu_backward
grad_inputsincsinc_slice_backwardsmooth_l1_losssmooth_l1_loss_backwardsoft_margin_losssoft_margin_loss_backward_softmax_backward_datasoftplussoftplus_backward
softshrinkspecial_entrspecial_log_ndtrspecial_xlog1pysplitTensorsplit_with_sizes_copysqueeze_copysqueezedimstd
correctioncorrection_outcorrection_names_outstd_meanstacksumrs   t_copytaketanh_backward	threshold
threshold_threshold_backwardtrace	transposeinttranspose_copytriltril_triutriu_unbindunfold_backwardunfold_copy_unsafe_index_unsafe_index_put_unsafe_masked_index#_unsafe_masked_index_put_accumulateunsafe_splitunsafe_split_with_sizesunsqueeze_copy_unsafe_viewupsample_linear1dupsample_bilinear2dupsample_trilinear3dupsample_nearest2d_backwardview_as_complexxlogyxlogy_zerozero_zeros
zeros_like
_chunk_cat_weight_norm_interface)r.   s    r1   '_core_aten_decompositions_post_autogradr  #  s    99>>Dx	
LLx	
MMx	
 LLx	
 MM	x	

 IIx	
 &&x	
 OOx	
 HHx	
 LLx	
 KKx	
 KKx	
 $$x	
 LLx	
 %%x	
 ..x	
  11!x	
" OO#x	
$ NN%x	
& NN""'x	
( II)x	
* JJ+x	
,   -x	
. NN/x	
0 NN1x	
2 KK3x	
4 5x	
6 7x	
8 !!9x	
: **;x	
< ++=x	
> LL?x	
@ MMAx	
B KKCx	
D OOEx	
F ""Gx	
H Ix	
J HHKx	
L IIMx	
N HHOx	
P IIQx	
R Sx	
T Ux	
V ))Wx	
X OOYx	
Z   (([x	
\ NN]x	
^ _x	
` HHax	
b IIcx	
d JJex	
f gx	
h IIix	
j JJkx	
l 00mx	
n JJox	
p qx	
r HHsx	
t ux	
v OOwx	
x yx	
z {x	
| %%}x	
~ NNx	
@ OOAx	
B ##Cx	
D NNEx	
F ""Gx	
H NNIx	
J OOKx	
L OOMx	
N $$Ox	
P KKQx	
R NNSx	
T NN""Ux	
V OOWx	
X OOYx	
Z OO##[x	
\ ]x	
^ OO&&_x	
` OO&&ax	
b OO**cx	
d OO**ex	
f gx	
h IIix	
j MMkx	
l MMmx	
n LLox	
p qx	
r ++sx	
t ux	
v $$wx	
x IIyx	
z JJ{x	
| MM}x	
~ NNx	
@ OOAx	
B JJCx	
D KKEx	
F Gx	
H %%Ix	
J $$Kx	
L ++Mx	
N MMOx	
P NN""Qx	
R Sx	
T Ux	
V Wx	
X Yx	
Z II[x	
\ JJ]x	
^ MM_x	
` ""ax	
b ""cx	
d //ex	
f GGgx	
h MMix	
j NNkx	
l KKmx	
n OOox	
p qx	
r KKsx	
t ++ux	
v ((wx	
x ++yx	
z ++{x	
| NN}x	
~ MMx	
@ MMAx	
B NNCx	
D ##Ex	
F $$Gx	
H ""Ix	
J !!Kx	
L II%%Mx	
N IIOx	
P II))Qx	
R II##Sx	
T IIUx	
V IIMMWx	
X II%%Yx	
Z II[x	
\ II))]x	
^ II  _x	
` IIax	
b NNcx	
d ex	
f   gx	
h ix	
j ''kx	
l mx	
n LLox	
p MMqx	
r !!sx	
t **ux	
v !!wx	
x **yx	
z !!{x	
| **}x	
~ ""x	
@ ""Ax	
B ""Cx	
D KKEx	
F LLGx	
H ""Ix	
J NNKx	
L IIMx	
N JJOx	
P !!Qx	
R ""Sx	
T IIUx	
V Wx	
X <<DDYx	
Z   [x	
\ ]x	
^ HH_x	
` IIax	
b !!cx	
d IIex	
f JJgx	
h ))ix	
j IIkx	
l JJmx	
n ox	
p qx	
r ((sx	
t !!ux	
v **wx	
x ''yx	
z MM{x	
| ""}x	
~ OOx	
@ Ax	
B !!Cx	
D   Ex	
F JJGx	
H &&Ix	
J Kx	
L LL  Mx	
N LLOx	
P HHQx	
R HHLLSx	
T HH##Ux	
V HHWx	
X HH))Yx	
Z MM$$[x	
\ MM((]x	
^ JJ_x	
` HHax	
b HHLLcx	
d FFex	
f KKgx	
h IIix	
j kx	
l NNmx	
n OOox	
p ##qx	
r JJsx	
t NNux	
v wx	
x IIyx	
z JJ{x	
| II}x	
~ JJx	
@ KKAx	
B   Cx	
D Ex	
F Gx	
H ""Ix	
J %%Kx	
L 44Mx	
N $$Ox	
P ((Qx	
R Sx	
T Ux	
V ""Wx	
X $$((Yx	
Z %%))[x	
\ ,,]x	
^   _x	
` JJax	
b KKcx	
d IIex	
f JJgx	
h JJix	
j OOkx	
l OOmx	
n ''ox	
z zr2   rE   )r!   )r%   r   )9rf   collectionsr   collections.abcr   	functoolsr   r   r   	itertoolsr   typingr	   r
   r   r   r   typing_extensionsr   torch.export.decomp_utilsr   r(   torch.library
torch._opsr   r   r   r   torch._prims_commonr   #torch._subclasses.functional_tensorr   torch.utilsr   r~   __all__r   r   dictr    strr)   rd   r   r   r   boolr   r?   rw   r   r   r   torch._decomp.decompositionstorch._refsr   r  rM   r2   r1   <module>r     s    # $ / /  D D ' ;   V V 8 @ ) T]t_
  Dd5::+B+BH+L&M!MN  1A #=n#M  '/
:EJJ,C,C : :$':Qj ,#$3E,#xB (2r6"223,#b  uUZZ446FFGH
 
%**
!
!8
+,@)00(:;)uZ)99:;) 
)( $ $~	JJX%2 ~r2   