Survey: What are you using the C++ API for?

yf225: <p>We are working on plans to improve the PyTorch C++ API, and we’d like to learn how the C++ API is serving your use case, and what we can do to make the experience better.</p>
<p>If you are interested in telling us your use case, we’d love to learn about the following:</p>
<ol>
<li>What are you using the PyTorch C++ API for?</li>
<li>How does your software stack look like? And how does the PyTorch C++ API interact with other components in your stack?</li>
<li>Are you using JIT (TorchScript) models or <code>torch::nn</code> models?</li>
<li>Are you training models in C++?</li>
<li>How do you think the PyTorch C++ API (or the ecosystem in general) should be improved?</li>
</ol>
<p>We appreciate any feedback you’d like to give us. Thanks a lot for your support!</p>

smth: 

tancl: <p>Hi, Thanks for initiative for the survey.</p>
<ol>
<li>I am planning to call the libtorch from opensource numerical software - Scilab so that it could be used together with other modules such as image processing module to make the design and prototype much easier.</li>
<li>Software stack looks like: I will have C++ gateways to call the libtorch, compiled and link so that it becomes a native function in Scilab. In user end, they will just call sth like : trained_model = torch_train(data, target, model_arch, configs) .</li>
<li>Currently I am exploring both</li>
<li>I prefer to, but the training would be done without recompiling the C++ codes.</li>
<li>Could do what PyTorch (with Python) could, which make it could be totally independent from python.</li>
</ol>
<p>Thanks.</p>
<p>Regards,<br>
Chin Luh</p>

torchok: <ol>
<li>
<p>What are you using the PyTorch C++ API for?<br>
== R&amp;D neuroevolution for Computer Vision</p>
</li>
<li>
<p>How does your software stack look like?<br>
== Win, Lin, Mac + Intel Fortran/C++ Compilers + VS2017 + CUDA SDK + in-house soft</p>
</li>
</ol>
<p>And how does the PyTorch C++ API interact with other components in your stack?<br>
== badly,… trying to understand to do organize the stack better, be happy to be in touch with FAI team for exchanging experience on it</p>
<ol start="3">
<li>
<p>Are you using JIT (TorchScript) models or  <code>torch::nn</code>  models?<br>
== torch::nn only</p>
</li>
<li>
<p>Are you training models in C++?<br>
== yes, that is why I’m using PyTorch instead of Tensorflow (no full C++ API yet)</p>
</li>
<li>
<p>How do you think the PyTorch C++ API (or the ecosystem in general) should be improved?<br>
== many ways <img src="https://discuss.pytorch.org/images/emoji/apple/slightly_smiling_face.png?v=9" title=":slightly_smiling_face:" class="emoji" alt=":slightly_smiling_face:"></p>
</li>
</ol>
<ul>
<li>clear instructions for compilation from the source in static and dynamic modes as I asked in <a href="https://github.com/pytorch/pytorch/issues/25699" rel="nofollow noopener">https://github.com/pytorch/pytorch/issues/25699</a>
</li>
<li>fixing the bugs described in <a href="https://github.com/pytorch/pytorch/issues/25698" rel="nofollow noopener">https://github.com/pytorch/pytorch/issues/25698</a>
</li>
<li>clear quick start documentation for installation</li>
<li>benchmark white paper to show the advantages (vs Tensorflow, vs python version,…)</li>
<li>support of Intel compilers</li>
<li>existing ready examples (a typical) projects for windows (vs2017 solution), linux (code+makefile), macos (xcode solution)</li>
<li>introductory video, explaining all this in 5 mins</li>
</ul>

Peter_Featherstone: <p>I would like to use the C++ api for inference. Though that’s fine on heavy duty machines, it’s very hard to do on low power, low resource, even embedded devices. I had trouble compiling libtorch on a raspberry pi 3. It kept on running out of memory and eventually crashed. It would be great if we could simplify a CPU-only build that produced a shared library and a monolithic static library (that contains all its dependencies). Ideally, it would be great if we could get it to a stage where we could build libtorch for Android, raspberry pi zero and even more embedded devices. Currently I train with the pytorch api, convert to onnx and use another lightweight framework for inference. There’s no real problem with that. But if you’re looking for ideas, I would pitch “pytorch-lite” or “pytorch-embedded”. In that light, maybe an OpenCL or Vulkan backend would be a good idea. By the way Tencent and Alibaba are using Vulkan now.</p>

jshanab: <p>Build from source must be fixed and stable. Get CI going and have variations on the build. In the windows world this means debug,release and release with symbols builds in /mt and /md linkings for a few CUDA versions as the tested ones. I should be able to install the msi and 5 min after installing CUDA build a sample.</p>
<p>I have been trying for weeks to get any c++ support claiming DL framework to work cross platform. I need c++ and I want to both train and infer. libTorch at least has 1 prebuilt but they include CUDA and are statically linked so are pinned to a particular version and have complete folders debug vs release with same names.<br>
Documention never in sync with actual.<br>
I have tried many ways and followed the build system and it just never works. Today’s checkout is hard coded somewhere to NINJA, completely ignores the setting of the Generator. Very frustrating.</p>

meganset: <p><em>1. What are you using the PyTorch C++ API for?</em><br>
I’m writing an interface for the q/kdb interpreter to/from pytorch<br>
There is already an interface to python, but I’m aiming to use only a shared library built on top of libtorch.</p>
<p><em>2. How does your software stack look like? And how does the PyTorch C++ API interact with other components in your stack?</em><br>
q interpreter (executable size is around 655k) + ktorch.so (around 4mb) + libraries from libtorch<br>
interaction is through a c-api from the interpreter.</p>
<p><em>3. Are you using JIT (TorchScript) models or  <code>torch::nn</code>  models?</em><br>
No plans for using jit/torchscript</p>
<p><em>4. Are you training models in C++?</em><br>
Yes, the model is phrased in the k/q interpreter, along with the optimizer &amp; loss function.<br>
(these are all pointers back and forth from interpreter &lt;—&gt; c-api &lt;—&gt;  torch,torch::nn objects</p>
<p><em>5. How do you think the PyTorch C++ API (or the ecosystem in general) should be improved?</em></p>
<p>It was tricky to sort out the best way to deal with c++ smart pointers through a c-api interface.<br>
It would be useful if there were some guidelines on how to use libtorch via a c-api.</p>
<p>I ended up writing all the modules that are available in python in c++ so that I could use<br>
them with torch::nn::Sequential, all the various pool &amp; padding layers, non-linear activations, etc.<br>
Most of these could be implemented with torch::nn::Functional, but I couldn’t figure out how to<br>
query a Functional layer to get the various options/control parameters back from a realized Sequential model in memory.<br>
(the interface is written so that any module/loss fn/optimizer in cpu/gpu memory can be retrieved in database tables)</p>
<p>I also built Loss objects to match those available in python in torch.nn.<br>
I haven’t been able how to make much use of the DataLoader/Dataset c++ setup yet,<br>
but that may be because my outer data loop starts outside of c++ in the q/kdb interpreter.</p>
<p>Thank you to goldsborough, yf225 and everybody for the c++ set up so far…</p>

pascal.soveaux: <p><em>1. What are you using the PyTorch C++ API for?</em><br>
Training and generation. We are using C++ because we need to “obfuscate” the code.</p>
<p>*2. How does your software stack look like? And how does the PyTorch C++ API interact with other components in your stack?<br>
Our app/lib is currently available on Linux only using MKL + CUDA + FAISS + RDKit + some other libs. Everything is C++ for the lib, everything is javascript for the client who is using a native NodeJS addon (NAPI used to interact with our lib).</p>
<p>*3. Are you using JIT (TorchScript) models or <code>torch::nn</code> models?<br>
Only torch::nn</p>
<p>*4. Are you training models in C++?<br>
Yes</p>
<p>*5. How do you think the PyTorch C++ API (or the ecosystem in general) should be improved?<br>
Resolve: Static linking master bug <span class="hashtag">#21737</span></p>
<p>C++17 :  I am stuck with &lt;experimental/ &gt;, I cannot use futures and the likes, constexpr if,… old style lambdas, old style template, …</p>
<p>Thread pools : c10 comes with a ThreadPool but there is no way to wait for job OOB. I think libtorch needs a solid single ThreadPool as creating a threadpool in every module create too many threads.</p>
<p>Documentation : At the very beginning because libtorch was not available for CUDA 10,  I spent a lot of time trying to understand how to compile a compact lib for my platform and my needs without using python: “what USE_THISFLAG is for ? for what purpose? What happen if I set it ON or OFF?” For devs like me who are not at all datascientists, it should be great to introduce the packages you are using (xxblas, xxpack, …).<br>
I am also spending time in converting python &lt;=&gt; C++ (simple example what is torch::mm in python ? what is t.mean() in C++,…)</p>
<p>Thank you</p>

MadmanNero: <ol>
<li>What are you using the PyTorch C++ API for?<br>
On the fly generation of neural networks for data stream analysis. Python’s GIL stands in the way of efficiently using multiple threads so C++ was the better option.</li>
<li>How does your software stack look like? And how does the PyTorch C++ API interact with other components in your stack?<br>
Linux - C/C++ - PyTorch</li>
<li>Are you using JIT (TorchScript) models or  <code>torch::nn</code>  models?<br>
torch::nn only</li>
<li>Are you training models in C++?<br>
Yes</li>
<li>How do you think the PyTorch C++ API (or the ecosystem in general) should be improved?</li>
</ol>
<ul>
<li>Better documentation</li>
<li>Better support for compilation from source</li>
</ul>

novioleo: <ol>
<li>What are you using the PyTorch C++ API for?<br>
inference for program on windows platform</li>
<li>How does your software stack look like? And how does the PyTorch C++ API interact with other components in your stack?<br>
Linux - C/C++ - PyTorch</li>
<li>Are you using JIT (TorchScript) models or  <code>torch::nn</code>  models?<br>
torch::nn only</li>
<li>Are you training models in C++?<br>
No</li>
<li>How do you think the PyTorch C++ API (or the ecosystem in general) should be improved?<br>
DO NOT CHANGE THE API FREQUENTLY</li>
</ol>

StefanD: <ol>
<li>What are you using the PyTorch C++ API for?<br>
Training &amp; Inference for image analysis (semantic segmentation at the moment) in a large C++ application.</li>
<li>How does your software stack look like? And how does the PyTorch C++ API interact with other components in your stack?<br>
Windows/VS2017. A plugin in our software links libtorch. This has caused some problems with thread local variables.</li>
<li>Are you using JIT (TorchScript) models or  <code>torch::nn</code>  models?<br>
Just torch::nn, no JIT.</li>
<li>Are you training models in C++?<br>
Yes. We switched from CNTK after the development was discontinued. Unfortunately, there are not a lot of options for training from C++.</li>
<li>How do you think the PyTorch C++ API (or the ecosystem in general) should be improved?<br>
-Saving/loading ONNX would be great (we have developed custom code for that).<br>
-Fix warnings in VS2017 conformance mode.<br>
-Compiling from source takes much longer than CNTK, DLLs are huge.<br>
-Fix memory leaks/clean shutdown (e.g. C10_DEFINE_TYPED_REGISTRY in pytorch/blob/master/c10/util/Registry.h).</li>
</ol>

Eatnow: <ol>
<li>What are you using the PyTorch C++ API for?</li>
</ol>
<p>I’m trying to port the LeelaZero Go AI to use Pytorch as a network inference backend. Hopefully this will enable rapid iteration of new network architectures via JIT models. Currently the network is implemented in OpenCL.</p>
<ol start="2">
<li>How does your software stack look like? And how does the PyTorch C++ API interact with other components in your stack?</li>
</ol>
<p>For training and experimentation the Python interface will be used.<br>
Networks are then converted via JIT and run (inference only) in the C++ multi-threaded MCTS engine.<br>
All this is mostly in Linux, but Windows compatibility would be nice too.</p>
<ol start="3">
<li>Are you using JIT (TorchScript) models or  <code>torch::nn</code>  models?</li>
</ol>
<p>JIT.</p>
<ol start="4">
<li>Are you training models in C++?</li>
</ol>
<p>Not likely.</p>
<ol start="5">
<li>How do you think the PyTorch C++ API (or the ecosystem in general) should be improved?</li>
</ol>
<p>First thing that comes to mind, I think the official tutorial would benefit from a short section explaining the way(s) to efficiently transfer between cuda tensors and various cpu data structures, both directions. In general the C++ APIs seem quite under-documented, at least compared to the Python APIs.</p>

Xavier31: <ol>
<li>semantic segmentation for augmented reality. We use the c++ API in a faily large c++ multiplatform codebase</li>
<li>native windows, linux and html5 through emscripten on any device (ios, android, windows, linux…)</li>
<li>JIT only</li>
<li>No and not planning to do so</li>
<li>better windows support, keep the c++ API up to date with the python API</li>
</ol>
<p>Thank you for your work !</p>

rtkaratekid: <ul>
<li>
<p>What are you using the PyTorch C++ API for?<br>
Making a faster and more lightweight deployment binary. I also just really enjoy C++.</p>
</li>
<li>
<p>How does your software stack look like? And how does the PyTorch C++ API interact with other components in your stack?<br>
We’re using C for our main product. Currently figuring out how the model with be incorporated into the main product.</p>
</li>
<li>
<p>Are you using JIT (TorchScript) models or <code>torch::nn</code> models?<br>
JIT for now</p>
</li>
<li>
<p>Are you training models in C++?<br>
Not right now but possibly in the future.</p>
</li>
<li>
<p>How do you think the PyTorch C++ API (or the ecosystem in general) should be improved?<br>
I’d like it if there was a way to compile the model to a binary file so as to reduce the time it takes to be loaded/called from the file.</p>
</li>
</ul>

AlexanderB: <ol>
<li>I implemented a method for computing optical flow (Spynet).</li>
<li>a) How does your software stack look like?<br>
I implemented a Custom dataset loader which uses OpenCV methods (imread, readOpticalFlow) to read images and corresponding optical flow from the FlyingChairs dataset. Training data is then augmented (with more OpenCV methods) and finally converted to torch Tensors.<br>
The tensors are fed into the CNN.</li>
<li>b) And how does the PyTorch C++ API interact with other components in your stack?<br>
I don’t understand the question.</li>
<li>Are you using JIT (TorchScript) models or <code>torch::nn</code> models?<br>
No.</li>
<li>Are you training models in C++?<br>
Yes.</li>
<li>How do you think the PyTorch C++ API (or the ecosystem in general) should be improved?<br>
When I started to learn PyTorch C++ I had no experience with PyTorch or CNNs. I found it difficult to find out what each function does since the documentation seems to be auto-generated from Code without doxygen-style comments. I had to constantly look up things in the Python interface docs. So I think the docs could be improved in a first step by linking the corresponding docs of the Python interface.<br>
Also I’ve seen Python code which selects for example [<em>,</em>,*,0] from a tensor of shape (N,W,H,C). I’d like to do that in C++ but I don’t know how (or if it’s possible).</li>
</ol>

Peter_Featherstone: <p>onnxruntime is the way to go. that library is awesome. it’s ridiculously fast. with a quick MRI script, you can easily built a monolythic static library ready to embed in your app.</p>

7starsea: <ol>
<li>What are you using the PyTorch C++ API for?<br>
a drop replacement of numpy on gpu, must faster and you also want to save some energy.</li>
<li>How does your software stack look like? And how does the PyTorch C++ API interact with other components in your stack?<br>
linux + gcc</li>
<li>Are you using JIT (TorchScript) models or <code>torch::nn</code> models?<br>
Not yet</li>
<li>Are you training models in C++?<br>
Will do it some time later.</li>
<li>How do you think the PyTorch C++ API (or the ecosystem in general) should be improved?<br>
Documentation and API reference, sometimes you need to dive into the source code to figure out the c++ api.</li>
</ol>

ericrhenry: <ol>
<li>
<p>What are you using the PyTorch C++ API for?<br>
Integrating it directly into the workflow of a large C++ image-processing application for the purpose of on-the-fly classification of cell morphologies from microscope images. Have a quite successful SVM classifier in place which uses sets of geometric descriptors computed from individual cell pixel sets, but the demands on the classifier are increasing to the point where a deep-learning approach applied to full cell images looks promising.</p>
</li>
<li>
<p>Software stack?<br>
linux + gcc/clang</p>
</li>
<li>
<p>JIT or torch::nn models?<br>
No/ have build a dynamically configurable replacement for Sequential, which will be my baseline “model”. Hope to do the same with dataset/optimizer/etc. specifications.</p>
</li>
<li>
<p>Are you training models in C++?<br>
Not yet, but hopefully soon.</p>
</li>
<li>
<p>PyTorch C++ API review: Still new to it (a month or two). Like the previous commenter, I often end up digging through the source code (KDevelop is awesome for that, BTW). I understand that the various namespaces–e.g., c10::, at::, torch:: have a largely historical basis. Still, the documentation could use some meat.<br>
As a model, I would point to the astoundingly elegant and useful documentation for the open source finite-element package “dealii”; this has been created and maintained largely by a single academic group in Germany (AFAIK), but the class references and very extensive set of tutorials are substantial and beautifully integrated.</p>
</li>
</ol>

dendisuhubdy: <aside class="quote no-group" data-username="yf225" data-post="1" data-topic="55163">
<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/yf225/48/3418_2.png" class="avatar"> yf225:</div>
<blockquote>
<ul>
<li>What are you using the PyTorch C++ API for?</li>
</ul>
</blockquote>
</aside>
<p>Optimizing audio models in embedded systems. Also we use it in production in my crypto derivatives exchange for market surveillance.</p>
<aside class="quote no-group" data-username="yf225" data-post="1" data-topic="55163">
<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/yf225/48/3418_2.png" class="avatar"> yf225:</div>
<blockquote>
<ul>
<li>How does your software stack look like? And how does the PyTorch C++ API interact with other components in your stack?</li>
</ul>
</blockquote>
</aside>
<p>Mainly C++ that is why we prefer a C++ based DL library like libtorch.</p>
<aside class="quote no-group" data-username="yf225" data-post="1" data-topic="55163">
<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/yf225/48/3418_2.png" class="avatar"> yf225:</div>
<blockquote>
<ul>
<li>Are you using JIT (TorchScript) models or <code>torch::nn</code> models?</li>
</ul>
</blockquote>
</aside>
<p>torch::nn models</p>
<aside class="quote no-group" data-username="yf225" data-post="1" data-topic="55163">
<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/yf225/48/3418_2.png" class="avatar"> yf225:</div>
<blockquote>
<ul>
<li>Are you training models in C++?</li>
</ul>
</blockquote>
</aside>
<p>Not yet but soon we will</p>
<aside class="quote no-group" data-username="yf225" data-post="1" data-topic="55163">
<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/yf225/48/3418_2.png" class="avatar"> yf225:</div>
<blockquote>
<ul>
<li>How do you think the PyTorch C++ API (or the ecosystem in general) should be improved?</li>
</ul>
</blockquote>
</aside>
<p>I wish the API would be much more simpler. Please adjust the backend more towards the new C++20 standard, there’s a lot that are stuck in C++11.</p>

mxkrn: <p>Hey Xavier31, can you provide some insight into your build process for building libtorch with emscripten. I’m trying to do the same but running into issues. (Here’s a post describing my problems <a href="https://stackoverflow.com/questions/64062268/compiling-libtorch-with-emscripten-using-cmake" rel="nofollow noopener">https://stackoverflow.com/questions/64062268/compiling-libtorch-with-emscripten-using-cmake</a>)</p>