
    hg                        S SK r S SKJr  S SKJr  SS jrS\R                  S\ R                  S\ R                  4S jrSS jr	S\R                  S\ R                  S\ R                  4S	 jrSS
 jrS\R                  S\ R                  S\ R                  4S jrS\R                  4S jrS\R"                  4S jrS\R&                  4S jrg)    N)PackedSequencec                 6    XU-   -  U-   nU(       a  X1S-  -  nU$ )z`Calculate the total operations for an RNN cell given input size, hidden size, and optional bias.    )
input_sizehidden_sizebias	total_opss       H/home/james-whalen/.local/lib/python3.13/site-packages/thop/rnn_hooks.py_count_rnn_cellr      s)    K78;FI1_$	    mxyc                     [        U R                  U R                  U R                  5      nUS   R	                  S5      nX4-  nU =R
                  [        R                  " [        U5      /5      -  sl        g)z^Counts the total RNN cell operations based on input tensor, hidden size, bias, and batch size.r   N)	r   r   r   r	   sizer
   torchDoubleTensorintr   r   r   r
   
batch_sizes        r   count_rnn_cellr      U    ammQVVDI11JIKK5%%s9~&677Kr   c                     SnX-   U-  U-   nU(       a  XAS-  -  nX4S-  -  nX1U -   U-  U-   -  nU(       a  X1S-  -  nX1-  nX1S-  -  nU$ )zdCounts the total operations for a GRU cell based on input size, hidden size, and bias configuration.r   r      r   r   r   r	   r
   	state_opss        r   _count_gru_cellr      sz    I )[8;FI1_$	QI 
*k9KGGI1_$	I q Ir   c                     [        U R                  U R                  U R                  5      nUS   R	                  S5      nX4-  nU =R
                  [        R                  " [        U5      /5      -  sl        g)z\Calculates and updates the total operations for a GRU cell in a mini-batch during inference.r   N)	r   r   r   r	   r   r
   r   r   r   r   s        r   count_gru_cellr    3   r   r   c                 ^    SnX-   U-  U-   nU(       a  XAS-  -  nX4S-  -  nX1S-  -  nX1-  nU$ )zfCounts LSTM cell operations during inference based on input size, hidden size, and bias configuration.r   r      r   r   r   s        r   _count_lstm_cellr#   =   sT    I )[8;FI1_$	QI q I Ir   c                     [        U R                  U R                  U R                  5      nUS   R	                  S5      nX4-  nU =R
                  [        R                  " [        U5      /5      -  sl        g)zZCounts and updates the total operations for an LSTM cell in a mini-batch during inference.r   N)	r#   r   r   r	   r   r
   r   r   r   r   s        r   count_lstm_cellr%   T   sU     q}}affEI11JIKK5%%s9~&677Kr   c                 J   U R                   nU R                  nU R                  nU R                  n[	        US   [
        5      (       aB  [        R                  " US   R                  5      nUS   R                  R                  S5      nObU R                  (       a)  US   R                  S5      nUS   R                  S5      nO(US   R                  S5      nUS   R                  S5      nSn	U R                  (       a  U	[        XEU5      S-  -  n	OU	[        XEU5      -  n	[        US-
  5       H5  n
U	U R                  (       a  [        US-  XS5      S-  O[        XUU5      -  n	M7     X-  n	X-  n	U =R                  [        R                  " [!        U	5      /5      -  sl        g)zWCalculate and update the total number of operations for each RNN cell in a given batch.r      r   N)r	   r   r   
num_layers
isinstancer   r   maxbatch_sizesr   batch_firstbidirectionalr   ranger
   r   r   r   r   r   r	   r   r   r(   r   	num_stepsr
   _s              r   	count_rnnr2   ^   \   66DJ--KJ!A$''YYqt//0
aD$$))!,		
qTYYq\
aDIIaL	qTYYq\
aDIIaL	I_ZdCaGG	_ZdCC	:>" K!O[?!C 4@	
	 # IIKK5%%s9~&677Kr   c                 J   U R                   nU R                  nU R                  nU R                  n[	        US   [
        5      (       aB  [        R                  " US   R                  5      nUS   R                  R                  S5      nObU R                  (       a)  US   R                  S5      nUS   R                  S5      nO(US   R                  S5      nUS   R                  S5      nSn	U R                  (       a  U	[        XEU5      S-  -  n	OU	[        XEU5      -  n	[        US-
  5       H5  n
U	U R                  (       a  [        US-  XS5      S-  O[        XUU5      -  n	M7     X-  n	X-  n	U =R                  [        R                  " [!        U	5      /5      -  sl        g)zfCalculates total operations for a GRU layer, updating the model's operation count based on batch size.r   r'   r   N)r	   r   r   r(   r)   r   r   r*   r+   r   r,   r-   r   r.   r
   r   r   r/   s              r   	count_grur5      r3   r   c                 J   U R                   nU R                  nU R                  nU R                  n[	        US   [
        5      (       aB  [        R                  " US   R                  5      nUS   R                  R                  S5      nObU R                  (       a)  US   R                  S5      nUS   R                  S5      nO(US   R                  S5      nUS   R                  S5      nSn	U R                  (       a  U	[        XEU5      S-  -  n	OU	[        XEU5      -  n	[        US-
  5       H5  n
U	U R                  (       a  [        US-  XS5      S-  O[        XUU5      -  n	M7     X-  n	X-  n	U =R                  [        R                  " [!        U	5      /5      -  sl        g)zgCalculate total operations for LSTM layers, including bidirectional, updating model's total operations.r   r'   r   N)r	   r   r   r(   r)   r   r   r*   r+   r   r,   r-   r#   r.   r
   r   r   r/   s              r   
count_lstmr7      s^   66DJ--KJ!A$''YYqt//0
aD$$))!,		
qTYYq\
aDIIaL	qTYYq\
aDIIaL	I%jtDqHH	%jtDD	:>" [1_k@1D!+DA	
	 # IIKK5%%s9~&677Kr   )T)r   torch.nnnntorch.nn.utils.rnnr   r   RNNCellTensorr   r   GRUCellr    r#   LSTMCellr%   RNNr2   GRUr5   LSTMr7   r   r   r   <module>rB      s      -8bjj 8U\\ 8ell 808bjj 8U\\ 8ell 8.8r{{ 8u|| 8 8"8 "8J"8 "8J"8"'' "8r   