
    qiE                        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 d dlmZmZmZ ddgZd
dZ G d	 de      Zy)    )annotationsN)PerChannelMinMaxObserver)QuantizationAnnotationQuantizationSpec	Quantizer)OperatorConfigOperatorPatternTypeQuantizationConfigget_embedding_operators_configEmbeddingQuantizerc                 X   t        t        j                  t        j                  dt	        j
                  d            } t        d d | d       }t        j                  j                  gg}|j                  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       y/home/ubuntu/crypto_trading_bot/.venv/lib/python3.12/site-packages/torch/ao/quantization/quantizer/embedding_quantizer.pyr   r      s    /kk66#;#E#E&#Q	  -T49QSWX',xx'9'9&:%;CJJ}%3"c&" ==788    c                  b    e Zd Zedd       Ze	 	 	 	 d	d       Zd
dZddZddZedd       Z	y)r   c                h    | j                         D ch c]  \  }}|	 }}}t        |      S c c}}w N)get_supported_operatorslist)clsspec_
op_configss       r$   "get_supported_quantization_configsz5EmbeddingQuantizer.get_supported_quantization_configs,   s=     !$ ; ; =/
T1D/

 /
 J/
s   .c                H    | j                         D ]  \  }}||k(  s|c S  g S r(   )r)   )r+   r!   r   r"   s       r$   .get_supported_operator_for_quantization_configzAEmbeddingQuantizer.get_supported_operator_for_quantization_config3   s7     668 	KFC ,,
	 	r%   c                <    | j                  |j                         |S )z!just handling global spec for now)_annotate_embedding_opsgraphselfmodels     r$   annotatezEmbeddingQuantizer.annotateA   s    $$U[[1r%   c                   t               }|j                  D ]  }|j                  dk(  s|j                  t        j
                  j                  j                  j                  u sN|j                  j                  t        d      t        |j                  d   |j                  j                  i      |j                  d<    y )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)r6   r4   embedding_confignodes       r$   r3   z*EmbeddingQuantizer._annotate_embedding_opsF   s    +I+KKK 	D ?*KK599>>#;#;#C#CC#**119$V  8N		!&6&=&=&D&D%8		34	r%   c                     y r(    r5   s     r$   validatezEmbeddingQuantizer.validateY   s    r%   c                    t               gS r(   )r   )r+   s    r$   r)   z*EmbeddingQuantizer.get_supported_operators\   s    .011r%   N)returnzlist[QuantizationConfig])r!   r
   rL   zlist[OperatorPatternType])r7   torch.fx.GraphModulerL   rM   )r4   ztorch.fx.GraphrL   None)r7   rM   rL   rN   )rL   zlist[OperatorConfig])
__name__
__module____qualname__classmethodr/   r1   r8   r3   rJ   r)   rI   r%   r$   r   r   +   s[        "4	" 
& 2 2r%   )rL   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   rI   r%   r$   <module>rZ      sK    "     C 
  %9 32 32r%   