
     `i                     N    d Z ddlmZmZmZ  e            rddlmZ 	 	 	 	 ddZdS )z7SpQR (Sparse-Quantized Representation) integration file   )is_accelerate_availableis_spqr_availableis_torch_available    NFc                 ^   |g }t                      rddlm} t                      rddlm} |                                 D ]\  }}|g }|                    |           t          |t          j
                  rPd                    |          dz   |vr4 |            5  d                    |          }	|j        }
|
                                }|	 d|v o|	 d|v o|	 d	|v o|	 d
|v }|st          d|	 d          |
|	 d         }|
|	 d         }|
|	 d	         }|
|	 d
         }|j        }|j        }|                    |||j        |j        |j        ||||	  	        | j        |<   d}t-          |          | j        |         _        | j        |                             d           ddd           n# 1 swxY w Y   n	 t3          t5          |                                                    dk    rt9          |||||          \  }}|                    d           | |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   
spqr_quantr	   named_childrenappend
isinstancennLinearjoinshapeskeys
ValueErrorin_featuresout_features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!   shapes_keysshapes_validr   r   r   r   r$   r%   _s                       r/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/integrations/spqr.pyr.   r.      s?   4 %!#   2111111 /......,,.. @! @!f#!%%%fbi(( 1	xx())I5=SSS'')) ,? ,?"%((+;"<"<K07F"(++--K '<<<K J*>>>+MJ*;;;{JJ  +:::kI	 ! ( (M1<M M M   +1K1U1U1U*V'(.+/Q/Q/Q(R%%+{,K,K,K%LN$*k+I+I+I$JM"("4K#)#6L+:+M+M)(051717,?*;'5&3 ,N 
, 
,EN4( )-% 7;6llEN4(3N4(77>>>Y,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,? ,?\ tFOO%%&&''!++#;$7'=!1"3$ $ $ A  	R    ###s    DF>>G	G	)NNNF)__doc__utilsr   r   r   torch.nnr   r.        r7   <module>r=      s    : 9 R R R R R R R R R R  
 c$ c$ c$ c$ c$ c$r<   