o
    i                  	   @   sl   d dl mZ ddlmZ erddlmZ ddlmZ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   )HfQuantizer   )PreTrainedModel)is_quark_availableloggingz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)weight_scale
bias_scaleinput_scaleoutput_scaleweight_zero_pointbias_zero_pointinput_zero_pointoutput_zero_pointc                       sh   e Zd ZdZdZ fddZdd Zdd	d
Zdddede	fddZ
dd Zedd Zdd Z  ZS )QuarkHfQuantizerz?
    Quark quantizer (https://quark.docs.amd.com/latest/).
    Tc                    s    t  j|fi | |j| _d S )N)super__init__json_export_config)selfquantization_configkwargs	__class__ i/sda-disk/www/egybert/egybert_env/lib/python3.10/site-packages/transformers/quantizers/quantizer_quark.pyr   0   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_environment5   s
   z%QuarkHfQuantizer.validate_environmentmodelr   c                 K   s,   ddl m} ||| jj| jj| jjd |S )Nr   )_map_to_quark)pack_methodcustom_mode)quark.torch.export.apir    r   quant_configr   r!   r"   )r   r   r   r    r   r   r   $_process_model_before_weight_loading;   s   z5QuarkHfQuantizer._process_model_before_weight_loading
param_namereturnc                 K      dS )NTr   )r   r   r&   r   r   r   r   param_needs_quantizationG      z)QuarkHfQuantizer.param_needs_quantizationc                 C   r(   NFr   r   r   r   r   is_serializableJ   r*   z QuarkHfQuantizer.is_serializablec                 C   r(   r+   r   r,   r   r   r   is_trainableM   s   zQuarkHfQuantizer.is_trainablec                 C   sJ   ddl m} ddlm} g }t D ]}|||g||| gd q|S )Nr   )WeightConverter)QuarkDeserialize)source_patternstarget_patterns
operations)core_model_loadingr/   integrations.quarkr0   CHECKPOINT_KEYSkeysappend)r   r/   r0   
converterskeyr   r   r   get_weight_conversionsQ   s   z'QuarkHfQuantizer.get_weight_conversions)r   r   )__name__
__module____qualname____doc__requires_calibrationr   r   r%   strboolr)   r-   propertyr.   r;   __classcell__r   r   r   r   r   )   s    

r   N)typingr   baser   modeling_utilsr   utilsr   r   
get_loggerr<   loggerr6   r   r   r   r   r   <module>   s    
