Package: thoplw
pytorch-op-counter-layerwiseThis repository providesthoplwthat is a ython module to compute MACs
(multiply–accumulate operations) and number of parameters for each layer
of neural network models implemented by PyTorch.InstallationDependenciesThethoplwmodule requires:PyTorch >= 2.0.0 (will work on the older version, but not checked)and the sample code require:Torchvision >= 0.15.0User installationpip install thoplwUsageMinimal exampleimporttorch,torchvision,thoplw# Instanciate the target model.model=torchvision.models.resnet18()# Compute MACs, number of parameters, and details of each layer.macs,params,layers_info=thoplw.profile(model,tensor=torch.randn(1,3,224,224))# Print the total MACs and number of parameters.print("Total MACs and params:")print("  - Macs   =",macs)print("  - Params =",params)print()# Print layer details.print(layers_info.summary())Running the above code will give you the output below
(the table has been partially omitted because it is too long to show everything in this README).Total MACs and params:
  - Macs   = 1824010216                                                                                                                                                       
  - Params = 11699112

| Name                  | Class             | Input shape    | Output shape   | MACs       | Params   |
+-----------------------+-------------------+----------------+----------------+------------+----------+
| conv1                 | Conv2d            | 3 x 224 x 224  | 64 x 112 x 112 | 118013952  | 9408     |
| bn1                   | BatchNorm2d       | 64 x 112 x 112 | 64 x 112 x 112 | 3211264    | 256      |
| relu                  | ReLU              | 64 x 112 x 112 | 64 x 112 x 112 | 0          | 0        |
| maxpool               | MaxPool2d         | 64 x 112 x 112 | 64 x 56 x 56   | 0          | 0        |
...
| layer4.1.conv2        | Conv2d            | 512 x 7 x 7    | 512 x 7 x 7    | 115605504  | 2359296  |
| layer4.1.bn2          | BatchNorm2d       | 512 x 7 x 7    | 512 x 7 x 7    | 100352     | 2048     |
| avgpool               | AdaptiveAvgPool2d | 512 x 7 x 7    | 512 x 1 x 1    | 1024       | 0        |
| fc                    | Linear            | 512            | 1000           | 513000     | 513000   |
+-----------------------+-------------------+----------------+----------------+------------+----------+
| Total                 | ResNet            | 3 x 224 x 224  | 1000           | 1824010216 | 11699112 |Clever formattingThethoplwprovidesclever_formatfunction that returns appropriate expressions
of the given numbers likewisethoppackage.macs,params=thoplw.clever_format([macs,params],"%.3f")print("Total MACs and params:")print("  - Macs   =",macs)print("  - Params =",params)The table of layers detail supports three types of formatting, raw number (default choice),
clever format like theclever_formatfunction, and ratio.# Print the table with clever formatting.print(layers_info.summary(kind="text",fmt="clever"))# Print the table with ratio formatting.print(layers_info.summary(kind="text",fmt="ratio"))Table typeThe above example prints the NN model summary as a plain text, but you can dump the summary
as CSV and Markdown format too. The following is an example to save the table as CSV and
Markdown format respectively.# Save as CSV format.withopen("summary.csv")asofp:ofp.write(layers_info.summary(kind="csv"))# Save as Markdown format.withopen("summary.md")asofp:ofp.write(layers_info.summary(kind="md"))API referencethoplw.profilemacs,params,layers_info=thoplw.profile(model:torch.nn.Module,tensor:torch.Tensor,custom_ops:dict={},verbose:bool=True)Computes MADDs and the number of parameters.Argsmodel: the target NN model.tensor: input tensor for themodel.custom_ops: optional custom operations.verbose: shows extra message on your terminal ifTrue.Returnsmacs: total MADDs of the target model and the given input tensor.params: number of parameters of the target model.layer_info:LayerInfoclass that store the details of each layer.thoplw.clever_formatformatted_values=thoplw.clever_format(values:intorlist,fmt:str="%6.2f")Returns formatted string of the given integer(s).Argsvalues: input value, or values.fmt: format specifier.Returnsformatted_values: format result.LayerInfo classclassLayerInfo:...defsummary(self,kind:str="text",fmt:str="raw")->str:...A class to store layer details. Only thesummaryfunction is opened to users.Argskind: table type to be returned ("text"means simple table,"csv"means CSV, and"md"means Markdown).fmt: output format ("raw"means raw integer,"clever"means auto formatting, and"ratio"means ratio format).Returnsformatted string.Results of Recent ModelsThe following results can be obtained by runningtests/test_benchmarks.py.
Click the model anem to see the layer details.Model nameParams [M]MACs [G]Model nameParams [M]MACs [G]alexnet714.22 M61.10 Mresnext50_32x4d4.29 G25.10 Mvgg117.61 G132.86 Mresnext101_32x8d16.54 G88.99 Mvgg11_bn7.64 G132.87 Mdensenet1212.90 G8.06 Mvgg1311.31 G133.05 Mdensenet1617.85 G28.90 Mvgg13_bn11.36 G133.06 Mdensenet1693.44 G14.31 Mvgg1615.47 G138.36 Mdensenet2014.39 G20.24 Mvgg16_bn15.52 G138.37 Mgooglenet1.51 G6.64 Mvgg1919.63 G143.67 Minception_v35.75 G23.87 Mvgg19_bn19.69 G143.69 Msqueezenet1.0818.93 M1.25 Mresnet181.82 G11.70 Msqueezenet1.1349.16 M1.24 Mresnet343.68 G21.81 Mmobilenet_v2327.49 M3.54 Mresnet504.13 G25.61 Mmobilenet_v3_small62.17 M2.55 Mresnet1017.87 G44.65 Mmobilenet_v3_large234.21 M5.51 Mresnet15211.60 G60.34 Mshufflenet_v2_x0.544.57 M1.37 Mwide_resnet50_222.84 G127.02 Mshufflenet_v2_x1.0152.71 M2.29 Mwide_resnet101_211.46 G68.95 Mmnasnet_0.5116.72 M2.24 Mmnasnet_1.0336.24 M4.42 MGratitudeDevelopers and maintainers ofpytorch-OpCounter.
The author learned a lot from the repository.