
    |hg                        d dl Z d dlmZ d dlmZ ddZdej                  de j                  de j                  fdZddZ	dej                  de j                  de j                  fd	Zdd
Zdej                  de j                  de j                  fdZdej                  fdZdej"                  fdZdej&                  fdZy)    N)PackedSequencec                 0    || |z   z  |z   }|r||dz  z  }|S )z`Calculate the total operations for an RNN cell given input size, hidden size, and optional bias.    )
input_sizehidden_sizebias	total_opss       M/var/www/html/test/engine/venv/lib/python3.12/site-packages/thop/rnn_hooks.py_count_rnn_cellr      s-    zK78;FI[1_$	    mxyc                     t        | j                  | j                  | j                        }|d   j	                  d      }||z  }| xj
                  t        j                  t        |      g      z  c_        y)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      W    ammQVVDI11JIKK5%%s9~&677Kr   c                     d}|| z   |z  |z   }|r||dz  z  }||dz  z  }||| z   |z  |z   z  }|r||dz  z  }||z  }||dz  z  }|S )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      s    I z)[8;FI[1_$	QI +
*k9KGGI[1_$	I q Ir   c                     t        | j                  | j                  | j                        }|d   j	                  d      }||z  }| xj
                  t        j                  t        |      g      z  c_        y)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                 ^    d}| |z   |z  |z   }|r||dz  z  }||dz  z  }||dz  z  }||z  }|S )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#   =   s^    I k)[8;FI[1_$	QI q I Ir   c                     t        | j                  | j                  | j                        }|d   j	                  d      }||z  }| xj
                  t        j                  t        |      g      z  c_        y)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   sW     q}}affEI11JIKK5%%s9~&677Kr   c                 &   | j                   }| j                  }| j                  }| j                  }t	        |d   t
              rAt        j                  |d   j                        }|d   j                  j                  d      }n]| j                  r)|d   j                  d      }|d   j                  d      }n(|d   j                  d      }|d   j                  d      }d}	| j                  r|	t        |||      dz  z  }	n|	t        |||      z  }	t        |dz
        D ]1  }
|	| j                  rt        |dz  ||      dz  nt        |||      z  }	3 |	|z  }	|	|z  }	| xj                  t        j                  t!        |	      g      z  c_        y)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   ^   j   66DJ--KJ!A$'YYqt//0
aD$$))!,		
qTYYq\
aDIIaL	qTYYq\
aDIIaL	I_ZdCaGG	_ZdCC	:>" 
 K!O[$?!C k4@	
	
 IIKK5%%s9~&677Kr   c                 &   | j                   }| j                  }| j                  }| j                  }t	        |d   t
              rAt        j                  |d   j                        }|d   j                  j                  d      }n]| j                  r)|d   j                  d      }|d   j                  d      }n(|d   j                  d      }|d   j                  d      }d}	| j                  r|	t        |||      dz  z  }	n|	t        |||      z  }	t        |dz
        D ]1  }
|	| j                  rt        |dz  ||      dz  nt        |||      z  }	3 |	|z  }	|	|z  }	| xj                  t        j                  t!        |	      g      z  c_        y)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                   }| j                  }| j                  }| j                  }t	        |d   t
              rAt        j                  |d   j                        }|d   j                  j                  d      }n]| j                  r)|d   j                  d      }|d   j                  d      }n(|d   j                  d      }|d   j                  d      }d}	| j                  r|	t        |||      dz  z  }	n|	t        |||      z  }	t        |dz
        D ]1  }
|	| j                  rt        |dz  ||      dz  nt        |||      z  }	3 |	|z  }	|	|z  }	| xj                  t        j                  t!        |	      g      z  c_        y)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      sl   66DJ--KJ!A$'YYqt//0
aD$$))!,		
qTYYq\
aDIIaL	qTYYq\
aDIIaL	I%j+tDqHH	%j+tDD	:>" 
 [1_k4@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   