L iJdZddlmZmZmZmZerddlmZ ddZy)z?AQLM (Additive Quantization of Language Model) integration file)ACCELERATE_MIN_VERSIONis_accelerate_availableis_aqlm_availableis_torch_availableNc `ts tdtstdtd|g}ddlm}ddlm}|jD]I\}}|g}|j|t|tjrdj|d z|vr|5|j} |j} || | |j du|j"|j$|j&|j( |j*|<d }t-||j*|_|j*|j1d dddt3t5|j7dkDrt9||||| \} }|j;dL||fS#1swYYxYw)an Public method that recursively replaces the Linear layers of the given model with AQLM 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 (`AqlmConfig`): The quantization config object that contains the quantization parameters. linear_weights_not_to_quantize (`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. zIAQLM is not available. Please install it with `pip install aqlm[cpu,gpu]`zDAQLM requires Accelerate to be installed: `pip install 'accelerate>=z'`Nr)init_empty_weights)QuantizedLinear.z.weight)bias in_group_sizeout_group_size num_codebooksnbits_per_codebookTF)quantization_configlinear_weights_not_to_quantizecurrent_key_namehas_been_replaced)r ValueErrorrr accelerater aqlmr named_childrenappend isinstancennLinearjoin in_features out_featuresr r rrr_modulestype source_clsrequires_grad_lenlistchildrenreplace_with_aqlm_linearpop) modelrrrrr r namemodulerr _s d/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/transformers/integrations/aqlm.pyr(r(s4  dee " $RSiRjjl m  &-)+&-$,,.$! f  #! % fbii (xx()I5=[[')?"("4"4K#)#6#6L+:#$#[[4&9&G&G':'I'I&9&G&G+>+Q+Q,ENN4()-%7;6lENN4(3NN4(77>%?& tFOO%& '! +#;$7/M!1"3 $ A  R I$!J # ##;??s &B&F$$F- )NNNF) __doc__utilsrrrrtorch.nnrr(r.r4s0Bjj #' M$r3