Using ATen as a stand-alone tensor library in C++

a_d: <p>Hello all,<br>
I require a powerful tensor library in c++ and was looking for a c++ NumPy equivalent and decided to use the ATen library. I downloaded the PyTorch source code from Github and I just wanted to clarify that if I simply copy the ATen folder into my project working directory can I get all the functionalities of creating and manipulating tensors the way we get in libtorch ?</p>
<p>TIA</p>

ptrblck: <p>You would still have to build <code>libtorch</code> to be able to use it and you could use the pre-built package to include it in your project, if possible.</p>

a_d: <p>Thank you for replying <a class="mention" href="/u/ptrblck">@ptrblck</a>. I essentially wanted just the tensor functionality and not the other neural network modules. Can I just build the Aten and the C10 package for my use case ?</p>

ptrblck: <p>I don’t know if you can build the ATen lib (i.e. only (some) tensor functions) in a standalone way, but maybe <a class="mention" href="/u/alband">@albanD</a> or <a class="mention" href="/u/tom">@tom</a> might know it (I doubt you can, but am just speculating).</p>

albanD: <p>From the PyTorch repo itself, we don’t have any tool to do that no.<br>
And the old version of the lib from <a href="https://github.com/zdevito/ATen" class="inline-onebox">GitHub - zdevito/ATen: ATen: A TENsor library for C++11</a> is outdated and does not contain c10.</p>
<p>That being said, nothing prevents you from doing it. But you will have to write the build files yourself <img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=9" title=":confused:" class="emoji" alt=":confused:"></p>

tom: <p>To add to Alban’s comments:</p>
<p>If you want just avoid the C++ API for NNs to be included, there is the <code>NO_API</code> cmake flag and there is (but now you’re in the danger zone) <code>INTERN_DISABLE_AUTOGRAD</code>, but I must admit I haven’t experimented with it.</p>
<p>If you want to remove more, you might want to dig into the built of PyTorch (personally, I find this more daunting than the C++ code, but this could be me). The little-known and perhaps not completely intuitive detail is that the libtorch build is currently (so if you read this in late 2021 or 2022 it might have changed) defined in the caffe2/CMakeList.txt. More specifically, this is the place where the Torch bits (C++ API, Autograd, JIT, …) get added to libtorch.so:</p>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/blob/e606e603312cf874127b560bcbb8b78b9574ac84/caffe2/CMakeLists.txt#L614" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/pytorch/blob/e606e603312cf874127b560bcbb8b78b9574ac84/caffe2/CMakeLists.txt#L614" target="_blank" rel="noopener nofollow ugc">pytorch/pytorch/blob/e606e603312cf874127b560bcbb8b78b9574ac84/caffe2/CMakeLists.txt#L614</a></h4>
<pre class="onebox"><code class="lang-txt"><ol class="start lines" start="604" style="counter-reset: li-counter 603 ;">
<li>      ${TORCH_SRC_DIR}/csrc/api/src/optim/lbfgs.cpp</li><li>      ${TORCH_SRC_DIR}/csrc/api/src/optim/optimizer.cpp</li><li>      ${TORCH_SRC_DIR}/csrc/api/src/optim/rmsprop.cpp</li><li>      ${TORCH_SRC_DIR}/csrc/api/src/optim/serialize.cpp</li><li>      ${TORCH_SRC_DIR}/csrc/api/src/optim/sgd.cpp</li><li>      ${TORCH_SRC_DIR}/csrc/api/src/serialize/input-archive.cpp</li><li>      ${TORCH_SRC_DIR}/csrc/api/src/serialize/output-archive.cpp</li><li>    )</li><li>  endif()</li><li></li><li class="selected">  list(APPEND Caffe2_CPU_SRCS ${TORCH_SRCS})</li><li>endif()</li><li></li><li># NOTE [ Linking AVX and non-AVX files ]</li><li>#</li><li># Regardless of the CPU capabilities, we build some files with AVX and AVX2</li><li># instruction set. If the host CPU doesn't support those, we simply ignore their</li><li># functions at runtime during dispatch.</li><li>#</li><li># We must make sure that those files are at the end of the input list when</li><li># linking the torch_cpu library. Otherwise, the following error scenario might</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>For a quick test, you could see to changing that, but it probably is a good idea to not call the result libtorch if you plan on doing things with it.</p>
<p>Whether or not I would actually recommend to do such a thing is on another page, you’re clearly in “use at your own peril”-land here.</p>
<p>Best regards</p>
<p>Thomas</p>

a_d: <p>Thanks a lot for your detailed replies <a class="mention" href="/u/tom">@tom</a> and <a class="mention" href="/u/alband">@albanD</a>. I remember digging into the source code in the past and  I will admit that the PyTorch source code structure is really complex and I was not able to extract the parts I wanted <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=9" title=":sweat_smile:" class="emoji" alt=":sweat_smile:">. I essentially just wanted the ways to create the tensors and the <code>tensor accessor</code> parts and I would probably go ahead with the CMake solution and let you know how that turns out.  Even though outdated, Since I only want the ways to create and access tensors, the link shared by <a class="mention" href="/u/alband">@albanD</a> might work out too. Thanks a lot for your answers and for helping me out <a class="mention" href="/u/ptrblck">@ptrblck</a>, <a class="mention" href="/u/tom">@tom</a> <a class="mention" href="/u/alband">@albanD</a>.</p>

albanD: <p>Hi,</p>
<p>Note that if you want autograd, you will need to have full libtorch.<br>
Also the current public API from libtorch and all the Tensor ops are actually the “autograd version” of these functions. So if you strip out autograd, you might have to use a different API.</p>

ShunyuYao: <p><a class="mention" href="/u/a_d">@a_d</a> Hi,<br>
I met the same question. I also want to only use the ATen library.<br>
Could you tell me how the things going on if you have any progress.</p>

daishan12: <p><a class="mention" href="/u/a_d">@a_d</a> <a class="mention" href="/u/shunyuyao">@ShunyuYao</a> Hi,I met the same question. But I’m trying to build my tensor  library for my personal program.Could you teel me how the things going on if you have any progress.Thanks</p>