o
    vZh                     @  s   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	 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mZ erTd d
lmZ d dlmZ d dlmZ eddG dd dZdddZdS )    )annotations)defaultdict)	dataclass)TYPE_CHECKING)dest)DispatcherSignature)method_with_native_function)BaseTyBaseTypeDispatchKeyNativeFunctionVariant)	concatMapTarget)Sequence)ETKernelIndex)SelectiveBuilderT)frozenc                   @  s   e Zd ZedddZdS )	ComputeNativeFunctionStubfr   return
str | Nonec                   s  t j jvrd S tj jd jjj ddd}|d usJ t jj	dkr)d}nt jj	dkrh jj
jr? jj
jd j}nt fdd	 jj
jD d}|sg jj	d jttjkr_d
}nntd j net jj
jt jj	krd}d}d||gt jj	  d|dd  jj
jD  d}n5tdd	  jj	D sJ 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    Q/var/www/auris/lib/python3.10/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>A   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'   D   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+   K   s    zreturn ;
z {
    z
}
    )r   functionvariantsr   Zfrom_schemar   r    Zoverload_name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__   sX   


	
 z"ComputeNativeFunctionStub.__call__N)r   r   r   r   )__name__
__module____qualname__r   r;   r%   r%   r%   r&   r      s    r   native_functionsSequence[NativeFunction]selectorr   kernel_indexr   rocmboolr   tuple[str, str]c                 C  s   t j}| }d}tt}| D ]
}||j | q| D ].\}	}
t|
dkr)q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)rC   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itemsr1   r6   r   r   ZRegisterDispatchKeyr   ZREGISTRATIONZANONYMOUS_DEFINITION)r?   rA   rB   rC   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_registrationU   sb   		rJ   N)
r?   r@   rA   r   rB   r   rC   rD   r   rE   )
__future__r   collectionsr   dataclassesr   typingr   Ztorchgenr   Ztorchgen.api.typesr   Ztorchgen.contextr   Ztorchgen.modelr	   r
   r   r   r   Ztorchgen.utilsr   r   collections.abcr   Ztorchgen.executorch.modelr   Z!torchgen.selective_build.selectorr   r   rJ   r%   r%   r%   r&   <module>   s     :