a
    h
                  	   @   s   d dl mZmZ ddlmZ ddlmZ erFddlmZ e rFd dl	Z	ddl
mZmZmZ e rld d	lmZ eeZd
ddddddddZG dd deZdS )    )TYPE_CHECKINGAny   )is_torch_available   )HfQuantizer)PreTrainedModelN)is_accelerate_availableis_quark_availablelogging)set_module_tensor_to_devicezweight_quantizer.scalezbias_quantizer.scalezinput_quantizer.scalezoutput_quantizer.scalezweight_quantizer.zero_pointzbias_quantizer.zero_pointzinput_quantizer.zero_pointzoutput_quantizer.zero_point)Zweight_scaleZ
bias_scaleZinput_scaleZoutput_scaleZweight_zero_pointZbias_zero_pointZinput_zero_pointZoutput_zero_pointc                       s   e Zd ZdZdZdgZdZ fddZdd Zdd	d
dZ	dde
ee
ef edddZddddZdd	ddZdddZedd Z  ZS )QuarkHfQuantizerz?
    Quark quantizer (https://quark.docs.amd.com/latest/).
    TZquarkc                    s    t  j|fi | |j| _d S N)super__init__json_export_config)selfquantization_configkwargs	__class__ c/var/www/html/assistant/venv/lib/python3.9/site-packages/transformers/quantizers/quantizer_quark.pyr   >   s    zQuarkHfQuantizer.__init__c                 O   s   t  stdd S )NzLoading a Quark quantized model requires the `quark` library but it was not found in the environment. Please refer to https://quark.docs.amd.com/latest/install.html.)r
   ImportError)r   argsr   r   r   r   validate_environmentC   s    z%QuarkHfQuantizer.validate_environmentr   )modelc                 K   s,   ddl m} ||| jj| jj| jjd |S )Nr   )_map_to_quark)pack_methodcustom_mode)Zquark.torch.export.apir   r   Zquant_configr   r   r   )r   r   r   r   r   r   r   $_process_model_before_weight_loadingI   s    z5QuarkHfQuantizer._process_model_before_weight_loadingztorch.Tensor)r   param_value
param_name
state_dictreturnc                 K   s   dS )NTr   )r   r   r!   r"   r#   r   r   r   r   check_quantized_paramU   s    z&QuarkHfQuantizer.check_quantized_paramztorch.nn.Parameter)r$   c                 C   s:   | dd }|tv r&||t| }t||||d d S )N.)value)splitCHECKPOINT_KEYSreplacer   )r   r   paramr"   Zparam_devicer#   Zunexpected_keysZpostfixr   r   r   create_quantized_param_   s    z'QuarkHfQuantizer.create_quantized_paramc                 K   s   |S r   r   )r   r   r   r   r   r   #_process_model_after_weight_loadingi   s    z4QuarkHfQuantizer._process_model_after_weight_loadingNc                 C   s   dS NFr   )r   Zsafe_serializationr   r   r   is_serializablel   s    z QuarkHfQuantizer.is_serializablec                 C   s   dS r/   r   )r   r   r   r   is_trainableo   s    zQuarkHfQuantizer.is_trainable)N)__name__
__module____qualname____doc__Zrequires_calibrationZrequired_packagesZ requires_parameters_quantizationr   r   r    strdictr   boolr%   r-   r.   r0   propertyr1   __classcell__r   r   r   r   r   1   s&   


r   )typingr   r   Z
file_utilsr   baser   Zmodeling_utilsr   Ztorchutilsr	   r
   r   Zaccelerate.utilsr   Z
get_loggerr2   loggerr*   r   r   r   r   r   <module>   s(   
