
    *`iX                         d dl Z d dlmZmZmZ g dZdefdZdede j        de j        fdZd	e j        de j        fd
Z	d	e j        de j        fdZ
dS )    N)BFLOAT16_DATAFP4_E2M1_DATAQuantizationArgs)maybe_convert_from_mxfp4_expgenerate_mxfp4_scalesround_to_power_2should_generatre_mxfp4_scalesargsc                 D    | j         dk    o| j        dk    o
| j        dk    S )N   float    )num_bitstype
group_size)r
   s    /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/compressed_tensors/quantization/utils/mxfp4_utils.pyr	   r	   !   s'    =AP$)w"6P4?b;PP    scalereturnc                     |j         }t          |           rY|                    t          j                  dz
  }d|                    t          j                  z  }|                    |          S |S )a  
    Converts mxfp4 scales. Scales are powers of 2, with the
    exponents stored in uint8. Converts to dense dtype so that
    they can be applied to the weights and activations during QDQ

    :param scale: uint8 exponent scale
    :param dtype: dense dtype
       g       @)dtyper	   totorchint32r   )r
   r   original_dtype	scale_exps       r   r   r   %   sb     [N$T** (HHU[))C/	ek223xx'''Lr   xc                    | j         t          j        k    sJ |                     t          j                                      t          j                  } dt          j        t          j        z
  dz
  z  }dt          j
        dz   z  dz
  t          j        z  }t          j        | |z   |          }|                    t          j                                      t          j                  S )a  
    Round values to the closest power of 2.
    This is done by masking the values with BFLOAT16_SIGN_EXPONENT_MASK
    which essentially removes the mantissa and keeps the exponent.
    i.e the closest power of 2 for the input_value.

    E.g:
        0.0825 = 1.32 (mantissa) x 2**-4 (exponent)
        0.0825 ==> -4 (exponent) + 127 = 123 = 01111011 (8 bits for bfloat16)
        0.0825 ==> 0.32 (mantissa) = 0101001 (7 bits for bfloat16)
        0.0825 == 0b01111011_0101001 (bfloat16)
        0b01111011_0101001 & 111111111_0000000 == 0b01111011_0000000
        Keep the exponent + sign bit to give you the closest power of 2, 0.0625

    :param x: tensor to round to closest power of 2
       )r   r   bfloat16viewuint16r   r   r   mantissar   exponentbitwise_and)r   BFLOAT16_VAL_TO_ADDBFLOAT16_SIGN_EXPONENT_MASKblock_max_uints       r   r   r   8   s    " 7en$$$$	u|,,A  69O ORS ST 
}%)	*a/		# 
 &	!< N U\**//???r   c                 x    t          |           }dt          j        t          j        |                    z   dz
  S )a  
    Generate mxfp4 scales. The scales require the following steps
    1. Round to the closest power of 2
    2. Convert to exponent

    Called when calculating qparams using observers.

    :param x: tensor to round to closest power of 2
    :returns scales as exponents
    r      )r   r   floorlog2)r   scale_power_2s     r   r   r   Z   s5     %Q''MUZ66777!;;r   )r   *compressed_tensors.quantization.quant_argsr   r   r   __all__r	   Tensorr   r   r    r   r   <module>r3      s              Q(8 Q Q Q Q
#(<
\   &@ @ @ @ @ @D<U\ <el < < < < < <r   