CUDA error: an illegal memory access was encountered in cuda extension

ansj11: <p>I am realize sparse median filter with cuda extension, it is ok when kernel size is 5, but failed when kernel size 7, here is my code</p>
<pre><code class="lang-auto">#include &lt;torch/extension.h&gt;
#include &lt;cuda.h&gt;
#include &lt;cuda_runtime.h&gt;
#include &lt;vector&gt;


template &lt;typename scalar_t&gt;
__device__ void quickSort(scalar_t * src, int begin, int end, int idn, int idh, int idw, int H, int W, int C) {
    if (begin &gt;= end) {
        return ;
    }
    auto pivot = src[idn * C * H * W + begin * H * W + idh * W + idw];
    int i = begin;
    int j = end;
    while (i &lt; j) {
        while (i &lt; j &amp;&amp; src[idn * C * H * W + j * H * W + idh * W + idw] &gt;= pivot)
            j--;
        while (i &lt; j &amp;&amp; src[idn * C * H * W + i * H * W + idh * W + idw] &lt;= pivot)
            i++;
        if (i &lt; j) {
            float tmp = src[idn * C * H * W + i * H * W + idh * W + idw];
            src[idn * C * H * W + i * H * W + idh * W + idw] = src[idn * C * H * W + j * H * W + idh * W + idw];
            src[idn * C * H * W + j * H * W + idh * W + idw] = tmp;
        }
    }
    float tmp = src[idn * C * H * W + begin * H * W + idh * W + idw];
    src[idn * C * H * W + begin * H * W + idh * W + idw] = src[idn * C * H * W + i * H * W + idh * W + idw];
    src[idn * C * H * W + i * H * W + idh * W + idw] = tmp;

    quickSort(src, begin, i-1, idn, idh, idw, H, W, C);
    quickSort(src, i+1, end, idn, idh, idw, H, W, C);
}


template &lt;typename scalar_t&gt;
__global__ void median_cuda_ker(scalar_t *__restrict__ depth,
                              const scalar_t *__restrict__ mask,
                              scalar_t *__restrict__ output,
                              int N, int C, int H, int W, int ksize)
{
    int index = blockIdx.x * blockDim.x + threadIdx.x;
    int stride = blockDim.x * gridDim.x;
    // printf("blockIdx.x: %d, blockDim.x: %d, threadIdx.x:%d, gridDim.x: %d\n", blockIdx.x, blockDim.x, threadIdx.x, gridDim.x);
    for (int ids = index; ids &lt; N * 1 * H * W; ids += stride)
    {
        // ids = nCHW + cHW + hW + w
        int idw = ids % W;
        ids = (ids - idw) / W;
        int idh = ids % H;
        ids = (ids - idh) / H;
        int idc = ids % 1;
        ids = (ids - idc) / 1;
        int idn = ids;

        // printf("%d, %d, %d, %d\n", idn, idc, idh, idw);
        if (mask[idn * C * H * W + C/2 * H * W + idh * W + idw] &gt; 0){
            return;
        }

        int count = 0;
        for (int idx = 0; idx &lt; C; idx++)
        {
            float m = mask[idn * C * H * W + idx * H * W + idh * W + idw];
            if (idh &gt; -1 &amp;&amp; idh &lt; H &amp;&amp; idw &gt; -1 &amp;&amp; idw &lt; W &amp;&amp; m &gt; 0)
            {
                count += 1;
            }
        }
        if (count &gt; 0){
            quickSort(depth, 0, C - 1, idn, idh, idw, H, W, C);
            int mid = (C - count) + count / 2;  // 0 num + count / 2
            // printf("%d, %d\n", mid, C, depth[idn * C * H * W + mid * H * W + idh * W + idw]);
            output[idn * 1 * H * W + 0 * H * W + idh * W + idw] = depth[idn * C * H * W + mid * H * W + idh * W + idw];
        }
    }
}


std::vector&lt;torch::Tensor&gt; median_cuda(torch::Tensor depth, torch::Tensor mask, int ksize, torch::Tensor output)
{
    const int N = depth.size(0);
    const int C = depth.size(1);
    const int H = depth.size(2);
    const int W = depth.size(3);

    // std::cout &lt;&lt; "N=" &lt;&lt; N &lt;&lt; ", C=" &lt;&lt; C &lt;&lt; ", H=" &lt;&lt; H &lt;&lt; ", W=" &lt;&lt; W &lt;&lt; std::endl;

    const int threads = 1024;
    const int blocks = (N * 1 * H * W + threads - 1) / threads;

    AT_DISPATCH_FLOATING_TYPES(depth.type(), "median_cuda", ([&amp;] {
                                   median_cuda_ker&lt;scalar_t&gt;&lt;&lt;&lt;blocks, threads&gt;&gt;&gt;(
                                       depth.data&lt;scalar_t&gt;(),
                                       mask.data&lt;scalar_t&gt;(),
                                       output.data&lt;scalar_t&gt;(),
                                       N, C, H, W, ksize);
                               }));

    return {output};
}

</code></pre>
<p>and python code is</p>
<pre><code class="lang-auto">def median_filter(depth, continuity_map, ksize=3):
    B, _, H, W = depth.shape
    radius = ksize // 2
    depth_pad = F.pad(depth, (radius, radius, radius, radius), mode='replicate')
    cmap_pad = F.pad(continuity_map, (radius, radius, radius, radius), mode='replicate')
    
    depth_patch = F.unfold(depth_pad, ksize, stride=1, padding=0).view(B, -1, H, W).contiguous()   # BC(N=HW)
    cmap_patch = F.unfold(cmap_pad, ksize, stride=1, padding=0).view(B, -1, H, W).contiguous()   # BC(N=HW)
    depth_patch[cmap_patch == 0] = 0
    
    filtered_depth = depth.clone()
    t0 = time()
    # print(depth_patch.shape, cmap_patch.shape)
    filtered_depth = median(depth_patch, cmap_patch, ksize, filtered_depth)[0]  # BCHW
    # print("median: ", time() - t0)

    return filtered_depth.unsqueeze(0)
</code></pre>
<p>what is the reason?</p>

ptrblck: <p>Run the code through <code>compute-sanitizer</code> which should point you towards the illegal memory access. Alternatively, post a minimal and executable code snippet reproducing the issue ideally using <a href="https://pytorch.org/docs/stable/cpp_extension.html#torch.utils.cpp_extension.load_inline"><code>torch.utils.cpp_extension.load_inline</code></a>.</p>