
    jg                        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dS )    N)PackedSequenceTc                 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       S/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/thop/rnn_hooks.py_count_rnn_cellr      s2    zK78;FI %[1_$	    mxyc                     t          | j        | j        | j                  }|d                             d          }||z  }| xj        t          j        t          |          g          z  c_        dS )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      ^    amQVDDI11JIKK5%s9~~&6777KKKKr   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                             d          }||z  }| xj        t          j        t          |          g          z  c_        dS )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#   =   sc    I k)[8;FI %[1_$	QI q I Ir   c                     t          | j        | j        | j                  }|d                             d          }||z  }| xj        t          j        t          |          g          z  c_        dS )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   s^     q}afEEI11JIKK5%s9~~&6777KKKKr   c                 H   | j         }| j        }| j        }| j        }t	          |d         t
                    r@t          j        |d         j                  }|d         j        	                    d          }nt| j
        r7|d         	                    d          }|d         	                    d          }n6|d         	                    d          }|d         	                    d          }d}	| j        r|	t          |||          dz  z  }	n|	t          |||          z  }	t          |dz
            D ]4}
|	| j        rt          |dz  ||          dz  nt          |||          z  }	5|	|z  }	|	|z  }	| xj        t          j        t!          |	          g          z  c_        dS )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   ^      6DJ-KJ!A$'' !Yqt/00
aD$))!,,			
 !qTYYq\\
aDIIaLL		qTYYq\\
aDIIaLL	I D_ZdCCaGG		_ZdCCC	:>"" 
 
AOK!O[$??!CC k4@@	
		 IIKK5%s9~~&6777KKKKr   c                 H   | j         }| j        }| j        }| j        }t	          |d         t
                    r@t          j        |d         j                  }|d         j        	                    d          }nt| j
        r7|d         	                    d          }|d         	                    d          }n6|d         	                    d          }|d         	                    d          }d}	| j        r|	t          |||          dz  z  }	n|	t          |||          z  }	t          |dz
            D ]4}
|	| j        rt          |dz  ||          dz  nt          |||          z  }	5|	|z  }	|	|z  }	| xj        t          j        t!          |	          g          z  c_        dS )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                 H   | j         }| j        }| j        }| j        }t	          |d         t
                    r@t          j        |d         j                  }|d         j        	                    d          }nt| j
        r7|d         	                    d          }|d         	                    d          }n6|d         	                    d          }|d         	                    d          }d}	| j        r|	t          |||          dz  z  }	n|	t          |||          z  }	t          |dz
            D ]4}
|	| j        rt          |dz  ||          dz  nt          |||          z  }	5|	|z  }	|	|z  }	| xj        t          j        t!          |	          g          z  c_        dS )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   6DJ-KJ!A$'' !Yqt/00
aD$))!,,			
 !qTYYq\\
aDIIaLL		qTYYq\\
aDIIaLL	I E%j+tDDqHH		%j+tDDD	:>"" 
 
B[1_k4@@1DD!+{DAA	
		 IIKK5%s9~~&6777KKKKr   )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`          - - - - - -   8bj 8U\ 8el 8 8 8 8   08bj 8U\ 8el 8 8 8 8   .8r{ 8u| 8 8 8 8 8"8 "8 "8 "8 "8J"8 "8 "8 "8 "8J"8"' "8 "8 "8 "8 "8 "8r   