U
    dF                     @   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 |ks0t|||j < q|S N)r   dictfunc	signaturekindAssertionError)r   pre_grouped_native_functionsfd r!   G/tmp/pip-unpacked-wheel-ua33x9lu/torchgen/native_function_generation.pypre_group_native_functions9   s    r#   )r   r   c              
   C   s~   |   tjkst| jjd k	s"t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   r,   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"    




r4   c           
   	      sD  |   tjksttdd | jD r*tdd | jD }t|dksJt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 k	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_liker5   r!   r!   r"   
<listcomp>   s     
 z,mutable_to_out_signature.<locals>.<listcomp>r   c                 S   s   | j d krg S | j jS r   )r*   Z	alias_set)ar!   r!   r"   <lambda>       z*mutable_to_out_signature.<locals>.<lambda>c                    s   g | ]}| kr|qS r!   r!   )r6   xZused_annotationsr!   r"   r;      s     abcdefghijklmnopqrstuvwxyzc                 s   s   | ]}|j ttjkV  qd S r   )r(   r   r   ZTensorr5   r!   r!   r"   r8      s     r$   !r&   )r'   r(   r*   r%   r+   )r   r   mutabler   anyr-   lenr   r,   Zflat_allall	enumerater(   r:   r   r   parseappendr   r*   r   r'   r/   r0   r1   r2   tuple)
r   Ztensorlike_retsZvalid_annotationsZall_rets_are_tensorsZnew_out_argsZnew_returnsir7   Znew_outZnew_retr!   r@   r"   mutable_to_out_signaturej   sP    


  

rL   )r
   r   )r   kr   c                 C   s:  ddl m} |tjkrl| j tjks*t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variantsrP   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.apirN   r   rO   r   r   r   r   	with_namer'   r/   r0   r1   r$   r.   r4   rC   rL   r   ZCompositeExplicitAutogradr   r	   rR   setr   functionr   ZNoCheckrU   rV   )r   rM   rN   Zgets_composite_kernelr   Zbackend_metadatar!   r!   r"   generate_function   sn    

 
r\   N)rsindicesr   c                 C   s   t | }| D ]\}}tj|k}tj|k}tj|k}tj|k}|sN|sN|rtdd | D }	|rxd|tj j	kpt
dd | D }
tdd | D }|	s|
s|rq|rt| dkrt|tj jjtkrtdt|tj j q|rt|tj jjtk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k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   )rT   r6   r   r!   r!   r"   r8   '  s     z1add_generated_native_functions.<locals>.<genexpr>Zinplace_viewc                 s   s   | ]}|j V  qd S r   )Z
is_view_opr_   r!   r!   r"   r8   +  s     c                 s   s   | ]}|j V  qd S r   )rW   r_   r!   r!   r"   r8   0  s       zEFound an out= operator that we could not find any other variants of: c                 s   s   | ]}|j  V  qd S r   r9   r5   r!   r!   r"   r8   b  s     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   rO   r.   rC   r$   rF   valuesrX   rD   rE   strr   r'   &OUT_OPS_THAT_DONT_GET_GROUPED_PROPERLYr   *INPLACE_OPS_THAT_DONT_GET_GROUPED_PROPERLYr   r-   *MUTABLE_OPS_THAT_CANNOT_GET_AN_OUT_VARIANTr\   r   Z
grow_indexrI   )r]   r^   r   rM   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#   r4   rL   r\   ri   r!   r!   r!   r"   <module>   s0   @M X