
    &Vf                     x   d dl Z d dlmZ d dlmZ d dlmZ 	  eddg           G d d                      Z ed	          d
d ej                    fd            Z ed           G d de                      Z		  ed          dd            Z
 ed          d             Z ed          d             ZdS )    N)backend)ops)keras_exportzkeras.Quantizerzkeras.quantizers.Quantizerc                   8    e Zd ZddZd Zed             Zd ZdS )	Quantizerint8c                     || _         d S Noutput_dtype)selfr   s     \/var/www/html/software/conda/lib/python3.11/site-packages/keras/src/quantizers/quantizers.py__init__zQuantizer.__init__   s    (    c                     |S )z0Compute a quantized output from an input tensor. )r   xs     r   __call__zQuantizer.__call__   s    r   c                      | di |S )a  Creates a quantizer from its config.

        This method is the reverse of `get_config`,
        capable of instantiating the same quantizer from the config
        dictionary.

        This method is used by Keras `model_to_estimator`, saving and
        loading models to HDF5 formats, Keras model cloning, some visualization
        utilities, and exporting models to and from JSON.

        Args:
            config: A Python dictionary, typically the output of get_config.

        Returns:
            A quantizer instance.
        r   r   )clsconfigs     r   from_configzQuantizer.from_config   s    $ s}}V}}r   c                 &    t          |  d          )a  Returns the config of the quantizer.

        An quantizer config is a Python dictionary (serializable)
        containing all configuration parameters of the quantizer.
        The same quantizer can be reinstantiated later
        (without any saved state) from this configuration.

        This method is optional if you are just training and executing models,
        exporting to and from SavedModels, or using weight checkpoints.

        This method is required for Keras `model_to_estimator`, saving and
        loading models to HDF5 formats, Keras model cloning, some visualization
        utilities, and exporting models to and from JSON.

        Returns:
            Python dictionary.
        z  does not implement get_config())NotImplementedErrorr   s    r   
get_configzQuantizer.get_config'   s    $ "T"K"K"KLLLr   N)r   )__name__
__module____qualname__r   r   classmethodr   r   r   r   r   r   r   
   sf        ) ) ) )     [&M M M M Mr   r   z!keras.quantizers.abs_max_quantizei   r   c           
      p   t          j        |d         t          j        t          j        t          j        |           |d          |                    }t          j        | |          }t          j        t          j        |          |d         |d                   }t          j        ||          }||fS )N   T)axiskeepdimsr   )	r   divideaddmaxabsmultiplycliproundcast)inputsr%   value_rangedtypeepsilonscaleoutputss          r   abs_max_quantizer5   <   s     JAdTBBBGLL E l65))Ghsy));q>;q>JJGhw&&GE>r   z keras.quantizers.AbsMaxQuantizerc                   B    e Zd Zd ej                    dfdZd Zd ZdS )AbsMaxQuantizerr!   r   c                     t                               | |           t          |t                    r|f}t	          |          | _        || _        || _        d S )Nr   )r   r   
isinstanceinttupler%   r0   r2   )r   r%   r0   r2   r   s        r   r   zAbsMaxQuantizer.__init__P   sV     	4l;;;dC   	7D$KK	&r   c                 ^    t          || j        | j        | j        | j                  \  }}||fS r
   )r5   r%   r0   r   r2   )r   r   quantized_xr3   s       r   r   zAbsMaxQuantizer.__call__^   s8    -ty$*D,=t|
 
U E!!r   c                 8    | j         | j        | j        | j        dS )Nr%   r0   r2   r   r?   r   s    r   r   zAbsMaxQuantizer.get_configd   s'    I+| -	
 
 	
r   N)r   r   r   r   r2   r   r   r   r   r   r   r7   r7   N   s[        
  !!   " " "
 
 
 
 
r   r7   z%keras.quantizers.compute_float8_scalec                 ,   t          j        |          }t          j        t          j        ||           d|z            }t          j        | dk    ||          }t          j        t          j        |           ||          }t          j        |          S )N   g        )r   
reciprocalr'   whereisfinite)amaxr3   	dtype_maxmarginsfs        r   compute_float8_scalerI   p   sx     N5!!E	CJy$//F	;	;B	4#:r5	)	)B	3<%%r5	1	1B>"r   z,keras.quantizers.compute_float8_amax_historyc                    t          j        t          j        t          j        |                     |j                  }t          j        t          j        |d          dggt          j        |dg                    }|S )N)shiftr   r$   )r   r.   r)   r*   r1   scatter_updaterollreshape)r   amax_historyamax_updatenew_amax_historys       r   compute_float8_amax_historyrS   }   sm    (37371::..0BCCK)R(((
K!%% 
 r   z(keras.quantizers.quantize_and_dequantizec                    t          j        t          t          j        |          j                  |          }t          j        | t          j        ||                    }t          j        || |          }t          j        ||          }t          j        t          j        ||          t          j        ||                    }|S r
   )	r   r.   float	ml_dtypesfinfor)   r'   r,   r+   )r/   r3   quantized_dtypecompute_dtypequantized_dtype_maxr   s         r   quantize_and_dequantizer[      s     (ioo..233]  	
638E=99::A((*=>>AO$$A 	SXa//%1O1OPPAHr   )r   )rV   	keras.srcr   r   keras.src.api_exportr   r   r2   r5   r7   rI   rS   r[   r   r   r   <module>r^      s                   - - - - - - &  ">?@@.M .M .M .M .M .M .M A@.Mb 122 
GO   32" 011
 
 
 
 
i 
 
 21
<  566	 	 	 76	 <==  >= 899  :9  r   