o
    i                     @   s   d dl mZ d dlmZ d dlmZmZ e r!ddlZddlm	Z	 e
eZG dd deZG dd	 d	ejjZG d
d de	jZddee dB fddZdS )   )ConversionOps)should_convert_module)is_torch_availablelogging    Nc                	   @   sJ   e Zd Zdd Z	d	deeeej f dedB deeejf fddZ	dS )
EetqQuantizec                 C   s
   || _ d S N)hf_quantizer)selfr	    r   `/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/transformers/integrations/eetq.py__init__   s   
zEetqQuantize.__init__N
input_dictfull_layer_namereturnc           	      K   sn   t | d \}}|d }|j}t|  }t|tj	d\}}|
|}|
|}||| d|iS )Nr   F_scales)tupleitemsdevicetorcht
contiguouscpueetq_kernels_hubquant_weightsint8to)	r
   r   r   kwargs_valuevalue_deviceint8_weightscalesr   r   r   convert   s   

zEetqQuantize.convertr   )
__name__
__module____qualname__r   dictstrlistr   Tensorr#   r   r   r   r   r      s    r   c                   @   s&   e Zd ZedddZedd ZdS )EetqLinearMMFunctionNc                 C   s:   |  |||| t|||}|d ur|| }|S |}|S r   )save_for_backwardr   w8_a16_gemm)ctxxweightr"   biasoutputr   r   r   forward0   s   zEetqLinearMMFunction.forwardc                 C   sp   | j \}}}}t|jd |j|j}t|||}| j	d r2|
d|ddd}|d d d fS )Nr      )saved_tensorsr   eyeshaper   r   dtyper   r-   needs_input_gradsqueezematmul	transpose	unsqueeze)r.   grad_outputinputr0   r"   r1   identity
grad_inputr   r   r   backward8   s    
zEetqLinearMMFunction.backwardr   )r$   r%   r&   staticmethodr3   rB   r   r   r   r   r+   /   s
    r+   c                       s,   e Zd Zejdf fdd	Zdd Z  ZS )
EetqLinearFc                    sj   t    tjtj||f|ddd| _ttj|tjd| _|r0ttj|tjd| _	d S d | _	d S )N)r8   F)requires_grad)
superr   nn	Parameterr   emptyr0   float16weight_scalesr1   )r
   in_featuresout_featuresr8   r1   	__class__r   r   r   H   s   

zEetqLinear.__init__c                 C   s   t || j| j| j}|S r   )r+   applyr0   rK   r1   )r
   r?   r2   r   r   r   r3   Q   s   zEetqLinear.forward)r$   r%   r&   r   r   r   r3   __classcell__r   r   rN   r   rD   G   s    	rD   Fmodules_to_not_convertc           	   	   C   s   ddl m} |dad}|ri nddi}|  D ]=\}}t||s"qtd& t|tj	rFt
|j|jfd|jdui|}| || d	}W d   n1 sPw   Y  q|s]td
 | S )a  
    A helper function to replace all `torch.nn.Linear` modules by `EetqLinear` modules.

    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 `None`):
            Names of the modules to not convert in `EetqLinear`. In practice we keep the `lm_head` in full precision
            for numerical stability reasons.
    r4   )
get_kernelz#kernels-community/quantization-eetqFr8   Nmetar1   TzYou 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.)hub_kernelsrS   r   named_modulesr   r   r   
isinstancerG   LinearrD   rL   rM   r1   set_submoduleloggerwarning)	modelrR   pre_quantizedrS   has_been_replacedmodule_kwargsmodule_namemodule
new_moduler   r   r   replace_with_eetq_linearV   s4   
rc   )NF)core_model_loadingr   quantizers.quantizers_utilsr   utilsr   r   r   torch.nnrG   
get_loggerr$   rZ   r   autogradFunctionr+   ModulerD   r)   r(   rc   r   r   r   r   <module>   s   
