
    PiM                        d dl mZ d dlZd dlZd dlmc mZ d dlm	Z	 d dl
mZmZmZmZmZmZ d dlmZ ddgZdd
Z G d de          ZdS )    )annotationsN)PerChannelMinMaxObserver)OperatorConfigOperatorPatternTypeQuantizationAnnotationQuantizationConfigQuantizationSpec	Quantizer)Q_ANNOTATION_KEYget_embedding_operators_configEmbeddingQuantizerreturnr   c                 J   t          t          j        t          j        dt	          j        d                    } t          d d | d           }t          j        j        gg}|	                    t          j        g           t          ||          }t          j        |          S )Nr   g      0?)eps)dtypeqschemech_axisobserver_or_fake_quant_ctr)config	operators)r	   torchuint8 per_channel_affine_float_qparamsr   	with_argsr   nn	EmbeddingappendF	embeddingr   copydeepcopy)weight_quantization_specquantization_configopssupported_config_and_operatorss       /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torchao/quantization/pt2e/quantizer/embedding_quantizer.pyr   r       s    /k6#;#E&#Q#Q#Q	      -T49QSWXX',x'9&:%;CJJ}%3"c& & &" =7888    c                       e Zd Zd fdZedd            Zedd	            ZddZddZddZ	edd            Z
 xZS )r   r   Nonec                H    t                                                       d S N)super__init__)self	__class__s    r&   r-   zEmbeddingQuantizer.__init__1   s    r'   list[QuantizationConfig]c                \    d |                                  D             }t          |          S )Nc                    h | ]\  }}|S  r3   ).0spec_s      r&   	<setcomp>zHEmbeddingQuantizer.get_supported_quantization_configs.<locals>.<setcomp>6   s)     /
 /
 /
T1D/
 /
 /
r'   )get_supported_operatorslist)cls
op_configss     r&   "get_supported_quantization_configsz5EmbeddingQuantizer.get_supported_quantization_configs4   s<    /
 /
 # ; ; = =/
 /
 /

 Jr'   r#   r   list[OperatorPatternType]c                N    |                                  D ]\  }}||k    r|c S g S r+   )r8   )r:   r#   r   r$   s       r&   .get_supported_operator_for_quantization_configzAEmbeddingQuantizer.get_supported_operator_for_quantization_config;   sD     6688 	 	KFC ,,,


 -	r'   modeltorch.fx.GraphModulec                :    |                      |j                   |S )z!just handling global spec for now)_annotate_embedding_opsgraphr.   r@   s     r&   annotatezEmbeddingQuantizer.annotateI   s    $$U[111r'   rD   torch.fx.Graphc                2   t                      }|j        D ]}|j        dk    rs|j        t          j        j        j        j        k    rO|j	        j
        t          d          t          |j        d         |j	        j
        i          |j        t          <   d S )Ncall_functionz<Embedding config must have a valid weight quantization spec.r   )input_qspec_map)r   nodesoptargetr   r$   atenr   defaultr   weight
ValueErrorr   argsmetar   )r.   rD   embedding_confignodes       r&   rC   z*EmbeddingQuantizer._annotate_embedding_opsN   s    +I+K+KK 	 	D ?**K59>#;#CCC#*19$V   /E	!&6&=&D%/ / /	*+	 	r'   c                    d S r+   r3   rE   s     r&   validatezEmbeddingQuantizer.validatea   s    r'   list[OperatorConfig]c                     t                      gS r+   )r   )r:   s    r&   r8   z*EmbeddingQuantizer.get_supported_operatorsd   s    .0011r'   )r   r)   )r   r0   )r#   r   r   r=   )r@   rA   r   rA   )rD   rG   r   r)   )r@   rA   r   r)   )r   rX   )__name__
__module____qualname__r-   classmethodr<   r?   rF   rC   rW   r8   __classcell__)r/   s   @r&   r   r   0   s                    [     [   
   &    2 2 2 [2 2 2 2 2r'   )r   r   )
__future__r   r    r   torch.nn.functionalr   
functionalr   "torchao.quantization.pt2e.observerr   #torchao.quantization.pt2e.quantizerr   r   r   r   r	   r
   -torchao.quantization.pt2e.quantizer.quantizerr   __all__r   r   r3   r'   r&   <module>rf      s   # " " " " "            G G G G G G                K J J J J J %9 9 9 9 62 62 62 62 62 62 62 62 62 62r'   