
    h                         S SK r S SKJr  S SKrS SKrS SKJr  S SKJr  S SK	J
r
  SSKJrJr  SSKJr  \" \R                   5      \" S5      :  a   \ R"                  " S	\R                    S
35        S rS rS rS rS rS\R.                  4S jrS\R.                  4S jrS\R4                  4S jrS\R8                  4S jr\R<                  \R>                  \R@                  \RB                  \RD                  \RF                  4r$\RJ                  \\R4                  \\R8                  \S\S\S\S\S\S\S\0
r&\$ H  r'\\&\''   M
     0 r(S r)S)S\R.                  S\RT                  4S  jjr+\,S!:X  a_   " S" S#\R.                  5      r- " S$ S%\R                  R.                  5      r.\." 5       r/\R`                  " S&S'5      r1\+" \/\1SS(9r2\3" \25        gg)*    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                     g)zYEnsures tensor array sizes are appropriate by clamping specified input and output shapes.r    input_shapesoutput_shapess     I/home/james-whalen/.local/lib/python3.13/site-packages/thop/fx_profile.pycount_clampr              c                 (    US   R                  5       $ )z9Returns the number of elements in the first output shape.r   numelr   s     r   	count_mulr      s    !!##r   c                 H    U S   nUS   nUS   nUR                  5       nXE-  $ )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      s3    AHa I2,K??$L%%r   c                 R    [        X5      nSU;   a  XAS   R                  5       -  nU$ )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#   (   s/    5Eq!''))Lr   c                    Uu  pEpgpn
[        U 5      S:X  a  U u  pO[        U 5      S:X  a  U u  pnUS   nWSS R                  5       nSnWS   n[        UXR                  5       UU
5      R                  5       n[	        U5      $ )zqCalculates 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weightr   stridepaddingdilationgroupsx_shapek_shapeb_shaper   kernel_parametersbias_op
in_channel	total_opss                      r   count_fn_conv2dr7   0   s     ?C;FD'V
<A'	\	a	$0!'a I))+GJw(9??;LjZ`affhIy>r   modulec                     [        X5      $ )zQCounts the FLOPs for a fully connected (linear) layer in a neural network module.)r   )r8   r   r   s      r   count_nn_linearr:   C   s    44r   c                     g)zRReturns 0 for a neural network module, input shapes, and output shapes in PyTorch.r   r   )r8   r   r   r    r!   s        r   count_zero_opsr<   H   r   r   c                    U R                   b  SOSnUS   nU R                  nU R                  nU R                  R                  SS R                  5       n[        X7UR                  5       XV5      R                  5       n[        U5      $ )zUCalculates FLOPs for a 2D Conv2D layer in an nn.Module using input and output shapes.Nr   r   r%   )	r   in_channelsr/   r+   shaper   r	   r(   r)   )	r8   r   r   r4   r   r5   r/   
kernel_opsr6   s	            r   count_nn_conv2drA   M   sw    ;;*aGa I##J]]F$$QR(..0JwIOO4EzZ__aIy>r   c                 ^    [        U5      S:X  d   S5       eUS   nSUR                  5       -  $ )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   )r8   r   r   ys       r   count_nn_bn2drD   Y   s5    }"P$PP"aAqwwy=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                      g)zOA no-op print function that takes any arguments without performing any actions.Nr   )r    r!   s     r   
null_printrG   |   s    
r   Fmodinputc                 R   [        U 5      n[        U5      R                  U5        [        nU(       a  [        n0 nSnUR
                  R                   GH  nU" SUR                   SUR                   SUR                   SUR                   35        Sn/ n	U" SSS	9  UR                   HE  n
[        U
5      U;  a  M  U" U[        U
5          SS	9  U	R                  U[        U
5         5        MG     U" 5         U" S
UR                  S   R                   35        UR                  S   R                  /nUR                  S;   a  SnGOKUR                  S:X  a  [        UR                  5      R                  S5      S   R!                  SS5      R!                  SS5      R#                  5       nU[$        ;   a)  [$        U   " X/UR                  Q70 UR&                  D6nGOUR                  U4[(        U'   [+        SU S35        GOUR                  S:X  aU  [        UR                  5      nU[$        ;   a  [$        U   " X5      nGOMUR                  U4[(        U'   [+        U S35        GO(UR                  S:X  Ga  U R-                  UR                  5      n[/        U5      nU" [/        U5      [/        U5      [$        ;   5        [/        U5      [$        ;   a  [$        [/        U5         " XU5      nO"UR                  4[(        U'   [+        U S35        [	        S[/        U5      5        [1        U[2        5      (       a  [	        S5        OJ[	        [/        U5      5        [	        SU R5                  5       UR                   S3   R                   35        UR                  S   R                  U[        UR                  5      '   Ub  Xh-  n[7        SU SU 35        U" S5        GM     [9        [(        R;                  5       5      S:  a  SSKJn  [	        S5        U" [(        5        U$ ) 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   	propagaterG   printgraphnodesoptargetnamer    strappendmetar?   splitreplacestrip	count_mapr!   missing_mapsr   get_submoduletype
isinstancezero_ops
state_dictr   r'   keysrX   )rH   rI   verbosegmfprintv_mapstotal_flopsnode
node_flopsr   argr   keymrX   s                  r   
fx_profilerx      sw   -c2BbME"FFK	DKK=TYYK|\`\e\e[fgh
~4(99C3xv%fSX&'d3s3x 01	 
 	=!9!?!? @AB=177877//JWW'dkk"((.q199#rBJJ3PRSYY[Ci&s^Lc$))cW[WbWbc
%)WWcNS!#l+,WW% dkk"Ci&s^LH
%)WWcNS![)*WW% !!$++.Aq'C47DGy01Aw)#&tAw/O
%)WWJS![)*.$q'*!X&&*+d1gs~~'74;;-w8O'P'V'V&WXY!%=!9!?!?s499~!%K)*_[MRSys v <!#!#$|r   __main__c                       \ rS rSrSrS rSrg)MyOP   zHCustom operator that performs a simple forward pass dividing input by 1.c                     US-  $ )z*Performs forward pass on given input data.r   r   )selfrI   s     r   forwardMyOP.forward   s    19r   r   N)__name__
__module____qualname____firstlineno____doc__r   __static_attributes__r   r   r   r{   r{      s
    V	r   r{   c                   2   ^  \ rS rSrSrU 4S jrS rSrU =r$ )MyModule   zHNeural network module with two linear layers and a custom MyOP operator.c                    > [         TU ]  5         [        R                  R	                  SS5      U l        [        R                  R	                  SS5      U l        [        5       U l        g)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   MyModule.__init__   sC    G 88??1a0DL 88??1a0DLDIr   c                     U R                  U5      nU R                  U5      R                  SSS9nU R                  X#-   5      $ )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   rE   r   )r~   xout1out2s       r   r   MyModule.forward   s?     <<?D<<?((Sc(:D99T[))r   )r   r   r   )	r   r   r   r   r   r   r   r   __classcell__)r   s   @r   r   r      s    V		* 	*r   r      r   )rn   )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#   r7   Moduler:   r<   Conv2drA   BatchNorm2drD   ReLUReLU6Dropout	MaxPool2d	AvgPool2dAdaptiveAvgPool2drk   r   rf   krg   rG   Tensorrx   r   r{   r   netrandndatar"   rZ   r   r   r   <module>r      s    *    # 0 " ,!!"\'%::OO
`afarar`ssuv

$
&&5BII 5
299 
	BII 	"..  GGHHJJLLLL IIIINNM[^.+_Y	 
A!IaL 
 
LBII Lbii L^ zryy *588?? *$ *C88B?DsD%0E	%L= r   