Ë
    Dêñi×   ã                   ó–  — d dl Z d dlmZ d dlZd dlZd dlmZ d dlmZ d dl	m
Z
 ddlmZmZ ddlmZ  eej                   «       ed«      k  r  e j"                  d	ej                   › d
�«       d„ Zd„ Zd„ Zd„ Zd„ Zdej.                  fd„Zdej.                  fd„Zdej4                  fd„Zdej8                  fd„Zej<                  ej>                  ej@                  ejB                  ejD                  ejF                  fZ$ejJ                  eej4                  eej8                  edededededededei
Z&e$D ]  Z'ee&e'<   Œ	 i Z(d„ Z)d)dej.                  dejT                  fd „Z+e,d!k(  rb G d"„ d#ej.                  «      Z- G d$„ d%ej                  j.                  «      Z. e.«       Z/ ej`                  d&d'«      Z1 e+e/e1d¬(«      Z2 e3e2«       yy)*é    N)ÚLooseVersion)Úsymbolic_trace)Ú	ShapePropé   )ÚprRedÚprYellow)Úcalculate_convz1.8.0zVtorch.fx requires version higher than 1.8.0. But You are using an old version PyTorch z. c                  ó   — y)zYEnsures tensor array sizes are appropriate by clamping specified input and output shapes.r   © ©Úinput_shapesÚoutput_shapess     úQ/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/thop/fx_profile.pyÚcount_clampr      ó   € àó    c                 ó(   — |d   j                  «       S )z9Returns the number of elements in the first output shape.r   ©Únumelr   s     r   Ú	count_mulr      s   € à˜Ñ×!Ñ!Ó#Ð#r   c                 óJ   — | d   }|d   }|d   }|j                  «       }||z  S )zgCalculates matrix multiplication ops based on input and output tensor shapes for performance profiling.r   éÿÿÿÿr   )r   r   Úin_shapeÚ	out_shapeÚin_featuresÚnum_elementss         r   Úcount_matmulr      s5   € à˜A‰€HØ˜aÑ €IØ˜2‘,€KØ—?‘?Ó$€LØ˜Ñ%Ð%r   c                 óR   — t        | |«      }d|v r||d   j                  «       z  }|S )zVCalculates the total FLOPs for a linear layer, including bias operations if specified.Úbiasr   )r   r   )r   r   ÚargsÚkwargsÚflopss        r   Úcount_fn_linearr#   (   s3   € ä˜ }Ó5€EØ�ÑØ�˜qÑ!×'Ñ'Ó)Ñ)ˆØ€Lr   c                 ó  — |\  }}}}}}	}
t        | «      dk(  r| \  }}nt        | «      dk(  r| \  }}}|d   }dd j                  «       }d}d   }t        |||j                  «       ||
«      j                  «       }t	        |«      S )zuCalculates total operations (FLOPs) for a 2D conv layer based on input and output shapes using `calculate_conv`.
    é   é   r   Nr   )Úlenr   r	   ÚitemÚint)r   r   r    r!   Ú_inputsÚ_weightÚ_biasÚ_strideÚ_paddingÚ	_dilationÚgroupsÚx_shapeÚk_shapeÚ_b_shaper   Úkernel_parametersÚbias_opÚ
in_channelÚ	total_opss                      r   Úcount_fn_conv2dr8   0   s¥   € ð EIÑA€GˆW�e˜W h°	¸6Ü
ˆ<Ó˜AÒØ'Ñˆ‘Ü	ˆ\Ó	˜aÒ	Ø%1Ñ"ˆ�˜(Ø˜aÑ €Ià  ˜×)Ñ)Ó+ÐØ€GØ˜‘€Jä˜wÐ(9¸9¿?¹?Ó;LÈjÐZ`Óa×fÑfÓh€IÜˆy‹>Ðr   Úmodulec                 ó   — t        ||«      S )zQCounts the FLOPs for a fully connected (linear) layer in a neural network module.)r   )r9   r   r   s      r   Úcount_nn_linearr;   B   s   € ä˜ mÓ4Ð4r   c                  ó   — y)zRReturns 0 for a neural network module, input shapes, and output shapes in PyTorch.r   r   )r9   r   r   r    r!   s        r   Úcount_zero_opsr=   G   r   r   c                 ó  — | j                   �dnd}|d   }| j                  }| j                  }| j                  j                  dd j                  «       }t        |||j                  «       ||«      j                  «       }t        |«      S )zUCalculates FLOPs for a 2D Conv2D layer in an nn.Module using input and output shapes.Nr   r   r%   )	r   Úin_channelsr0   ÚweightÚshaper   r	   r(   r)   )	r9   r   r   r5   r   r6   r0   Ú
kernel_opsr7   s	            r   Úcount_nn_conv2drC   L   s|   € à—;‘;Ð*‰a°€GØ˜aÑ €Ià×#Ñ#€JØ�]‰]€FØ—‘×$Ñ$ Q RÐ(×.Ñ.Ó0€JÜ˜w¨
°I·O±OÓ4EÀzÐSYÓZ×_Ñ_Óa€IÜˆy‹>Ðr   c                 ó\   — t        |«      dk(  sJ d«       ‚|d   }d|j                  «       z  S )zLCalculate FLOPs for an nn.BatchNorm2d layer based on the given output shape.r   z*nn.BatchNorm2d should only have one outputr   r%   )r'   r   )r9   r   r   Úys       r   Úcount_nn_bn2drF   X   s5   € äˆ}Ó Ò"ÐPÐ$PÓPÐ"Ø�aÑ€AØˆq�w‰w‹y‰=Ðr   zfunction linearÚclampzbuilt-in function addzbuilt-in method flz%built-in method conv2d of type objectzbuilt-in function mulzbuilt-in function truedivc                   ó   — y)zOA no-op print function that takes any arguments without performing any actions.Nr   )r    r!   s     r   Ú
null_printrI   {   s   € à
r   FÚmodÚinputc                 ó"  — t        | «      }t        |«      j                  |«       t        }|rt        }i }d}|j
                  j                  D �]€  } |d|j                  › d|j                  › d|j                  › d|j                  › �«       d}g }	 |dd¬	«       |j                  D ]D  }
t        |
«      |vrŒ ||t        |
«         › d¬	«       |	j                  |t        |
«         «       ŒF  |«         |d
|j                  d   j                  › �«       |j                  d   j                  g}|j                  dv rd}�n<|j                  dk(  r¬t        |j                  «      j                  d«      d   j!                  dd«      j!                  dd«      j#                  «       }|t$        v r)t%        |   |	|g|j                  ¢­i |j&                  ¤Ž}�n§|j                  |ft(        |<   t+        d|› d�«       �n�|j                  dk(  rSt        |j                  «      }|t$        v rt%        |   |	|«      }�nD|j                  |ft(        |<   t+        |› d�«       �n|j                  dk(  �r| j-                  |j                  «      }t/        |«      } |t/        |«      t/        |«      t$        v «       t/        |«      t$        v rt%        t/        |«         ||	|«      }n"|j                  ft(        |<   t+        |› d�«       t	        dt/        |«      «       t1        |t2        «      rt	        d«       nJt	        t/        |«      «       t	        d| j5                  «       |j                  › d�   j                  › �«       |j                  d   j                  |t        |j                  «      <   |�||z  }t7        d|› d|› �«        |d«       �Œƒ t9        t(        j;                  «       «      dkD  rddlm} t	        d«        |t(        «       |S ) zVProfiles nn.Module for total FLOPs per operation and prints detailed nodes if verbose.r   zNodeOP:z	,	Target:z,	NodeName:z,	NodeArgs:Nzinput_shape:ú	)Úendzoutput_shape:	Útensor_meta)ÚoutputÚplaceholderÚcall_functionÚatú<Ú ú>ú|z| is missingÚcall_methodz is missingÚcall_modulezmodule type:zweight_shape: Nonezweight_shape: z.weightzCurrent node's FLOPs: z, total FLOPs: zP================================================================================)ÚpprintzMissing operators: )r   r   Ú	propagaterI   ÚprintÚgraphÚnodesÚopÚtargetÚnamer    ÚstrÚappendÚmetarA   ÚsplitÚreplaceÚstripÚ	count_mapr!   Úmissing_mapsr   Úget_submoduleÚtypeÚ
isinstanceÚzero_opsÚ
state_dictr   r'   ÚkeysrZ   )rJ   rK   ÚverboseÚgmÚfprintÚv_mapsÚtotal_flopsÚnodeÚ
node_flopsr   Úargr   ÚkeyÚmrZ   s                  r   Ú
fx_profilerz   €   s†  € ä-¨cÓ2€BÜˆbƒM×Ñ˜EÔ"ä€FÙÜˆà€FØ€Kà—‘—‘ó 9ˆá�˜Ÿ™˜	 ¨D¯K©K¨=¸ÀTÇYÁYÀKÈ|Ð\`×\eÑ\eÐ[fÐgÔhàˆ
àˆÙˆ~ 4Õ(Ø—9‘9ò 	2ˆCÜ�3‹x˜vÑ%ØÙ�fœS ›XÑ&Ð'¨dÕ3Ø×Ñ ¤s¨3£xÑ 0Õ1ð		2ñ
 	ŒÙ� §¡¨=Ñ!9×!?Ñ!?Ð @ÐAÔBØŸ™ =Ñ1×7Ñ7Ð8ˆØ�7‰7Ð/Ñ/ØŠJØ�W‰W˜Ò'ä�d—k‘kÓ"×(Ñ(¨Ó.¨qÑ1×9Ñ9¸#¸rÓB×JÑJÈ3ÐPRÓS×YÑYÓ[ˆCØ”iÑÜ& s™^¨L¸-ÐcÈ$Ï)É)ÒcÐW[×WbÑWbÑc’
à%)§W¡W¨c N”˜SÑ!Ü˜˜#˜˜lÐ+Ö,Ø�W‰W˜Ò%ô �d—k‘kÓ"ˆCØ”iÑÜ& s™^¨L¸-ÓH’
à%)§W¡W¨c N”˜SÑ!Ü˜˜˜[Ð)Ö*Ø�W‰W˜Ó%ð ×!Ñ! $§+¡+Ó.ˆAÜ�q“'ˆCÙ”4˜“7œD ›G¤yÐ0Ô1Ü�A‹wœ)Ñ#Ü&¤t¨A£wÑ/°°<ÀÓO‘
à%)§W¡W J”˜SÑ!Ü˜˜˜[Ð)Ô*Ü�.¤$ q£'Ô*Ü˜!œXÔ&ÜÐ*Õ+ä”d˜1“g”Ü˜ s§~¡~Ó'7¸4¿;¹;¸-ÀwÐ8OÑ'P×'VÑ'VÐ&WÐXÔYà!%§¡¨=Ñ!9×!?Ñ!?ˆŒs�4—9‘9‹~ÑØÐ!Ø˜:Ñ%ˆKÜÐ)¨*¨°_À[ÀMÐRÔSÙˆyÖðs9ôv Œ<×ÑÓÓ !Ò#Ý!äÐ#Ô$ÙŒ|ÔØÐr   Ú__main__c                   ó   — e Zd ZdZd„ Zy)ÚMyOPzHCustom operator that performs a simple forward pass dividing input by 1.c                 ó   — |dz  S )z*Performs forward pass on given input data.r   r   )ÚselfrK   s     r   ÚforwardzMyOP.forwardÔ   s   € à˜1‘9Ðr   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r€   r   r   r   r}   r}   Ñ   s
   „ ÙVó	r   r}   c                   ó(   ‡ — e Zd ZdZˆ fd„Zd„ Zˆ xZS )ÚMyModulezHNeural network module with two linear layers and a custom MyOP operator.c                 óÔ   •— t         ‰| �  «        t        j                  j	                  dd«      | _        t        j                  j	                  dd«      | _        t        «       | _        y)zGInitializes MyModule with two linear layers and a custom MyOP operator.é   r&   N)	ÚsuperÚ__init__ÚtorchÚnnÚLinearÚlinear1Úlinear2r}   Úmyop)r   Ú	__class__s    €r   rŠ   zMyModule.__init__Û   sC   ø€ ä‰GÑÔÜ Ÿ8™8Ÿ?™?¨1¨aÓ0ˆDŒLÜ Ÿ8™8Ÿ?™?¨1¨aÓ0ˆDŒLÜ›ˆD�Ir   c                 ó�   — | j                  |«      }| j                  |«      j                  dd¬«      }| j                  ||z   «      S )z“Applies two linear transformations to the input tensor, clamps the second, then combines and processes
            with MyOP operator.
            g        g      ð?)ÚminÚmax)rŽ   r�   rG   r�   )r   ÚxÚout1Úout2s       r   r€   zMyModule.forwardâ   sA   € ð —<‘< “?ˆDØ—<‘< “?×(Ñ(¨S°cÐ(Ó:ˆDØ—9‘9˜T D™[Ó)Ð)r   )r�   r‚   rƒ   r„   rŠ   r€   Ú__classcell__)r‘   s   @r   r†   r†   Ø   s   ø„ ÙVô	ö	*r   r†   é   rˆ   )rp   )F)4ÚloggingÚdistutils.versionr   r‹   ÚthÚtorch.nnrŒ   Útorch.fxr   Útorch.fx.passes.shape_propr   Úutilsr   r   Úvision.calc_funcr	   Ú__version__Úwarningr   r   r   r#   r8   ÚModuler;   r=   ÚConv2drC   ÚBatchNorm2drF   ÚReLUÚReLU6ÚDropoutÚ	MaxPool2dÚ	AvgPool2dÚAdaptiveAvgPool2drm   r�   rh   Úkri   rI   ÚTensorrz   r�   r}   r†   ÚnetÚrandnÚdatar"   r\   r   r   r   ú<module>r²      sÙ  ðó Ý *ã Û Ý Ý #Ý 0ç "Ý ,á�×!Ñ!Ó"¡\°'Ó%:Ò:Ø€G‡O�OØ
`Ðaf×arÑarÐ`sÐsuÐvôò
ò
$ò
&òòð$5˜BŸI™Ió 5ð
˜2Ÿ9™9ó ð
	˜BŸI™Ió 	ð˜"Ÿ.™.ó ð ‡G�GØ‡H�HØ‡J�JØ‡L�LØ‡L�LØ×Ñð€ð ‡I�IˆØ‡I�IˆØ‡N�N�MØ�Øˆ[Ø˜^Ø˜.Ø+¨_Ø˜YØ ð€	ð 
ò "€AØ!€Iˆa‚Lð"ð €òñ
L�B—I‘Ið L b§i¡ió Lð^ ˆzÒôˆr�y‰yô ô*�5—8‘8—?‘?ô *ñ$ ‹*€CØˆ2�8‰8�B˜‹?€DÙ�s˜D¨%Ô0€EÙ	ˆ%…Lð= r   