How to include torch/extension.h correctly?

username: pvtien96
cooked: <p>Hi, I’m following this <a href="https://pytorch.org/tutorials/advanced/cpp_extension.html" rel="noopener nofollow ugc">tutorial</a> to extend torch to cpp/cuda.<br>
My setup is Ubuntu 22, Visual Code, Cuda toolkit 12.1, torch install by conda.</p>
<pre data-code-wrap="bash"><code class="lang-bash">&gt;&gt;&gt; torch.version.cuda
'12.1'
&gt;&gt;&gt; torch.__version__
'2.1.0.dev20230614'
</code></pre>
<p>I create my cpp file as follows:</p>
<pre><code class="lang-auto">#include &lt;torch/extension.h&gt;
#include &lt;cuda.h&gt;
#include &lt;cuda_runtime.h&gt;


// Forward pass 

// Backward pass 
</code></pre>
<p>In <code>.vscode/c_cpp_properties.json</code>, I added include path:</p>
<pre><code class="lang-auto">{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/local/cuda/include",
                "/home/tien/anaconda3/lib/python3.9/site-packages/torch/include",
                "/home/tien/anaconda3/lib/python3.9/site-packages/torch/include/torch/csrc/api/include"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c17",
            "cppStandard": "gnu++17",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}
</code></pre>
<p>But visual code still yields an error:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/1/71d183737d70f88062946725c00cfb4f13e90b02.png" data-download-href="https://discuss.pytorch.org/uploads/default/71d183737d70f88062946725c00cfb4f13e90b02" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/7/1/71d183737d70f88062946725c00cfb4f13e90b02_2_690x256.png" alt="image" data-base62-sha1="geSPKFZyk0YMTfQYJchnR2vns7E" width="690" height="256" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/7/1/71d183737d70f88062946725c00cfb4f13e90b02_2_690x256.png, https://discuss.pytorch.org/uploads/default/optimized/3X/7/1/71d183737d70f88062946725c00cfb4f13e90b02_2_1035x384.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/7/1/71d183737d70f88062946725c00cfb4f13e90b02_2_1380x512.png 2x" data-dominant-color="252525"><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">1923×716 142 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>
<p>How can I fix this problem?</p>
<p>Thanks in advance!</p>

username: linzhouli
cooked: <p>I found that you should include “/torch/include/torch/csrc/api/include” before “/torch/include”</p>

username: npz7yyk
cooked: <p>Hi, I faced the same problem and solved it (hopefully).<br>
In short, we need ~/miniconda3/include/python-x.xx</p>
<p>Actually, you can fix this bug by following the hint of C/C++ step by step:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/5/350e1a07388cd468ebedc37d7120d1b61ba07ee6.png" data-download-href="https://discuss.pytorch.org/uploads/default/350e1a07388cd468ebedc37d7120d1b61ba07ee6" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/5/350e1a07388cd468ebedc37d7120d1b61ba07ee6_2_690x125.png" alt="image" data-base62-sha1="7zlw9BCh2LNpP4nNlxnYV2CAhCK" width="690" height="125" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/5/350e1a07388cd468ebedc37d7120d1b61ba07ee6_2_690x125.png, https://discuss.pytorch.org/uploads/default/optimized/3X/3/5/350e1a07388cd468ebedc37d7120d1b61ba07ee6_2_1035x187.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/5/350e1a07388cd468ebedc37d7120d1b61ba07ee6_2_1380x250.png 2x" data-dominant-color="303032"><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">1695×308 39.4 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>
<p>OK, so we need Python.h:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/f/ff1b87a4786ec4fc657e118722a5d1052386a4a1.png" data-download-href="https://discuss.pytorch.org/uploads/default/ff1b87a4786ec4fc657e118722a5d1052386a4a1" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/f/f/ff1b87a4786ec4fc657e118722a5d1052386a4a1.png" alt="image" data-base62-sha1="AoMJIzRmcGoCNm6gLiloLEdkp5n" width="690" height="197" data-dominant-color="353636"><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">1350×387 47.4 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>
<p>Then it is apparent: we need ~/miniconda3/include/python3.10.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/b/bb233d1145263da6eee43dfb2a8e91ffc9d31625.png" data-download-href="https://discuss.pytorch.org/uploads/default/bb233d1145263da6eee43dfb2a8e91ffc9d31625" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/b/bb233d1145263da6eee43dfb2a8e91ffc9d31625_2_690x124.png" alt="image" data-base62-sha1="qHuMGl2vB5mTVUdz5bs3iR2nReB" width="690" height="124" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/b/bb233d1145263da6eee43dfb2a8e91ffc9d31625_2_690x124.png, https://discuss.pytorch.org/uploads/default/optimized/3X/b/b/bb233d1145263da6eee43dfb2a8e91ffc9d31625_2_1035x186.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/b/b/bb233d1145263da6eee43dfb2a8e91ffc9d31625_2_1380x248.png 2x" data-dominant-color="2A2624"><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">1726×311 38.9 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>
<p>Solved:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/e/2e9282ac91004b8f63f647d16f416dc4dd8e49a9.png" data-download-href="https://discuss.pytorch.org/uploads/default/2e9282ac91004b8f63f647d16f416dc4dd8e49a9" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/2/e/2e9282ac91004b8f63f647d16f416dc4dd8e49a9.png" alt="image" data-base62-sha1="6DZRn0aP3Kn7JKD3XAsxxQIHfQJ" width="690" height="242" data-dominant-color="2C2828"><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">732×257 21.9 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>