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