Skip to content

Initial version of hybrid C++/Python static analysis

Olivier BICHLER requested to merge static_analysis into dev

This MR builds on !159 (closed) ideas to provide a native C++ and Python interface to static analysis. The main features are:

  • Base C++ classes OperatorStats and StaticAnalysis, binded and extendable in Python;
  • Registry for Operator static analysis is in C++, making it possible to use and extend it in Python while accessing some base methods in C++ (requirement for implementing graph transformations heuristics based on static analysis in C++);
  • summary() function similar to Keras;
  • Fix possible segfault in DynamicAttributes::dict() method;
  • Added a Python simplify_graph() recipe, that currently simplifies DinoV2.

TODO

  • Correctly compute number of parameters. This is trivial for Conv and FC, because they have a Param input category, but how to handle other operators like MatMul or Add? I struggle to find a simple solution here! Solution is to count the direct input Producers of nodes as well as internal Producers (in case of meta-ops). Known limitation: if the input Producer is in the GraphView, it will be counted as parameters.

TODO later:

  • Add all operators support (only a dozen for now) too much work for this MR. Unsupported operators are clearly indicated in the plot results (label marked in red).

Usage example

from aidge_core.static_analysis import StaticAnalysisExt

stats = StaticAnalysisExt(model)
stats.summary()
stats.log_nb_params("stats.png", title="Nb params per operator")
stats.log_nb_mac_ops("stats_macs.png", log_scale=True)
--------------------------------------------------------------------------------
                        Layer (type)               Output Shape         Param #
================================================================================
                       (FusedConv#0)          [1, 32, 112, 112]             896
                       (FusedConv#1)          [1, 32, 112, 112]             320
                       (FusedConv#2)          [1, 64, 112, 112]            2112
                       (FusedConv#3)            [1, 64, 56, 56]             640
                       (FusedConv#4)           [1, 128, 56, 56]            8320
                       (FusedConv#5)           [1, 128, 56, 56]            1280
                       (FusedConv#6)           [1, 128, 56, 56]           16512
                       (FusedConv#7)           [1, 128, 28, 28]            1280
                       (FusedConv#8)           [1, 256, 28, 28]           33024
                       (FusedConv#9)           [1, 256, 28, 28]            2560
                      (FusedConv#10)           [1, 256, 28, 28]           65792
                      (FusedConv#11)           [1, 256, 14, 14]            2560
                      (FusedConv#12)           [1, 512, 14, 14]          131584
                      (FusedConv#13)           [1, 512, 14, 14]            5120
                      (FusedConv#14)           [1, 512, 14, 14]          262656
                      (FusedConv#15)           [1, 512, 14, 14]            5120
                      (FusedConv#16)           [1, 512, 14, 14]          262656
                      (FusedConv#17)           [1, 512, 14, 14]            5120
                      (FusedConv#18)           [1, 512, 14, 14]          262656
                      (FusedConv#19)           [1, 512, 14, 14]            5120
                      (FusedConv#20)           [1, 512, 14, 14]          262656
                      (FusedConv#21)           [1, 512, 14, 14]            5120
                      (FusedConv#22)           [1, 512, 14, 14]          262656
                      (FusedConv#23)             [1, 512, 7, 7]            5120
                      (FusedConv#24)            [1, 1024, 7, 7]          525312
                      (FusedConv#25)            [1, 1024, 7, 7]           10240
                      (FusedConv#26)            [1, 1024, 7, 7]         1049600
================================================================================
Total params: 3196032
--------------------------------------------------------------------------------
Input size (MB): 0
Forward/backward pass size (MB): 0.60113525390625
Params size (MB): 12.19189453125
Estimated Total Size (MB): 12.79302978515625
--------------------------------------------------------------------------------

stats

stats_flops

Edited by Olivier BICHLER

Merge request reports

Loading