o
    0 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)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Z	dd	de
defddZdd Z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__ s/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/transformers/quantizers/quantizer_quark.pyr   7   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_environment<   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)Zquark.torch.export.apir   r   Zquant_configr   r   r   )r   r   r   r   r   r   r   $_process_model_before_weight_loadingB   s   z5QuarkHfQuantizer._process_model_before_weight_loading
param_namereturnc                 K      dS )NTr   )r   r   r   r   r   r   r   param_needs_quantizationN      z)QuarkHfQuantizer.param_needs_quantizationc                 K   sH   ddl m} |dd }|tv r||t| }||||| d S )Nr   )_load_parameter_into_model.)modeling_utilsr"   splitCHECKPOINT_KEYSreplaceto)r   r   paramr   Zparam_devicer   r"   Zpostfixr   r   r   create_quantized_paramQ   s
   z'QuarkHfQuantizer.create_quantized_paramc                 K   s   |S r
   r   )r   r   r   r   r   r   #_process_model_after_weight_loading[   r!   z4QuarkHfQuantizer._process_model_after_weight_loadingNc                 C   r   NFr   )r   Zsafe_serializationr   r   r   is_serializable^   r!   z QuarkHfQuantizer.is_serializablec                 C   r   r-   r   )r   r   r   r   is_trainablea   s   zQuarkHfQuantizer.is_trainable)r   r   r
   )__name__
__module____qualname____doc__Zrequires_calibrationZrequired_packagesZ requires_parameters_quantizationr   r   r   strboolr    r+   r,   r.   propertyr/   __classcell__r   r   r   r   r	   *   s    



r	   N)typingr   baser   r%   r   utilsr   r   Z
get_loggerr0   loggerr'   r	   r   r   r   r   <module>   s    
