a
    f                      @   s   d dl Z d dlmZ d dlmZ dddZeje je jdddZdd	d
Z	ej
e je jdddZdddZeje je jdddZejdddZejdddZejdddZdS )    N)PackedSequenceTc                 C   s$   || |  | }|r ||d 7 }|S )z`Calculate the total operations for an RNN cell given input size, hidden size, and optional bias.    )
input_sizehidden_sizebias	total_opsr   r   J/var/www/html/django/DPS/env/lib/python3.9/site-packages/thop/rnn_hooks.py_count_rnn_cell   s    r
   )mxyc                 C   sF   t | j| j| j}|d d}||9 }|  jtt|g7  _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_sizer   r   r	   count_rnn_cell   s    r   c                 C   sl   d}||  | | }|r$||d 7 }||d 7 }|||  | | 7 }|rT||d 7 }||7 }||d 7 }|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   Z	state_opsr   r   r	   _count_gru_cell   s    r   c                 C   sF   t | j| j| j}|d d}||9 }|  jtt|g7  _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   r   r   r	   count_gru_cell1   s    r   c                 C   sH   d}| | | | }|r$||d 7 }||d 7 }||d 7 }||7 }|S )zfCounts LSTM cell operations during inference based on input size, hidden size, and bias configuration.r   r      r   r   r   r   r   r	   _count_lstm_cell;   s    r   c                 C   sF   t | j| j| j}|d d}||9 }|  jtt|g7  _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   r   r   r	   count_lstm_cellR   s    r   )r   c                 C   s"  | j }| j}| j}| j}t|d trHt|d j}|d j	d}n@| j
rl|d 	d}|d 	d}n|d 	d}|d 	d}d}	| jr|	t|||d 7 }	n|	t|||7 }	t|d D ].}
|	| jrt|d ||d n
t|||7 }	q|	|9 }	|	|9 }	|  jtt|	g7  _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   Z	num_stepsr   _r   r   r	   	count_rnn\   s2    
r&   c                 C   s"  | j }| j}| j}| j}t|d trHt|d j}|d j	d}n@| j
rl|d 	d}|d 	d}n|d 	d}|d 	d}d}	| jr|	t|||d 7 }	n|	t|||7 }	t|d D ].}
|	| jrt|d ||d n
t|||7 }	q|	|9 }	|	|9 }	|  jtt|	g7  _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$   r   r   r	   	count_gru   s2    
r'   c                 C   s"  | j }| j}| j}| j}t|d trHt|d j}|d j	d}n@| j
rl|d 	d}|d 	d}n|d 	d}|d 	d}d}	| jr|	t|||d 7 }	n|	t|||7 }	t|d D ].}
|	| jrt|d ||d n
t|||7 }	q|	|9 }	|	|9 }	|  jtt|	g7  _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$   r   r   r	   
count_lstm   s2    
r(   )T)T)T)r   Ztorch.nnnnZtorch.nn.utils.rnnr   r
   ZRNNCellZTensorr   r   ZGRUCellr   r   ZLSTMCellr   ZRNNr&   ZGRUr'   ZLSTMr(   r   r   r   r	   <module>   s   
	




%%