
    |-jK                     v   U d dl Zd dlZd dl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 ddlmZ d	d
lmZ  ej        e          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! n# e"$ r dZ!Y nw xY w	 d dlm#Z$ dZ%n# e"$ r dZ%Y nw xY wej&        '                    dd          (                                Z)dZ*e)ev Z+de,fdZde,fdZ#d edd          id edd          iej-         edd          iej-         edd          iej-         ed d!          iej-         ed"d!          iej-         edd          id#d ed$d%          iej.         ed&d'          ej-         ed&d'          iej-         ed(d'          iej-         ed&d'          iej-         ed&d)          id*dej-        ej/        z   ed+d,d	-          iidej-        ej/        z   ed+d.d	-          iidej-        ej/        z   ed+d/d	-          iidej-        ej/        z   ed+d0d	-          iidej-        ej/        z   ed+d1d	-          iidej-        ej/        z   ed+d2d	-          iid3Z0e1e,e1ee,z  ee1eef         z  f         f         e2d4<   e!>ej-         e!d5d67          iej.         e!d5d67          ej-         e!d5d67          id8e0d9<   d: Z3d^d;Z4n+# e"$ r# dZ*dZ+d< Zd= Z# G d> d?          Zd@ ZdA ZdB Z4Y nw xY wdCd	dDdEd	dDdEd	dDdFd	dDdGd	dDdHdIdJdKZ5e1e,e1e,e,f         f         e2dL<   i Z6e1e,e	dz  f         e2dM<   dNe,dz  dOe7fdPZ8	 d_dNe,dQedz  dRe7dOe	dz  fdSZ9e6fdTe,dUe1e,e	dz  f         fdVZ:	 	 	 d`dTe,dWe,dz  dXe;e,z  dz  dRe7dOe	f
dYZdZe<e         ez  fd[Z=da>ed\             Z?g d]Z@dS )a    N)Callable)contextmanager)
ModuleType)version   )ENV_VARS_TRUE_VALUESlogging)is_kernels_available   )flash_attention_forward)DeviceLayerRepositoryModeregister_kernel_mappingreplace_kernel_forward_from_hub)
get_kernel)use_kernel_forward_from_hub)FuncRepository)use_kernel_func_from_hubTFUSE_HUB_KERNELSYES
layer_namec                 x    t           rt          |           S t                              dt                      d S )NFkernels hub usage is disabled through the environment USE_HUB_KERNELS=c                     | S N clss    e/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/transformers/integrations/hub_kernels.py<lambda>z-use_kernel_forward_from_hub.<locals>.<lambda>F   s    s     )_kernels_enabled$_kernels_use_kernel_forward_from_hubloggerwarning_once_TRANSFORMERS_USE_HUB_KERNELS)r   s    r    r   r   ?   sF     	#7
CCCxYvxx   #?"r"   	func_namec                     t           rt          rt          |           S t          st                              d           n"t                              dt
                      d S )Nzwuse_kernel_func_from_hub is not available in the installed kernels version. Please upgrade kernels to use this feature.r   c                     | S r   r   funcs    r    r!   z*use_kernel_func_from_hub.<locals>.<lambda>U   s     r"   )r#   _has_use_kernel_func_from_hub!_kernels_use_kernel_func_from_hubr%   r&   r'   )r(   s    r    r   r   H   s|     	% = 	%4Y???0 ##B   
 ##|]z||   %$$r"   cudaz!kernels-community/deformable-detrMultiScaleDeformableAttention)repo_idr   zkernels-community/moeLlama4TextMoezkernels-community/liger_kernelsLigerRMSNormzkernels-community/rmsnormRMSNormzkernels-community/mlx_rmsnorm)r/   rocmxpumpsnpuzmedmekk/triton-llama-mlpTritonLlamaMLPzkernels-community/megablocksMegaBlocksMoeMLPzahadnagy/megablocksCPUMegaBlocksMoeMLP)r/   r5   r6   cpuzkernels-community/activationFastGELU)r1   r   r   	QuickGELUNewGELUSiluGeluGeluTanh)r0   r2   r4   MLPr:   r=   r>   r?   SiLUGeLUrB   _KERNEL_MAPPINGzkernels-community/rotaryapply_rotary_transformers)r1   r(   )r6   r/   rotary_pos_embc                 f    | v p,t          fd|                                 D                       S )Nc              3   b   K   | ])}t          |t                    ot          |          V  *d S r   )
isinstancedicthas_key).0vkeys     r    	<genexpr>zhas_key.<locals>.<genexpr>   s:      [[1z!T22Fwq#[[[[[[r"   )anyvalues)drP   s    `r    rM   rM      s<    ax[3[[[[PQPXPXPZPZ[[[[[[r"   c                     | t           } t          | d          rt          d          st          d          t	          |            d S )Nr6   z0.10.2)MIN_VERSIONzfkernels uses an incompatible version. Please install the latest version with `pip install -U kernels`.)rF   rM   r
   ImportErrorr   )mappings    r    $register_kernel_mapping_transformersrY      s\    ?%G7E"" 	+?H+U+U+U 	x   	 (((((r"   c                      d }|S )Nc                     | S r   r   r   s    r    	decoratorz.use_kernel_forward_from_hub.<locals>.decorator  s    Jr"   r   argskwargsr\   s      r    r   r      s    	 	 	 r"   c                      d }|S )Nc                     | S r   r   r+   s    r    r\   z+use_kernel_func_from_hub.<locals>.decorator  s    Kr"   r   r]   s      r    r   r     s    	 	 	 r"   c                       e Zd Zd ZdS )r   c                      t          d          )NzNLayerRepository requires `kernels` to be installed. Run `pip install kernels`.RuntimeError)selfr^   r_   s      r    __init__zLayerRepository.__init__  s    opppr"   N)__name__
__module____qualname__rg   r   r"   r    r   r     s(        	q 	q 	q 	q 	qr"   r   c                       t          d          )Nz^replace_kernel_forward_from_hub requires `kernels` to be installed. Run `pip install kernels`.rd   r^   r_   s     r    r   r     s    l
 
 	
r"   c                       t          d          )NzVregister_kernel_mapping requires `kernels` to be installed. Run `pip install kernels`.rd   rl   s     r    r   r     s    stttr"   c                       t          d          )Nzcregister_kernel_mapping_transformers requires `kernels` to be installed. Run `pip install kernels`.rd   rl   s     r    rY   rY     s    q
 
 	
r"   zkernels-community/causal-conv1d)r1   r   zkernels-community/mamba-ssmz!kernels-community/finegrained-fp8zkernels-community/deep-gemmzkernels-community/sonic-moez
ep-support)r1   revision)zcausal-conv1dz	mamba-ssmzfalcon_mamba-ssmzfinegrained-fp8z	deep-gemmz	sonic-moe_HUB_KERNEL_MAPPING_KERNEL_MODULE_MAPPINGattn_implementationreturnc                 8    | duot          j        d|           duS )zJCheck whether `attn_implementation` matches a kernel pattern from the hub.Nz'^[^/:]+/[^/:]+(?:@[^/:]+)?(?::[^/:]+)?$)research)rr   s    r    	is_kernelrw   *  s.     	4' 	cI@BUVV^bbr"   attention_wrapperallow_all_kernelsc                    ddl m} ddlm} d| v r|                     d          d         n| }t          |          sdS t          st          d          d|v r-|                    d          \  }}|                                }n|}d}|                                }|	                    d	          \  }}}	|                                }|	r|	                                nd}		 t          ||	|
          }
n(# t          $ r}t          d| d| d          d}~ww xY wt          |
d          r|t          }|}n|t          |
|          }|                    | |           |                    | |d                    |
S )a  
    Load and register the kernel associated to `attn_implementation`.

    Args:
        attn_implementation: A string, usually a kernel repo like "kernels-community/flash-mla".
        attn_wrapper: a callable for the wrapper around the attention implementation. In `transformers` we
            have a wrapper around the `flash_attn_var_len` call, and the same goes for `sdpa` and `eager`.
            They just prepare the arguments properly. This is mostly used for continious batching, where we
            want the `paged` wrapper, which calls the paged cache.
        allow_all_kernels (`bool`, optional):
            Whether to load kernels from unverified hub repos, if it is a custom kernel outside of the `kernels-community`
            hub repository.
    r   )ALL_MASK_ATTENTION_FUNCTIONS)ALL_ATTENTION_FUNCTIONS|r   N`kernels` is either not installed or uses an incompatible version. Please install the latest version with `pip install -U kernels`.:@)ro   ry   z-An error occurred while trying to load from 'z': .flash_attn_varlen_funcflash_attention_2)masking_utilsr{   modeling_utilsr|   splitrw   _kernels_availablerW   strip	partitionr   	Exception
ValueErrorhasattrr   getattrregister)rr   rx   ry   r{   r|   actual_attn_namer1   kernel_name_revkernelekernel_functions                r    load_and_register_attn_kernelr   2  s     =<<<<<888888<?CV<V<V*0055a88\o%&& t 
O
 
 	
 /55c::!''))"mmooG'',,OGQmmooG
&#))+++$C[GcEVWWW [ [ [YYYUVYYYZZZ[ v/00 7$ 7+		 !&+66 $$%8/JJJ ))*=?[\o?pqqqMs   $C7 7
DDDr   rX   c                    | |v r#t          ||          t                    r||          S | t          vr%t                              d|  d           d || <   d S t
          r	 t          |          d         }t          |                              dd           }t          |                              dd           }t          |||          }||| <   n# t          $ r/}d || <   t                              d|  d|            Y d }~nd }~wt          $ r d || <   Y nw xY wd	d l
}|                     d
d          }d| d}		  |j        dt                    }
t          |
|	d           }n# t          $ r d }Y nw xY wt!          |          r8 |            r.	  |j        |           }||| <   |S # t          $ r d || <   Y n	w xY wd || <   ||          S )NzKernel z! not found in _HUB_KERNEL_MAPPINGr1   ro   r   ro   r   zFailed to load kernel z: r   -r   is_
_availablez..utils.import_utils)rK   r   rp   r%   r&   r   getr   FileNotFoundErrorAssertionError	importlibreplaceimport_module__package__r   r   callable)r   rX   r1   ro   r   r   r   r   new_kernel_namer(   	utils_modis_kernel_availablemodules                r    lazy_load_kernelr   o  sr   g*W[-A:"N"N{##---TkTTTUUU#t $(	()+6yAG*;7;;JMMH)+6::9dKKG(GLLLF#)GK    	M 	M 	M#'GK  K K K K KLLLLLLLL 	( 	( 	(#'GK   	( 	%--c3775/555		'/	/0FTTI"))Y"E"E 	' 	' 	'"&	' '(( 		(-@-@-B-B 		(,00O1EFF'-$ , , ,'+$$$, $(GK ;sC   A,C 
D%C??DD8'E   E/.E/F$ $F65F6ro   r   c                    ddl m} t          st          d          |                     d          d         }|dk    r|st          d          d|| d	}t          j                            d
          }t          j
        |          t          j
        d          k    rt          | |||          S t          | ||          S )Nr   )__version__r~   /r   zkernels-communityziYou need to specify `allow_all_kernels=True` to use kernels outside of the `kernels-community` repositorytransformers)	frameworkr   r1   kernelsz0.10.4)ro   r   
user_agentr   ) r   r   rW   r   r   r   metadatar   pkg_versionparseget_kernel_hub)r   ro   r   ry   r   repo_parentr   kernels_versions           r    r   r     s      
-
 
 	

 ##C((+K)))2C)w
 
 	
  .+R]^^J(00;;O))[->x-H-HHHkHgZdeeeekHgNNNNr"   module_namesc                 B     t           t                    r g  fd}|S )aO  
    This decorator attaches the target function within the module as a plain attribute (not as a submodule).
    Keep in mind that this registration is only meant for `kernelize` to recognize its target modules (i.e.
    function exchanged for a weightless `nn.Module` with the same forward) to then exchange to the kernel
    variation (in-place) if the conditions are met.

    We cache each of these function-based registrations: After proper registration and exchange it is removed
    from the module's `_modules` dict as it does not really act as `nn.Module` but a base function.
    c                 2    | j         fd}|| _         | S )Nc                      | g|R i | | j                             di           }D ]N}t          |dd           p!t          |dd           pt          |dd           }|t          d|          |||<   Od S )N_hidden_kernelsrh   kernel_layer_namer(   z)Could not infer kernel function name for )__dict__
setdefaultr   r   )rf   r^   r_   hidden_kernelsfnnamer   	orig_inits         r    new_initz8use_kernelized_func.<locals>.decorator.<locals>.new_init  s    Id,T,,,V,,, "]556GLLN" 
* 
*B
D11 6r#6==6r;55 
 <$%WQS%W%WXXX (*t$$
* 
*r"   )rg   )r   r   r   r   s     @r    r\   z&use_kernelized_func.<locals>.decorator  s7    L		* 	* 	* 	* 	* 	*"  
r"   )rK   r   )r   r\   s   ` r    use_kernelized_funcr     s?     ,)) &$~    . r"   c               #   *   K   	 da dV  da dS # da w xY w)z
    Context manager used to adjust the value of the global `ALLOW_HUB_KERNELS`. This is needed, as this argument
    cannot be forwarded directly to the `__init__` of the models, where we set the attention implementation.
    TNF)ALLOW_ALL_KERNELSr   r"   r    allow_all_hub_kernelsr     s:      "  "E!!!!s    )	r   r   r   r   rY   r   r   r   r   r   )NF)NNF)Aimportlib.metadatar   osru   collections.abcr   
contextlibr   typesr   	packagingr   r   utilsr   r	   utils.import_utilsr
   flash_attentionr   
get_loggerrh   r%   r   r   r   r   r   r   r   r   r   r$   r   rW   r   r.   r-   environr   upperr'   r   r#   str	INFERENCETRAININGTORCH_COMPILErF   rL   __annotations__rM   rY   rp   rq   boolrw   r   r   intlistr   r   r   __all__r   r"   r    <module>r      s,	        				 				 $ $ $ $ $ $ % % % % % %       , , , , , , 1 1 1 1 1 1 1 1 5 5 5 5 5 5 4 4 4 4 4 4 
	H	%	%}
                       
*******   .YYYYYY(,%% . . .(-%%%. %'JNN3De$L$L$R$R$T$T!48LL# # # # #%C % % % %" OO;:  *
 OO/*  
 =-! ! ! =-! ! ! 7(! ! ! ;(! ! ! =-! ! !5 
  
D OO2+  
 :1      :1! ! !	 11! ! ! :1! ! ! :4! ! !/
 
> !33__:)6 6 6
 !33__:*6 6 6
 !33__:(6 6 6
 !33__:vWX6 6 6
 !33__:vWX6 6 6
 !33__:z[\6 6 6
wBe BeOT#tFSL/DI^D_2_$_``a B B BJ ! 6B]! ! ! ~~6B]      6B]! ! !	-
 -
() \ \ \) ) ) ) )  !
 !
 !
    q q q q q q q q
 
 

u u u
 
 
 
 
=!
J "CqQQ:qII$AaPP#FSTUU:qII:UU2 2 T#tCH~-.    8: S*t"334 9 9 93: $     di: ::19D:\`:$: : : :z Pf -  - # - S*t:K5K0L -  -  -  - d   $#	O OODjO 3YO 	O
 O O O O:$d8nx&? $ $ $ $P   " " " 
 
 
s[   K &A- ,K -A74K 6A77K ;B K BK BIK %LL