o
    iM                     @  s   d dl mZ d dlZd dlZd dlm  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
dZG dd deZdS )    )annotationsN)PerChannelMinMaxObserver)OperatorConfigOperatorPatternTypeQuantizationAnnotationQuantizationConfigQuantizationSpec	Quantizer)Q_ANNOTATION_KEYget_embedding_operators_configEmbeddingQuantizerreturnr   c                  C  sZ   t tjtjdtjddd} td d | d }tjjgg}|	t
jg t||d}t|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_operators r$   k/home/ubuntu/.local/lib/python3.10/site-packages/torchao/quantization/pt2e/quantizer/embedding_quantizer.pyr       s   

c                      sf   e Zd Zd fddZedddZedddZdddZdddZdddZ	ed ddZ
  ZS )!r   r   Nonec                   s   t    d S N)super__init__)self	__class__r$   r%   r)   1   s   zEmbeddingQuantizer.__init__list[QuantizationConfig]c                 C  s   dd |   D }t|S )Nc                 S  s   h | ]\}}|qS r$   r$   ).0spec_r$   r$   r%   	<setcomp>6   s    zHEmbeddingQuantizer.get_supported_quantization_configs.<locals>.<setcomp>)get_supported_operatorslist)cls
op_configsr$   r$   r%   "get_supported_quantization_configs4   s   z5EmbeddingQuantizer.get_supported_quantization_configsr!   r   list[OperatorPatternType]c                 C  s&   |   D ]\}}||kr|  S qg S r'   )r2   )r4   r!   r   r"   r$   r$   r%   .get_supported_operator_for_quantization_config;   s
   zAEmbeddingQuantizer.get_supported_operator_for_quantization_configmodeltorch.fx.GraphModulec                 C  s   |  |j |S )z!just handling global spec for now)_annotate_embedding_opsgraphr*   r9   r$   r$   r%   annotateI   s   zEmbeddingQuantizer.annotater<   torch.fx.Graphc                 C  sd   t  }|jD ])}|jdkr/|jtjjjjkr/|j	j
d u r tdt|jd |j	j
id|jt< q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*   r<   embedding_confignoder$   r$   r%   r;   N   s   

z*EmbeddingQuantizer._annotate_embedding_opsc                 C  s   d S r'   r$   r=   r$   r$   r%   validatea   s   zEmbeddingQuantizer.validatelist[OperatorConfig]c                 C  s   t  gS r'   )r   )r4   r$   r$   r%   r2   d   s   z*EmbeddingQuantizer.get_supported_operators)r   r&   )r   r-   )r!   r   r   r7   )r9   r:   r   r:   )r<   r?   r   r&   )r9   r:   r   r&   )r   rN   )__name__
__module____qualname__r)   classmethodr6   r8   r>   r;   rM   r2   __classcell__r$   r$   r+   r%   r   0   s    


)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   r$   r$   r$   r%   <module>   s    
