o
    Zh                     @  sZ   d dl mZ d dlmZ ddlmZmZ er d dlZd dlm	Z	 dgZ
G dd deZdS )	    )annotations)TYPE_CHECKING   )QuantizationAnnotation	QuantizerN)NodeComposableQuantizerc                      sJ   e Zd ZdZd fddZdddZdddZdddZdddZ  Z	S )r   at  
    ComposableQuantizer allows users to combine more than one quantizer into a single quantizer.
    This allows users to quantize a model with multiple quantizers. E.g., embedding quantization
    maybe supported by one quantizer while linear layers and other ops might be supported by another
    quantizer.

    ComposableQuantizer is initialized with a list of `Quantizer` instances.
    The order of the composition matters since that is the order in which the quantizers will be
    applies.
    Example:
    ```
    embedding_quantizer = EmbeddingQuantizer()
    linear_quantizer = MyLinearQuantizer()
    xnnpack_quantizer = XNNPackQuantizer() # to handle ops not quantized by previous two quantizers
    composed_quantizer = ComposableQuantizer([embedding_quantizer, linear_quantizer, xnnpack_quantizer])
    prepared_m = prepare_pt2e(model, composed_quantizer)
    ```
    
quantizerslist[Quantizer]c                   s   t    || _i | _d S N)super__init__r	   _graph_annotations)selfr	   	__class__ c/var/www/auris/lib/python3.10/site-packages/torch/ao/quantization/quantizer/composable_quantizer.pyr   %   s   

zComposableQuantizer.__init__gmtorch.fx.GraphModule	quantizerr   returnNonec                 C  s   |j jD ]@}d|jv r3|| jv r*t| j| t|jd kr*td|jj d| |jd | j|< q|| jv rDtd|jj d| qd S )NZquantization_annotationz
Quantizer z! has changed annotations on node z! has removed annotations on node )graphnodesmetar   idRuntimeErrorr   __name__)r   r   r   nr   r   r    _record_and_validate_annotations*   s    


z4ComposableQuantizer._record_and_validate_annotationsmodelc                 C  s&   | j D ]}|| | || q|S )z!just handling global spec for now)r	   annotater    r   r!   r   r   r   r   r"   @   s   

zComposableQuantizer.annotatec                 C  s   | j D ]}||}q|S r   )r	   transform_for_annotationr#   r   r   r   r$   G   s   
z,ComposableQuantizer.transform_for_annotationc                 C  s   d S r   r   )r   r!   r   r   r   validateN   s   zComposableQuantizer.validate)r	   r
   )r   r   r   r   r   r   )r!   r   r   r   )r!   r   r   r   )
r   
__module____qualname____doc__r   r    r"   r$   r%   __classcell__r   r   r   r   r      s    


)
__future__r   typingr   r   r   r   ZtorchZtorch.fxr   __all__r   r   r   r   r   <module>   s    