o
    ÔÙ¾iº  ã                   @   s|   d dl mZ d dlmZ d dlmZmZmZmZm	Z	m
Z
mZ eƒ Zeƒ Zeƒ Zeƒ Ze
ƒ Zeƒ Ze	ƒ ZG dd„ dejƒZdS )é    )ÚCallable)Únn)Úcpu_has_amx_supportÚis_cpuÚis_cudaÚis_hipÚis_musaÚis_npuÚis_xpuc                       s‚   e Zd Z‡ fdd„Zdefdd„Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚMultiPlatformOpc                    s$   t ƒ  ¡  |  ¡ | _d | _d| _d S ©NF)ÚsuperÚ__init__Údispatch_forwardÚ_forward_methodÚ_original_forward_methodÚis_torch_compile©Úself©Ú	__class__© úZ/home/ubuntu/.local/lib/python3.10/site-packages/sglang/srt/layers/utils/multi_platform.pyr      s   


zMultiPlatformOp.__init__Ú
num_tokensc                 C   sj   | j rd S | j| _d| jjv r|dkrddlm} || _nd| jjv r,|dkr+| j| _n| j| _d| _ d S )NÚFusedMoEé   r   )Úfused_moe_forward_nativeÚTopKT)r   r   r   r   Ú__name__Ú&sglang.srt.layers.moe.fused_moe_nativer   Úforward_native)r   r   r   r   r   r   Úenter_torch_compile!   s   €€
z#MultiPlatformOp.enter_torch_compilec                 C   s"   | j sd S | j| _d | _d| _ d S r   )r   r   r   r   r   r   r   Úleave_torch_compile<   s
   
z#MultiPlatformOp.leave_torch_compilec                 O   ó   | j |i |¤ŽS ©N)r   ©r   ÚargsÚkwargsr   r   r   ÚforwardF   ó   zMultiPlatformOp.forwardc                 O   ó   t ‚r$   ©ÚNotImplementedErrorr%   r   r   r   r    I   ó   zMultiPlatformOp.forward_nativec                 O   r*   r$   r+   r%   r   r   r   Úforward_cudaL   r-   zMultiPlatformOp.forward_cudac                 O   r*   r$   r+   r%   r   r   r   Úforward_npuO   r-   zMultiPlatformOp.forward_npuc                 O   r#   r$   )r.   r%   r   r   r   Úforward_hipR   r)   zMultiPlatformOp.forward_hipc                 O   r#   r$   ©r    r%   r   r   r   Úforward_xpuU   r)   zMultiPlatformOp.forward_xpuc                 O   r#   r$   r1   r%   r   r   r   Úforward_musaX   s   zMultiPlatformOp.forward_musac                 O   r#   r$   r1   r%   r   r   r   Úforward_hpu^   r)   zMultiPlatformOp.forward_hpuc                 O   r#   r$   r1   r%   r   r   r   Úforward_cpua   r)   zMultiPlatformOp.forward_cpuc                 C   sF   t r| jS tr
| jS trtr| jS tr| jS t	r| j
S tr | jS | jS r$   )Ú_is_cudar.   Ú_is_hipr0   Ú_is_cpuÚ_is_cpu_amx_availabler5   Ú_is_npur/   Ú_is_xpur2   Ú_is_musar3   r    r   r   r   r   r   d   s   z MultiPlatformOp.dispatch_forward)r   Ú
__module__Ú__qualname__r   Úintr!   r"   r(   r    r.   r/   r0   r2   r3   r4   r5   r   Ú__classcell__r   r   r   r   r      s    
r   N)Útypingr   Útorchr   Úsglang.srt.utilsr   r   r   r   r   r	   r
   r6   r7   r8   r9   r:   r;   r<   ÚModuler   r   r   r   r   Ú<module>   s    $
