
    קg!                     $   d dl mZmZ d dlmZmZ d dlmZmZm	Z	m
Z
mZmZ d dlZd dlmZ d dlmZ d dlmZ d dlmZ g d	Z G d
 de          Z edd           G d de                      Z edd           G d de                      Z	 eeeef         ef         Zde_         edd           G d de                      Z edd           G d de                      Ze G d d                      Z G d de          ZdS )    )ABCabstractmethod)	dataclassfield)CallableDictListOptionalTupleUnionN)Tensor)ObserverOrFakeQuantize)"_ObserverOrFakeQuantizeConstructor)Node)	QuantizerQuantizationSpecBaseQuantizationSpecFixedQParamsQuantizationSpec
EdgeOrNodeSharedQuantizationSpecDerivedQuantizationSpecQuantizationAnnotationc                       e Zd ZdZdS )r   zBase class for different types of quantization specs that allows users to
    specify how to quantize a Tensor (input/output of a Node) in the model
    N)__name__
__module____qualname____doc__     e/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/ao/quantization/quantizer/quantizer.pyr   r      s           r   r   T)eqfrozenc                       e Zd ZU dZej        ed<   eed<   dZe	e
         ed<   dZe	e
         ed<   dZe	ej                 ed<   dZe	e
         ed<   d	Zeed
<   d ZdS )r   zQuantization spec for common operators that allows user to specify how to
    quantize a Tensor, this includes dtype, quant_min, quant_max etc.
    dtypeobserver_or_fake_quant_ctrN	quant_min	quant_maxqschemech_axisF
is_dynamicc                     | j         7| j        0| j         | j        k    r t          d| j          d| j         d          | j        | j        dk     rt          d          d S d S )Nz
quant_min z must be <= quant_max .r   zCh_axis is < 0.)r&   r'   
ValueErrorr)   )selfs    r    __post_init__zQuantizationSpec.__post_init__1   s     N&*//TT^TT4>TTT   <#q(8(8./// $#(8(8r   )r   r   r   r   torchr$   __annotations__r   r&   r
   intr'   r(   r)   r*   boolr/   r   r   r    r   r      s           ;
 !CBBB#Ix}####Ix}###'+GXem$+++!GXc]!!!J0 0 0 0 0r   r   c                       e Zd ZU ej        ed<   eed<   eed<   dZe	e         ed<   dZ
e	e         ed<   dZe	ej                 ed<   dZeed	<   dS )
r   r$   scale
zero_pointNr&   r'   r(   Fr*   )r   r   r   r0   r$   r1   floatr2   r&   r
   r'   r(   r*   r3   r   r   r    r   r   C   s         ;LLLOOO#Ix}####Ix}###'+GXem$+++Jr   r   z)torch.ao.quantization.quantizer.quantizerc                       e Zd ZU dZeed<   dS )r   zg
    Quantization spec for the Tensors whose quantization parameters are shared with other Tensors
    edge_or_nodeN)r   r   r   r   r   r1   r   r   r    r   r   X   s*          
 r   r   c                       e Zd ZU dZee         ed<   eee         ge	e
e
f         f         ed<   ej        ed<   dZee         ed<   dZee         ed<   dZeej                 ed<   dZee         ed	<   d
Zeed<   dS )r   z^Quantization spec for the Tensors whose quantization parameters are derived from other Tensorsderived_fromderive_qparams_fnr$   Nr&   r'   r(   r)   Fr*   )r   r   r   r   r	   r   r1   r   r   r   r   r0   r$   r&   r
   r2   r'   r(   r)   r*   r3   r   r   r    r   r   b   s         hhz""""&<!= >ffn@U UVVVV;#Ix}####Ix}###'+GXem$+++!GXc]!!!Jr   r   c                       e Zd ZU dZ ee          Zeee	e
         f         ed<   dZe	e
         ed<   dZeed<   dZeed	<   dS )
r   zHow are input arguemnt or output should be quantized,
    expressed as QuantizationSpec, this corresponds to how a Tensor in the
    operator Graph is observed (PTQ) or fake quantized (QAT)
    )default_factoryinput_qspec_mapNoutput_qspecTallow_implicit_sharingF
_annotated)r   r   r   r   r   dictr?   r   r   r
   r   r1   r@   rA   r3   rB   r   r   r    r   r   p   s           CH%C C COT$)= >>?    48L(/0777
 $(D''' Jr   r   c                       e Zd Zdej        j        dej        j        fdZedej        j        dej        j        fd            Zedej        j        ddfd            Z	dS )r   modelreturnc                     |S )a  Allows for user defined transforms to run before annotating the graph.
        This allows quantizer to allow quantizing part of the model that are otherwise not quantizable.
        For example quantizer can
        a) decompose a compound operator like scaled dot product attention,
        into bmm and softmax if quantizer knows how to quantize bmm/softmax but not sdpa
        or b) transform scalars to tensor to allow quantizing scalares.

        Note: this is an optional method
        r   r.   rE   s     r    transform_for_annotationz"Quantizer.transform_for_annotation   s	     r   c                     d S Nr   rH   s     r    annotatezQuantizer.annotate       r   Nc                     d S rK   r   rH   s     r    validatezQuantizer.validate   rM   r   )
r   r   r   r0   fxGraphModulerI   r   rL   rO   r   r   r    r   r      s        X)		     eh2 ux7K    ^ eh2 t    ^  r   r   )abcr   r   dataclassesr   r   typingr   r   r	   r
   r   r   r0   r   torch.ao.quantizationr   torch.ao.quantization.qconfigr   torch.fxr   __all__r   r   r   r   r   r   r   r   r   r   r   r    <module>rY      s   # # # # # # # # ( ( ( ( ( ( ( ( ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?        8 8 8 8 8 8 L L L L L L      	 	 	    3    d4    0  0  0  0  0+  0  0 !  0F d4       #7   !  5t$d*+
C
  d4       1   !  d4   
 
 
 
 
2 
 
 ! 
        0         r   