a
    .=icF                     @   s  d dl mZmZmZmZmZmZmZmZm	Z	m
Z
mZmZmZmZ d dlmZ d dlmZmZmZmZ d dlmZ ddgZddgZd	gZee eeeeef f d
ddZeedddZeedddZeeeeeeed f f dddZee eeee	ef f ddddZdS )    )ArgumentDispatchKeyFunctionSchemaBaseTypeBaseTyReturn
AnnotationNativeFunctionOperatorNameBackendIndexBackendMetadataDeviceCheckType
SchemaKindVariant)	concatMap)ListTupleSequenceDict)defaultdictz'adaptive_avg_pool3d_backward.grad_inputz _slow_conv2d_backward.grad_inputZ_cummax_helperZ_cummin_helperZ
polygamma_)native_functionsreturnc                 C   sD   t t}| D ]2}||j  }|j |vs0J |||j < q|S N)r   dictfunc	signaturekind)r   pre_grouped_native_functionsfd r    t/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/lib/python3.9/site-packages/torchgen/native_function_generation.pypre_group_native_functions9   s    r"   )r   r   c              
   C   s~   |   tjksJ | jjd us"J t| j | jj	s:dn| jj	 d| j
 td| jjjjd | jjjjdg| jdS )Nout_outnametypedefault
annotationr&   	argumentsreturns)r   r   inplacer+   Zself_argr   r&   remove_inplacewith_overloadoverload_nameZremove_self_annotationwith_out_argsr   argumentr'   r)   r,   )r   r    r    r!   self_to_out_signatureK   s"    




r3   c           
   	      sD  |   tjksJ tdd | jD r*J dd | jD }t|dksJJ tdd | jj  fddd	D }t	d
d | jD }g }g }t
| jD ]p\}}|j rtd| |jd t||  dd}|| |rtd |j|jd}	||	 q|| qt| j | jjs dn| jj d| j|t|dS )Nc                 s   s    | ]}|j d uo|j jV  qd S r   )r)   Zis_write.0rr    r    r!   	<genexpr>|   s   z+mutable_to_out_signature.<locals>.<genexpr>c                 S   s   g | ]}|j  r|qS r    r'   is_tensor_liker4   r    r    r!   
<listcomp>       z,mutable_to_out_signature.<locals>.<listcomp>r   c                 S   s   | j d u rg S | j jS r   )r)   Z	alias_set)ar    r    r!   <lambda>   r;   z*mutable_to_out_signature.<locals>.<lambda>c                    s   g | ]}| vr|qS r    r    )r5   xZused_annotationsr    r!   r:      s   abcdefghijklmnopqrstuvwxyzc                 s   s   | ]}|j ttjkV  qd S r   )r'   r   r   ZTensorr4   r    r    r!   r7      r;   r#   !r%   )r&   r'   r)   r$   r*   )r   r   mutableanyr,   lenr   r+   Zflat_allall	enumerater'   r9   r   r   parseappendr   r)   r   r&   r.   r/   r0   r1   tuple)
r   Ztensorlike_retsZvalid_annotationsZall_rets_are_tensorsZnew_out_argsZnew_returnsir6   Znew_outZnew_retr    r?   r!   mutable_to_out_signaturej   sL    





rK   )r
   r   )r   kr   c                 C   s:  ddl m} |tjkrl| j tjks*J d}| jjdd| jj	 
| jjjsVdn| jjj d}nX|tjkrd}| j tjkrt| j}q| j tjkrt| j}qtdntd	|rtj|jt||dd
ii}ni }t|| jttjgdd d d g i ddd d dtj| jt | jd|tdgd|fS )Nr   )cppT)Zkeep_return_names
functionalZ_functionalFzPWe only bother generating out= functions from either inplace or mutable variantszDWe currently only generate either functional or out= NativeFunctions)
structured	generated)r   !use_const_ref_for_mutable_tensorsvariantsrO   Zstructured_delegateZstructured_inheritsZprecomputedZautogenZufunc_inner_loopZmanual_kernel_registrationmanual_cpp_bindingZpython_moduleZcategory_overrideZdevice_guardZdevice_checklocZcpp_no_default_argsis_abstract&has_composite_implicit_autograd_kernelZ&has_composite_explicit_autograd_kerneltags)Ztorchgen.apirM   r   rN   r   r   r   	with_namer&   r.   r/   r0   r#   r-   r3   rB   rK   AssertionErrorr   ZCompositeExplicitAutogradr   r	   rQ   setr   functionr   ZNoCheckrT   rU   )r   rL   rM   Zgets_composite_kernelr   Zbackend_metadatar    r    r!   generate_function   sl    


r\   N)rsindicesr   c                 C   s   t | }| D ]\}}tj|v }tj|v }tj|v }tj|v }|sN|sN|rtdd | D }	|rxd|tj j	v pt
dd | D }
tdd | D }|	s|
s|rq|rt| dkrt|tj jjtvrtdt|tj j q|rt|tj jjtv rq|r(|tj n|r8|tj n|tj }| op|j tjkppt
dd |jjD }|s|st|jjtvrtd	t|j d
|rt|tj\}}||tj< t|| | | |s|s|rt|tj\}}||tj< t|| | | qd S )Nc                 s   s   | ]}|j V  qd S r   )rS   r5   r   r    r    r!   r7   '  r;   z1add_generated_native_functions.<locals>.<genexpr>Zinplace_viewc                 s   s   | ]}|j V  qd S r   )Z
is_view_opr_   r    r    r!   r7   +  r;   c                 s   s   | ]}|j V  qd S r   )rV   r_   r    r    r!   r7   0  s      zEFound an out= operator that we could not find any other variants of: c                 s   s   | ]}|j  V  qd S r   r8   r4   r    r    r!   r7   b  r;   zIFound a mutable operator that we could not generate an out= variant for: a  .
These operators are problematic, because we can't easily auto-generate functionalization code for them. If you really need
the operator have the schema mentioned, that add the name of the operator to the allow-list. Otherwise if possible,
please convert it to an inplace operator)r"   itemsr   rN   r-   rB   r#   rE   valuesrW   rC   rD   strr   r&   &OUT_OPS_THAT_DONT_GET_GROUPED_PROPERLYrY   *INPLACE_OPS_THAT_DONT_GET_GROUPED_PROPERLYr   r,   *MUTABLE_OPS_THAT_CANNOT_GET_AN_OUT_VARIANTr\   r   Z
grow_indexrH   )r]   r^   r   rL   r   Zhas_functionalZhas_inplaceZhas_mutableZhas_outZ
are_manualZhas_view_opsZare_composite_implicitZbase_fnZgets_out_variantfnmetadatar    r    r!   add_generated_native_functions  sz    







ri   ) Ztorchgen.modelr   r   r   r   r   r   r   r	   r
   r   r   r   r   r   Ztorchgen.utilsr   typingr   r   r   r   collectionsr   rd   rf   re   r"   r3   rK   r\   ri   r    r    r    r!   <module>   s.   @MX