o
    Èhg  ã                   @   sÆ   d dl Z d dlmZ d dlmZ ddd„Zdejde jde jfd	d
„Zddd„Z	dej
de jde jfdd„Zddd„Zdejde jde jfdd„Zdejfdd„Zdejfdd„Zdejf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   úb/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/thop/rnn_hooks.pyÚ_count_rnn_cell   s   r
   ÚmÚxÚyc                 C   óF   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   ó   r   c                 C   sl   d}||  | | }|r||d 7 }||d 7 }|||  | | 7 }|r*||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   Ú	state_opsr   r   r	   Ú_count_gru_cell   s   r   c                 C   r   )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_cell3   r   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   r   )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_cellT   r   r   c                 C   ó"  | j }| j}| j}| j}t|d tƒr$t |d j¡}|d j 	d¡}n | j
r6|d  	d¡}|d  	d¡}n|d  	d¡}|d  	d¡}d}	| jrT|	t|||ƒd 7 }	n|	t|||ƒ7 }	t|d ƒD ]}
|	| jrrt|d ||ƒd nt|||ƒ7 }	qb|	|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   Ú	num_stepsr   Ú_r   r   r	   Ú	count_rnn^   ó2   ÿ
ýr+   c                 C   r   )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ƒ   r,   r-   c                 C   r   )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¨   r,   r.   )T)r   Útorch.nnÚnnÚtorch.nn.utils.rnnr   r
   ÚRNNCellÚTensorr   r   ÚGRUCellr   r   ÚLSTMCellr   ÚRNNr+   ÚGRUr-   ÚLSTMr.   r   r   r   r	   Ú<module>   s   
	




%%