§
    ‚Štj53  ã                   óD   — d dl mZ d„ Z	 d
d„Zd„ Z G d„ d	e¦  «        ZdS )é   )ÚPushToHubMixinc                 ó
  — d}	 t          |                      ¦   «         ¦  «        }n # t          $ r t          d|› �¦  «        ‚w xY w|j        j        }|dk    r+ddlm}  |¦   «         rddl}t          |d¦  «        j
        �d	S |S )
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         ú^/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/utils/kernel_config.pyÚinfer_devicer      sÂ   € ð	€Oð
Ý�U×%Ò%Ñ'Ô'Ñ(Ô(ˆˆøÝð 
ð 
ð 
ÝØpÐ_nÐpÐpñ
ô 
ð 	
ð
øøøð
 Œ|Ô €HØ�6ÒÐà.Ð.Ð.Ð.Ð.Ð.àÐÑÔð 	ØˆLˆLˆLå�u˜iÑ(Ô(Ô,Ð8Ø�và€Os	   „!& ¦Aé   NFc           	      óØ   — ddl m} |dvrt          d|› �¦  «        ‚|                     d¦  «        d         }	|                     d¦  «        d         }
|| ||
|	|||¬¦  «        ii|| <   d S )Nr   ©ÚLayerRepository©r   r	   ÚxpuÚnpuÚneuronÚtpuúBOnly cuda, rocm, xpu, npu, neuron and tpu devices supported, got: ú:r   )Ú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/   :   s¦   € ð (Ð'Ð'Ð'Ð'Ð'àÐDÐDÐDÝÐfÐ^dÐfÐfÑgÔ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|› �¦  «        ‚|                     d¦  «        d         }|                     d¦  «        d         }|| | ||¦  «        |¬¦  «        ii|| <   d S )	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   s·   € ØÐÐÐÐÐà,Ð,Ð,Ð,Ð,Ð,àÐDÐDÐDÝÐfÐ^dÐfÐfÑgÔgÐgØ—o’o cÑ*Ô*¨1Ô-€OØ—’ Ñ$Ô$ QÔ'€IàØÐ&Ð&Ø˜$˜y™/œ/Ø*ðñ ô ð
ð&Ð�zÑ"Ð"Ð"r0   c                   ó8   — e Zd ZdZd
d„Z	 dd„Zd„ Zd„ Zdd	„ZdS )ÚKernelConfigze
    Kernel configuration class. This class is used to configure the kernel mapping for a model.
    NFc                 ó8   — |�|ni | _         i | _        || _        d S )N)Úkernel_mappingÚregistered_layer_namesÚuse_local_kernel)Úselfr:   r<   s      r   Ú__init__zKernelConfig.__init__h   s*   € Ø0>Ð0J˜n˜nÐPRˆÔØ&(ˆÔ#Ø 0ˆÔÐÐr0   r   c	           	      óJ   — ddl m}	 || |	|||||¬¦  «        ii| j        |<   d S )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   sZ   € ð 	,Ð+Ð+Ð+Ð+Ð+ð Ø�o�oØ#Ø)Ø%Ø#Ø&7ðñ ô ðð
0
ˆÔ˜OÑ,Ð,Ð,r0   c                 óx   — |                      ¦   «         D ]$\  }}t          |d¦  «        r|j        | j        |<   Œ%d S )NÚkernel_layer_name)Únamed_modulesÚhasattrrC   r;   )r=   r   ÚnameÚmodules       r   Ústore_registered_layer_namesz)KernelConfig.store_registered_layer_names~   sT   € Ø!×/Ò/Ñ1Ô1ð 	Mð 	M‰LˆD�&Ý�vÐ2Ñ3Ô3ð MØ4:Ô4L�Ô+¨DÑ1øð	Mð 	Mr0   c           	      óp  — d}|                       |¦  «         t          | j        t          ¦  «        s't	          d|› dt          | j        ¦  «        › �¦  «        ‚| j                             ¦   «         D �]Â\  }}|| j                             ¦   «         vrt	          d|› d�¦  «        ‚d}t          |t          t          f¦  «        rd|i}d}t          |t          ¦  «        �rA|                     ¦   «         D �])\  }}|}|s|d	vrt	          d
|› �¦  «        ‚t          |t          ¦  «        rÂ|\  }}	t          |	t          ¦  «        s#t	          dt          |	¦  «        ›d|	› d�¦  «        ‚|	                     dd¦  «        x}
�'t          |
t          ¦  «        st	          d|
› �¦  «        ‚|	                     dd¦  «        x}€.|	                     dd¦  «        x}	 €t	          d|›d|›�¦  «        ‚t          |t          ¦  «        rd|vsd|vrt	          d|› d|› �¦  «        ‚�Œ+�Œ¯t	          d|› d|› �¦  «        ‚dS )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ƒ   s2  € ð()ˆðT 	×)Ò)¨%Ñ0Ô0Ð0å˜$Ô-­tÑ4Ô4ð 	ÝØ|È.Ð|Ð|ÕaeÐfjÔfyÑazÔazÐ|Ð|ñô ð ð #'Ô"5×";Ò";Ñ"=Ô"=ð .	lñ .	lÑˆJ˜Ø Ô!<×!CÒ!CÑ!EÔ!EÐEÐEÝ ð E˜Zð  Eð  Eð  Eñô ð ð !&ÐÝ˜&¥3­ ,Ñ/Ô/ð )Ø ˜�Ø$(Ð!å˜&¥$Ñ'Ô'ñ #lØ$*§L¢L¡N¤Nð  ñ  ‘L�F˜DØ $�IØ,ð x°Ð?nÐ1nÐ1nÝ(Ð)vÐntÐ)vÐ)vÑwÔwÐwõ " $­Ñ.Ô.ð Ø.2Ñ+˜	 8å)¨(µDÑ9Ô9ð Ý",ð!GÝ#'¨¡>¤>ð!Gð !GØ;Cð!Gð !Gð !Gñ#ô #ð ð
 2:·²Ð>QÐSWÑ1XÔ1XÐXÐ-ÐeÝ#-Ð.?ÅÑ#FÔ#Fð "Ý&0Ø$pÐ]nÐ$pÐ$pñ'"ô '"ð !"ð )1¯ª°ZÀÑ(FÔ(FÐF˜HÐOØ'/§|¢|°I¸tÑ'DÔ'DÐD˜GØ!ðU"õ #-ð!kØNVð!kð !kØ_fð!kð !kñ#ô #ð õ
 & iµÑ5Ô5ð ¸ÀIÐ9MÐ9MÐQTÐ\eÐQeÐQeÝ(ð w°:ð  wð  wð  luð  wð  wñô ð ñ Rfñ; õD !Ð!jÈ>Ð!jÐ!jÐbhÐ!jÐ!jÑkÔkÐkð].	lð .	lr0   c                 ór  — ddl m} i }t          |¦  «        }| j                             ¦   «         D ]þ\  }}|j        r|j        n|j        }|r
||j        z  }t          |t          t          f¦  «        r||i}|                     ¦   «         D ]£\  }	}
|	|k    rŒ| j        rt          ||	|
||¦  «         Œ't          |
t          ¦  «        rH|
\  }}|                     dd¦  «        }|                     dd¦  «        }|                     dd¦  «        }n|
}d}d}d}t          ||	||||||¬	¦  «         Œ¤Œÿ|| _        dS )
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ø   sž  € ðd 	!Ð Ð Ð Ð Ð àÐÝ% eÑ,Ô,ˆØ"&Ô"5×";Ò";Ñ"=Ô"=ð &	ð &	ÑˆJ˜à$)¤NÐF�4”=�=¸¼ˆDØð 1Ø˜dÔ0Ñ0�å˜&¥3­ ,Ñ/Ô/ð 2Ø(¨&Ð1�à &§¢¡¤ð ð ‘�˜Ø˜^Ò+Ð+ØàÔ(ð Ý(¨°V¸TÀ4ÐI[Ñ\Ô\Ð\Øõ ˜d¥EÑ*Ô*ð 	.Ø*.Ñ'�I˜xØ'Ÿ|š|¨J¸Ñ=Ô=�HØ&Ÿlšl¨9°dÑ;Ô;�GØ(0¯ªÐ5HÈ%Ñ(PÔ(PÐ%Ð%à $�IØ#�HØ�GØ(-Ð%åØØ!Ø'ØØ'9Ø#Ø%Ø&7ð	ñ 	ô 	ð 	ð 	ð)ð> 1ˆÔÐÐr0   )NF)Nr   F)F)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r>   rA   rH   rY   rb   © r0   r   r8   r8   c   s�   € € € € € ðð ð1ð 1ð 1ð 1ð otð
ð 
ð 
ð 
ð"Mð Mð Mð
slð slð slðj^1ð ^1ð ^1ð ^1ð ^1ð ^1r0   r8   )r   NF)r   r   r   r/   r6   r8   rg   r0   r   ú<module>rh      s‘   ðð #Ð "Ð "Ð "Ð "Ð "ð%ð %ð %ðR joðð ð ð ð,ð ð ð&s1ð s1ð s1ð s1ð s1�>ñ s1ô s1ð s1ð s1ð s1r0   