o
    0 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_kernelsZLigerRMSNorm)r   Zrocmzmedmekk/triton-llama-mlpZTritonLlamaMLPzkernels-community/megablocksMegaBlocksMoeMLPzahadnagy/megablockszkernels-community/activationFastGELUz>=0.0.4,<0.1.0)r   r   version	QuickGELUNewGELUZSiluz>=0.1.0ZGeluGeluTanh)r   r   ZRMSNormZMLPr   r   r   r   ZSiLUZGeLUr   _KERNEL_MAPPINGFc                  O   s   dd }|S )Nc                 S   s   | S )N )clsr   r   q/home/app/PaddleOCR-VL-test/.venv_paddleocr/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#   r+   r   r   r   r      r'   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)r,   r   r   r   	is_kernel   s   r0   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': .Zflash_attn_varlen_func)implementationT)Zforce_importZflash_attention_2)r0   _kernels_availableImportErrorZmasking_utilsr1   Zmodeling_utilsr2   splitgetstrip	partitionr   	Exception
ValueErrorhasattrr   r   r   getattrregister)r,   r1   r2   Zattention_wrapperZactual_attn_namer   Zkernel_name_revZkerneleZkernel_functionr   r   r   load_and_register_kernel   sJ   



rG   )r
   r   r   r   )r.   	functoolsr   typingr   r   Zmodeling_flash_attention_utilsr   Zflash_attentionr   Zkernelsr	   r
   r   r   r   r   r   r9   Z	INFERENCEZTRAININGZTORCH_COMPILEr   dictstr__annotations__r:   boolr0   rG   __all__r   r   r   r   <module>   s   
$



&f4