L iFdZddlmZmZmZerddlmZ ddZy)z7SpQR (Sparse-Quantized Representation) integration file)is_accelerate_availableis_spqr_availableis_torch_availableNc |g}trddlm}trddlm}|j D]\}}|g}|j|t|tjrCdj|dz|vr+5dj|} |j} | j} | d| vxr| d| vxr| d | vxr| d | v} | std | d | | d} | | d}| | d }| | d }|j}|j }j#|||j$|j&|j(| ||| |j*|<d}t-||j*|_|j*|j1ddddn t3t5|j7dkDrt9|||||\}}|j;d||fS#1swYYxYw)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.) rowscolsbitsbeta1beta2dense_weights_shaperow_offsets_shapecol_vals_shape in_perm_shapeTF)quantization_configmodules_to_not_convertcurrent_key_namehas_been_replaced)r acceleraterr spqr_quantr named_childrenappend isinstancennLinearjoinshapeskeys ValueError in_features out_featurescreate_placehodlerr rr_modulestype source_clsrequires_grad_lenlistchildrenreplace_with_spqr_linearpop)modelrrrrrr namemodule tensor_namer! shapes_keys shapes_validrrrrr$r%_s d/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/transformers/integrations/spqr.pyr.r.s4%!# 1.,,.@! f  #! % fbii (xx()I5=SS'),?"%((+;"+MJ*m?;{JJ +m>:kI !((11< >LM +1K=@T1U*V'(.+>P/Q(R%%+{m?,K%LN$*k].+I$JM"("4"4K#)#6#6L+:+M+M)(055177177,?*;'5&3,N ,ENN4()-%7;6lENN4(3NN4(77>Y,?,?\ tFOO%& '! +#;$7'=!1"3 $ A  R A@!B # ##s,?,?s DG??H )NNNF)__doc__utilsrrrtorch.nnrr.r7r=s/:RR  c$r<