Linking pytorch c++ (prebuilt GPU version) with Qt and GCC

naser_piltan: <p>I wrote a simple program in C++ to use my PyTorch model in <code>libtorch 1.7.1</code> . I have CUDA 10.2 on my OS, thus I downloaded CUDA 10.2 prebuilt version of <code>libtorch</code> from this <a href="https://download.pytorch.org/libtorch/cu102/libtorch-cxx11-abi-shared-with-deps-1.7.1.zip" rel="noopener nofollow ugc">link</a>. The program works fine with CMake and CUDA. CMake can load my model on GPU and returns “Yes” when I run this simple program:</p>
<blockquote>
<p><span class="hashtag-raw">#include</span> &lt;torch/torch.h&gt;<br>
<span class="hashtag-raw">#include</span> &lt;torch/nn.h&gt;<br>
<span class="hashtag-raw">#include</span> &lt;torch/script.h&gt;<br>
<span class="hashtag-raw">#include</span> <br>
int main(int argc, const char* argv<span class="chcklst-box fa fa-square-o fa-fw"></span>) {<br>
std::cout&lt;&lt;"Is CUDA avalaible ? "&lt;&lt;((torch::cuda::is_available() == 1 )?“Yes” : "No ")&lt;&lt;std::endl;<br>
torch::Tensor a = torch::ones({ 2, 2 }, torch::requires_grad());<br>
torch::Tensor b = torch::randn({ 2, 2 });<br>
a = a.to(torch::kCUDA);<br>
b = b.to(torch::kCUDA);<br>
return 0;<br>
}</p>
</blockquote>
<p>Now, I would like to use the model in my Qt Application. It gives this result when I try it :<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/a/fa60f8d9b719613bbb3992f0746f166539f155dd.png" data-download-href="https://discuss.pytorch.org/uploads/default/fa60f8d9b719613bbb3992f0746f166539f155dd" title="Screenshot from 2021-01-02 18-07-04"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/a/fa60f8d9b719613bbb3992f0746f166539f155dd_2_690x198.png" alt="Screenshot from 2021-01-02 18-07-04" data-base62-sha1="zIX816dqNhaCl3PEmpIlC2Ey5HL" width="690" height="198" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/a/fa60f8d9b719613bbb3992f0746f166539f155dd_2_690x198.png, https://discuss.pytorch.org/uploads/default/optimized/3X/f/a/fa60f8d9b719613bbb3992f0746f166539f155dd_2_1035x297.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/f/a/fa60f8d9b719613bbb3992f0746f166539f155dd.png 2x" data-dominant-color="45253B"><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">Screenshot from 2021-01-02 18-07-04</span><span class="informations">1107×319 88.3 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>This is my project QMake file :</p>
<pre><code class="lang-auto">QT -= gui
CONFIG +=  c++14 console
CONFIG -= app_bundle
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
    main.cpp
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

QMAKE_CXXFLAGS += -DGLIBCXX_USE_CXX11_ABI=0
CONFIG += no_keywords
QMAKE_CXXFLAGS += -Wno-unused-parameter
QMAKE_LFLAGS += -INCLUDE:?warp_size@cuda@at@@YAHXZ

INCLUDEPATH += /home/eaglesoft/libtorch/include/torch/csrc/api/include
INCLUDEPATH += /home/eaglesoft/libtorch/include
DEPENDPATH +=  /home/eaglesoft/libtorch/include/torch/csrc/api/include
DEPENDPATH +=  /home/eaglesoft/libtorch/include

LIBS += -L/home/eaglesoft/libtorch/lib -lc10 \
        -lc10_cuda \
        -lcaffe2_module_test_dynamic \
        -lcaffe2_observers \
        -ltorch_cpu \
        -ltorch \
        -lc10d_cuda_test \
        -lcaffe2_detectron_ops_gpu \
        -lcaffe2_nvrtc \
        -lfmt \
        -lnvrtc-builtins \
        -lshm \
        -ltensorpipe \
        -ltorch_cuda
CUDA_DIR = /usr/local/cuda-10.2
INCLUDEPATH  += $$CUDA_DIR/include
QMAKE_LIBDIR += $$CUDA_DIR/lib64

LIBS += -L/usr/local/cuda-10.2/lib64 -lcuda -lcudart -lcurand
LIBS += -L/usr/lib/x86_64-linux-gnu -lcublas
LIBS += -lstdc++fs
</code></pre>