o
    h                     @   sD   d Z ddlmZmZmZmZ e rddlmZ 				dddZdS )	z?AQLM (Additive Quantization of Language Model) integration file   )ACCELERATE_MIN_VERSIONis_accelerate_availableis_aqlm_availableis_torch_available    NFc                 C   sJ  t  stdt stdt d|du rg }ddlm} ddlm} |  D ]x\}}|du r2g }|	| t
|tjrd|d	 |vr| 5 |j}	|j}
||	|
|jdu|j|j|j|jd
| j|< d}t|| j| _| j| d W d   n1 sw   Y  tt| dkrt|||||d\}}|d q(| |fS )ao  
    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 successfull 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_sizeout_group_sizenum_codebooksnbits_per_codebookTF)quantization_configlinear_weights_not_to_quantizecurrent_key_namehas_been_replaced)r   
ValueErrorr   r   
accelerater   aqlmr   named_childrenappend
isinstancennLinearjoinin_featuresout_featuresr
   r   r   r   r   _modulestype
source_clsrequires_grad_lenlistchildrenreplace_with_aqlm_linearpop)modelr   r   r   r   r   r   namemoduler   r   _ r,   r/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/integrations/aqlm.pyr&      sT   

	
r&   )NNNF)	__doc__utilsr   r   r   r   torch.nnr   r&   r,   r,   r,   r-   <module>   s   