o
    Ih                     @  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 d dlmZmZmZ ddgZdd
dZG dd deZdS )    )annotationsN)PerChannelMinMaxObserver)QuantizationAnnotationQuantizationSpec	Quantizer)OperatorConfigOperatorPatternTypeQuantizationConfig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#   g/var/www/vscode/kcb/lib/python3.10/site-packages/torch/ao/quantization/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(   ,   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>1   s    zHEmbeddingQuantizer.get_supported_quantization_configs.<locals>.<setcomp>)get_supported_operatorslist)cls
op_configsr#   r#   r$   "get_supported_quantization_configs/   s   z5EmbeddingQuantizer.get_supported_quantization_configsr    r	   list[OperatorPatternType]c                 C  s&   |   D ]\}}||kr|  S qg S r&   )r1   )r3   r    r   r!   r#   r#   r$   .get_supported_operator_for_quantization_config6   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)   r8   r#   r#   r$   annotateD   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d< qd S )Ncall_functionz<Embedding config must have a valid weight quantization spec.r   )input_qspec_mapquantization_annotation)r
   nodesoptargetr   r!   atenr   defaultr   weight
ValueErrorr   argsmeta)r)   r;   embedding_confignoder#   r#   r$   r:   I   s   

z*EmbeddingQuantizer._annotate_embedding_opsc                 C  s   d S r&   r#   r<   r#   r#   r$   validate\   s   zEmbeddingQuantizer.validatelist[OperatorConfig]c                 C  s   t  gS r&   )r
   )r3   r#   r#   r$   r1   _   s   z*EmbeddingQuantizer.get_supported_operators)r   r%   )r   r,   )r    r	   r   r6   )r8   r9   r   r9   )r;   r>   r   r%   )r8   r9   r   r%   )r   rN   )__name__
__module____qualname__r(   classmethodr5   r7   r=   r:   rM   r1   __classcell__r#   r#   r*   r$   r   +   s    


)r   r   )
__future__r   r   r   torch.nn.functionalr   
functionalr   torch.ao.quantization.observerr   )torch.ao.quantization.quantizer.quantizerr   r   r   7torch.ao.quantization.quantizer.xnnpack_quantizer_utilsr   r   r	   __all__r
   r   r#   r#   r#   r$   <module>   s   
