o
    pi                     @   sL   d dl Zd dlZddlmZ ddlmZ G dd deZG dd deZdS )	    N   )BaseObserver)ObserverFactoryc                       s*   e Zd ZdZd fdd	Zdd Z  ZS )	GroupWiseWeightObservera  
    It collects channel-wise maximum absolute values of target weights.
    Args:
        bit_length(int, optional): Number of bits to represent an quantized integer in binary.
        dtype(str, optional): The data type of input tensor.
        name (str, optional): This parameter is used by developers to print debugging information. \
            For details, please refer to :ref:`api_guide_Name`. Default is None.
    Examples:
       .. code-block:: python
            from paddle.quantization import QuantConfig
            from paddle.quantization.quanters import AbsMaxChannelWiseWeightObserver
            quanter = AbsMaxChannelWiseWeightObserver()
            q_config = QuantConfig(activation=None, weight=quanter)
          c                    s   t  j|d d S )N)
quant_bits)super__init__)selfr   
group_size	__class__ n/home/app/PaddleOCR-VL/.venv_paddleocr/lib/python3.10/site-packages/paddle/quantization/observers/groupwise.pyr
   '   s   z GroupWiseWeightObserver.__init__c                 C   s   t S N)GroupWiseWeightObserverLayerr   r   r   r   
_get_class*      z"GroupWiseWeightObserver._get_classr   r   )__name__
__module____qualname____doc__r
   r   __classcell__r   r   r   r   r      s    r   c                       sr   e Zd Zd fdd	Zdd Zdd Zd	efd
dZd	efddZdd Z	dd Z
dd Zdd Zdd Z  ZS )r   r   r   c                    s2   t    || _|| _|| _d | _d | _d | _d S r   )r	   r
   _quant_bitsr   Z_layer_max_scale_zero_point)r   layerr   r   r   r   r   r
   /   s   

z%GroupWiseWeightObserverLayer.__init__c                 C   s   |  || _|S r   )_cal_abs_maxr   )r   inputsr   r   r   forward8   s   z$GroupWiseWeightObserverLayer.forwardc                 C   s   |j }| jdks| jdksJ d|j d | j dksJ dt|j dks*J d|ddg|d |d | j | jg}tjt|dd	d
}t	|t
dkt
d|}|ddg}|S )zeUse group_size to group the input, then use the
        absmax method to calculate the scale
        @   r   z!group_size only support 64 or 128r   z-group_size must be a factor of input channelsr   z Currently only support 2D tensor   )Zaxisfloat32g:0yE>)shaper   lenZ	transposeZreshapepaddlemaxabscastwherenpr&   )r   r"   Zinput_shapeZinput_processedZabs_max_valuesr   r   r   r!   <   s&   z)GroupWiseWeightObserverLayer._cal_abs_maxreturnc                 C      dS )Ng        r   r   r   r   r   	min_valueU   r   z&GroupWiseWeightObserverLayer.min_valuec                 C      | j S r   )r   r   r   r   r   	max_valueX      z&GroupWiseWeightObserverLayer.max_valuec                 C   r2   r   )r   r   r   r   r   
bit_length[   r4   z'GroupWiseWeightObserverLayer.bit_lengthc                 C   r0   )Nr   r   r   r   r   
quant_axis^   r   z'GroupWiseWeightObserverLayer.quant_axisc                 C   s$   | j du r	| j| _ t| j | _dS )z$Compute thresholds for MAX function.N)r   r   r)   Z
zeros_liker   r   r   r   r   cal_thresholdsa   s   
z+GroupWiseWeightObserverLayer.cal_thresholdsc                 C      | j du r	|   | j S )zReturn output scales.N)r   r8   r   r   r   r   scalesg      
z#GroupWiseWeightObserverLayer.scalesc                 C   r9   )zReturn output zero points.N)r   r8   r   r   r   r   zero_pointsm   r;   z(GroupWiseWeightObserverLayer.zero_pointsr   )r   r   r   r
   r#   r!   floatr1   r3   r5   r7   r8   r:   r<   r   r   r   r   r   r   .   s    	r   )	numpyr.   r)   Zbase_observerr   factoryr   r   r   r   r   r   r   <module>   s   