
    ij.                         d dl mZ d dlmZ d dlmZ  ed       G d d             Z ed       G d d	e             Z ed
       G d de             Z ed       G d de             Z	d Z
d Zd Zy)    )keras_export)QUANTIZATION_MODES)serialization_libz#keras.quantizers.QuantizationConfigc                   ^    e Zd ZdZd	dZed        Zd Zed        Z	e
d        Ze
d        Zy)
QuantizationConfiga  Base class for quantization configs.

    Subclasses must implement the `mode` property and the `get_config` and
    `from_config` class methods.

    Args:
        weight_quantizer: Quantizer for weights.
        activation_quantizer: Quantizer for activations.
    Nc                      || _         || _        y Nweight_quantizeractivation_quantizer)selfr   r   s      }/var/www/html/emotional.easysim.app/public_html/venv/lib/python3.12/site-packages/keras/src/quantizers/quantization_config.py__init__zQuantizationConfig.__init__   s     0$8!    c                     t        d      )NzXSubclasses must implement this property. Do not instantiate QuantizationConfig directly.)NotImplementedErrorr   s    r   modezQuantizationConfig.mode   s    !+
 	
r   c                     t        j                  | j                        t        j                  | j                        dS )Nr
   )r   serialize_keras_objectr   r   r   s    r   
get_configzQuantizationConfig.get_config   s<     1 H H%%! %6$L$L))%	
 	
r   c                     t        j                  |j                  d            }t        j                  |j                  d            } | ||      S )Nr   r   r
   r   deserialize_keras_objectget)clsconfigr   r   s       r   from_configzQuantizationConfig.from_config'   sU    ,EEJJ)*
  1IIJJ-. 
 -!5
 	
r   c                 :    | | j                   | j                   S |S r	   )r   r   defaults     r   weight_quantizer_or_defaultz.QuantizationConfig.weight_quantizer_or_default4   s$    &"9"9"E***r   c                 "    | | j                   S |S r	   )r   r    s     r   activation_quantizer_or_defaultz2QuantizationConfig.activation_quantizer_or_default:   s    ...r   )NN)__name__
__module____qualname____doc__r   propertyr   r   classmethodr   staticmethodr"   r$    r   r   r   r      sa    9 
 

 

 

  
  r   r   z'keras.quantizers.Int8QuantizationConfigc                   4     e Zd ZdZd fd	Zed        Z xZS )Int8QuantizationConfigzInt8 quantization config.

    Args:
        weight_quantizer: Quantizer for weights.
        activation_quantizer: Quantizer for activations. If "default", uses
            AbsMaxQuantizer with axis=-1.
    c                     ddl m} |dk(  r |       }t        |   ||       | j                  <| j                  j
                  dk7  r"t        d| j                  j
                         y y )Nr   )AbsMaxQuantizerr!   int8zdInt8QuantizationConfig requires a weight_quantizer with output_dtype='int8'. Received: output_dtype=)keras.src.quantizers.quantizersr0   superr   r   output_dtype
ValueError)r   r   r   r0   	__class__s       r   r   zInt8QuantizationConfig.__init__K   s|    C9,#2#4 )+?@  ,$$11V; $$($9$9$F$F#GI  < -r   c                      y)Nr1   r,   r   s    r   r   zInt8QuantizationConfig.modeY       r   )Nr!   )r%   r&   r'   r(   r   r)   r   __classcell__r6   s   @r   r.   r.   A   s!      r   r.   z'keras.quantizers.Int4QuantizationConfigc                   T     e Zd ZdZ	 	 	 d fd	Zed        Z fdZed        Z	 xZ
S )Int4QuantizationConfigaC  Int4 quantization config.

    Args:
        weight_quantizer: Quantizer for weights.
        activation_quantizer: Quantizer for activations. If "default", uses
            AbsMaxQuantizer with axis=-1.
        block_size: Size of groups along the input dimension for sub-channel
            quantization. If a positive integer, uses sub-channel quantization
            with `ceil(input_dim / block_size)` groups. If `None` or `-1`,
            uses per-channel quantization (one scale per output channel).
            Default: `128` (sub-channel with 128-element groups).
    c                    |dk(  rd }t         |   ||       ||dk7  r|dk  rt        d|       || _        |d uxr |dkD  }| j                  d uxs | j
                  d u}|r|rt        d|       | j                  w| j                  j                  dk7  r"t        d| j                  j                         | j                  j                  dk7  r"t        d	| j                  j                         y y )
Nr!   r   zIblock_size must be None, -1, or a positive integer. Received: block_size=zInt4 sub-channel quantization (block_size > 0) does not support custom quantizers. Either set block_size to None or -1 for per-channel quantization, or remove the custom quantizer arguments. Received: block_size=)i   zcInt4QuantizationConfig requires a weight_quantizer with value_range=(-8, 7). Received: value_range=r1   zdInt4QuantizationConfig requires a weight_quantizer with output_dtype='int8'. Received: output_dtype=)r3   r   r5   
block_sizer   r   value_ranger4   )r   r   r   r@   is_sub_channelhas_custom_quantizerr6   s         r   r   zInt4QuantizationConfig.__init__m   sW     9,#' )+?@ !jB&6:?((2|5  % $4/BJN!!- 5((4 	 2= >HLJ    ,$$00G; ##'#8#8#D#D"EG  $$11V; $$($9$9$F$F#GI  < -r   c                      y)Nint4r,   r   s    r   r   zInt4QuantizationConfig.mode   r8   r   c                 B    t         |          }| j                  |d<   |S )Nr@   )r3   r   r@   )r   r   r6   s     r   r   z!Int4QuantizationConfig.get_config   s#    #%#|r   c                     t        j                  |j                  d            }t        j                  |j                  d            }|j                  dd       } | |||      S )Nr   r   r@   )r   r   r@   r   )r   r   r   r   r@   s        r   r   z"Int4QuantizationConfig.from_config   sh    ,EEJJ)*
  1IIJJ-. 

 ZZd3
-!5!
 	
r   )Nr!      r%   r&   r'   r(   r   r)   r   r   r*   r   r9   r:   s   @r   r<   r<   ^   sF     &	.`  
 
 
r   r<   z)keras.quantizers.Float8QuantizationConfigc                   H     e Zd ZdZ fdZed        Zd Zed        Z	 xZ
S )Float8QuantizationConfigzFP8 quantization config.

    FP8 mixed-precision training does not support user defined quantizers.
    This config is only used to indicate that FP8 mixed-precision training
    should be used.
    c                 &    t         |   d d        y r	   )r3   r   )r   r6   s    r   r   z!Float8QuantizationConfig.__init__   s    t$r   c                      y)Nfloat8r,   r   s    r   r   zFloat8QuantizationConfig.mode   s    r   c                     i S r	   r,   r   s    r   r   z#Float8QuantizationConfig.get_config   s    	r   c                      |        S r	   r,   )r   r   s     r   r   z$Float8QuantizationConfig.from_config   s	    ur   rI   r:   s   @r   rK   rK      s:    %    r   rK   c                    t        |t              r|} d}t        |        |k| dk(  rt               }n| dk(  rt	               }nv| dk(  rt               }nf| dk(  rt        d      | dk(  rt        d      | t        d	|        t        d
      t        |t              st        d| dt        |       d      | +|j                  | k7  rt        d|  d|j                   d      |j                  } t        |        | dk(  r)ddl
m} t        ||      st        dt        |             | dk(  r)ddlm} t        ||      st        dt        |             |S )aS  Validate and resolve quantization config.

    This function validates the quantization config and resolves the mode.
    If mode is not provided, it is inferred from the config.
    If config is not provided, a default config is inferred from the mode.

    Args:
        mode: Quantization mode.
        config: Quantization config.
    Nr1   rE   rN   gptqzGFor GPTQ, you must pass a `GPTQConfig` object in the `config` argument.awqzFFor AWQ, you must pass an `AWQConfig` object in the `config` argument.z*Invalid quantization mode. Received: mode=z9You must provide either `mode` or `config` to `quantize`.zPArgument `config` must be an instance of `QuantizationConfig`. Received: config=z
 (of type )zContradictory arguments: mode='z' but config.mode=''r   )
GPTQConfigzOMode 'gptq' requires a valid `config` argument of type `GPTQConfig`. Received: )	AWQConfigzMMode 'awq' requires a valid `config` argument of type `AWQConfig`. Received: )
isinstancestr_validate_moder.   r<   rK   r5   r   typer    keras.src.quantizers.gptq_configrV   keras.src.quantizers.awq_configrW   )r   r   rV   rW   s       r   validate_and_resolve_configr^      s    &#4 ~6>+-FV^+-FX-/FV^%  U]% 
  @G  K  &"45$$*8:d6l^1F  FKK4/-dV 4"KK=+
 	
 ;;D 4v~?&*-++/<.: 
 u}=&),**.v,9 
 Mr   c                 D    | | t         vrt        dt          d|        yy)zValidates quantization mode.Nz+Invalid quantization mode. Expected one of z. Received: mode=)r   r5   )r   s    r   rZ   rZ   &  s=    D(::122CD6K
 	
 ;r   c                 d   ddl m} ddlm} |rt	        |t
              r|j                  S t	        | j                  |      r| j                  j                  }|dk(  rdS |S t	        | j                  |      r;| j                  | j                     }t	        ||      r|j                  }|dk(  rdS |S yy)a  Determine the block size for int4 quantization.

    The block size can be specified either through the `config` argument
    or through the `dtype_policy` if it is of type `Int4DTypePolicy`.

    The config argument is usually available when quantizing the layer
    via the `quantize` method. If the layer was deserialized from a
    saved model, the block size should be specified in the `dtype_policy`.

    Args:
        layer: The layer being quantized.
        config: An optional configuration object that may contain the
            `block_size` attribute.
    Returns:
        int or None. The determined block size for int4 quantization.
        Returns `None` or `-1` for per-channel quantization.
    r   )Int4DTypePolicy)DTypePolicyMapr>   N)	%keras.src.dtype_policies.dtype_policyra   )keras.src.dtype_policies.dtype_policy_maprb   rX   r<   r@   dtype_policypath)layerr   ra   rb   r@   policys         r   get_block_size_for_layerri   /  s    $ FH*V%;<   	E&&	8''22
!R't7Z7	E&&	7##EJJ/fo.**J%+4;; r   N)keras.src.api_exportr   keras.src.dtype_policiesr   keras.src.savingr   r   r.   r<   rK   r^   rZ   ri   r,   r   r   <module>rm      s    - 7 . 347 7 57t 78/  98 78V
/ V
 9V
r 9:1  ;.Sl
%r   