ó
    qyüiÈ(  ã                   ó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         Ú]/home/mande/repo/quber/.venv/lib/python3.13/site-packages/transformers/utils/kernel_config.pyÚinfer_devicer      sŽ   € ð	€Oð
Ü�U×%Ñ%Ó'Ó(ˆð �|‰|× Ñ €HØ�6Óå.á×ÑÛä�u˜iÓ(×,Ñ,Ñ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   € Ý'àÐ=Ó=ÜÐXÐY_ÐX`ÐaÓbÐbØ—o‘o cÓ*¨1Ñ-€OØ�o‰o˜cÓ" 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‹   € Ýå,àÐ=Ó=ÜÐXÐY_ÐX`ÐaÓbÐbØ—o‘o cÓ*¨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>Ñ0J™nÐPRˆÔØ&(ˆÔ#Ø 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;   € Ø!×/Ñ/Ö1‰LˆDÜ�vÐ2×3Ó3Ø4:×4LÑ4L�×+Ñ+¨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È.ÐIYÐY`ÔaeÐfj×fyÑfyÓazÐ`{Ð|óð ð #'×"5Ñ"5×";Ñ";Ö"=ÑˆJØ×!<Ñ!<×!CÑ!CÓ!EÓEÜ Ø˜Z˜Lð  )Dð  Eóð ô ˜&¤#×&Ñ&Ø˜fÓ$¨°6Ó(9Ü$Ø.¨z¨lð  ;gð  hnð  goð  póð ñ ):ô
 ˜F¤D×)Ñ)Ø)/¯©®Ñ%�FØÐ%MÓMÜ(Ð+hÐioÐhpÐ)qÓrÐrä% i´×5Ñ5¸ÀIÓ9MÐQTÐ\eÕQeÜ(Ø2°:°,ð  ?kð  luð  kvð  wóð ó *8ô !Ð#JÈ>ÐJZÐZaÐbhÐaiÐ!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  XƒR                  -  n[        U[        5      (       a1  Un	U R                  (       d  [        XeX˜U5        Mz  [        XeX˜U5        M‰  [        U[        5      (       d  M   UR	                  5        H9  u  p©X¥:w  a  M  U R                  (       d  [        XjX˜U5        M,  [        XjX˜U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à$)§N§N�4—=’=¸¿¹ˆDÞØ×0Ñ0Ñ0�ä˜&¤#×&Ñ&Ø"�	Ø×,×,Ü" :¸yÐPbÖcä(¨ÀYÐVhÖiÜ˜F¤D×)Ó)Ø)/¯©®Ñ%�FØÓ/Ù Ø×0×0Ü& z¸9ÐL^Ö_ä,¨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-   