
    rh                     F    d Z ddlmZmZmZ  e       rddlmZ 	 	 	 	 ddZy)z7SpQR (Sparse-Quantized Representation) integration file   )is_accelerate_availableis_spqr_availableis_torch_available    Nc                    |g }t               rddlm} t               rddlm} | j                         D ]  \  }}|g }|j                  |       t        |t        j                        rCdj                  |      dz   |vr+        5  dj                  |      }	|j                  }
|
j                         }|	 d|v xr |	 d|v xr |	 d	|v xr |	 d
|v }|st        d|	 d      |
|	 d   }|
|	 d   }|
|	 d	   }|
|	 d
   }|j                  }|j                   }j#                  |||j$                  |j&                  |j(                  ||||	      | j*                  |<   d}t-        |      | j*                  |   _        | j*                  |   j1                  d       ddd       n	 t3        t5        |j7                                     dkD  rt9        |||||      \  }}|j;                  d        | |fS # 1 sw Y   YxY w)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                       q/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/transformers/integrations/spqr.pyr.   r.      s   4 %!# 1.,,. @!f#!%fbii(xx()I5=SS') ,?"%((+;"<K077F"(++-K '-';<K J*m+=>+MJ*m?;{JJ  +m>:kI	 ! ((11< >LM  +1K=@T1U*V'(.+>P/Q(R%%+{m?,K%LN$*k].+I$JM"("4"4K#)#6#6L+:+M+M)(055177177,?*;'5&3 ,N 
,ENN4( )-% 7;6lENN4(3NN4(77>Y,? ,?\ tFOO%&'!+#;$7'=!1"3$ A  	R A@!B ###s,? ,?s   
DG??H	)NNNF)__doc__utilsr   r   r   torch.nnr   r.        r7   <module>r=      s/    : R R 
 c$r<   