Ë
    ÜÓ^j53  ã                   ó<   — d dl mZ d„ Z	 d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         úk/var/www/ramen.bs-engineer-server.com/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:Nc           	      ó¬   — 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ÚtpuúBOnly cuda, rocm, xpu, npu, neuron and tpu devices supported, got: ú:é   )Úrepo_idÚ
layer_namer   ÚrevisionÚtrust_remote_code)Úkernelsr   r   Úsplit)r&   r   Ú	repo_nameÚmodeÚcompatible_mappingr   r'   r(   r   Úrepo_layer_namer%   s              r   Úadd_to_mappingr/   :   sy   € õ (àÐDÑDÜÐ]Ð^dÐ]eÐfÓgÐgØ—o‘o cÓ*¨1Ñ-€OØ�o‰o˜cÓ" 1Ñ%€GàØ‘/ØØ*ØØ!Ø"3ôð
ð
&Ð�zÒ"ó    c                 ó¾   — ddl m} ddlm} |dvrt	        d|› �«      ‚|j                  d«      d   }|j                  d«      d   }|| | ||«      |¬«      ii|| <   y )	Nr   )ÚPath)ÚLocalLayerRepositoryr   r"   r#   r$   )Ú	repo_pathr&   )Úpathlibr2   r)   r3   r   r*   )	r&   r   r+   r,   r-   r2   r3   r.   r4   s	            r   Úadd_to_mapping_localr6   P   sv   € Ýå,àÐDÑDÜÐ]Ð^dÐ]eÐfÓgÐgØ—o‘o cÓ*¨1Ñ-€OØ—‘ Ó$ QÑ'€IàØÑ&Ù˜y›/Ø*ôð
ð&Ð�zÒ"r0   c                   ó6   — 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__h   s"   € Ø0>Ð0J™nÐPRˆÔØ&(ˆÔ#Ø 0ˆÕr0   c	           	      óL   — ddl m}	 || |	|||||¬«      ii| j                  |<   y )Nr   r   )r%   r&   r'   r   r(   )r)   r   r:   )
r=   r%   Úregistered_namer&   r   r,   r'   r   r(   r   s
             r   Úupdate_kernelzKernelConfig.update_kernelm   s<   € õ 	,ð Ø‘oØ#Ø)Ø%Ø#Ø&7ôðð
0
ˆ×Ñ˜OÒ,r0   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_names~   sA   € Ø!×/Ñ/Ó1ò 	M‰LˆD�&Ü�vÐ2Õ3Ø4:×4LÑ4L�×+Ñ+¨DÒ1ñ	Mr0   c           	      óÊ  — d}| j                  |«       t        | j                  t        «      s$t	        d|› dt        | j                  «      › �«      ‚| j                  j                  «       D �]t  \  }}|| j                  j                  «       vrt	        d|› d�«      ‚d}t        |t        t        f«      rd|i}d}t        |t        «      �r|j                  «       D ]ð  \  }}|}|s|d	vrt	        d
|› �«      ‚t        |t        «      rœ|\  }}	t        |	t        «      st	        dt        |	«      ›d|	› d�«      ‚|	j                  dd«      x}
�t        |
t        «      st	        d|
› �«      ‚|	j                  dd«      x}€&|	j                  dd«      x}	 €t	        d|›d|›�«      ‚t        |t        «      r	d|vsd|vsŒát	        d|› d|› �«      ‚ �Œft	        d|› d|› �«      ‚ y)ar  
        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 tuple with the same as string and a dict of {"revision"/"version/trust_remote_code": ...},
            - or a dict mapping device types ("cuda", "rocm", "xpu", "npu") to such values as above.
        3. Each device key in a dict is one of "cuda", "rocm", "xpu", or "npu".
        5. Each trust remote code key must be a bool.
        6. Each revision or version key must exist mutually exclusive if it has been passed explicitly.
        7. 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).
        8. If a local path is detected, it should be in the format '/abs/path:layer_name', where the absolute path points to the kernel repository, 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",
                ...
            },
            ...
        }
        You can also pass metadata along to inform about specific kernel information
        {
            "RMSNorm":
                ("kernels-community/layer_norm:LlamaRMSNorm", {"version": 1, "trust_remote_code": True}),
            ...
        },
        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_hubFNTr   r"   z^The passed metadata as second entry in a tuple needs to be a dict but found: type(metadata) = z for ú.r(   z6Expected a bool value for `trust_remote_code` but got r'   r   zŠExpected valid combination for version/revision (mutually exclusive but one of them) to be passed when passed as tuple, but got revision= z and version= ú/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: z'Kernel mapping must follow the format: )rH   Ú
isinstancer:   Údictr   r   Úitemsr;   ÚvaluesÚstrÚtupleÚgetÚbool)r=   r   ÚMAPPING_FORMATr&   ÚkernelÚskip_device_checkr   Úrepor+   Úmetadatar(   r'   r   s                r   Úsanitize_kernel_mappingz$KernelConfig.sanitize_kernel_mappingƒ   ss  € ð()ˆðT 	×)Ñ)¨%Ô0ä˜$×-Ñ-¬tÔ4ÜØIÈ.ÐIYÐY`ÔaeÐfj×fyÑfyÓazÐ`{Ð|óð ð #'×"5Ñ"5×";Ñ";Ó"=ó .	lÑˆJ˜Ø ×!<Ñ!<×!CÑ!CÓ!EÑEÜ Ø˜Z˜Lð  )Dð  Eóð ð !&ÐÜ˜&¤3¬ ,Ô/Ø ˜�Ø$(Ð!ä˜&¤$Õ'Ø$*§L¡L£Nò  ‘L�F˜DØ $�IÙ,°Ð?nÑ1nÜ(Ð+mÐntÐmuÐ)vÓwÐwô " $¬Ô.Ø.2Ñ+˜	 8ä)¨(´DÔ9Ü",ð!5Ü#'¨£>Ð"5°U¸8¸*ÀAð!Gó#ð ð
 2:·±Ð>QÐSWÓ1XÐXÐ-ÐeÜ#-Ð.?ÄÔ#FÜ&0Ø&\Ð]nÐ\oÐ$pó'"ð !"ð )1¯©°ZÀÓ(FÐF˜HÐOØ'/§|¡|°I¸tÓ'DÐD˜GØ!ðU"ô #-ð!YØNVÈ\ÐYhÐ_fÐ^ið!kó#ð ô
 & i´Ô5¸ÀIÑ9MÐQTÐ\eÒQeÜ(Ø2°:°,ð  ?kð  luð  kvð  wóð ò= ôD !Ð#JÈ>ÐJZÐZaÐbhÐaiÐ!jÓkÐkñ].	lr0   c                 ó\  — ddl m} i }t        |«      }| j                  j	                  «       D ]õ  \  }}|j
                  r|j                  n|j                  }|r||j                  z  }t        |t        t        f«      r||i}|j	                  «       D ]Ž  \  }	}
|	|k7  rŒ| j                  rt        ||	|
||«       Œ(t        |
t        «      r<|
\  }}|j                  dd«      }|j                  dd«      }|j                  dd«      }n|
}d}d}d}t        ||	||||||¬	«       Œ� Œ÷ || _        y)
a¥  
        Transforms a simple kernel_mapping of the form:
            {
                "RMSNorm":
                    ("kernels-community/layer_norm:LlamaRMSNorm", {"version": 1, "trust_remote_code": True}),
                ...
            },

            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",
                            version=1,
                            trust_remote_code=True,
                        )
                    }
                }
            }

            or for local path:

            {
                "RMSNorm": {
                    "cuda": {
                        Mode.INFERENCE: LocalLayerRepository(
                            repo_path=Path("/home/user/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   )ÚModer'   Nr   r(   Fr$   )r   r+   r,   r-   r   r'   r(   )r)   r[   r   r:   rN   ÚtrainingÚTRAININGÚ	INFERENCEÚTORCH_COMPILErL   rP   rQ   r<   r6   rR   r/   )r=   r   Úcompiler[   r-   Úcurrent_devicer&   rU   r,   r   rW   r+   rX   r'   r   r(   s                   r   Úcreate_compatible_mappingz&KernelConfig.create_compatible_mappingø   sD  € õd 	!àÐÜ% eÓ,ˆØ"&×"5Ñ"5×";Ñ";Ó"=ò &	ÑˆJ˜à$)§N¢N�4—=’=¸¿¹ˆDÙØ˜d×0Ñ0Ñ0�ä˜&¤3¬ ,Ô/Ø(¨&Ð1�à &§¡£ò ‘�˜Ø˜^Ò+Øà×(Ò(Ü(¨°V¸TÀ4ÐI[Ô\Øô ˜d¤EÔ*Ø*.Ñ'�I˜xØ'Ÿ|™|¨J¸Ó=�HØ&Ÿl™l¨9°dÓ;�GØ(0¯©Ð5HÈ%Ó(PÑ%à $�IØ#�HØ�GØ(-Ð%äØØ!Ø'ØØ'9Ø#Ø%Ø&7ö	ñ)ð&	ðP 1ˆÕr0   )NF)Nr$   F)F)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r>   rA   rH   rY   rb   © r0   r   r8   r8   c   s,   „ ñó1ð otó
ò"Mò
slôj^1r0   r8   )r$   NF)r   r   r   r/   r6   r8   rg   r0   r   ú<module>rh      s/   ðõ #ò%ðR joóò,ô&s1�>õ s1r0   