Calling tensor.packed_accessor32() throws memory error

username: Wesley_Neill
cooked: <h3>
<a name="problem-summary-1" class="anchor" href="#problem-summary-1"></a>Problem Summary</h3>
<p>Inside my main method, I create some tensors and pass them to the function <code>measure_distance_cuda</code>. From there, I try to create accessors to pass to a kernel that I’ve written (removed for minimal working example). However, when creating the accessors using <code>tensor.packed_accessor&lt;&gt;()</code> I get the following runtime error coming from TensorBase.h:</p>
<pre><code class="lang-auto">Exception has occurred: CPP/c10::Error
Unhandled exception at 0x00007FF8071ECF19 in cuda_test.exe: Microsoft C++ exception: c10::Error at memory location 0x0000004A42CFE4F0.
</code></pre>
<h3>
<a name="what-ive-tried-2" class="anchor" href="#what-ive-tried-2"></a>What I’ve tried:</h3>
<p>My first thought was that memory errors are weird and can point to the wrong line, so I removed the call to the Cuda kernel that would actually use the accessors. So no indexing is occurring whatsoever. However, the error persists.</p>
<h3>
<a name="minimal-reproducible-code-3" class="anchor" href="#minimal-reproducible-code-3"></a>Minimal reproducible code</h3>
<p>My main function:</p>
<pre><code class="lang-auto">#include &lt;iostream&gt;

#include &lt;ATen/ATen.h&gt;
#include &lt;torch/types.h&gt;

#include "raycast_cuda.cuh"

int main() {

	auto vert_options = at::TensorOptions().dtype(torch::kFloat64).device(torch::kCUDA);

	torch::Tensor vertices = torch::tensor(
		{{-1, 1, 0},
		 {1, 1, 0},
		 {-1, -1, 0}}, vert_options
	);

    at::Tensor distances = measure_distance_cuda(vertices);
	std::cout &lt;&lt; distances &lt;&lt; std::endl;
}
</code></pre>
<p>raycast_cuda.cu</p>
<pre><code class="lang-auto">#include &lt;cuda.h&gt;
#include &lt;cuda_runtime.h&gt;

#include &lt;ATen/ATen.h&gt;
#include &lt;torch/types.h&gt;

__host__
at::Tensor measure_distance_cuda(at::Tensor vertices) {

    // get return tensor and accessor ****NO ERROR HERE****
    at::TensorOptions return_tensor_options = at::TensorOptions().device(torch::kCUDA);
    at::Tensor distances = at::zeros({n_rays, n_faces}, return_tensor_options);
    at::PackedTensorAccessor32&lt;float_t, 2&gt; d_acc = distances.packed_accessor32&lt;float_t, 2&gt;();

    // get accessors for inputs ****ERROR HAPPENS HERE****
    at::PackedTensorAccessor32&lt;float_t, 2&gt; vert_acc = vertices.packed_accessor32&lt;float_t, 2&gt;()

    return distances;
}
</code></pre>
<p><strong>Some thoughts:</strong></p>
<ul>
<li>I noted that creating an accessor for the return values (<code>distances</code>) gives me no issues. It’s only angry at me for trying it on the tensors I passed into the function. So I’m suspicious that I’m doing something in the wrong scope.</li>
</ul>
<p>Why is this happening?</p>

username: eqy
cooked: <p>Could you check if the issue is the wrong type being specified? It looks like you are creating a <code>kFloat64</code> tensor which would correspond to <code>double_t</code> rather than <code>float_t</code> as the type is 64-bits wide.</p>
<p>Note that the default PyTorch dtype is 32-bit float which would explain why it is working for the <code>distances</code> tensor.</p>

username: Wesley_Neill
cooked: <p>That was the issue, thank you very much!</p>