From 76e870f17c284b8eba0c24dbedfbf3779c8647dc Mon Sep 17 00:00:00 2001 From: Wenhao Wu <79644370+wHao-Wu@users.noreply.github.com> Date: Mon, 7 Mar 2022 21:08:36 +0800 Subject: [PATCH] [Fix] Fix bugs in Voxelization op (#1746) * Fix bugs in Voxelization op * fix comments * fix lint * add comments --- mmcv/ops/csrc/pytorch/cpu/voxelization.cpp | 30 +++++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/mmcv/ops/csrc/pytorch/cpu/voxelization.cpp b/mmcv/ops/csrc/pytorch/cpu/voxelization.cpp index 25cc2b52c..a21f849a0 100644 --- a/mmcv/ops/csrc/pytorch/cpu/voxelization.cpp +++ b/mmcv/ops/csrc/pytorch/cpu/voxelization.cpp @@ -26,13 +26,22 @@ void dynamic_voxelize_forward_cpu_kernel( coor[ndim_minus_1 - j] = c; } - if (failed) - memset(&coors[i][0], -1, NDim * sizeof(T_int)); - else - memcpy(&coors[i][0], &coor[0], NDim * sizeof(T_int)); + // memcpy and memset will cause problem because of the memory distribution + // discontinuity of TensorAccessor, so here using loops to replace memcpy + // or memset + if (failed) { + for (int k = 0; k < NDim; ++k) { + coors[i][k] = -1; + } + } else { + for (int k = 0; k < NDim; ++k) { + coors[i][k] = coor[k]; + } + } } delete[] coor; + return; } template @@ -72,14 +81,21 @@ void hard_voxelize_forward_cpu_kernel( voxel_num += 1; coor_to_voxelidx[coor[i][0]][coor[i][1]][coor[i][2]] = voxelidx; - memcpy(&coors[voxelidx][0], &coor[i][0], NDim * sizeof(T_int)); + // memcpy will cause problem because of the memory distribution + // discontinuity of TensorAccessor, so here using loops to replace memcpy + for (int k = 0; k < NDim; ++k) { + coors[voxelidx][k] = coor[i][k]; + } } // put points into voxel num = num_points_per_voxel[voxelidx]; if (max_points == -1 || num < max_points) { - memcpy(&voxels[voxelidx][num][0], &points[i][0], - num_features * sizeof(T)); + // memcpy will cause problem because of the memory distribution + // discontinuity of TensorAccessor, so here using loops to replace memcpy + for (int k = 0; k < num_features; ++k) { + voxels[voxelidx][num][k] = points[i][k]; + } num_points_per_voxel[voxelidx] += 1; } }