Pytorch source code csrc and aten in v2.2.0

mahuichao: <p>Hello, I learned code of <strong>pytorch 2.2.0</strong> recently, and I am confused in python invoking c++ backend.<br>
I know pytorch us <strong>pybind11</strong> to register c++ function to python, and different divices use <strong>dispatcher</strong> to register their implementation in <strong>aten</strong> directory。But what does <strong>csrc</strong> in torch directory do? If I invoke conv2d in python, follow the trace I find there is a <strong>torch::conv2d</strong> in <strong>csrc</strong> directory(torch/csrc/api/include/torch/nn/functional/conv.h), but the implementation of <strong>aten</strong> have the namespace <strong>torch::aten::xxxx</strong> , so I am confused, does <strong>torch::conv2d</strong> in <strong>csrc</strong> invoke function in <strong>aten</strong> ?</p>

GLZhu: <p>Namespace is exported from c10 to aten in pytorch cuda.</p>

mahuichao: <p>I don’t understand, if we choose one from following two graph, which is right?<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/3/0/30c0bc58768a08af63fab60ea4bfc16ac2d9a2ff.png" alt="image" data-base62-sha1="6XhQSO3GTG8L5AvmA4Onfytzbpd" width="586" height="400"></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/c/9cedff6f56cf08d63c6239b428b4bec8e3b62aa5.png" data-download-href="https://discuss.pytorch.org/uploads/default/9cedff6f56cf08d63c6239b428b4bec8e3b62aa5" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/c/9cedff6f56cf08d63c6239b428b4bec8e3b62aa5_2_690x309.png" alt="image" data-base62-sha1="mogonSzV5y28c7UOW4bywpnIrpX" width="690" height="309" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/c/9cedff6f56cf08d63c6239b428b4bec8e3b62aa5_2_690x309.png, https://discuss.pytorch.org/uploads/default/optimized/3X/9/c/9cedff6f56cf08d63c6239b428b4bec8e3b62aa5_2_1035x463.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/9/c/9cedff6f56cf08d63c6239b428b4bec8e3b62aa5_2_1380x618.png 2x" data-dominant-color="EBEB47"><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">1458×654 18.7 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>

GLZhu: <p>Graph 2 is slightly off. after csrc it directly go to c++ functions. Try to read python files under torch/cuda/*.py.<br>
As the following code:</p>
<blockquote>
<p>torch._C._cuda_memorySnapshot()</p>
</blockquote>
<p>will directly go to corresponding c++ functions registered in Module.cpp.</p>
<p>Note that other than pybind11, ctypes is also used,</p>
<p>For torch.rand like ops, it is indeed registered with pybind11, try compile pytorch from source and check torch/csrc/autograd/generated/python_torch_functionsEverything.cpp.</p>
<p>In these generated file, the call will be dispatched to target specific native implementation.</p>

mahuichao: <p>OK, I will check these files, Thank you very much for your detailed explanation. close question.</p>