o
    h,                     @   s  d dl T d dlT ddlmZ ejZi eje	ej
eejeejeejeejeejeejeejeejeejeejeejeejeejeejeeje	i eje	eje ej!e	ej"e	ej#e	ej$e	ej%e	ej&e	ej'e(ej)e(ej*e(ej+e,ej-e,ej.e,ej/e0ej1e	ej2e3ej4e3ej5e3ej6e7ej8e9ej:e;ej<e=ej>e?ej@eAejBe	ejCe	ejDeiZEdddZF				dd	ejGfd
dZHdS )    )*   )prRedNTFc                    s2  g t   du ri  rd fdd}| j}|   | | t  | |  W d   n1 s8w   Y  d}d}|  D ]}	t|	 rNqE||	j	7 }||	j
7 }qE| }| }| | D ]}
|
  qh|  D ]!\}}	t|	 r~qsd|	jv r|	jd d|	jv r|	jd qs||fS )z^Profiles a PyTorch model's operations and parameters, applying either custom or default hooks.NTc                    sL  t |  rd S t| dst| drtdt|  d | dtjdt	d | dtjdt	d | 
 D ]}|  jt| g7  _q7t| }d }| v rh | }|vrgrgtd|j d| d	 n)|tv rt| }|vrrtd
|j d| d	 n|vrrtd| d |d ur| |}| | d S )N	total_opstotal_paramsz9Either .total_ops or .total_params is already defined in z3. Be careful, it might change your code's behavior.r   dtype[INFO] Customize rule () .[INFO] Register () for [WARN] Cannot find rule for (. Treat it as zero Macs and zero Params.)listchildrenhasattrloggingwarningstrregister_buffertorchzerosdefault_dtype
parametersr   DoubleTensornumeltypeprint__qualname__register_hooksr   register_forward_hookappendadd)mpm_typefnhandler
custom_opshandler_collectionreport_missingtypes_collectionverbose `/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/thop/profile.py	add_hooksD   s8   

z!profile_origin.<locals>.add_hooksr   r   r   )settrainingevalapplyr   no_gradmodulesr   r   r   r   itemtrainremovenamed_modules_bufferspop)modelinputsr*   r.   r,   r1   r3   r   r   r$   r(   nr/   r)   r0   profile_origin;   sD   $







rA   r>   c                    s  i t   du ri  rddtjf fdd}| j}|   | | t  | |  W d   n1 s<w   Y  ddtjdttfffd	d
| \}}	}
| 	| 
 D ]\}\}}|  |  |jd |jd qa|r||	|
fS ||	fS )zdProfiles a PyTorch model, returning total operations, parameters, and optionally layer-wise details.NTr$   c                    s   |  dtjdtjd |  dtjdtjd t| }d}| v r9 | }|vr8r8td|j d| d n)|tv rTt| }|vrSrStd	|j d
| d n|vrbrbtd| d |durr| 	|| 	t
f| < | dS )zTRegisters hooks to a neural network module to track total operations and parameters.r   r   r   r   Nr	   r
   r   r   r   r   r   )r   r   r   float64r   r   r   r    r   r!   count_parametersr#   )r$   r&   r'   r)   r/   r0   r1      s*   zprofile.<locals>.add_hooks	modulereturnc           
         s   | j  d}}i }|  D ]9\}}i }|v r-t|tjtjfs-|j  |j }}	n ||d d\}}	}||	|f||< ||7 }||	7 }q|||fS )zfRecursively counts the total operations and parameters of the given PyTorch module and its submodules.r   rD   )prefix)r   r8   named_children
isinstancenn
Sequential
ModuleListr   )
rE   rG   r   r   ret_dictr@   r$   	next_dictm_opsm_params)	dfs_countr+   r/   r0   rQ      s   

zprofile.<locals>.dfs_countr   r   )rD   )r2   rJ   Moduler3   r4   r5   r   r6   intr9   itemsr:   r<   r=   )r>   r?   r*   r.   ret_layer_infor,   r1   prev_training_statusr   r   rM   r$   
op_handlerparams_handlerr/   )r*   rQ   r+   r,   r-   r.   r0   profile   s0   	


 

rY   )NTF)NTFF)Ithop.rnn_hooksthop.vision.basic_hooksutilsr   r   rB   r   rJ   	ZeroPad2dzero_opsConv1dcount_convNdConv2dConv3dConvTranspose1dConvTranspose2dConvTranspose3dBatchNorm1dcount_normalizationBatchNorm2dBatchNorm3d	LayerNormInstanceNorm1dInstanceNorm2dInstanceNorm3dPReLUcount_preluSoftmaxcount_softmaxReLUReLU6	LeakyReLU
count_relu	MaxPool1d	MaxPool2d	MaxPool3dAdaptiveMaxPool1dAdaptiveMaxPool2dAdaptiveMaxPool3d	AvgPool1dcount_avgpool	AvgPool2d	AvgPool3dAdaptiveAvgPool1dcount_adap_avgpoolAdaptiveAvgPool2dAdaptiveAvgPool3dLinearcount_linearDropoutUpsamplecount_upsampleUpsamplingBilinear2dUpsamplingNearest2dRNNCellcount_rnn_cellGRUCellcount_gru_cellLSTMCellcount_lstm_cellRNN	count_rnnGRU	count_gruLSTM
count_lstmrK   PixelShuffleSyncBatchNormr    rA   rR   rY   r/   r/   r/   r0   <module>   s   	
 !"#
1T