§
    l¬¸jð  ã                   ó  — d dl Z d dl mZ d dlmZ 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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)Únn)ÚPackedSequencec                 ó(  — t          | t          ¦  «        r&t          | j                             ¦   «         ¦  «        S |                      ¦   «         dk    r|                      d¦  «        n*|                      d¦  «        |                      d¦  «        z  S )zDReturn how many cell evaluations a recurrent layer's input asks for.é   r   é   )Ú
isinstancer   ÚintÚbatch_sizesÚsumÚdimÚsize)Úxs    ú^/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/thop/rnn_hooks.pyÚ_cell_evaluationsr      sm   € å�!•^Ñ$Ô$ð (Ý�1”=×$Ò$Ñ&Ô&Ñ'Ô'Ð'ØŸš™œ 1š˜ˆ1�6Š6�!‰9Œ9ˆ9¨!¯&ª&°©)¬)°a·f²f¸Q±i´iÑ*?Ð?ó    Tc                 ó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.r   © )Ú
input_sizeÚhidden_sizeÚbiasÚ	total_opss       r   Ú_count_rnn_cellr      s2   € à˜z¨KÑ7Ñ8¸;ÑF€IØð %Ø�[ 1‘_Ñ$ˆ	àÐr   Úmr   Úyc                 óü   — t          | j        | j        | j        ¦  «        }||d                              ¦   «         dk    r|d                              d¦  «        ndz  }| xj        t          |¦  «        z  c_        dS )z^Counts the total RNN cell operations based on input tensor, hidden size, bias, and batch size.r   r   r   N)r   r   r   r   r   r   r   r	   ©r   r   r   r   s       r   Úcount_rnn_cellr      óe   € å ¤¨a¬m¸Q¼VÑDÔD€Ið   1¤§¢¡¤¨q¢ ��1”—’˜1‘”�°aÑ7€Ià€K„K•3�y‘>”>Ñ!€K„K€K€Kr   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¸;ÑF€IØð %Ø�[ 1‘_Ñ$ˆ	Ø�˜Q‘Ñ€Ið �+ 
Ñ*¨kÑ9¸KÑGÑG€IØð %Ø�[ 1‘_Ñ$ˆ	à�Ñ€Ið �˜q‘Ñ €IàÐr   c                 óü   — t          | j        | j        | j        ¦  «        }||d                              ¦   «         dk    r|d                              d¦  «        ndz  }| xj        t          |¦  «        z  c_        dS )z\Calculates and updates the total operations for a GRU cell in a mini-batch during inference.r   r   r   N)r"   r   r   r   r   r   r   r	   r   s       r   Úcount_gru_cellr$   :   r   r   c                 ór   — d}|p|}| |z   |z  |z   }|r||dz  z  }||dz  z  }||dz  z  }||||z  z   z  }|S )zfCounts LSTM cell operations during inference based on input size, hidden size, and bias configuration.r   r   é   r    r   )r   r   r   Ú	proj_sizer   Úreal_hidden_sizer!   s          r   Ú_count_lstm_cellr)   D   sy   € à€IØ Ð/ KÐð Ð.Ñ.°+Ñ=ÀÑK€IØð %Ø�[ 1‘_Ñ$ˆ	Ø�˜Q‘Ñ€Ið �˜q‘Ñ €Ið �˜{¨YÑ6Ñ6Ñ6€IàÐr   c                 óü   — t          | j        | j        | j        ¦  «        }||d                              ¦   «         dk    r|d                              d¦  «        ndz  }| xj        t          |¦  «        z  c_        dS )zZCounts and updates the total operations for an LSTM cell in a mini-batch during inference.r   r   r   N)r)   r   r   r   r   r   r   r	   r   s       r   Úcount_lstm_cellr+   \   se   € å  ¤¨q¬}¸a¼fÑEÔE€Ið   1¤§¢¡¤¨q¢ ��1”—’˜1‘”�°aÑ7€Ià€K„K•3�y‘>”>Ñ!€K„K€K€Kr   c                 ó¤  — | j         }| j        }| j        }| j        }t	          |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  }| xj        t          |¦  «        z  c_        dS )zWCalculate and update the total number of operations for each RNN cell in a given batch.r   r   r   N)
r   r   r   Ú
num_layersr   Úbidirectionalr   Úranger   r	   ©
r   r   r   r   r   r   r-   Úevaluationsr   Ú_s
             r   Ú	count_rnnr3   f   óó   € àŒ6€DØ”€JØ”-€KØ”€Jå# A a¤DÑ)Ô)€Kà€IØ„ð DØ•_ Z°¸dÑCÔCÀaÑGÑGˆ	ˆ	à•_ Z°¸dÑCÔCÑCˆ	å�: ‘>Ñ"Ô"ð 
ð 
ˆØàŒðA�O˜K¨!™O¨[¸$Ñ?Ô?À!ÑCÐCå  ¨k¸4Ñ@Ô@ñ	
ˆ	ˆ	ð
 �Ñ€Ià€K„K•3�y‘>”>Ñ!€K„K€K€Kr   c                 ó¤  — | j         }| j        }| j        }| j        }t	          |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  }| xj        t          |¦  «        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	   r0   s
             r   Ú	count_grur6   €   r4   r   c                 óÂ  — | j         }| j        }| j        }| j        }| j        }|p|}t          |d         ¦  «        }	d}
| j        r|
t          ||||¦  «        dz  z  }
n|
t          ||||¦  «        z  }
t          |dz
  ¦  «        D ]6}|
| j        rt          |dz  |||¦  «        dz  nt          ||||¦  «        z  }
Œ7|
|	z  }
| xj	        t          |
¦  «        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-   r'   Ústacked_sizer1   r   r2   s               r   Ú
count_lstmr9   š   s  € àŒ6€DØ”€JØ”-€KØ”€JØ”€IØÐ+ €Lå# A a¤DÑ)Ô)€Kà€IØ„ð PØÕ% j°+¸tÀYÑOÔOÐRSÑSÑSˆ	ˆ	àÕ% j°+¸tÀYÑOÔOÑOˆ	å�: ‘>Ñ"Ô"ð 
ð 
ˆØàŒðNÕ˜\¨AÑ-¨{¸DÀ)ÑLÔLÈqÑPÐPå! ,°¸TÀ9ÑMÔMñ	
ˆ	ˆ	ð
 �Ñ€Ià€K„K•3�y‘>”>Ñ!€K„K€K€Kr   )T)Tr   )Útorchr   Útorch.nn.utils.rnnr   r   r   ÚRNNCellÚTensorr   r"   ÚGRUCellr$   r)   ÚLSTMCellr+   ÚRNNr3   ÚGRUr6   ÚLSTMr9   r   r   r   ú<module>rC      sp  ðð €€€Ø Ð Ð Ð Ð Ð Ø -Ð -Ð -Ð -Ð -Ð -ð@ð @ð @ðð ð ð ð"�b”jð " U¤\ð "°e´lð "ð "ð "ð "ðð ð ð ð0"�b”jð " U¤\ð "°e´lð "ð "ð "ð "ðð ð ð ð0"�r”{ð " u¤|ð "¸¼ð "ð "ð "ð "ð"�”ð "ð "ð "ð "ð4"�”ð "ð "ð "ð "ð4"�"”'ð "ð "ð "ð "ð "ð "r   