U
    Mh5                     @   s   d dl 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 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 d d	lmZ d d
lmZmZ eddG dd dZee eeee	eef dddZ dS )    )defaultdict)	dataclass)DictListOptionalSequenceTuple)dest)DispatcherSignature)method_with_native_function)ETKernelIndex)BaseTyBaseTypeDispatchKeyNativeFunctionVariant)SelectiveBuilder)	concatMapTargetT)frozenc                   @   s$   e Zd Zeeee dddZdS )ComputeNativeFunctionStub)freturnc                    s  t j jkrd S tj jd jjj ddd}|d k	s<tt	 jj
dkrTd}nLt	 jj
dkr҈ jjjr jjjd j}nt fdd	 jjjD d}|sЈ jj
d jttjkrd
}ntd j nt	 jjjt	 jj
kr4d}d}d||gt	 jj
  d|dd  jjjD  d}nltdd	  jj
D s^td jj
 d}d}d||gt	 jj
  d|dd  jj
D  d}t	 jj
dkrd| dnd}d|  d| dS )NZwrapper_CPU__F)prefixsymintr       c                 3   s(   | ] }|j  jjd  j kr|jV  qdS )r   N)typefuncreturnsname.0ar    T/var/www/html/venv/lib/python3.8/site-packages/torchgen/executorch/api/custom_ops.py	<genexpr>%   s   z5ComputeNativeFunctionStub.__call__.<locals>.<genexpr>at::Tensor()zCan't handle this return type zat::Tensor &, z::std::tuple<z>(
                c                 S   s   g | ]
}|j qS r&   )r!   )r#   rr&   r&   r'   
<listcomp>:   s     z6ComputeNativeFunctionStub.__call__.<locals>.<listcomp>z
            )c                 s   s   | ]}|j ttjkV  qd S )N)r   r   r   Tensorr"   r&   r&   r'   r(   =   s    z$Only support tensor returns but got z
at::Tensorc                 S   s   g | ]}d qS )r)   r&   )r#   r   r&   r&   r'   r,   D   s     zreturn ;
z {
    z
}
    )r   functionvariantsr
   Zfrom_schemar   r!   Zoverload_nameAssertionErrorlenr    	argumentsoutnextZflat_non_outr   r   r   r-   	ExceptionjoinallZdefn)selfr   sigZret_nameZtensor_typecommaZret_strr&   r%   r'   __call__   s^      



"z"ComputeNativeFunctionStub.__call__N)__name__
__module____qualname__r   r   r   strr=   r&   r&   r&   r'   r      s   r   )native_functionsselectorkernel_indexrocmr   c                 C   s   t j}| }d}tt}| D ]}||j | q| D ]\\}	}
t|
dkrRq<d	tt
tj|tj||dddd|
}|d|	 d| d	| d
7 }q<d	tt
tj|tj||dddd| }||fS )ad  
    Generate custom ops registration code for dest.RegisterDispatchKey.

    :param native_functions: a sequence of `NativeFunction`
    :param selector: for selective build.
    :param kernel_index: kernels for all the ops.
    :param rocm: bool for dest.RegisterDispatchKey.
    :return: generated C++ code to register custom operators into PyTorch
    r   r   r/   FN)rE   r   Zclass_method_nameZskip_dispatcher_op_registrationz
TORCH_LIBRARY_IMPL(r*   z, m) {
z
};)r   ZCPUZ_to_backend_indexr   list	namespaceappenditemsr3   r8   r   r	   ZRegisterDispatchKeyr   ZREGISTRATIONZANONYMOUS_DEFINITION)rB   rC   rD   rE   Zdispatch_keyZbackend_indexZ"static_init_dispatch_registrationsZns_grouped_native_functionsZnative_functionrG   Z	functionsZdispatch_registrations_bodyZanonymous_definitionr&   r&   r'   gen_custom_ops_registrationN   sb    		rJ   N)!collectionsr   dataclassesr   typingr   r   r   r   r   Ztorchgenr	   Ztorchgen.api.typesr
   Ztorchgen.contextr   Ztorchgen.executorch.modelr   Ztorchgen.modelr   r   r   r   r   Z!torchgen.selective_build.selectorr   Ztorchgen.utilsr   r   r   boolrA   rJ   r&   r&   r&   r'   <module>   s"   <
