
    Z j(                     8    S SK Jr  S rS rS r " S S\5      rg)   )PushToHubMixinc                 
   Sn [        U R                  5       5      nUR                  R
                  nUS:X  a.  SSKJn  U" 5       (       a  SSKn[        US5      R                  b  g	U$ ! [         a    [        SU 35      ef = f)
zw
Infers the device type from the model parameters.
Args:
    model: The model instance.

Returns:
    The device type.
z
    {
        "RMSNorm": {
            "cuda":
                "kernels-community/layer_norm:LlamaRMSNorm",
            ...
        },
        ...
    }
    zPCannot determine model device, please provide a device to the mapping. Example: cudar   )is_torch_available    Nversionrocm)next
parametersStopIteration
ValueErrordevicetypeutilsr   torchgetattrhip)modelEXAMPLE_MAPPINGparamdev_typer   r   s         q/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/transformers/utils/kernel_config.pyinfer_devicer      s    	O
U%%'( ||  H6.ui(,,8O!  
^_n^op
 	

s   A) )Bc                     SSK Jn  US;  a  [        SU 35      eUR                  S5      S   nUR                  S5      S   nUX5" UUS900X@'   g )Nr   LayerRepositoryr   r	   xpunpuneuron=Only cuda, rocm, xpu, npu and neuron devices supported, got: :   )repo_id
layer_name)kernelsr   r   split)r%   r   	repo_namemodecompatible_mappingr   repo_layer_namer$   s           r   add_to_mappingr,   :   sl    '==XY_X`abbooc*1-Oooc"1%G/*
&"    c                     SSK Jn  SSKJn  US;  a  [	        SU 35      eUR                  S5      S   nUR                  S5      S   nUR                  S5      S	   n	UX6" U" U5      U	US
900X@'   g )Nr   )Path)LocalLayerRepositoryr   r!   r"   r#   /)	repo_pathpackage_namer%   )pathlibr/   r&   r0   r   r'   )
r%   r   r(   r)   r*   r/   r0   r+   r3   repo_package_names
             r   add_to_mapping_localr7   K   s    ,==XY_X`abbooc*1-O$Q'I!,R0&y/.*
&"r-   c                   B    \ rS rSrSrS
S jrSS jrS rS rSS jr	S	r
g)KernelConfig`   z]
Kernel configuration class. This class is used to configure the kernel mapping for a model.
Nc                 6    Ub  UO0 U l         0 U l        X l        g Nkernel_mappingregistered_layer_namesuse_local_kernel)selfr>   r@   s      r   __init__KernelConfig.__init__e   s     0>0JnPR&(# 0r-   c                 @    SSK Jn  UXW" UUUS900U R                  U'   g )Nr   r   )r$   r%   revision)r&   r   r>   )rA   r$   registered_namer%   r   r)   rE   r   s           r   update_kernelKernelConfig.update_kernelj   s4    + o#)%0
O,r-   c                     UR                  5        H1  u  p#[        US5      (       d  M  UR                  U R                  U'   M3     g )Nkernel_layer_name)named_moduleshasattrrJ   r?   )rA   r   namemodules       r   store_registered_layer_names)KernelConfig.store_registered_layer_namesw   s;    !//1LDv2334:4L4L++D1 2r-   c                    SnU R                  U5        [        U R                  [        5      (       d$  [	        SU S[        U R                  5       35      eU R                  R                  5        H  u  p4X0R                  R                  5       ;  a  [	        SU S35      e[        U[        5      (       a  SU;  d  SU;  a  [	        SU S	U 35      eMe  [        U[        5      (       aa  UR                  5        HK  u  pVUS
;  a  [	        SU 35      e[        U[        5      (       a  SU;  d  SU;  d  M<  [	        SU S	U 35      e   M  [	        SU SU 35      e   g)a  
Validates the kernel_mapping to ensure that:
1. Each layer_name in the mapping is registered in the model (i.e., the model contains a module with a matching kernel_layer_name).
2. Each kernel value is either a string of the form 'org/repo:layer_name' or a dict mapping device types ("cuda", "rocm", "xpu", "npu") to such strings.
3. Each device key in a dict is one of "cuda", "rocm", "xpu", or "npu".
4. Each repo_name is a valid repository and layer name in the format 'org/repo:layer_name' (i.e., a string containing both a slash and a colon).
5. If a local path is detected, it should be in the format '/abs/path:layer_name'. The absolute path must include the `package_name`, like "/home/user/layer_norm".

Args:
    model: The model instance whose modules are checked for registered kernel_layer_name attributes.

Raises:
    ValueError: If a layer_name is not registered in the model, if a device is not supported,
                or if a repo_name is not a valid 'org/repo:layer_name' string.
a  
        For single device form remote
        {
            "RMSNorm":
                "kernels-community/layer_norm:LlamaRMSNorm",
            ...
        },
        For multiple devices form remote
        {
            "RMSNorm": {
                "cuda":
                    "kernels-community/layer_norm:LlamaRMSNorm",
                "rocm":
                    "kernels-community/layer_norm:LlamaRMSNorm",
                ...
            },
            ...
        }
        For single device form local
        {
            "RMSNorm":
                "/abs/path:LlamaRMSNorm",
            ...
        },
        For multiple devices form local
        {
            "RMSNorm": {
                "cuda":
                    "/abs/path:LlamaRMSNorm",
                "rocm":
                    "/abs/path:LlamaRMSNorm",
                ...
            },
            ...
        }
        z7Kernel mapping must be a dict of the following format: z, got: zLayer z[ is not registered in the model, please register it first using use_kernel_forward_from_hubr1   r"   zKernel mapping for 'zl' must be a valid repo name with a layer name (e.g., 'org/repo:layer_name' or '/abs/path:layer_name'), got: r   r!   z'Kernel mapping must follow the format: N)
rO   
isinstancer>   dictr   r   itemsr?   valuesstr)rA   r   MAPPING_FORMATr%   kernelr   r(   s          r   sanitize_kernel_mapping$KernelConfig.sanitize_kernel_mapping|   s    #H 	))%0$--t44I.IYY`aefjfyfyaz`{|  #'"5"5";";"=J!<!<!C!C!EE ZL  )D  E  &#&&f$6(9$.zl  ;g  hn  go  p  ):
 FD)))/%F%MM(+hiohp)qrr%i55I9MQT\eQe(2:,  ?k  lu  kv  w  *8 !#J>JZZabhai!jkk- #>r-   c           	      L   SSK Jn  0 n[        U5      nU R                  R	                  5        H  u  pgUR
                  (       a  UR                  OUR                  nU(       a  XR                  -  n[        U[        5      (       a1  Un	U R                  (       d  [        XeXU5        Mz  [        XeXU5        M  [        U[        5      (       d  M  UR	                  5        H9  u  pX:w  a  M  U R                  (       d  [        XjXU5        M,  [        XjXU5        M;     M     X@l        g)a  
Transforms a simple kernel_mapping of the form:
    {
        "RMSNorm":
            "kernels-community/layer_norm:LlamaRMSNorm",
        ...
    },

    or for local path:

    {
        "RMSNorm":
            "/home/user/liger_kernels:LigerRMSNorm",
        ...
    },

into a nested mapping:

    {
        "RMSNorm": {
            "cuda": {
                Mode.INFERENCE: LayerRepository(
                    repo_id="kernels-community/layer_norm",
                    layer_name="LlamaRMSNorm",
                )
            }
        }
    }

    or for local path:

    {
        "RMSNorm": {
            "cuda": {
                Mode.INFERENCE: LocalLayerRepository(
                    repo_path=Path("/home/user/liger_kernels"),
                    package_name="liger_kernels",
                    layer_name="LigerRMSNorm",
                )
            }
        }
    }

that's compatible with the kernels library.

The device is inferred from the model's parameters if not provided.
The Mode is inferred from the model's training state.
r   )ModeN)r&   r\   r   r>   rT   trainingTRAINING	INFERENCETORCH_COMPILErR   rV   r@   r,   r7   rS   )rA   r   compiler\   r*   current_devicer%   rX   r)   r(   r   s              r   create_compatible_mapping&KernelConfig.create_compatible_mapping   s    b 	!%e,"&"5"5";";"=J$)NN4==D000&#&&"	,,":yPbc(YVhiFD)))/%F/ 00&z9L^_,ZRde *8 #>* 1r-   r=   )NFr<   )F)__name__
__module____qualname____firstlineno____doc__rB   rG   rO   rY   rc   __static_attributes__ r-   r   r9   r9   `   s%    1

M
QlfJ1r-   r9   N)r   r   r   r,   r7   r9   rk   r-   r   <module>rl      s(    #%P"*y1> y1r-   