Vizualizing the computation graph in C++ (minimal example)

username: DuaneNielsen
cooked: <p>A naive and minimal example of printing out the computation graph in libtorch c++.</p>
<pre><code class="lang-auto">
//
// Created by duane on 10/2/20.
//

#include &lt;torch/torch.h&gt;

int main(int arg, char *argv[]){

    torch::cuda::is_available();
    auto x = torch::randn(3, torch::requires_grad());
    auto z = torch::randn(3, torch::requires_grad());
    auto y = x * 2;
    y = y * 2;
    y = y * z;
    y += z;
    y = y * 2;

    std::cout &lt;&lt; "digraph G {" &lt;&lt; std::endl;

    std::deque&lt;std::shared_ptr&lt;torch::autograd::Node&gt;&gt; nodes;
    nodes.push_front(y.grad_fn());
    while (!nodes.empty()){
        std::cout &lt;&lt; "\"" &lt;&lt; nodes.back() &lt;&lt; "\"" &lt;&lt; " [label=\"" &lt;&lt; nodes.back()-&gt;name() &lt;&lt; "\"]" &lt;&lt; std::endl;
        for (const auto &amp;edge : nodes.back()-&gt;next_edges()) {
            nodes.push_front(edge.function);
            std::cout &lt;&lt; "\"" &lt;&lt; nodes.back() &lt;&lt; "\"" &lt;&lt; " -&gt; " &lt;&lt; "\"" &lt;&lt; edge.function &lt;&lt; "\"" &lt;&lt; std::endl;
        }
        nodes.pop_back();
    }

    std::cout &lt;&lt; "}" &lt;&lt; std::endl;

}
</code></pre>
<p>Output</p>
<pre><code class="lang-auto">digraph G {
"0x557c2b12b230" [label="MulBackward1"]
"0x557c2b12b230" -&gt; "0x557c2b12b080"
"0x557c2b12b080" [label="AddBackward0"]
"0x557c2b12b080" -&gt; "0x557c2b12abd0"
"0x557c2b12b080" -&gt; "0x557c2b12ae00"
"0x557c2b12abd0" [label="MulBackward0"]
"0x557c2b12abd0" -&gt; "0x557c2b12a660"
"0x557c2b12abd0" -&gt; "0x557c2b12ae00"
"0x557c2b12ae00" [label="torch::autograd::AccumulateGrad"]
"0x557c2b12a660" [label="MulBackward1"]
"0x557c2b12a660" -&gt; "0x557c2a61f300"
"0x557c2b12ae00" [label="torch::autograd::AccumulateGrad"]
"0x557c2a61f300" [label="MulBackward1"]
"0x557c2a61f300" -&gt; "0x557c29ff6f30"
"0x557c29ff6f30" [label="torch::autograd::AccumulateGrad"]
}

</code></pre>
<p>Paste into <a href="http://www.webgraphviz.com/" rel="noopener nofollow ugc">http://www.webgraphviz.com/</a> to vizualize</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/c/5c3645e1b9373875ce3a5f42aa99554700805b85.png" data-download-href="https://discuss.pytorch.org/uploads/default/5c3645e1b9373875ce3a5f42aa99554700805b85" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/c/5c3645e1b9373875ce3a5f42aa99554700805b85_2_580x500.png" alt="image" data-base62-sha1="d9KcO9onvOdeXQ2GuFGNRTfWyVL" width="580" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/c/5c3645e1b9373875ce3a5f42aa99554700805b85_2_580x500.png, https://discuss.pytorch.org/uploads/default/original/3X/5/c/5c3645e1b9373875ce3a5f42aa99554700805b85.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/c/5c3645e1b9373875ce3a5f42aa99554700805b85.png 2x" data-dominant-color="F8F8F8"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">713×614 42.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>