
    Vh                         d dl Z d dlmZ d dlmc mZ d dl mZ ddgZ G d dej                        Z G d dej                        Z	y)    N)Tensor	EmbeddingEmbeddingBagc                   v     e Zd ZdZej
                  Z	 	 	 	 	 	 	 	 	 d	 d fdZdefdZ	e
d	d       Zd Z xZS )
r   a  
    An embedding bag module attached with FakeQuantize modules for weight,
    used for quantization aware training.

    We adopt the same interface as `torch.nn.Embedding`, please see
    https://pytorch.org/docs/stable/generated/torch.nn.Embedding.html#torch.nn.Embedding
    for documentation.

    Similar to `torch.nn.Embedding`, with FakeQuantize modules initialized to
    default.

    Attributes:
        weight: fake quant module for weight
    returnc           
      8   |	|
d}t        |   ||||||||fi | |sJ d       |j                         j                  t        j
                  k(  s+J dt        |j                         j                        z          || _        |j                  |      | _        y )Ndevicedtype'qconfig must be provided for QAT moduleSEmbedding weights requires a qscheme of torch.per_channel_affine_float_qparams Got factory_kwargs	super__init__weightqschemetorch per_channel_affine_float_qparamsstrqconfigweight_fake_quant)selfnum_embeddingsembedding_dimpadding_idxmax_norm	norm_typescale_grad_by_freqsparse_weightr
   r   r   r   	__class__s                U/home/dcms/DCMS/lib/python3.12/site-packages/torch/ao/nn/qat/modules/embedding_ops.pyr   zEmbedding.__init__   s     %+U;
	
 
	
 AAAw~~''5+Q+QQ 	
a'.."**+,	
Q !(~!N    c           	          t        j                  || j                  | j                        | j                  | j
                  | j                  | j                  | j                        S N)	F	embeddingr   r   r   r   r   r    r!   )r   inputs     r$   forwardzEmbedding.forward>   sO    {{""4;;/MMNN##KK
 	
r%   c                 L   t        |      | j                  k(  s.J d| j                  z   dz   | j                  j                  z          t        |d      sJ d       |j                  sJ d       |j                  j                         j                  }|t        j                  k(  sJ dt        |      z          |j                  } | |j                  |j                  |j                  |j                  |j                  |j                  |j                   |j
                  |	      }|S )Create a qat module from a float module

        Args: `mod` a float module, either produced by torch.ao.quantization utilities
        or directly from user
         qat..from_float only works for r   ,Input float module must have qconfig defined,Input float module must have a valid qconfigr   r   )type_FLOAT_MODULE__name__hasattrr   r   r   r   r   r   r   r   r   r   r   r    r!   clsmoduse_precomputed_fake_quantweight_qschemer   qat_embedding_bags         r$   
from_floatzEmbedding.from_floatI   s    CyC--- 	
ll+, (()	
- sI&V(VV&{{JJJ{++-55!G!GG 	
a.!"	
G
 ++OOLLMM""JJJJ

 ! r%   c           
         t         j                  j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  d       }t         j                  j                  | j                  j                               |_        |j                  | j                         |S r'   )r   nnr   r   r   r   r   r   r    r!   	Parameterr   detachtraintrainingr   embedding_bags     r$   to_floatzEmbedding.to_floatm   s    **MMNN##KK	
  %xx11$++2D2D2FGDMM*r%   )	NN       @FFNNNNr   NF)r5   
__module____qualname____doc__r?   r   r4   r   r   r+   classmethodr=   rF   __classcell__r#   s   @r$   r   r      sh     LLM   O 
 OD	
 	
 !! !!Fr%   c                   |     e Zd ZdZej
                  Z	 	 	 	 	 	 	 	 	 	 	 d	 d fdZd	defdZ	e
d
d       Zd Z xZS )r   a  
    An embedding bag module attached with FakeQuantize modules for weight,
    used for quantization aware training.

    We adopt the same interface as `torch.nn.EmbeddingBag`, please see
    https://pytorch.org/docs/stable/generated/torch.nn.EmbeddingBag.html#torch.nn.EmbeddingBag
    for documentation.

    Similar to `torch.nn.EmbeddingBag`, with FakeQuantize modules initialized to
    default.

    Attributes:
        weight: fake quant module for weight
    r   c                 <   ||d}t        |   |||||||||	|
f
i | |sJ d       |j                         j                  t        j
                  k(  s+J dt        |j                         j                        z          || _        |j                  |      | _        y )Nr	   r   WEmbedding Bag weights requires a qscheme of torch.per_channel_affine_float_qparams Got r   r   )r   r   r   r   r   r    moder!   r"   include_last_offsetr   r   r
   r   r   r#   s                  r$   r   zEmbeddingBag.__init__   s      %+U;	
 	
 AAAw~~''5+Q+QQ 	
e'.."**+,	
Q !(~!Nr%   c                     t        j                  || j                  | j                        || j                  | j
                  | j                  | j                  | j                  || j                  | j                        S r'   )r(   rE   r   r   r   r   r    rS   r!   rT   r   )r   r*   offsetsper_sample_weightss       r$   r+   zEmbeddingBag.forward   se    ""4;;/MMNN##IIKK$$
 	
r%   c                 x   t        |      | j                  k(  s.J d| j                  z   dz   | j                  j                  z          t        |d      sJ d       |j                  sJ d       |j                  j                         j                  }|t        j                  k(  sJ dt        |      z          |j                  } | |j                  |j                  |j                  |j                  |j                  |j                  |j                   |j
                  |j"                  |j$                  |      }|S )r-   r.   r/   r   r0   r1   rR   r2   )r3   r4   r5   r6   r   r   r   r   r   r   r   r   r   r   r    rS   r!   rT   r   r7   s         r$   r=   zEmbeddingBag.from_float   s,    CyC--- 	
ll+, (()	
- sI&V(VV&{{JJJ{++-55!G!GG 	
e.!"	
G
 ++LLMM""HHJJJJ##OO
 ! r%   c                    t         j                  j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  d | j                  | j                  
      }t         j                  j                  | j                  j                               |_        |j                  | j                          |S r'   )r   r?   r   r   r   r   r   r    rS   r!   rT   r   r@   r   rA   rB   rC   rD   s     r$   rF   zEmbeddingBag.to_float   s    --MMNN##IIKK$$
  %xx11$++2D2D2FGDMM*r%   )NrG   FmeanFNFNNNNrH   )NNrI   )r5   rJ   rK   rL   r?   r   r4   r   r   r+   rM   r=   rF   rN   rO   s   @r$   r   r   }   sn     OOM  !$O 
$OL
v 
 #! #!Jr%   )
r   torch.nnr?   torch.nn.functional
functionalr(   r   __all__r   r    r%   r$   <module>r`      sE         
'o od{2?? {r%   