
    i(                     8    d dl mZ d Zd Zd Z G d de      Zy)   )PushToHubMixinc                     d}	 t        | j                               }|j                  j
                  }|dk(  r(ddlm}  |       rddl}t        |d      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: 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/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/transformers/utils/kernel_config.pyinfer_devicer      s    	O
U%%'( ||  H6.ui(,,8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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,   :   sn    '==XY_X`abb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&   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/.*
&z"r-   c                   4    e Zd ZdZd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.
    Nc                 6    ||ni | _         i | _        || _        y N)kernel_mappingregistered_layer_namesuse_local_kernel)selfr<   r>   s      r   __init__zKernelConfig.__init__e   s"    0>0JnPR&(# 0r-   c                 H    ddl m} || ||||      ii| j                  |<   y )Nr   r   )r$   r%   revision)r&   r   r<   )r?   r$   registered_namer%   r   r)   rB   r   s           r   update_kernelzKernelConfig.update_kernelj   s4    + o#)%0
O,r-   c                     |j                         D ]+  \  }}t        |d      s|j                  | j                  |<   - y )Nkernel_layer_name)named_moduleshasattrrF   r=   )r?   r   namemodules       r   store_registered_layer_namesz)KernelConfig.store_registered_layer_namesw   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 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)
rK   
isinstancer<   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  )D  E  &#&f$6(9$.zl  ;g  hn  go  p  FD))/ %FI%MM(+hiohp)qrr%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&   rV   r   r<   rO   trainingTRAINING	INFERENCETORCH_COMPILErM   rQ   r>   r,   r7   rN   )r?   r   compilerV   r*   current_devicer%   rS   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-   )NFr;   )F)	__name__
__module____qualname____doc__r@   rD   rK   rT   r]    r-   r   r9   r9   `   s%    1

M
QlfJ1r-   r9   N)r   r   r   r,   r7   r9   rb   r-   r   <module>rc      s(    #%P"*y1> y1r-   