o
    ib                      @   s,  U d dl Z d dlmZ d dlmZmZ ddlmZ ddlm	Z	 zd dl
mZmZmZmZmZmZmZ d	Zd
edddid
edddiedddejedddidd
edddiejedddejedddiejedddidd
ejejB eddddiid
ejejB eddddiid
ejejB eddddiid
ejejB eddddiid
ejejB edd ddiid
ejejB edd!ddiid"Zeeeeeef ef f ed#< ee W n ey   d$Zd%d& ZG d'd( d(Zd)d* Zd+d, ZY nw d-ee d.efd/d0Zd-ed.dfd1d2Zg d3ZdS )4    N)partial)OptionalUnion   )lazy_import_flash_attention   )flash_attention_forward)DeviceLayerRepositoryMode
get_kernelregister_kernel_mappingreplace_kernel_forward_from_hubuse_kernel_forward_from_hubTcudaz!kernels-community/deformable-detrMultiScaleDeformableAttention)repo_id
layer_namezkernels-community/moeLlama4TextMoezkernels-community/liger_kernelsLigerRMSNorm)r   rocmzmedmekk/triton-llama-mlpTritonLlamaMLPzkernels-community/megablocksMegaBlocksMoeMLPzahadnagy/megablockszkernels-community/activationFastGELUz>=0.0.4,<0.1.0)r   r   version	QuickGELUNewGELUSiluz>=0.1.0GeluGeluTanh)r   r   RMSNormMLPr   r   r   r   SiLUGeLUr   _KERNEL_MAPPINGFc                  O   s   dd }|S )Nc                 S   s   | S )N )clsr%   r%   Y/home/ubuntu/.local/lib/python3.10/site-packages/transformers/integrations/hub_kernels.py	decorator   s   z.use_kernel_forward_from_hub.<locals>.decoratorr%   )argskwargsr(   r%   r%   r'   r      s   r   c                   @   s   e Zd Zdd ZdS )r
   c                 O      t d)NzNLayerRepository requires `kernels` to be installed. Run `pip install kernels`.RuntimeError)selfr)   r*   r%   r%   r'   __init__      zLayerRepository.__init__N)__name__
__module____qualname__r/   r%   r%   r%   r'   r
      s    r
   c                  O   r+   )Nz^replace_kernel_forward_from_hub requires `kernels` to be installed. Run `pip install kernels`.r,   r)   r*   r%   r%   r'   r      s   r   c                  O   r+   )NzVregister_kernel_mapping requires `kernels` to be installed. Run `pip install kernels`.r,   r4   r%   r%   r'   r      r0   r   attn_implementationreturnc                 C   s   | duot d| duS )zJCheck whether `attn_implementation` matches a kernel pattern from the hub.Nz'^[^/:]+/[^/:]+(?:@[^/:]+)?(?::[^/:]+)?$)research)r5   r%   r%   r'   	is_kernel   s   r9   c              
   C   sR  t | sdS tstdddlm} ddlm} d}| }d| v r,| d\}}||}d|v r<|d\}}|	 }n|}d}|	 }|
d\}}}|	 }|rV|	 nd}zt||d	}	W n tyw }
 ztd
| d|
 dd}
~
ww t|	dr|du rt}t||	d}t|	dd n	|durt|	|}|| | || |d  dS )zALoad and register the kernel associated to `attn_implementation`.Nz`kernels` is either not installed or uses an incompatible version. Please install the latest version with `pip install -U kernels`.r   )ALL_MASK_ATTENTION_FUNCTIONS)ALL_ATTENTION_FUNCTIONS|:@)revisionz-An error occurred while trying to load from 'z': .flash_attn_varlen_func)implementationT)force_importflash_attention_2)r9   _kernels_availableImportErrormasking_utilsr:   modeling_utilsr;   splitgetstrip	partitionr   	Exception
ValueErrorhasattrr   r   r   getattrregister)r5   r:   r;   attention_wrapperactual_attn_namer   kernel_name_revkernelekernel_functionr%   r%   r'   load_and_register_kernel   sJ   



rZ   )r
   r   r   r   )r7   	functoolsr   typingr   r   modeling_flash_attention_utilsr   flash_attentionr   kernelsr	   r
   r   r   r   r   r   rE   	INFERENCETRAININGTORCH_COMPILEr$   dictstr__annotations__rF   boolr9   rZ   __all__r%   r%   r%   r'   <module>   s   
$



&f4