C++ API Exception (CPP/c10::Error) when using nearest_cuda

MaxMayya: <p>Hi,<br>
I’m trying to use the C++ API of pytorch_cluster in conjunction with the LibTorch C++ API. I managed to build and install the former <code>WITH_CUDA=ON</code> and tried to reproduce the <a href="https://github.com/rusty1s/pytorch_cluster#nearest" rel="noopener nofollow ugc">nearest example</a> below:</p>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;
#include &lt;torchcluster/cluster.h&gt;

int main() {
    std::cout &lt;&lt; torch::cuda::is_available() &lt;&lt; std::endl;
    torch::Device device(torch::kCUDA);
    auto options = torch::TensorOptions()
        .dtype(torch::kFloat16)
        .layout(torch::kStrided)
        .device(device)
        .requires_grad(false);
    auto x = torch::tensor({{-1, -1}, {-1, 1}, {1, -1}, {1, 1}}, options);
    auto batch_x = torch::tensor({0, 0, 0, 0}, options);
    auto y = torch::tensor({{-1, 0}, {1, 0}}, options);
    auto batch_y = torch::tensor({0, 0}, options);
    auto cluster = nearest(x, y, batch_x, batch_y);
    std::cout &lt;&lt; cluster &lt;&lt; std::endl;
    return 0;
}
</code></pre>
<p>Reaching the <code>nearest</code> function (which calls <code>nearest_cuda</code>), I get the following exception error:</p>
<blockquote>
<p>Exception has occurred: CPP/c10::Error<br>
Unhandled exception at 0x00007FFC29FBCD29 in TorchExample.exe: Microsoft C++ exception: c10::Error at memory location 0x000000125A1DEDC0.</p>
</blockquote>
<p>Precisely, the error occurs when invoking <code>AT_DISPATCH_FLOATING_TYPES_AND</code>.</p>
<p>Environment:</p>
<ul>
<li>OS: Windows 10 Pro</li>
<li>Torch C++ installation: libtorch-win-shared-with-deps-1.13.1+cu117</li>
<li>Compiler: Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30147 for x64</li>
<li>Build variant: RelWithDebInfo</li>
<li>Torch Cluster built from 1.6.1 stable release</li>
</ul>
<p>I’m sure that the torch_cluster build links correctly to the LibTorch. I’ve already, successfully, used LibTorch alone with CUDA (including autograd functionalities), but cannot tell weather this is fully related to torch_cluster.</p>
<p>Any thoughts?</p>
<p>Thank you in advance</p>

ptrblck: <p>Based on the error message these errors could be raised if some objects were not moved to the GPU as described e.g. <a href="https://discuss.pytorch.org/t/c-exception-c10-error-at-memory-location/70137/3">here</a>. However, the actual error message in Windows doesn’t point to the failing operation. Are you able to reproduce the issue on Linux?</p>

MaxMayya: <p>Thank you for the quick reply.<br>
All 4 variables are in GPU (I checked them with <code>is_cuda()</code>). Unfortunately I don’t have Linux to test!</p>

MaxMayya: <p>Another input that could help, I was obliged to copy-paste the torchcluster.dll from the install directory to the target application directory. Don’t know if this breaks any implicite mechanisms but I’m already successfully doing so for the torch library (based on the <a href="https://pytorch.org/cppdocs/installing.html#minimal-example" rel="noopener nofollow ugc">torch example</a>).</p>

ptrblck: <p>Are you using <a href="https://github.com/rusty1s/pytorch_cluster#c-api">this section</a> directly to reproduce the issue on Windows?<br>
If so, I could try to run it on Linux assuming I can figure out all dependencies.</p>

MaxMayya: <p>Except for exporting the python path to <code>Torch_DIR</code>, yes. As a matter of fact, since I’m using the LibTorch C++ installation, the <code>Torch_DIR</code> links to <code>&lt;some-root-path&gt;/libtorch-win-shared-with-deps-1.13.1+cu117/libtorch/share/cmake/Torch</code>. I’m currently trying to reproduce it using conda/python installation.</p>