Calling tensor.packed_accessor32() throws memory error

Problem Summary

Inside my main method, I create some tensors and pass them to the function measure_distance_cuda. 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 tensor.packed_accessor<>() I get the following runtime error coming from TensorBase.h:

Exception has occurred: CPP/c10::Error
Unhandled exception at 0x00007FF8071ECF19 in cuda_test.exe: Microsoft C++ exception: c10::Error at memory location 0x0000004A42CFE4F0.

What I’ve tried:

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.

Minimal reproducible code

My main function:

#include <iostream>

#include <ATen/ATen.h>
#include <torch/types.h>

#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 << distances << std::endl;
}

raycast_cuda.cu

#include <cuda.h>
#include <cuda_runtime.h>

#include <ATen/ATen.h>
#include <torch/types.h>

__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<float_t, 2> d_acc = distances.packed_accessor32<float_t, 2>();

    // get accessors for inputs ****ERROR HAPPENS HERE****
    at::PackedTensorAccessor32<float_t, 2> vert_acc = vertices.packed_accessor32<float_t, 2>()

    return distances;
}

Some thoughts:

  • I noted that creating an accessor for the return values (distances) 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.

Why is this happening?

Could you check if the issue is the wrong type being specified? It looks like you are creating a kFloat64 tensor which would correspond to double_t rather than float_t as the type is 64-bits wide.

Note that the default PyTorch dtype is 32-bit float which would explain why it is working for the distances tensor.

1 Like

That was the issue, thank you very much!

1 Like