
    h              	           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j                  e
      Zddd	d
dddddZ G d de      Zy)    )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                   x     e Zd ZdZdZdgZdZ fdZd ZddZ	ddd	e
d
efdZd ZddZddZed        Z xZS )QuarkHfQuantizerz?
    Quark quantizer (https://quark.docs.amd.com/latest/).
    Tquarkc                 H    t        |   |fi | |j                  | _        y N)super__init__json_export_config)selfquantization_configkwargs	__class__s      f/var/www/html/aiagenthome/venv/lib/python3.12/site-packages/transformers/quantizers/quantizer_quark.pyr   zQuarkHfQuantizer.__init__7   s$    ,77"5"H"H    c                 .    t               st        d      y )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   s      r   validate_environmentz%QuarkHfQuantizer.validate_environment<   s     !# x  $r   modelr   c                     ddl m}  ||| j                  j                  | j                  j
                  | j                  j                         |S )Nr   )_map_to_quark)pack_methodcustom_mode)quark.torch.export.apir&   r   quant_configr   r'   r(   )r   r$   r   r&   s       r   $_process_model_before_weight_loadingz5QuarkHfQuantizer._process_model_before_weight_loadingB   sD    8$$11//;;00<<		
 r   
param_namereturnc                      y)NT )r   r$   r,   r   s       r   param_needs_quantizationz)QuarkHfQuantizer.param_needs_quantizationN   s    r   c                     ddl m} |j                  d      d   }|t        v r|j	                  |t        |         } ||||j                  |             y )Nr   )_load_parameter_into_model.)modeling_utilsr2   splitCHECKPOINT_KEYSreplaceto)r   r$   paramr,   param_devicer   r2   postfixs           r   create_quantized_paramz'QuarkHfQuantizer.create_quantized_paramQ   sN    ?""3'+o%#++G_W5MNJ"5*ehh|6LMr   c                     |S r   r/   )r   r$   r   s      r   #_process_model_after_weight_loadingz4QuarkHfQuantizer._process_model_after_weight_loading[   s    r   c                      yNFr/   )r   safe_serializations     r   is_serializablez QuarkHfQuantizer.is_serializable^   s    r   c                      yrA   r/   )r   s    r   is_trainablezQuarkHfQuantizer.is_trainablea   s    r   )r$   r   r   )__name__
__module____qualname____doc__requires_calibrationrequired_packages requires_parameters_quantizationr   r#   r+   strboolr0   r=   r?   rC   propertyrE   __classcell__)r   s   @r   r   r   *   sp       	
 (,$I

.? S _c N  r   r   N)typingr   baser   r5   r   utilsr   r	   
get_loggerrF   loggerr7   r   r/   r   r   <module>rV      sY     !  0 / 
		H	% -(*,6246	9{ 9r   