CUDA kernel segfaults on dispatch

varunagrawal: <p>Here is my code. Even if I set the kernel code to be empty, it still segfaults. An equivalent file using only CPU works great so I am not sure what I am doing wrong.</p>
<p>Any ideas what may be the problem?</p>
<pre><code class="lang-auto">#include &lt;ATen/ATen.h&gt;
#include &lt;ATen/TensorUtils.h&gt;
#include &lt;ATen/cuda/CUDAContext.h&gt;
#include &lt;c10/cuda/CUDAGuard.h&gt;
#include &lt;ATen/cuda/CUDAApplyUtils.cuh&gt;

#include &lt;iostream&gt;
#include &lt;stdio.h&gt;
#include "cuda_helpers.h"

template &lt;typename T&gt;
__global__ void RgbGradientForward(
    const int nthreads,
    const T* data,
    const int batch_size,
    const int channels,
    const int height,
    const int width,
    T* Ix,
    T* Iy,
    T* output) {
  printf("inside kernel");
  int x =10;

}

at::Tensor RGBGradient_forward_cuda(const at::Tensor&amp; input) {
  // NOTE We assume input and output are NHWC to make accessing the data a lot
  // easier
  AT_ASSERTM(input.device().is_cuda(), "input must be a CUDA tensor");

  at::TensorArg input_t{input, "input", 1};

  at::CheckedFrom c = "RgbGradient_forward_cuda";
  at::checkAllSameGPU(c, {input_t});
  at::checkAllSameType(c, {input_t});

  at::cuda::CUDAGuard device_guard(input.device());

  auto batch_size = input.size(0);
  auto height = input.size(1);
  auto width = input.size(2);
  auto channels = input.size(3);

  // Define the gradient variables
  at::Tensor Ix = at::zeros({batch_size, height, width}, input.options());
  at::Tensor Iy = at::zeros({batch_size, height, width}, input.options());
  at::Tensor output = at::zeros({batch_size, height, width}, input.options());

  auto output_size = batch_size * height * width;
  cudaStream_t stream = at::cuda::getCurrentCUDAStream();

  dim3 grid(std::min(
      at::cuda::ATenCeilDiv(
          static_cast&lt;int64_t&gt;(output_size), static_cast&lt;int64_t&gt;(512)),
      static_cast&lt;int64_t&gt;(4096)));
  dim3 block(512);

  if (output.numel() == 0) {
    AT_CUDA_CHECK(cudaGetLastError());
    return output;
  }

  std::cout &lt;&lt; "launching kernel" &lt;&lt; std::endl;

  AT_DISPATCH_FLOATING_TYPES_AND_HALF(input.type(), "RGBGradient_forward", [&amp;] {
    RgbGradientForward&lt;scalar_t&gt;&lt;&lt;&lt;grid, block, 0, stream&gt;&gt;&gt;(
        output_size,
        input.contiguous().data&lt;scalar_t&gt;(),
        batch_size,
        channels,
        height,
        width,
        Ix.contiguous().data&lt;scalar_t&gt;(),
        Iy.contiguous().data&lt;scalar_t&gt;(),
        output.contiguous().data&lt;scalar_t&gt;());
  });
  AT_CUDA_CHECK(cudaGetLastError());
  return output;
}
</code></pre>

varunagrawal: <p><a class="mention" href="/u/goldsborough">@goldsborough</a> <a class="mention" href="/u/alband">@albanD</a> any thoughts?</p>

yf225: <p><a class="mention" href="/u/varunagrawal">@varunagrawal</a> If the CPU version works, it seems to suggest that it’s a CUDA memory pointer problem. Could you give us the backtrace for the segfault to further debug?</p>

varunagrawal: <p>I found the issue. I had to rename the CUDA kernel function to <code>RgbGradientForwardKernel</code> since I was using <code>RgbGradientForward</code> for the CPU version. This shouldn’t be an issue though since torchvision uses the same function names for the CPU and GPU kernel.</p>
<p>Maybe a bug somewhere?</p>