Model Summary for libTorch jit model?

username: tancl
cooked: <p>Hi,</p>
<p>Is it possible to get the model summary described in this link</p><aside class="quote quote-modified" data-post="5" data-topic="2678">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sksq96/48/13282_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/is-there-similar-pytorch-function-as-model-summary-as-keras/2678/5">Is there similar pytorch function as model.summary() as keras?</a> 
  </div>
  <blockquote>
    Yes, you can get exact Keras representation, using <a href="https://github.com/sksq96/pytorch-summary" rel="noopener nofollow ugc">this code</a>. 
Example for VGG16
from torchvision import models
from summary import summary

vgg = models.vgg16()
summary(vgg, (3, 224, 224))

----------------------------------------------------------------
        Layer (type)               Output Shpae         Param #
================================================================
            Conv2d-1         [-1, 64, 224, 224]            1792
              ReLU-2         [-1, 64, 224, 224]    …
  </blockquote>
</aside>
<p>
, but for cpp jit model?</p>
<p>This is an enhancement for printing the model in this link;</p><aside class="quote quote-modified" data-post="2" data-topic="60297">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/driazati/48/15393_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/print-network-architecture-in-cpp-jit/60297/2">Print network architecture in cpp jit</a> <a class="badge-category__wrapper " href="/c/jit/13"><span data-category-id="13" data-drop-close="true" class="badge-category " title="A category for TorchScript and the PyTorch JIT compiler"><span class="badge-category__name">jit</span></span></a>
  </div>
  <blockquote>
    We don’t have any built-in utils for this, but you can do a simple version manually: 
#include &lt;torch/script.h&gt;

#include &lt;iostream&gt;
#include &lt;memory&gt;

void tabs(size_t num) {
  for (size_t i = 0; i &lt; num; i++) {
    std::cout &lt;&lt; "\t";
  }
}

void print_modules(const torch::jit::script::Module&amp; module, size_t level = 0) {
  std::cout &lt;&lt; module.name().qualifiedName() &lt;&lt; " (\n";
  for (const auto&amp; module : module.get_modules()) {
    tabs(level + 1);
    print_modules(module.module, level + 1);
  …
  </blockquote>
</aside>

<p>Thanks.</p>
<p>rgds,<br>
CL</p>

username: tancl
cooked: <p>Just for sharing, this the one of the way of show network architecture for jit model in cpp.</p>
<pre><code class="lang-auto">// torchex1.cpp : This file contains the 'main' function. Program execution
// begins and ends there.
//

#include &lt;torch/script.h&gt;
#include &lt;iostream&gt;
#include &lt;inttypes.h&gt;

#include &lt;iostream&gt;
#include &lt;memory&gt;

void tabs(size_t num) {
  for (size_t i = 0; i &lt; num; i++) {
	std::cout &lt;&lt; "\t";
  }
}

void print_modules(const torch::jit::script::Module&amp; module, size_t level = 0) {
  // std::cout &lt;&lt; module.name().qualifiedName() &lt;&lt; " (\n";
  std::cout &lt;&lt; module.name().name() &lt;&lt; " (\n";	
  
  for (const auto&amp; parameter : module.get_parameters()) {
	tabs(level + 1);
	std::cout &lt;&lt; parameter.name() &lt;&lt; '\t';
	std::cout &lt;&lt; parameter.value().toTensor().sizes() &lt;&lt; '\n';
  } 


  for (const auto&amp; module : module.get_modules()) {
	tabs(level + 1);
	print_modules(module, level + 1);
  }

  tabs(level);
  std::cout &lt;&lt; ")\n";
}

int main(int argc, const char* argv[]) {
  torch::jit::script::Module container = torch::jit::load("net.pt");
  print_modules(container);
  return 0;
}

</code></pre>
<p>The output looks like:</p>
<pre><code class="lang-auto">net (
        conv1 (
                weight  [10, 1, 5, 5]
                bias    [10]
        )
        conv2 (
                weight  [20, 10, 5, 5]
                bias    [20]
        )
        conv2_drop (
        )
        fc1 (
                weight  [50, 320]
                bias    [50]
        )
        fc2 (
                weight  [10, 50]
                bias    [10]
        )
)

</code></pre>
<p>It seems like a stupid way, any smarter way please feel free to share.</p>
<p>thanks.</p>
<p>rgds,<br>
CL</p>

username: driazati
cooked: <p>It also looks like there is <code>script::Module::dump()</code> which will print something like this, you can toggle it to include the sections that are relevant to you:</p>
<pre><code class="lang-cpp">  void dump(
      bool print_method_bodies,  // you probably want this to be `false` for a summary
      bool print_attr_values,
      bool print_param_values) const;
</code></pre>
<pre><code class="lang-auto">module __torch__.M {
  parameters {
  }
  attributes {
    training = True
  }
  methods {
    method forward {
      graph(%self : ClassType&lt;M&gt;,
            %x.1 : Tensor):
        %3 : Tensor = prim::CallMethod[name="other_fn"](%self, %x.1) # ../test.py:36:15
        return (%3)
  
    }
    method other_fn {
      graph(%self : ClassType&lt;M&gt;,
            %x.1 : Tensor):
        %4 : int = prim::Constant[value=1]()
        %3 : int = prim::Constant[value=10]() # ../test.py:33:19
        %5 : Tensor = aten::add(%x.1, %3, %4) # ../test.py:33:15
        return (%5)
  
    }
  }
  submodules {
  }
}
</code></pre>

username: tancl
cooked: <p>thanks for the recommendation again.</p>

username: Rem_H
cooked: <p>when i use the code ,i met the trouble “class torch::jit::script::Module have no members name and  get_parameters()”</p>