
    |h'                         d dl Z d dlmZ d dlZd dl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
dlmZ d Zdej                   fdZdej                   fdZdej&                  fdZdej*                  fdZej.                  ej0                  ej2                  ej4                  ej6                  ej8                  fZej<                  eej&                  eej*                  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 dl"m#Z# d dl$m%Z% d
dl&m'Z'm(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)LooseVersionz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     N/var/www/html/test/engine/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    }5Eq!''))Lr      )calculate_convc                    |\  }}}}}}	}
t        |       dk(  r| \  }}nt        |       dk(  r| \  }}}|d   }dd j                         }d}d   }t        |||j                         ||
      j                         }t	        |      S )zyCalculates 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_conv2dr3   .   s     ?C;FFD&'8V
<A'	\	a	$0!'a I))+GJw(99??;LjZ`affhIy>r   modulec                     t        ||      S )zQCounts the FLOPs for a fully connected (linear) layer in a neural network module.)r   )r4   r   r   s      r	   count_nn_linearr6   A   s    m44r   c                      y)zRReturns 0 for a neural network module, input shapes, and output shapes in PyTorch.r   r   )r4   r   r   r   r   s        r	   count_zero_opsr8   F   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_channelsr+   r'   shaper   r   r$   r%   )	r4   r   r   r0   r   r1   r+   
kernel_opsr2   s	            r	   count_nn_conv2dr=   K   s|    ;;*aGa I##J]]F$$QR(..0Jw
IOO4EzSYZ__aI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   )r4   r   r   ys       r	   count_nn_bn2dr@   W   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 truediv)symbolic_trace)	ShapeProp)prRedprYellowc                       y)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                 "   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(  rt        |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: )rB   rC   	propagaterG   printgraphnodesoptargetnamer   strappendmetar;   splitreplacestrip	count_mapr   missing_mapsrD   get_submoduletype
isinstancezero_ops
state_dictrE   r#   keysrX   )rH   rI   verbosegmfprintv_mapstotal_flopsnode
node_flopsr   argr   keymrX   s                  r	   
fx_profilerx      s   -c2BbME"FFK 9	DKK=TYYK|\`\e\e[fgh
~4(99 	2C3xv%fSX&'d3s3x 01		2
 	=!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9v <!#!#$|r   __main__c                       e Zd Zd Zy)MyOPc                     |dz  S )z*Performs forward pass on given input data.r   r   )selfrI   s     r	   forwardzMyOP.forward   s    19r   N)__name__
__module____qualname__r~   r   r   r	   r{   r{      s    	r   r{   c                   $     e Zd Z fdZd Z xZS )MyModulec                     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 88??1a0DL 88??1a0DLDIr   c                     | j                  |      }| j                  |      j                  dd      }| j                  ||z         S )zApplies two linear transformations to the input tensor, clamps the second, then combines and processes
            with MyOP operator.
            g        g      ?)minmax)r   r   rA   r   )r}   xout1out2s       r	   r~   zMyModule.forward   sA     <<?D<<?((Sc(:D99TD[))r   )r   r   r   r   r~   __classcell__)r   s   @r	   r   r      s    		*r   r      r   )rn   )F)4loggingdistutils.versionr   r   thtorch.nnr   __version__warningr
   r   r   r   vision.calc_funcr   r3   Moduler6   r8   Conv2dr=   BatchNorm2dr@   ReLUReLU6Dropout	MaxPool2d	AvgPool2dAdaptiveAvgPool2drk   r   rf   krg   torch.fxrB   torch.fx.passes.shape_proprC   utilsrD   rE   rG   Tensorrx   r   r{   r   netrandndatar   rZ   r   r   r	   <module>r      s    *   !!"\'%::GOO
`afarar`ssuv

$
& -&5BII 5
299 
	BII 	"..  GGHHJJLLLL IIIINNM[^.+_Y	 
 "A!IaL"  # 0 "
LBII Lbii L^ zryy 
*588?? *  *C288B?DsD%0E	%L5 r   