Ensure compatibility between libtorch and PyTorch

saandeep_aathreya: <p>I am writing a simple c++ code to generate random <code>torch::Tensor</code>. Later, I will compile the library to .so and load it into my python code using <code>torch.ops.load_library(&lt;path&gt;.so)</code></p>
<p>I compile libtorch (cxx ABI) 2.1.2 from source and PyTorch 2.1.2 from pip from the <a href="https://pytorch.org/get-started/locally/" rel="noopener nofollow ugc">link</a>. Both use CUDA 12.1.</p>
<p>I have a <code>op.cpp</code> which defines my C++ operator, a CMakeLists.txt file to build a .so.<br>
Finally, I load the .so in my <code>test.py</code> file</p>
<ol>
<li><strong>CMakeLists.txt</strong></li>
</ol>
<pre><code class="lang-auto">cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
project(warp_perspective)

SET(CMAKE_PREFIX_PATH /home/saandeepaath-admin/projects/learning/cpp_cmake/example2/libtorch)
find_package(Torch REQUIRED)

# Define our library target
add_library(warp_perspective SHARED op.cpp)
# Enable C++14
target_compile_features(warp_perspective PRIVATE cxx_std_14)
# Link against LibTorch
target_link_libraries(warp_perspective "${TORCH_LIBRARIES}")
</code></pre>
<ol start="2">
<li><strong>op.cpp</strong></li>
</ol>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;
#include &lt;iostream&gt;


torch::Tensor create_rand(){
  torch::Tensor ten = torch::rand({5});
  std::cout&lt;&lt;"TENSOR: "&lt;&lt;ten&lt;&lt;std::endl;
  return ten;
}


TORCH_LIBRARY(op_cpp, m) {
  m.def("create_rand", create_rand);
}

</code></pre>
<ol start="3">
<li><strong>test.py</strong></li>
</ol>
<pre><code class="lang-auto">import sys
import torch
# import torch.utils

print(torch.utils.cmake_prefix_path)
print(torch._C._GLIBCXX_USE_CXX11_ABI)
torch.ops.load_library("build/libwarp_perspective.so")
print(torch.ops.op_cpp.create_rand())
</code></pre>
<p><strong>Output of running cmake</strong></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/0/e010d2b5503d04d580b85f9f232594c8f4096364.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/e010d2b5503d04d580b85f9f232594c8f4096364" title="Screen Shot 2023-12-28 at 11.44.12 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/0/e010d2b5503d04d580b85f9f232594c8f4096364_2_626x500.jpeg" alt="Screen Shot 2023-12-28 at 11.44.12 PM" data-base62-sha1="vYaZNajqvyFrqJ44OIYv2uKqZWk" width="626" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/0/e010d2b5503d04d580b85f9f232594c8f4096364_2_626x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/e/0/e010d2b5503d04d580b85f9f232594c8f4096364_2_939x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/e/0/e010d2b5503d04d580b85f9f232594c8f4096364_2_1252x1000.jpeg 2x" data-dominant-color="07100E"><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">Screen Shot 2023-12-28 at 11.44.12 PM</span><span class="informations">1476×1178 204 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>However, when I try to load my .so file into my test.py file, I get the following error</p>
<p><code>OSError: &lt;path&gt;/libwarp_perspective.so: undefined symbol: _ZN5torch3jit17parseSchemaOrNameERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE</code></p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/1/813b3d5f153a129ddef860b977813eff59277a27.png" data-download-href="https://discuss.pytorch.org/uploads/default/813b3d5f153a129ddef860b977813eff59277a27" title="Screen Shot 2023-12-28 at 11.46.41 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/1/813b3d5f153a129ddef860b977813eff59277a27_2_690x75.png" alt="Screen Shot 2023-12-28 at 11.46.41 PM" data-base62-sha1="irevSH4AIiP3xCWYoXj3uRsPqGb" width="690" height="75" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/1/813b3d5f153a129ddef860b977813eff59277a27_2_690x75.png, https://discuss.pytorch.org/uploads/default/optimized/3X/8/1/813b3d5f153a129ddef860b977813eff59277a27_2_1035x112.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/8/1/813b3d5f153a129ddef860b977813eff59277a27_2_1380x150.png 2x" data-dominant-color="121716"><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">Screen Shot 2023-12-28 at 11.46.41 PM</span><span class="informations">2868×312 238 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>Please advise.</p>

saandeep_aathreya: <p>I realized I can compile PyTorch directly from source and also use libtorch without additional installation. I set the CMAKE_PREFIX_PATH to pytorch directory and enabled CXX_ABI during installation.</p>