
    ijE                     H    d dl mZ d dlmZ  ed       G d de             Zy)    )keras_export)QuantizationConfigzkeras.quantizers.AWQConfigc                   x     e Zd ZdZdddddddded	ed
edededef fdZed        Zd Z	d Z
ed        Z xZS )	AWQConfiga  Configuration class for AWQ (Activation-aware Weight Quantization).

    AWQ is a post-training quantization method that identifies and protects
    salient weights based on activation magnitudes. It applies per-channel
    scaling before quantization to minimize accuracy loss.

    Methodology:
    1. Collects activation statistics from calibration data
    2. Identifies salient weight channels based on activation magnitudes
    3. Searches for optimal per-channel scaling factors via grid search
    4. Applies scaling before quantization to protect important weights

    References:
    - Original AWQ paper: "AWQ: Activation-aware Weight Quantization for
      LLM Compression and Acceleration" (https://arxiv.org/abs/2306.00978)
    - Reference implementation: https://github.com/mit-han-lab/llm-awq

    Args:
        dataset: The calibration dataset. It can be an iterable that yields
            strings or pre-tokenized numerical tensors (e.g., a list of
            strings, a generator, or a NumPy array). This data is used to
            analyze activation patterns.
        tokenizer: A tokenizer instance (or a similar callable) that is used
            to process the `dataset`.
        weight_bits: The number of bits for weight quantization. AWQ presently
            only supports 4-bit quantization. Defaults to 4.
        num_samples: The number of calibration data samples to use from the
            dataset. Defaults to 128.
        sequence_length: The sequence length to use for each calibration
            sample. Defaults to 512.
        group_size: The size of weight groups to quantize together. A
            `group_size` of -1 indicates per-channel quantization.
            Defaults to 128.
        num_grid_points: The number of grid search points for finding optimal
            per-channel scales. Higher values may find better scales but
            take longer. Defaults to 20.
        quantization_layer_structure: A dictionary defining the model's
            quantization structure. It should contain:
            - "pre_block_layers": list of layers to run before the first
              block (e.g., embedding layer).
            - "sequential_blocks": list of transformer blocks to quantize
              sequentially.
            If not provided, the model must implement
            `get_quantization_layer_structure`.

    Example:
    ```python
    from keras.quantizers import AWQConfig

    # Create configuration for 4-bit AWQ quantization
    config = AWQConfig(
        dataset=calibration_data,          # Your calibration dataset
        tokenizer=your_tokenizer,          # Tokenizer for text data
        num_samples=128,                   # Number of calibration samples
        sequence_length=512,               # Sequence length for each sample
        group_size=128,                    # Weight grouping for quantization
        num_grid_points=20,                # Grid search points for scale search
    )

    # Apply quantization to your model
    model.quantize("awq", config=config)
    ```

          i      Nweight_bitsnum_samplessequence_length
group_sizenum_grid_pointsquantization_layer_structurer   r   r   r   r   r   c                L   t         	|           |dk7  rt        d| d      |dk  rt        d      |dk  rt        d      |dk  s|dk(  rt        d| d      |dk  rt        d	      || _        || _        || _        || _        || _        || _        || _	        || _
        y )
Nr   z;AWQ only supports 4-bit quantization. Received weight_bits=.r   z'num_samples must be a positive integer.z+sequence_length must be a positive integer.zYInvalid group_size. Supported values are -1 (per-channel) or a positive integer, but got z+num_grid_points must be a positive integer.)super__init__
ValueErrordataset	tokenizerr   r   r   r   r   r   )
selfr   r   r   r   r   r   r   r   	__class__s
            t/var/www/html/emotional.easysim.app/public_html/venv/lib/python3.12/site-packages/keras/src/quantizers/awq_config.pyr   zAWQConfig.__init__H   s     	!((3}A7  !FGGaJKK?jAo22<Q@  aJKK"&&.$.,H)    c                      y)Nawq r   s    r   modezAWQConfig.modep   s    r   c                 :    d| j                    d| j                   S )zReturns the dtype policy string for this configuration.

        Returns:
            A string representing the dtype policy, e.g. "awq/4/128".
        zawq//)r   r   r    s    r   dtype_policy_stringzAWQConfig.dtype_policy_stringt   s#     d&&'q(9::r   c           	          d d | j                   | j                  | j                  | j                  | j                  | j
                  dS )N)r   r   r   r   r   r   r   r   r
   r    s    r   
get_configzAWQConfig.get_config|   sH     ++++#33//#33,0,M,M
 	
r   c                      | di |S )Nr   r   )clsconfigs     r   from_configzAWQConfig.from_config   s    }V}r   )__name__
__module____qualname____doc__intdictr   propertyr!   r$   r&   classmethodr*   __classcell__)r   s   @r   r   r      s    ?L "!-1&I
 &I &I &I &I &I '+&IP  ;
  r   r   N)keras.src.api_exportr   (keras.src.quantizers.quantization_configr   r   r   r   r   <module>r6      s0    - G *+F" F ,Fr   