o
    Gôj•  ã                   @   s@   d Z ddlmZmZmZ eƒ rddlmZ 				ddd„ZdS )	z7SpQR (Sparse-Quantized Representation) integration fileé   )Úis_accelerate_availableÚis_spqr_availableÚis_torch_availableé    NFc                 C   sÐ  |du rg }t ƒ rddlm} tƒ rddlm} |  ¡ D ]Ç\}}|du r&g }| |¡ t|t	j
ƒrÉd |¡d |vrÈ|ƒ �‚ d |¡}	|j}
|
 ¡ }|	› d�|v oe|	› d�|v oe|	› d	�|v oe|	› d
�|v }|sptd|	› d�ƒ‚|
|	› d� }|
|	› d� }|
|	› d	� }|
|	› d
� }|j}|j}|j|||j|j|j||||d�	| j|< d}t|ƒ| j| _| j|  d¡ W d  ƒ n1 sÂw   Y  n	 tt| ¡ ƒƒdkrÞt|||||d�\}}| d¡ q| |fS )af  
    Public method that recursively replaces the Linear layers of the given model with SpQR quantized layers.
    `accelerate` is needed to use this method. Returns the converted model and a boolean that indicates if the
    conversion has been successful or not.

    Args:
        model (`torch.nn.Module`):
            The model to convert, can be any `torch.nn.Module` instance.
        quantization_config (`SpQRConfig`):
            The quantization config object that contains the quantization parameters.
        modules_to_not_convert (`list[str]`, *optional*):
            A list of nn.Linear weights to not convert. If a parameter path is in the list (e.g. `lm_head.weight`), the corresponding module will not be
            converted.
        current_key_name (`list`, *optional*):
            A list that contains the current key name. This is used for recursion and should not be passed by the user.
        has_been_replaced (`bool`, *optional*):
            A boolean that indicates if the conversion has been successful or not. This is used for recursion and
            should not be passed by the user.
    Nr   )Úinit_empty_weights)ÚQuantizedLinearÚ.z.weightz.dense_weights.shapez.row_offsets.shapez.col_vals.shapez.in_perm.shapezJThe SpQR quantization config does not contain the shape configuration for zG. This indicates that the configuration is either invalid or corrupted.)	ÚrowsÚcolsÚbitsÚbeta1Úbeta2Údense_weights_shapeÚrow_offsets_shapeÚcol_vals_shapeÚin_perm_shapeTF)Úquantization_configÚmodules_to_not_convertÚcurrent_key_nameÚhas_been_replacedéÿÿÿÿ)r   Ú
accelerater   r   Z
spqr_quantr   Únamed_childrenÚappendÚ
isinstanceÚnnÚLinearÚjoinÚshapesÚkeysÚ
ValueErrorÚin_featuresÚout_featuresZcreate_placehodlerr   r   r   Ú_modulesÚtypeÚ
source_clsÚrequires_grad_ÚlenÚlistÚchildrenÚreplace_with_spqr_linearÚpop)Úmodelr   r   r   r   r   r   ÚnameÚmoduleÚtensor_namer   Zshapes_keysZshapes_validr   r   r   r   r!   r"   Ú_© r1   úq/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/transformers/integrations/spqr.pyr*      s|   

ÿþüÿÿ÷Ô€.
ûr*   )NNNF)Ú__doc__Úutilsr   r   r   Útorch.nnr   r*   r1   r1   r1   r2   Ú<module>   s   û