
    bi0(                     `    d dl mZmZ  e            rddlZd Zd Zd Z G d de          ZdS )	   )PushToHubMixinis_torch_available    Nc                     d}	 t          |                                           }n # t          $ r t          d|           w xY w|j        j        }|dk    rt          j        j        dS |S )z
    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: cudaNrocm)	next
parametersStopIteration
ValueErrordevicetypetorchversionhip)modelEXAMPLE_MAPPINGparamdev_types       [/root/projects/butler/venv/lib/python3.11/site-packages/transformers/utils/kernel_config.pyinfer_devicer      s    	O
U%%''(( 
 
 
p_npp
 
 	


 | H6=(6Os	   !& Ac                     ddl m} |dvrt          d|           |                    d          d         }|                    d          d         }|| |||          ii|| <   d S )Nr   LayerRepositoryr   r   xpunpu5Only cuda, rocm, xpu and npu 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)   9   s    ''''''333YQWYYZZZooc**1-Oooc""1%G//*  
&z"""    c                 (   ddl m} ddlm} |dvrt	          d|           |                    d          d         }|                    d          d         }|                    d          d	         }	|| | ||          |	|
          ii|| <   d S )Nr   )Path)LocalLayerRepositoryr   r   r   r    /)	repo_pathpackage_namer"   )pathlibr,   r#   r-   r   r$   )
r"   r   r%   r&   r'   r,   r-   r(   r0   repo_package_names
             r   add_to_mapping_localr4   J   s    ,,,,,,333YQWYYZZZooc**1-O$$Q'I!,,R0&&$y//.*  
&z"""r*   c                   :    e Zd ZdZi dfdZd	dZd Zd Zd
dZdS )KernelConfigze
    Kernel configuration class. This class is used to configure the kernel mapping for a model.
    Fc                 0    || _         i | _        || _        d S N)kernel_mappingregistered_layer_namesuse_local_kernel)selfr9   r;   s      r   __init__zKernelConfig.__init__d   s     ,&(# 0r*   Nc                 F    ddl m} || ||||          ii| j        |<   d S )Nr   r   )r!   r"   revision)r#   r   r9   )r<   r!   registered_namer"   r   r&   r?   r   s           r   update_kernelzKernelConfig.update_kerneli   sR    ++++++ oo#)%  0
O,,,r*   c                 x    |                                 D ]$\  }}t          |d          r|j        | j        |<   %d S )Nkernel_layer_name)named_moduleshasattrrC   r:   )r<   r   namemodules       r   store_registered_layer_namesz)KernelConfig.store_registered_layer_namesv   sT    !//11 	M 	MLD&v233 M4:4L+D1	M 	Mr*   c                    d}|                      |           t          | j        t                    s't	          d| dt          | j                             | j                                        D ]\  }}|| j                                        vrt	          d| d          t          |t                    rd|vsd|vrt	          d| d	|           ft          |t                    rc|                                D ]M\  }}|d
vrt	          d|           t          |t                    rd|vsd|vrt	          d| d	|           Nt	          d| d|           dS )aJ  
        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 register_kernel_forward_from_hubr.   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)
rH   
isinstancer9   dictr   r   itemsr:   valuesstr)r<   r   MAPPING_FORMATr"   kernelr   r%   s          r   sanitize_kernel_mappingz$KernelConfig.sanitize_kernel_mapping{   s,    #H 	))%000$-t44 	|.||aefjfyazaz||   #'"5";";"="= 	l 	lJ!<!C!C!E!EEE  JZ  J  J  J   &#&& lf$$6(9(9$ pz  p  p  hn  p  p   ):
 FD)) 
l)/  %FI%CCC()iag)i)ijjj%i55 I9M9MQT\eQeQe( w:  w  w  lu  w  w   Rf	 !!j>!j!jbh!j!jkkk-	l 	lr*   c           	         ddl m} i }t          |          }| j                                        D ]\  }}|j        r|j        n|j        }|r
||j        z  }t          |t                    r1|}	| j        st          |||	||           Xt          |||	||           lt          |t                    rO|                                D ]:\  }
}	|
|k    r| j        st          ||
|	||           't          ||
|	||           ;|| _        dS )aW  
        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#   rS   r   r9   rL   trainingTRAINING	INFERENCETORCH_COMPILErJ   rN   r;   r)   r4   rK   )r<   r   compilerS   r'   current_devicer"   rP   r&   r%   r   s              r   create_compatible_mappingz&KernelConfig.create_compatible_mapping   s_   b 	!     %e,,"&"5";";"="= 	f 	fJ$)NF4==D 1d00&#&& f"	, j":~y$Pbcccc(^YPTVhiiiiFD)) f)/ f f%FI// 0 f&z69dL^____,ZDRdeeee0r*   r8   )F)	__name__
__module____qualname____doc__r=   rA   rH   rQ   rZ    r*   r   r6   r6   _   s          ')5 1 1 1 1

 
 
 
M M M
Ql Ql QlfJ1 J1 J1 J1 J1 J1r*   r6   )utilsr   r   r   r   r)   r4   r6   r_   r*   r   <module>ra      s    7 6 6 6 6 6 6 6  LLL     F  "  *y1 y1 y1 y1 y1> y1 y1 y1 y1 y1r*   