
    j                         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j        fdZdej        fdZej        ej        ej         ej!        ej"        ej#        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 Z)d)dej        dej*        fd Z+e,d!k    ra G d" d#ej                  Z- G d$ d%ej        j                  Z. e.            Z/ ej0        d&d'          Z1 e+e/e1d(          Z2 e3e2           dS dS )*    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                     dS )zYEnsures tensor array sizes are appropriate by clamping specified input and output shapes.r    input_shapesoutput_shapess     T/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/thop/fx_profile.pycount_clampr          1    c                 6    |d                                          S )z9Returns the number of elements in the first output shape.r   numelr   s     r   	count_mulr      s    !!###r   c                 d    | d         }|d         }|d         }|                                 }||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      s8    AHa I2,K??$$L%%r   c                 h    t          | |          }d|v r||d                                         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#   (   s<    }55Eq!'')))Lr   c                 b   |\  }}}}}}	}
t          |           dk    r| \  }}nt          |           dk    r| \  }}}|d         }|dd                                         }d}|d         }t          |||                                ||
                                          }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AGWeWh	6
<A'	\		a		%1"(a I))++GJw(99??;L;LjZ`aaffhhI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44r   c                     dS )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                                         }t          |||                                ||                                          }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    ;*aaGa I#J]F$QRR(..00Jw
IOO4E4EzSYZZ__aaIy>>r   c                 z    t          |          dk    s
J d            |d         }d|                                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   s@    }"""$P"""aAqwwyy=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                      dS )zOA no-op print function that takes any arguments without performing any actions.Nr   )r    r!   s     r   
null_printrI   {   s    
Fr   Fmodinputc                    t          |           }t          |                              |           t          }|rt          }i }d}|j        j        D ]} |d|j         d|j         d|j	         d|j
                    d}g }	 |dd	           |j
        D ]]}
t          |
          |vr ||t          |
                    d	           |	                    |t          |
                              ^ |              |d
|j        d         j                    |j        d         j        g}|j        dv rd}n}|j        dk    rt          |j                                      d          d                             dd                              dd                                          }|t$          v r#t%          |         |	|g|j
        R i |j        }n|j        |ft(          |<   t+          d| d           n|j        dk    rZt          |j                  }|t$          v rt%          |         |	|          }ny|j        |ft(          |<   t+          | d           nT|j        dk    rH|                     |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           nSt	          t/          |                     t	          d|                                 |j         d         j                    |j        d         j        |t          |j	                  <   |||z  }t7          d| d|             |d           t9          t(                                                    dk    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      sr   -c22BbMME"""F FK 9 9gggDKggTYgg\`\egghhh
~4((((9 	2 	2C3xxv%%FfSXX&(d3333s3xx 01111A=!9!?AABBB=1787///JJW''dk""((..q199#rBBJJ3PRSSYY[[Ci&s^L-c$)cccW[Wbcc

%)WcNS!+#+++,,,,W%% dk""Ci&s^L-HH

%)WcNS!)))****W%% !!$+..Aq''CF477DGGy0111Aww)##&tAww/<OO

%)WJS!)))***.$q''***!X&& Z*++++d1ggXs~~'7'74;8O8O8O'P'VXXYYY!%=!9!?s49~~!:%KR*RR[RRSSSy
<!##!!!!!!#$$$|r   __main__c                       e Zd ZdZd ZdS )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    19r   N)__name__
__module____qualname____doc__r   r   r   r   r}   r}      s)        V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                            dd          | _        t          j                            dd          | _        t                      | _        dS )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__   sV    GG 8??1a00DL 8??1a00DLDIIIr   c                     |                      |          }|                     |                              dd          }|                     ||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   rG   r   )r   xout1out2s       r   r   zMyModule.forward   sK     <<??D<<??((Sc(::D99TD[)))r   )r   r   r   r   r   r   __classcell__)r   s   @r   r   r      sM        V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 0 0 0 0 0 " " " " " " " " , , , , , ,<!""\\'%:%:::GOvafarvvv  
  
$ $ $
& & &    $5BI 5 5 5 5
29    
	BI 	 	 	 	".     GHJLL IINM[^.+_Y	 
 " "A!IaLL  
L LBI Lbi L L L L^ z    ry   * * * * *58? * * *$ (**C28B??DJsD%000E	E%LLLLL= r   