o
    h                     @   sn   d dl mZmZmZ e rddlZddlmZ e r ddlmZ e	e
Z					d
ddZ	ddd	ZdS )   )is_accelerate_availableis_eetq_availablelogging    N)init_empty_weightsFc              	      s  |du rg }|   D ]}\}}|| t|tjrl||vrld| t fdd|D slt 4 |j}|j	}	t
||	|jdu|jj| j|< |rS| j| |jj d}| j| d W d   n1 sgw   Y  tt| dkrt||||||d\}
}|d	 q
| |fS )
z
    Private method that wraps the recursion for module replacement.

    Returns the converted model and a boolean that indicates if the conversion has been successfull or not.
    N.c                 3   s$    | ]}|d   v p| kV  qdS )r   N ).0keycurrent_key_name_strr   r/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/transformers/integrations/eetq.py	<genexpr>2   s    
z,_replace_with_eetq_linear.<locals>.<genexpr>TFr   )has_been_replacedpre_quantized)named_childrenappend
isinstancennLinearjoinanyr   in_featuresout_featureseetq
EetqLinearbiasweightdevice_modulesregister_scalerequires_grad_lenlistchildren_replace_with_eetq_linearpop)modelmodules_to_not_convertcurrent_key_namequantization_configr   r   namemoduler   r   _r   r   r   r&      s>   



	r&   c                 C   s\   |du rdgn|}|j dur||j  tt|}t| ||||d\} }|s,td | S )a  
    A helper function to replace all `torch.nn.Linear` modules by `eetq.EetqLinear` modules from the `eetq`
    library. This will enable running your models using high performance int8 weight-only gemm kerner from
    FasterTransformer and TensorRT-LLM. Make sure `eetq` compiled with the correct CUDA
    version of your hardware is installed before running this function. EETQ shall be installed via the source
    'https://github.com/NetEase-FuXi/EETQ'

    The function will be run recursively and replace all `torch.nn.Linear` modules except for the `lm_head` that should
    be kept as a `torch.nn.Linear` module. The replacement is done under `init_empty_weights` context manager so no
    CPU/GPU memory is required to run this function. Each weight will be quantized along the channel.

    Parameters:
        model (`torch.nn.Module`):
            Input model or `torch.nn.Module` as the function is run recursively.
        modules_to_not_convert (`List[`str`]`, *optional*, defaults to `["lm_head"]`):
            Names of the modules to not convert in `EetqLinear`. In practice we keep the `lm_head` in full precision
            for numerical stability reasons.
        current_key_name (`List[`str`]`, *optional*):
            An array to track the current key of the recursion. This is used to check whether the current key (part of
            it) is not in the list of modules to not convert (for instances modules that are offloaded to `cpu` or
            `disk`).
    Nlm_head)r   zYou are loading your model using eetq but no linear modules were found in your model. Please double check your model architecture, or submit an issue on github if you think this is a bug.)r)   extendr$   setr&   loggerwarning)r(   r)   r*   r+   r   r   r   r   r   replace_with_eetq_linearO   s   


r4   )NNNFF)NNNF)utilsr   r   r   r   torch.nnr   
accelerater   
get_logger__name__r2   r&   r4   r   r   r   r   <module>   s   

4