
    shCT                     8   S r SSKrSSKrSSKJr  SSKrSSKJr  SS/r " S S\R                  R                  5      r
 " S S	\R                  R                  5      r " S
 S\R                  R                  5      r " S S\R                  R                  5      rg)z
We will recreate all the RNN modules as we require the modules to be decomposed
into its building blocks to be able to observe.
    N)Optional)TensorLSTMCellLSTMc            
         ^  \ rS rSrSr\R                  R                  rS/r	   SSS.S\
S\
S	\S
S4U 4S jjjjr SS\S\\\\4      S
\\\4   4S jjr SS\
S\S
\\\4   4S jjrS r\SS j5       r\SS j5       rSrU =r$ )r      a  A quantizable long short-term memory (LSTM) cell.

For the description and the argument types, please, refer to :class:`~torch.nn.LSTMCell`

`split_gates`: specify True to compute the input/forget/cell/output gates separately
to avoid an intermediate tensor which is subsequently chunk'd. This optimization can
be beneficial for on-device inference latency. This flag is cascaded down from the
parent classes.

Examples::

    >>> import torch.ao.nn.quantizable as nnqa
    >>> rnn = nnqa.LSTMCell(10, 20)
    >>> input = torch.randn(6, 10)
    >>> hx = torch.randn(3, 20)
    >>> cx = torch.randn(3, 20)
    >>> output = []
    >>> for i in range(6):
    ...     hx, cx = rnn(input[i], (hx, cx))
    ...     output.append(hx)
split_gatesNFr	   	input_dim
hidden_dimbiasreturnc                  > XES.n[         T	U ]  5         Xl        X l        X0l        X`l        U(       d  [        R                  R                  " USU-  4SU0UD6U l	        [        R                  R                  " USU-  4SU0UD6U l
        [        R                  R                  R                  R                  5       U l        GO[        R                  R                  5       U l	        [        R                  R                  5       U l
        [        R                  R                  5       U l        S H  n[        R                  R                  " X4SU0UD6U R                  U'   [        R                  R                  " X"4SU0UD6U R                  U'   [        R                  R                  R                  R                  5       U R                  U'   M     [        R                  R!                  5       U l        [        R                  R!                  5       U l        [        R                  R'                  5       U l        [        R                  R!                  5       U l        [        R                  R                  R                  R                  5       U l        [        R                  R                  R                  R                  5       U l        [        R                  R                  R                  R                  5       U l        [        R                  R                  R                  R                  5       U l        SU l        SU l        [        R8                  U l        [        R8                  U l        g )Ndevicedtype   r   )inputforgetcelloutput)      ?r   )super__init__
input_sizehidden_sizer   r	   torchnnLinearigateshgatesao	quantizedFloatFunctionalgates
ModuleDictSigmoid
input_gateforget_gateTanh	cell_gateoutput_gatefgate_cxigate_cgatefgate_cx_igate_cgateogate_cyinitial_hidden_state_qparamsinitial_cell_state_qparamsquint8hidden_state_dtypecell_state_dtype)
selfr   r   r   r   r   r	   factory_kwargsg	__class__s
            w/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/ao/nn/quantizable/modules/rnn.pyr   LSTMCell.__init__,   sj    %+;#%	&+088??1z>,04,8F,DK ,188??A
N,15,9G,DK +0((++*?*?*O*O*QDJ  ((--/DK((--/DK,,.DJ:!&"04"8F"A "'"15"9G"A !& 5 5 E E G

1 ;  ((**, 88++- 88++---==? 88;;00@@B$)HHKK$9$9$I$I$K!--==??G)=E'/4||-2\\    xhiddenc                    Ub  US   b  US   c)  U R                  UR                  S   UR                  5      nUu  p4U R                  (       d  U R	                  U5      nU R                  U5      nU R                  R                  XV5      nUR                  SS5      u  ppU R                  U5      nU R                  U	5      n	U R                  U
5      n
U R                  U5      nO0 n[        U R                  R                  5       U R                  R                  5       U R
                  R                  5       5       H(  u  u  ppVUR                  U" U5      U" U5      5      X'   M*     U R                  US   5      nU R                  US   5      n	U R                  US   5      n
U R                  US   5      nU R                   R#                  X5      nU R$                  R#                  X5      nU R&                  R                  X5      nUn[(        R*                  " U5      nU R,                  R#                  UU5      nUU4$ )Nr      r   r   r   r   r   )initialize_hiddenshapeis_quantizedr	   r    r!   r%   addchunkr(   r)   r+   r,   zipitemsvaluesr-   mulr.   r/   r   tanhr0   )r6   r=   r>   hxcxr    r!   r%   r(   r)   r+   out_gategatekeyr-   r.   r/   cytanh_cyhys                       r:   forwardLSTMCell.forwardf   s    >VAY.&)2C++AGGAJGF[[^F[[_FJJNN62E;@;;q!;L8JY4J**;7Ky1I''1H D03

  """$""$1,f
 "IIfQi<	1 g7J**4>:KtF|4I''X7H==$$[5&&**:A#88<<XS! **R.]]x12vr<   
batch_sizerC   c                 L   [         R                  " XR                  45      [         R                  " XR                  45      pCU(       aZ  U R                  u  pVU R                  u  px[         R
                  " X5X`R                  S9n[         R
                  " XGXR                  S9nX44$ )Nscale
zero_pointr   )r   zerosr   r1   r2   quantize_per_tensorr4   r5   )	r6   rU   rC   hch_scaleh_zpc_scalec_zps	            r:   rA   LSTMCell.initialize_hidden   s     {{J(8(89:EKK))*=
1 "??OW"==OW))T9P9PA ))T9N9NA tr<   c                     g)NQuantizableLSTMCell r6   s    r:   	_get_nameLSTMCell._get_name   s    $r<   c                    USL USL :X  d   eUR                   S   nUR                   S   nU " UUUSLUS9nU(       d  [        R                  R                  U5      UR                  l        Ub.  [        R                  R                  U5      UR                  l        [        R                  R                  U5      UR                  l        Ub.  [        R                  R                  U5      UR                  l        U$ [        X/X4/UR                  UR                  /5       H  u  pn[        U	R                  SSS9UR                  5       5       H)  u  p[        R                  R                  U5      Ul        M+     U
c  M`  [        U
R                  SSS9UR                  5       5       H)  u  p[        R                  R                  U5      Ul        M+     M     U$ )zUses the weights and biases to create a new LSTM cell.

Args:
    wi, wh: Weights for the input and hidden layers
    bi, bh: Biases for the input and hidden layers
Nr@   )r   r   r   r	   r   r   )dim)rB   r   r   	Parameterr    weightr   r!   rF   rE   rH   )clswiwhbibhr	   r   r   r   wbr%   w_chunkrN   b_chunks                  r:   from_paramsLSTMCell.from_params   s    d
d
+++XXa[
hhqk "D.#	
 !&!3!3B!7DKK~#(88#5#5b#9 !&!3!3B!7DKK~#(88#5#5b#9    #B8bXT[[7QRe%():ELLN%KMG"'(("4"4W"=DK &L =),QWWQAW->)O$)HH$6$6w$?	 *P  S r<   c                 F   [        U5      U R                  :X  d   e[        US5      (       d   S5       eU R                  UR                  UR
                  UR                  UR                  US9nUR                  Ul        UR                  UR                  l        UR                  UR                  l        U(       ad  UR                  R                  5        H  nUR                  Ul        M     UR                  R                  5        H  nUR                  Ul        M     U$ )Nqconfigz$The float module must have 'qconfig'r
   )type_FLOAT_MODULEhasattrrv   	weight_ih	weight_hhbias_ihbias_hhry   r    r!   rH   )rm   otheruse_precomputed_fake_quantr	   observedr8   s         r:   
from_floatLSTMCell.from_float   s    E{c/////ui((P*PP(??OOOOMMMM# # 
 !=="'--"'--__++-!MM	 .__++-!MM	 .r<   )r   r+   r5   r-   r/   r)   r%   r!   r   r4   r.   r    r2   r1   r(   r   r0   r,   r	   TNNN)F)NNF)FF)__name__
__module____qualname____firstlineno____doc__r   r   r   r{   __constants__intboolr   r   r   tuplerS   rA   rg   classmethodrv   r   __static_attributes____classcell__r9   s   @r:   r   r      s    * HH%%M"OM 8: 8:8: 8: 	8: 
8: 8:v DH))!)%*?!@)	vv~	)X 5:-1	vv~	"% " "H  r<   c            
          ^  \ rS rSrSr   SSS.S\S\S\S	S4U 4S
 jjjjrSS\S\	\
\\4      4S jjr\S 5       rSrU =r$ )_LSTMSingleLayer   zA single one-directional LSTM layer.

The difference between a layer and a cell is that the layer can process a
sequence, while the cell only expects an instantaneous value.
NFr
   r   r   r   r   c                P   > XES.n[         TU ]  5         [        X4X6S.UD6U l        g Nr   )r   r	   )r   r   r   r   )	r6   r   r   r   r   r   r	   r7   r9   s	           r:   r   _LSTMSingleLayer.__init__   s7     %+;
(,
IW
	r<   r=   r>   c                     / nUR                   S   n[        U5       H+  nU R                  X   U5      nUR                  US   5        M-     [        R
                  " US5      nXb4$ )Nr   )rB   ranger   appendr   stack)r6   r=   r>   resultseq_leniresult_tensors          r:   rS   _LSTMSingleLayer.forward   s_    ''!*wAYYqtV,FMM&)$   FA.$$r<   c                     [         R                  " U0 UD6nU " UR                  UR                  UR                  UR
                  S9nX4l        U$ )Nr
   )r   rv   r   r   r   r	   r   )rm   argskwargsr   layers        r:   rv   _LSTMSingleLayer.from_params  sJ    ##T4V4OOT--tyydFVFV
 
r<   )r   r   r   )r   r   r   r   r   r   r   r   r   r   r   rS   r   rv   r   r   r   s   @r:   r   r      s     
 

 
 	
 

 
 % %%2G)H %  r<   r   c                      ^  \ rS rSrSr     SSS.S\S\S\S	\S
\SS4U 4S jjjjrSS\S\	\
\\4      4S jjr\SS j5       rSrU =r$ )
_LSTMLayeri  z#A single bi-directional LSTM layer.FNr
   r   r   r   batch_firstbidirectionalr   c                   > XgS.n	[         T
U ]  5         X@l        XPl        [	        X4X8S.U	D6U l        U R                  (       a  [	        UU4UUS.U	D6U l        g g r   )r   r   r   r   r   layer_fwlayer_bw)r6   r   r   r   r   r   r   r   r	   r7   r9   s             r:   r   _LSTMLayer.__init__  s|     %+;&*(
(,
IW
 , '	
 !DM r<   r=   r>   c                 L   U R                   (       a  UR                  SS5      nUc  Su  p4OUu  p4S nU R                  (       a)  Uc  S nO
US   nUS   nUc  S nO
US   nUS   nUb  Ub  Xg4nUc  Uc  S nO>[        R                  R                  U5      [        R                  R                  U5      4nU R                  X5      u  p[        U S5      (       Ga  U R                  (       a  UR                  S5      n
U R                  X5      u  pUR                  S5      n[        R                  " X/U	R                  5       S-
  5      nUc  Uc  S nS nOUc"  [        R                  R                  U5      u  pOUc"  [        R                  R                  U5      u  pOb[        R                  " US   US   /S5      n[        R                  " US   US   /S5      nO#U	n[        R                  R                  U5      u  pU R                   (       a  UR                  SS5        XU44$ )Nr   r@   )NNr   )r   	transposer   r   jit_unwrap_optionalr   r|   flipr   catrj   r   
transpose_)r6   r=   r>   hx_fwcx_fw	hidden_bwhx_bwcx_bw	hidden_fw	result_fw
x_reversed	result_bwr   r\   r]   s                  r:   rS   _LSTMLayer.forward*  s    Aq!A>'LE5!LE59	}aa}aa U%6!L	=U]I		2259599;U;U< I  $}}Q:	4$$););J#'==#G I!q)IYY	5y}}7JKF Y%6"33I>A"33I>AKK1y| <a@KK1y| <a@F99--i8DAa#1v~r<   c           	      2   [        US5      (       d  Uc   eUR                  SUR                  5      nUR                  SUR                  5      nUR                  SUR                  5      nUR                  SUR
                  5      nUR                  SUR                  5      n	UR                  SS	5      n
U " UUUUU	U
S
9n[        USU5      Ul        [        USU 35      n[        USU 35      n[        USU 3S5      n[        USU 3S5      n[        R                  XXU
S
9Ul        UR                  (       a\  [        USU S35      n[        USU S35      n[        USU S3S5      n[        USU S3S5      n[        R                  XXU
S
9Ul        U$ )z
There is no FP equivalent of this class. This function is here just to
mimic the behavior of the `prepare` within the `torch.ao.quantization`
flow.
ry   Nr   r   r   r   r   r	   Fr
   weight_ih_lweight_hh_l	bias_ih_l	bias_hh_l_reverse)r|   getr   r   r   r   r   getattrry   r   rv   r   r   )rm   r   	layer_idxry   r   r   r   r   r   r   r	   r   rn   ro   rp   rq   s                   r:   r   _LSTMLayer.from_float`  s    ui((W-@AAZZe.>.>?
jj0A0ABzz&%**-jj0A0AB

?E4G4GHjj6#
  y':Uk)56Uk)56Ui	{3T:Ui	{3T:)55B 6 
 +i[ ABB+i[ ABB)I;h ?FB)I;h ?FB-99K : EN r<   )r   r   r   r   )TFFNNr   )r   N)r   r   r   r   r   r   r   r   r   r   r   rS   r   r   r   r   r   s   @r:   r   r     s    . !#   	
   
 84 4%2G)H 4l ) )r<   r   c                      ^  \ rS rSrSr\R                  R                  r       SSS.S\	S\	S\	S	\
S
\
S\S\
S\
SS4U 4S jjjjrSS\S\\\\4      4S jjrS r\SS j5       r\S 5       rSrU =r$ )r   i  a  A quantizable long short-term memory (LSTM).

For the description and the argument types, please, refer to :class:`~torch.nn.LSTM`

Attributes:
    layers : instances of the `_LSTMLayer`

.. note::
    To access the weights and biases, you need to access them per layer.
    See examples below.

Examples::

    >>> import torch.ao.nn.quantizable as nnqa
    >>> rnn = nnqa.LSTM(10, 20, 2)
    >>> input = torch.randn(5, 3, 10)
    >>> h0 = torch.randn(2, 3, 20)
    >>> c0 = torch.randn(2, 3, 20)
    >>> output, (hn, cn) = rnn(input, (h0, c0))
    >>> # To get the weights:
    >>> # xdoctest: +SKIP
    >>> print(rnn.layers[0].weight_ih)
    tensor([[...]])
    >>> print(rnn.layers[0].weight_hh)
    AssertionError: There is no reverse path in the non-bidirectional layer
FNr
   r   r   
num_layersr   r   dropoutr   r	   r   c
                  >^ ^
^ XS.m[         TT ]  5         UT l        UT l        UT l        UT l        UT l        [        U5      T l        UT l	        ST l
        [        U[        R                  5      (       a%  SUs=::  a  S::  a  O  O[        U[        5      (       a  [        S5      eUS:  a8  [         R"                  " S5        US:X  a  [         R"                  " SU SU 35        [%        T R                  T R                  T R
                  4ST R                  T
S	.TD6/nUR'                  UU U
4S
 j[)        SU5       5       5        [*        R,                  R/                  U5      T l        g )Nr   Fr   r@   zbdropout should be a number in range [0, 1] representing the probability of an element being zeroedz|dropout option for quantizable LSTM is ignored. If you are training, please, use nn.LSTM version followed by `prepare` step.zdropout option adds dropout after all but last recurrent layer, so non-zero dropout expects num_layers greater than 1, but got dropout=z and num_layers=r   r   r	   c              3      >#    U  H?  n[        TR                  TR                  TR                  4S TR                  TS.TD6v   MA     g7f)Fr   N)r   r   r   r   ).0_r7   r6   r	   s     r:   	<genexpr> LSTM.__init__.<locals>.<genexpr>  sZ      
 *     		 ""00' ! *s   AA
)r   r   r   r   r   r   r   floatr   r   training
isinstancenumbersNumberr   
ValueErrorwarningswarnr   extendr   r   r   
ModuleListlayers)r6   r   r   r   r   r   r   r   r   r   r	   r   r7   r9   s   `         ` @r:   r   LSTM.__init__  sd    %+;$&$	&W~* 7GNN33$1$'4(( 
 Q;MM.
 QBBI K&&0\3   		 ""00' !

 	 
 1j)
 	
 hh))&1r<   r=   r>   c                     U R                   (       a  UR                  SS5      nUR                  S5      nU R                  (       a  SOSnUc  [        R
                  " UUU R                  [        R                  UR                  S9nUR                  S5        UR                  (       a!  [        R                  " USSUR                  S9n[        U R                  5       Vs/ s H  oeU4PM     nnO[        R                  R!                  U5      n[#        US   [$        5      (       a  US   R'                  U R                  XCU R                  5      n	US   R'                  U R                  XCU R                  5      n
[        U R                  5       Vs/ s H)  nX   R)                  S5      X   R)                  S5      4PM+     nnOUn/ n/ n[+        U R,                  5       Hr  u  pU" XU   5      u  nu  nnUR/                  [        R                  R!                  U5      5        UR/                  [        R                  R!                  U5      5        Mt     [        R0                  " U5      n[        R0                  " U5      nUR'                  SUR2                  S   UR2                  S   5      nUR'                  SUR2                  S   UR2                  S   5      nU R                   (       a  UR                  SS5      nUUU44$ s  snf s  snf )	Nr   r@      )r   r   r   rW   )r   r   sizer   r   rZ   r   r   r   squeeze_rC   r[   r   r   r   r   r   r   r   reshapesqueeze	enumerater   r   r   rB   )r6   r=   r>   max_batch_sizenum_directionsrZ   r   hxcxhidden_non_optrK   rL   idxhx_listcx_listr   r\   r]   	hx_tensor	cx_tensors                      r:   rS   LSTM.forward  s   Aq!A"00a>KK  kkxxE NN1~~11!'' -2$//,BC,BqEN,BDCD"YY77?N.+V44#A&..OO^TEUEU $A&..OO^TEUEU
  %T__55 W__Q');<5  
 &#DKK0JCac+IAv1NN59955a89NN59955a89 1 KK(	KK(	 %%b)//"*=yr?RS	%%b)//"*=yr?RS	Aq!A9i(((E Ds   !L0Lc                     g)NQuantizableLSTMre   rf   s    r:   rg   LSTM._get_name+  s     r<   c                    [        XR                  5      (       d   e[        US5      (       d	  U(       d   eU " UR                  UR                  UR
                  UR                  UR                  UR                  UR                  US9n[        USU5      Ul        [        UR
                  5       H&  n[        R                  XUSUS9UR                  U'   M(     UR                   (       a:  UR#                  5         [$        R&                  R(                  R+                  USS9nU$ UR-                  5         [$        R&                  R(                  R/                  USS9nU$ )Nry   r
   F)r   r	   T)inplace)r   r{   r|   r   r   r   r   r   r   r   r   ry   r   r   r   r   r   trainr   r"   quantizationprepare_qatevalprepare)rm   r   ry   r	   r   r   s         r:   r   LSTM.from_float.  s-   %!2!23333ui((G33JJMM#	
 #5)W=))*C#-#8#8GK $9 $HOOC  + >>NNxx,,8848PH  MMOxx,,44Xt4LHr<   c                     [        S5      e)NzuIt looks like you are trying to convert a non-quantizable LSTM module. Please, see the examples on quantizable LSTMs.)NotImplementedError)rm   r   s     r:   from_observedLSTM.from_observedK  s     "1
 	
r<   )	r   r   r   r   r   r   r   r   r   )r@   TFg        FNNr   )NF)r   r   r   r   r   r   r   r   r{   r   r   r   r   r   r   r   rS   rg   r   r   r  r   r   r   s   @r:   r   r     s    4 HHMMM !#H2 "H2H2 H2 	H2
 H2 H2 H2 H2 H2 
H2 H2T5) 5)%2G)H 5)n!  8 
 
r<   )r   r   r   typingr   r   r   __all__r   Moduler   r   r   r   re   r<   r:   <module>r     s         v
Kuxx K\'uxx 'T DF
588?? F
r<   