
    ёi                     X    S SK rS SKrSSKJr  SSKJr   " S S\5      r " S S\5      rg)	    N   )BaseObserver)ObserverFactoryc                   6   ^  \ rS rSrSrSU 4S jjrS rSrU =r$ )GroupWiseWeightObserver   a  
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                     > [         TU ]  US9  g )N)
quant_bits)super__init__)selfr
   
group_size	__class__s      g/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/quantization/observers/groupwise.pyr    GroupWiseWeightObserver.__init__'   s    J/    c                     [         $ N)GroupWiseWeightObserverLayerr   s    r   
_get_class"GroupWiseWeightObserver._get_class*   s    ++r          )	__name__
__module____qualname____firstlineno____doc__r   r   __static_attributes____classcell__r   s   @r   r   r      s    0, ,r   r   c                   r   ^  \ rS rSrSU 4S jjrS rS rS\4S jrS\4S jr	S r
S	 rS
 rS rS rSrU =r$ )r   .   c                 p   > [         TU ]  5         X l        X0l        Xl        S U l        S U l        S U l        g r   )r   r   _quant_bitsr   _layer_max_scale_zero_point)r   layerr
   r   r   s       r   r   %GroupWiseWeightObserverLayer.__init__/   s4    %$	r   c                 2    U R                  U5      U l        U$ r   )_cal_abs_maxr*   )r   inputss     r   forward$GroupWiseWeightObserverLayer.forward8   s    %%f-	r   c                    UR                   nU R                  S:X  d  U R                  S:X  d   S5       eUR                   S   U R                  -  S:X  d   S5       e[        UR                   5      S:X  d   S5       eUR                  SS/5      R	                  US   US   U R                  -  U R                  /5      n[
        R                  " [
        R                  " U5      SS	9R                  S
5      n[
        R                  " U[        R                  " S5      :H  [        R                  " S5      U5      nUR                  SS/5      nU$ )zUUse 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   )axisfloat32g:0yE>)shaper   len	transposereshapepaddlemaxabscastwherenpr8   )r   r1   input_shapeinput_processedabs_max_valuess        r   r0   )GroupWiseWeightObserverLayer._cal_abs_max<   s.    ll"$3(> 	
/	
> ||A0A5 	
;	
5 6<< A%I'II% **Aq62::^[^t>P
  FJJ$?aHMM
  bjjm+RZZ-=~
 (111a&9r   returnc                     g)Ng        r   r   s    r   	min_value&GroupWiseWeightObserverLayer.min_valueU   s    r   c                     U R                   $ r   )r*   r   s    r   	max_value&GroupWiseWeightObserverLayer.max_valueX   s    yyr   c                     U R                   $ r   )r(   r   s    r   
bit_length'GroupWiseWeightObserverLayer.bit_length[   s    r   c                     g)Nr   r   s    r   
quant_axis'GroupWiseWeightObserverLayer.quant_axis^   s    r   c                     U R                   c  U R                  U l         [        R                  " U R                   5      U l        g)z$Compute thresholds for MAX function.N)r+   r*   r=   
zeros_liker,   r   s    r   cal_thresholds+GroupWiseWeightObserverLayer.cal_thresholdsa   s.    ;;))DK!,,T[[9r   c                 T    U R                   c  U R                  5         U R                   $ )zReturn output scales.)r+   rW   r   s    r   scales#GroupWiseWeightObserverLayer.scalesg   s"    ;;!{{r   c                 T    U R                   c  U R                  5         U R                   $ )zReturn output zero points.)r,   rW   r   s    r   zero_points(GroupWiseWeightObserverLayer.zero_pointsm   s&    #!r   )r)   r*   r(   r+   r,   r   r   )r   r   r   r    r   r2   r0   floatrI   rL   rO   rS   rW   rZ   r]   r"   r#   r$   s   @r   r   r   .   sH     25 5  :   r   r   )	numpyrB   r=   base_observerr   factoryr   r   r   r   r   r   <module>rc      s-      ( %,o ,.C < C r   