Libtorch on jupyter using Cling C++

dambo: <p>Hello,<br>
Out of curiosity, and as far as I know no one did this before, I tried using Libtorch directly on Jupiter using Cling (<a href="https://github.com/QuantScientist/DarkTorch/blob/master/cpp_pytorch.ipynb" rel="nofollow noopener">https://github.com/QuantScientist/DarkTorch/blob/master/cpp_pytorch.ipynb</a>).<br>
I was partially successful and was able to load a CNN model, however this line:</p>
<pre><code class="lang-auto">auto input_tensor = torch::from_blob(m.data, {1, 224, 224, kCHANNELS});
</code></pre>
<p>Raises the following exception which to my best understanding is unrelated to Libtorch:</p>
<pre><code class="lang-auto">IncrementalExecutor::executeFunction: symbol '__emutls_v._ZSt11__once_call' unresolved while linking [cling interface function]!
</code></pre>
<p>Any help would be appreciated.</p>

ptrblck: <p>I think <a class="mention" href="/u/krshrimali">@krshrimali</a> is working on something similar and might have an idea.</p>

krshrimali: <p>Thanks for the mention, <a class="mention" href="/u/ptrblck">@ptrblck</a>.</p>
<p><a class="mention" href="/u/dambo">@dambo</a> - you can check my latest blog on using Xeus Cling here: <a href="https://krshrimali.github.io/Setting-Up-Xeus-Cling-Libtorch-OpenCV/" rel="nofollow noopener">https://krshrimali.github.io/Setting-Up-Xeus-Cling-Libtorch-OpenCV/</a>. I’ve shown how to solve this issue in the later part of the blog.</p>
<p>Let me know if the problem still persists.</p>
<p>Thanks!</p>

dambo: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> thanks!<br>
<a class="mention" href="/u/krshrimali">@krshrimali</a>, what an amazing repository you have there!. I noticed that you are using Mac OSX, so there imports are very different, by inspecting the imports, I couldn’t find any reason why I have that error in my Jupyter notebook.  Did you take a look at my code?</p>
<p>Thanks,</p>

krshrimali: <p>Hi <a class="mention" href="/u/dambo">@dambo</a></p>
<p>Thanks! Glad you liked it.</p>
<p>Can you please tell if you’re using a <code>conda</code> virtual environment for Xeus-Cling? If not, can you please try and install <code>xeus-cling</code> in a new virtual environment and double-check? I suspect the error might be because of the conflicts with existing libraries.</p>
<p>Also, if the above solution doesn’t work, I suggest changing <code>set(TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=")</code>  to  <code>set(TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=1")</code> and reload the libraries and header files. This, I suspect, might solve C++ ABI compatibility issue, if any.</p>
<p>Thanks!</p>

dambo: <p>Hello,<br>
I am using conda, see my docker here:<br>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/QuantScientist/DarkTorch/blob/master/docker/cuda10/Dockerfile" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/QuantScientist/DarkTorch/blob/master/docker/cuda10/Dockerfile" target="_blank" rel="nofollow noopener">QuantScientist/DarkTorch/blob/master/docker/cuda10/Dockerfile</a></h4>
<pre><code class="lang-">#FROM nvcr.io/nvidia/tensorrt:19.03-py3
# FROM nvcr.io/nvidia/pytorch:19.07-py3
# FROM nvcr.io/nvidia/pytorch:18.12-py3
FROM nvcr.io/nvidia/pytorch:19.02-py3
# https://github.com/freemanix/torchbuilder/blob/master/Dockerfile

RUN apt-get update &amp;&amp;\
    apt-get install -y sudo git bash


ENV PATH /usr/local/nvidia/bin:/usr/local/cuda/bin:/code/cmake-3.14.3-Linux-x86_64/bin:${PATH}
ENV PYTHONPATH=/opt/conda/lib/python3.6/site-packages/:$PYTHONPATH
ENV LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/opt/conda/lib/:/opt/conda/lib/python3.6

RUN apt-get update \
 &amp;&amp; apt-get install -y \
    build-essential git gfortran \
    cmake curl wget unzip libreadline-dev libjpeg-dev libpng-dev ncurses-dev \
    imagemagick gnuplot gnuplot-x11 libssl-dev libzmq3-dev graphviz

</code></pre>

  This file has been truncated. <a href="https://github.com/QuantScientist/DarkTorch/blob/master/docker/cuda10/Dockerfile" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
</p>
<p>Everything runs perfectly when I <strong>dont</strong> use cling (see <a href="https://github.com/QuantScientist/DarkTorch/tree/master/001_verify" rel="nofollow noopener">https://github.com/QuantScientist/DarkTorch/tree/master/001_verify</a>) on the same docker image; I already resolved anything related to <code>TORCH_CXX_FLAGS "-D_GLIBCXX_USE_CXX11_ABI=1</code></p>
<p>This happens only when I use cling. Is there a way to force cling to be ABI compatible or I have to compile from scratch?</p>
<p>Any would would loading the model.pt work if there is an ABI comptability problem?<br>
Thanks,</p>

krshrimali: <p>Hi <a class="mention" href="/u/dambo">@dambo</a></p>
<p>I checked the <code>Dockerfile</code> and I suggest creating a <code>conda environment</code> and then installing the required libraries in it. I had the same problem, and installing it in a <code>virtualenv</code> worked.</p>
<p>I have verified this on the <code>Dockerfile</code> as well.  I think there’s already a Dockerfile created, which I’ll release in a couple of days after some testing.</p>
<p>Please let me know if using a conda environment doesn’t work. I have also tested it on the Linux systems, and it works well. The last solution could be to compile from scratch and see if that works. (as also mentioned in some of the GitHub issues).</p>
<p>Thanks</p>

krshrimali: <p>Hi <a class="mention" href="/u/dambo">@dambo</a></p>
<p>I just had a quick look and was able to reproduce the problem. I could solve it by building PyTorch from the source. Also, I had to set the flag <code>D_GLIBCXX_USE_CXX11_ABI</code> to <code>0</code> from <code>1</code>.</p>
<p>System: Ubuntu 16.04<br>
CUDA: None. (Installation was done without CUDA)<br>
Instructions to build from source (from where followed): <a href="https://medium.com/repro-repo/build-pytorch-from-source-on-ubuntu-18-04-1c5556ca8fbf" rel="nofollow noopener">https://medium.com/repro-repo/build-pytorch-from-source-on-ubuntu-18-04-1c5556ca8fbf</a></p>
<p>Let me know if this doesn’t solve your problem<br>
Thanks!</p>

codonna9: <p>Hi <a class="mention" href="/u/krshrimali">@krshrimali</a>  Sorry for coming late to this discussion. I haven’t been able to solve this problem. Just to clarify, I have to do something like (I use Ubuntu 18.04):<br>
export D_GLIBCXX_USE_CXX11_ABI=1<br>
before compiling the Pytorch source code, right?<br>
Many thanks!</p>

justinblaber: <p>Same exact issue… using xeus-cling in conda environment, manually built pytorch. Seems to mostly work except for the “from_blob” function which has the same exact:</p>
<p><code>IncrementalExecutor::executeFunction: symbol '__emutls_v._ZSt11__once_call' unresolved while linking [cling interface function]!</code></p>
<p>Error…</p>

codonna9: <p>Just in case anyone reading this, I updated my libtorch cpu version to 1.7 and it worked smoothly with Cling C++</p>