a
    hEt                     @  s  d dl mZ d dlZd dlmZ d dlmZ d dlm  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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% erd d
l&m'Z' ddgZ(ddgZ)g dZ*dgZ+dddddZ,dddddZ-dddddZ.ddddd Z/dd!dd"d#Z0dddd$d%Z1d&d'd(d)d*d+Z2d,d-d.d/d0d1Z3d2d3dd4d5d6Z4ddd3d7d8d9Z5ed:dd;d<d=Z6ed:dd;d>d?Z7dS )@    )annotationsN)defaultdict)TYPE_CHECKING)	translate)BindingDispatcherSignatureExpr)with_native_function)
AnnotationArgumentBackendIndexBackendMetadataBaseOperatorNameBaseTyBaseTypeDEFAULT_KERNEL_NAMESPACEDeviceCheckTypeDispatchKeyFunctionSchemaNativeFunctionNativeFunctionsGroupOperatorNameReturn
SchemaKindVariant)	concatMap)Sequencez'adaptive_avg_pool3d_backward.grad_inputz _slow_conv2d_backward.grad_inputZ_cummax_helperZ_cummin_helper)$Z_assert_asyncz_assert_async.msgZ_assert_tensor_metadataZ_cslt_sparse_mm_searchZ_assert_scalarZ_dimIZ_dimVZ_has_same_storage_numelZ_linalg_check_errorsZ_local_scalar_denseZ%_nested_tensor_from_mask_left_alignedZ_nnzZ_use_cudnn_ctc_lossz_use_cudnn_ctc_loss.TensorZ#_validate_compressed_sparse_indicesZallcloseZ	dense_dimequalZis_coalesced	is_pinnedZis_same_sizeZ	is_set_toZq_per_channel_axisZq_scaleZq_zero_pointZqschemeZrecord_streamZ
sparse_dimZsym_constrain_rangeZsym_constrain_range_for_sizeZ_nested_tensor_storage_offsetsZ'_chunk_grad_outputs_efficient_attentionZ_fused_sdp_choiceZ_printZ_sink_tokensZ_nested_get_ragged_idxZ
polygamma_zSequence[NativeFunction]z6dict[FunctionSchema, dict[SchemaKind, NativeFunction]])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)   Q/var/www/auris/lib/python3.9/site-packages/torchgen/native_function_generation.pypre_group_native_functionsn   s    r+   z
str | Nonestr)overload_namer    c                 C  s   | sdS |  dS )NoutZ_outr)   )r-   r)   r)   r*   &get_expected_out_variant_overload_name|   s    r/   r   )r#   r    c              
   C  sp   |   tjksJ | jjd us"J t| j t	| jj
| j td| jjjjd | jjjjdg| jdS )Nr.   nametypedefault
annotationr1   	argumentsreturns)r%   r   inplacer6   Zself_argr   r1   remove_inplacewith_overloadr/   r-   Zremove_self_annotationwith_out_argsr   argumentr2   r4   r7   )r#   r)   r)   r*   self_to_out_signature   s"    





r=   c                 C  sL   |   tjksJ t| \}}t| jt| jj| j	
 |t|dS Nr5   )r%   r   
functionalgenerate_out_args_from_schemar   r1   r:   r/   r-   r6   r$   r;   tupler#   new_returnsnew_out_argsr)   r)   r*   functional_to_out_signature   s    

rE   z#tuple[list[Return], list[Argument]]c           
   	     s  t dd | jD rJ dd | jD }t|dks8J tdd | jj  fddtjD }td	d | jD }g }g }t	| jD ]\}}|j
 rtt| jd
krdnd| |j
d t||  dd}|| |rtd |j
|jd}	||	 q|| q||fS )Nc                 s  s    | ]}|j d uo|j jV  qd S r!   )r4   is_write.0rr)   r)   r*   	<genexpr>   s   z0generate_out_args_from_schema.<locals>.<genexpr>c                 S  s   g | ]}|j  r|qS r)   r2   is_tensor_likerG   r)   r)   r*   
<listcomp>       z1generate_out_args_from_schema.<locals>.<listcomp>r   c                 S  s   | j d u rg S | j jS r!   )r4   Z	alias_set)ar)   r)   r*   <lambda>   rN   z/generate_out_args_from_schema.<locals>.<lambda>c                   s   g | ]}| vr|qS r)   r)   )rH   xZused_annotationsr)   r*   rM      rN   c                 s  s   | ]}|j ttjkV  qd S r!   )r2   r   r   ZTensorrG   r)   r)   r*   rJ      rN      r.   !r0   )r1   r2   r4   )anyr7   lenr   r6   Zflat_allstringascii_lowercaseall	enumerater2   rL   r   r
   parseappendr   r4   )
r#   Ztensorlike_retsZvalid_annotationsZall_rets_are_tensorsrD   rC   irI   Znew_outZnew_retr)   rR   r*   r@      s:    


r@   c                 C  sL   |   tjksJ t| \}}t| j t| jj	| j
|t|dS r>   )r%   r   mutabler@   r   r1   r9   r:   r/   r-   r6   r;   rA   rB   r)   r)   r*   mutable_to_out_signature   s    


r_   r   r   zMtuple[NativeFunction, dict[DispatchKey, dict[OperatorName, BackendMetadata]]])r'   kr    c                 C  s  ddl m} |tjkrv| j tjks*J | jjddtt	| jj
j
jd| jj
j
j| j tjkd| jj
jd}np|tjkr| j tjkrt| j}q| j tjkrt| j}q| j tjkrt| j}qtdntd	| tjkr|j
 n|
|}| j r|d
7 }tj|j
t|dtdii}dht| jh d@ B }t|| jtj hdd d d g i ddd d dt!j"| j#t | j$dddd|| j%d|fS )Nr   )cppT)Zkeep_return_namesF)baser8   dunder_methodZfunctional_overload)r1   r-   z^We only bother generating out= functions from either inplace or mutable or functional variantszDWe currently only generate either functional or out= NativeFunctions_symint)kernel
structuredZcpp_namespace	generated>   Zpt2_compliant_tagZnondeterministic_seededZ	view_copy)r#   !use_const_ref_for_mutable_tensorsvariantsrf   Zstructured_delegateZstructured_inheritsZprecomputed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_kernelZ4has_composite_implicit_autograd_nested_tensor_kernelZ&has_composite_explicit_autograd_kernelZ5has_composite_explicit_autograd_non_functional_kerneltags	namespace)&Ztorchgen.apira   r   r?   r#   r%   r$   	with_namer   r   r1   rb   rc   r^   r-   r.   r8   r=   r_   rE   AssertionErrorunambiguous_name
has_symintr   ZCompositeExplicitAutogradr   r   setro   r   rh   r   functionr   ZNoCheckrl   rm   rp   )r'   r`   ra   r#   Zkernel_nameZbackend_metadataro   r)   r)   r*   generate_function  s    



		rw   zlist[NativeFunction]z6dict[DispatchKey, dict[OperatorName, BackendMetadata]]None)rsindicesr    c                 C  sj  t | }| D ]R}tj|v }tj|v }tj|v }tj|v }tdd | D }|sd|sd|sd|rtdd | D }	tdd | D }
tdd | D }|	s|
s|r|sq|rt	| dkrt
|tj jjtvrtdt
|tj j q|r"t
|tj jjtv r"q|r2|tj n(|rB|tj n|rR|tj n|tj }|j tjkptdd |jjD }td	d |jD }| o|o|}|s|st
|jjtvrt
|jjtvrtd
t
|j d|r&t|tj\}}||tj< t|| | | |s|s4|rt|tj\}}||tj< t|| | | qd S )Nc                 s  s   | ]}d |j v V  qdS )coreN)ro   )rH   variantr)   r)   r*   rJ     rN   z1add_generated_native_functions.<locals>.<genexpr>c                 s  s   | ]}|j V  qd S r!   )rk   rH   r'   r)   r)   r*   rJ     rN   c                 s  s&   | ]}|j ot|jjjd kV  qdS )set_N)Z
is_view_opr,   r#   r1   r}   r)   r)   r*   rJ     s   c                 s  s   | ]}|j V  qd S r!   )rn   r}   r)   r)   r*   rJ     s   rS   zEFound an out= operator that we could not find any other variants of: c                 s  s   | ]}|j  V  qd S r!   rK   rG   r)   r)   r*   rJ     s   c                 s  s   | ]}d t |v V  qdS )r.   N)r,   )rH   Zop_namer)   r)   r*   rJ     rN   zBFound an operator that we could not generate an out= variant for: z.
This type of operators don't have tensor-like return, making it difficult to generate a proper out= variant. If
out= variant is not needed, please add the function name into FUNCTIONAL_OPS_THAT_CANNOT_GET_AN_OUT_VARIANT list.)r+   valuesr   r?   r8   r^   r.   rU   rY   rV   r,   r#   r1   &OUT_OPS_THAT_DONT_GET_GROUPED_PROPERLYrr   *INPLACE_OPS_THAT_DONT_GET_GROUPED_PROPERLYr%   r7   rj   *MUTABLE_OPS_THAT_CANNOT_GET_AN_OUT_VARIANT-FUNCTIONAL_OPS_THAT_CANNOT_GET_AN_OUT_VARIANTrw   r   Z
grow_indexr\   )ry   rz   r&   r(   Zhas_functionalZhas_inplaceZhas_mutableZhas_outZis_coreZ
are_manualZhas_view_opsZare_composite_implicitZbase_fnZbase_fn_validZ	needs_outZgets_out_variantfnmetadatar)   r)   r*   add_generated_native_functions  s    








r   ztuple[Return, ...]z	list[str])retsnamesr    c                 C  sf   t | t |ksJ t | dkr$dS t | dkr@d|d  dS dt|   dd| dS d S )	Nr    rS   zreturn ;(, );)rV   
dispatcherZreturns_typeZcpp_typejoin)r   r   r)   r)   r*   
return_str  s    r   )r#   out_varr    c                 C  sX   |   }g }t| jdk}t|D ]0\}}|d u r"||rLd| d| dn| q"|S )NrS   	std::get<>())aliased_return_namesrV   r7   rZ   r\   )r#   r   Zaliased_retsZnon_aliased_namesZis_out_var_a_tupler]   rI   r)   r)   r*   gather_nonaliased_inner_rets  s    r   r   )gr    c                 C  s  d| j jvrd S | jd ur.d| jjvr.| j}n.| jd urLd| jjvrL| j}ntt| j jt| j j}t|j}g }g }g }tt	
| j jt	
|jD ]\}}|jd ur|jjr|d|j d|j d |t|j dt	j||jdd ||j d q|t	| qdd	d
 t|| D }	d}
t|jjdkr`d|
 dnd}t|j|
}t| j jj|| }d|}d|j| | jj rdnd d d| d| d|jj  d|	 d| dS )Nrg   zauto z_clone = clone_arg(r   Z_clone)Zbinds)exprr2   r   c                 S  s   g | ]
}|j qS r)   r   rH   er)   r)   r*   rM   ?  rN   z3gen_composite_functional_kernel.<locals>.<listcomp>outputr   z = r   
rd   r1   z {
  
  z
at::_ops::::call();
  
}
)r?   ro   r8   r^   rr   r,   r#   r   zipr   Zjit_argumentsr4   rF   r\   r1   r   Zargument_typer<   r   r   r6   rV   r7   r   r   defnr.   rt   rs   )r   Ztarget_fsig
target_sigcontextZclone_mutable_inputsZcloned_return_namesZa_currZa_tgtexprsout_nameZmaybe_assignZinner_return_namesZret_strZclone_mutable_inputs_strr)   r)   r*   gen_composite_functional_kernel  sb    


"
$
r   c                 C  s  d| j jvrd S t| j j}t| jj}ddd t| | D }g }d}t| j jjj D ]X\}}t	| jjj
dkr|nd| d| d	}|d
|j d| d|j d| d	 qbg }	t| j j D ]P\}}
|
d ur|	|
 qt	| jjj
dkr|nd| d| d	}|	| qd|}d|j| j jj | j j rPdnd d d| d| jjj  d| d| dt| j jj
|	 dS )Nrg   r   c                 S  s   g | ]
}|j qS r)   r   r   r)   r)   r*   rM   b  rN   z,gen_composite_out_kernel.<locals>.<listcomp>Z
tmp_outputrS   r   r   r   z  resize_out_helper(z);
  copy_arg(r   r   rd   r   r   z
 {
  auto z = at::_ops::r   r   r   r   )r.   ro   r   r#   r?   r   r   r6   rZ   rV   r7   r\   r1   r   r   rs   rt   r   )r   r   r   r   Z	copy_outsr   r]   Zout_argZfunctional_return_namer   Zret_nameZcopy_outs_strr)   r)   r*   gen_composite_out_kernelT  s`    
*r   )8
__future__r   rW   collectionsr   typingr   Ztorchgen.api.dispatcherapir   Ztorchgen.api.translater   Ztorchgen.api.typesr   r   r   Ztorchgen.contextr	   Ztorchgen.modelr
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Ztorchgen.utilsr   collections.abcr   r   r   r   r   r+   r/   r=   rE   r@   r_   rw   r   r   r   r   r   r)   r)   r)   r*   <module>   sD   L-	!3#n|>