
    7i0(                     R    d dl mZmZ  e       rddlZd Zd Zd Z G d de      Zy)	   )PushToHubMixinis_torch_available    Nc                     d}	 t        | j                               }|j                  j
                  }|dk(  rt        j                  j                  y|S # t        $ r t        d|       w xY w)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: cudarocm)	next
parametersStopIteration
ValueErrordevicetypetorchversionhip)modelEXAMPLE_MAPPINGparamdev_types       \/mnt/e/genesis-system/.venv/lib/python3.12/site-packages/transformers/utils/kernel_config.pyinfer_devicer      s|    	O
U%%'( ||  H6==(O  
^_n^op
 	

s   A A-c                     ddl m} |dvrt        d|       |j                  d      d   }|j                  d      d   }|| |||      ii|| <   y )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   sn    '33PQWPXYZZooc*1-Oooc"1%G/*
&z"    c                     ddl m} ddlm} |dvrt	        d|       |j                  d      d   }|j                  d      d   }|j                  d      d	   }	|| | ||      |	|
      ii|| <   y )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PQWPXYZZooc*1-O$Q'I!,R0&y/.*
&z"r*   c                   8    e Zd ZdZi dfdZd	dZd Zd Zd
dZy)KernelConfigze
    Kernel configuration class. This class is used to configure the kernel mapping for a model.
    Fc                 .    || _         i | _        || _        y N)kernel_mappingregistered_layer_namesuse_local_kernel)selfr9   r;   s      r   __init__zKernelConfig.__init__d   s    ,&(# 0r*   Nc                 H    ddl m} || ||||      ii| j                  |<   y )Nr   r   )r!   r"   revision)r#   r   r9   )r<   r!   registered_namer"   r   r&   r?   r   s           r   update_kernelzKernelConfig.update_kerneli   s4    + o#)%0
O,r*   c                     |j                         D ]+  \  }}t        |d      s|j                  | j                  |<   - y )Nkernel_layer_name)named_moduleshasattrrC   r:   )r<   r   namemodules       r   store_registered_layer_namesz)KernelConfig.store_registered_layer_namesv   sA    !//1 	MLD&v234:4L4L++D1	Mr*   c                 |   d}| j                  |       t        | j                  t              s$t	        d| dt        | j                               | j                  j                         D ]  \  }}|| j                  j                         vrt	        d| d      t        |t              rd|vsd|vsJt	        d| d	|       t        |t              rT|j                         D ]@  \  }}|d
vrt	        d|       t        |t              r	d|vsd|vs1t	        d| d	|        t	        d| d|        y)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$--t4I.IYY`aefjfyfyaz`{|  #'"5"5";";"= 	lJ!<!<!C!C!EE ZL  )I  J  &#&f$6(9$.zl  ;g  hn  go  p  FD))/ %FI%CC(+`ag`h)ijj%i5I9MQT\eQe(2:,  ?k  lu  kv  w  !#J>JZZabhai!jkk-	lr*   c           	         ddl m} i }t        |      }| j                  j	                         D ]  \  }}|j
                  r|j                  n|j                  }|r||j                  z  }t        |t              r.|}	| j                  st        |||	||       it        |||	||       yt        |t              s|j	                         D ]6  \  }
}	|
|k7  r| j                  st        ||
|	||       (t        ||
|	||       8  || _        y)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"5";";"= 	fJ$)NN4==Dd000&#&"	,,":~y$Pbc(^YPTVhiFD))/ f%FI/ 00&z69dL^_,ZDRdef	f* 1r*   r8   )F)	__name__
__module____qualname____doc__r=   rA   rH   rQ   rZ    r*   r   r6   r6   _   s,     ')5 1

M
QlfJ1r*   r6   )utilsr   r   r   r   r)   r4   r6   r_   r*   r   <module>ra      s3    7  F"*y1> y1r*   