
    sgB              	          U d dl Z d dlmZ d dlmZ d dlmZ d dlmZm	Z	m
Z
mZmZmZ d dlmZ d dlZd dlZd dlmZmZ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jB                  jD                  ef   f   e#d<   ed   Z$ed   Z%ed   Z&d Z'd Z(	 dddddeeeef   geeef   f   fdZ)	 ddeeejB                  jD                  ef      de de	ejB                  jD                  ef   fdZ*de	ejB                  jD                  ef   deeeef      ddfdZ+d dl,Zd dl-Zde	ejB                  jD                  ef   fdZ.y)    N)defaultdict)wraps)chain)CallableDictListSequenceTypeVarUnion)	ParamSpec)HigherOrderOperator
OpOverloadOpOverloadPacket)CustomOutParamAnnotation)_pytree)decomposition_table pre_autograd_decomposition_table
meta_tableregister_decompositionget_decompositionscore_aten_decompositions_T_Pglobal_decomposition_tablepost_autogradpre_autogradmetac                    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 )
isinstancer   r   appendr   	overloadsgetattrRuntimeErrortorch_C_dispatch_has_kernelname)registryopfnr!   olop_overloads         I/var/www/html/venv/lib/python3.12/site-packages/torch/_decomp/__init__.py_add_op_to_registryr.   '   s     79I")*	B
	#".///,,. 	.BWR_-	. ! '("!=k]KLL 88(()9)9);<$&H[!'    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     r-   	<genexpr>z3_convert_out_params.<locals>._fn.<locals>.<genexpr>T   s     Fqvzz!T2Fs   r   c              3   ,   K   | ]  }|d u k(    y wr5    )r8   r9   is_nones     r-   r;   z3_convert_out_params.<locals>._fn.<locals>.<genexpr>W   s     B!T	g-Bs   r1   )tupleall)argsr:   
out_kwargsr>   f	out_namess    ` @r-   _fnz _convert_out_params.<locals>._fnR   sT    FIFFJ mt+GBzBBBBdJfJ'$JJzJJr/   )kinddefault
annotationc              3   2   K   | ]  \  }}|d k7  s|  ywr1   Nr=   r8   kvs      r-   r;   z&_convert_out_params.<locals>.<genexpr>d   s     KdaU
K   )
parametersreturn_annotationc                  @    |j                  d       } | i |d|iS )Nr1   r6   )rA   r:   	out_kwargcustom_out_param_namerC   s      r-   rE   z _convert_out_params.<locals>._fnx   s)    

#8$?Id4f4)44r/   c              3   2   K   | ]  \  }}|d k7  s|  ywrJ   r=   rK   s      r-   r;   z&_convert_out_params.<locals>.<genexpr>   s     @41aQ%ZQ@rN   )__annotations__getr"   r?   inspect	signaturerP   _fieldsr   zip__args__	ParameterKEYWORD_ONLYr   rO   items	Signature__signature__rH   r'   !_torch_decompositions_out_wrapperr7   r   )rC   out_annotationsigrE   r9   t
out_paramsparamsrL   rM   	out_paramrS   rD   s   `          @@r-   _convert_out_paramsrh   D   s   &&**51N  ~|T2e;"))11	 
q	K 
	K I~'>'>?
 1 &&33	

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

 --112JDQ	q	5 
	5 %%!""//%	
	 "@3>>//1@9,
 $--1F1F

 120A0A0G0G0IX1QRWZq!tX.7.B.BINN+
Hs
 YJ Ys   ;K;K		K	 KKF)typeunsafereturnc                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*   rk   c                 v      }st                	t            fd}t        j                  |       |S )Nc                      t        |        y r5   )r.   )r)   r*   r(   s    r-   registerzIregister_decomposition.<locals>.decomposition_decorator.<locals>.register   s    "b1r/   )rh   r   pytree	tree_map_)r*   orig_fnro   aten_opr(   ri   rj   s   `  r-   decomposition_decoratorz7register_decomposition.<locals>.decomposition_decorator   sC    $R(B 1$7H	2 	7+r/   )r   r   r   )rs   r(   ri   rj   rt   s   ```` r-   r   r      sF    4 <<<<HRV$4 "b&9I    #"r/   aten_opsri   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   overloadpacketr    r$   _opsOperatorBase)ru   ri   r(   packets_to_overloadsopodecompositionsr)   r,   s           r-   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. r/   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   r!   r"   r7   r   )r}   ru   r)   overload_namer|   s        r-   remove_decompositionsr      sk      )b*+!# .b-0""3-. J'r4()r/   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.                  | 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^                  | 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                  | j                  | j                  | j                  | j                  | j                  | j                   | j"                  | j$                  | j&                  | j(                  | j"                  | j*                  | j,                  | j.                  | j0                  | j2                  | j4                  | j6                  | j8                  j                  | j:                  | j<                  | j>                  | j@                  | jB                  | jD                  | jF                  | jH                  | jJ                  | jL                  | jN                  | jP                  | jR                  | jT                  | jV                  | jX                  | jZ                  | j\                  | j^                  | j`                  | jb                  | jd                  | jf                  jh                  | jj                  | jl                  j                  | jl                  jn                  | jp                  | jr                  | jt                  | jv                  j                  | jv                  jx                  | jz                  | j|                  | j~                  | j                  | j                  | j                  | j                  | j                  | j                  j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  jh                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  | j                        S r5   )r$   opsatenr   addcdivaddcdiv_addcmuladdcmul_addraffine_grid_generator
alias_copyr@   aminmaxarangerG   startavg_pool2d_backwardbaddbmmbinary_cross_entropybinary_cross_entropy_backward binary_cross_entropy_with_logits
block_diag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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_add
index_add_
index_copyindex_copy_
index_fill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_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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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dimstdstd_meanstacksumr1   rd   t_copytaketanh_backward	threshold
threshold_threshold_backwardtrace	transposeint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    r-   r   r     s   99>>D^	
LL^	
MM^	
 LL^	
 MM	^	

 II^	
 &&^	
 OO^	
 HH^	
 LL^	
 KK^	
 KK^	
 $$^	
 LL^	
 %%^	
 ..^	
  11!^	
" OO#^	
$ II%^	
& JJ'^	
(   )^	
* NN+^	
, NN-^	
. KK/^	
0 1^	
2 3^	
4 !!5^	
6 **7^	
8 ++9^	
: LL;^	
< MM=^	
> KK?^	
@ OOA^	
B ""C^	
D HHE^	
F IIG^	
H HHI^	
J IIK^	
L M^	
N O^	
P ))Q^	
R OOS^	
T   ((U^	
V NNW^	
X Y^	
Z HH[^	
\ II]^	
^ JJ_^	
` a^	
b IIc^	
d JJe^	
f 00g^	
h JJi^	
j k^	
l HHm^	
n o^	
p OOq^	
r s^	
t u^	
v %%w^	
x NNy^	
z OO{^	
| ##}^	
~ NN^	
@ ""A^	
B NNC^	
D OOE^	
F OOG^	
H $$I^	
J KKK^	
L NNM^	
N OOO^	
P OOQ^	
R S^	
T OOU^	
V W^	
X IIY^	
Z MM[^	
\ MM]^	
^ LL_^	
` a^	
b ++c^	
d e^	
f $$g^	
h IIi^	
j JJk^	
l MMm^	
n NNo^	
p OOq^	
r JJs^	
t KKu^	
v w^	
x %%y^	
z $${^	
| ++}^	
~ MM^	
@ NN""A^	
B C^	
D E^	
F IIG^	
H JJI^	
J MMK^	
L ""M^	
N ""O^	
P //Q^	
R GGS^	
T MMU^	
V NNW^	
X KKY^	
Z OO[^	
\ ]^	
^ KK_^	
` ++a^	
b ((c^	
d ++e^	
f ++g^	
h NNi^	
j MMk^	
l MMm^	
n NNo^	
p ##q^	
r $$s^	
t ""u^	
v !!w^	
x IIy^	
z II{^	
| NN}^	
~ ^	
@   A^	
B C^	
D ''E^	
F G^	
H LLI^	
J MMK^	
L !!M^	
N **O^	
P !!Q^	
R **S^	
T !!U^	
V **W^	
X ""Y^	
Z ""[^	
\ ""]^	
^ KK_^	
` LLa^	
b ""c^	
d NNe^	
f IIg^	
h JJi^	
j !!k^	
l ""m^	
n IIo^	
p q^	
r <<DDs^	
t   u^	
v w^	
x HHy^	
z II{^	
| !!}^	
~ II^	
@ JJA^	
B C^	
D IIE^	
F JJG^	
H I^	
J K^	
L ((M^	
N !!O^	
P **Q^	
R ''S^	
T MMU^	
V ""W^	
X OOY^	
Z [^	
\ !!]^	
^   _^	
` JJa^	
b &&c^	
d LL  e^	
f LLg^	
h HHi^	
j MMk^	
l JJm^	
n HHo^	
p HHLLq^	
r FFs^	
t KKu^	
v IIw^	
x y^	
z NN{^	
| OO}^	
~ ##^	
@ JJA^	
B NNC^	
D IIE^	
F JJG^	
H III^	
J JJK^	
L KKM^	
N   O^	
P Q^	
R S^	
T ""U^	
V %%W^	
X 44Y^	
Z $$[^	
\ ((]^	
^ _^	
` a^	
b ""c^	
d $$e^	
f %%g^	
h ,,i^	
j   k^	
l JJm^	
n KKo^	
p IIq^	
r JJs^	
t JJu^	
v OOw^	
x OOy^	
z ''{^	
` `r/   r5   )r   )/rW   collectionsr   	functoolsr   	itertoolsr   typingr   r   r   r	   r
   r   typing_extensionsr   r$   torch.library
torch._opsr   r   r   torch._prims_commonr   torch.utilsr   rp   __all__r   r   dictr   strry   rz   rU   r   r   r   r.   rh   r   r   r   torch._decomp.decompositionstorch._refsr   r=   r/   r-   <module>rm     s    #   A A '   H H 8 ) T]t_  Dejj%%x/	00  1A #=n#M  '/
':Of ,#$3E,#xB (2r6"223,#b  uUZZ446FFGH
 
%**
!
!8
+,@)00(:;)uZ)99:;) 
)( $ b$uzz'>'>'H"I br/   