class VoxelsSumming(torch.autograd.Function): """Adapted from https://github.com/nv-tlabs/lift-splat-shoot/blob/master/src/tools.py#L193""" @staticmethod def forward(ctx, x, geometry, ranks): """The features x and geometry are ranked by voxel positions.""" # Cumulative sum of all features. x = x.cumsum(0) # Indicates the change of voxel. mask = torch.ones(x.shape[0], device=x.device, dtype=torch.bool) mask[:-1] = ranks[1:] != ranks[:-1] x, geometry = x[mask], geometry[mask] # Calculate sum of features within a voxel. x = torch.cat((x[:1], x[1:] - x[:-1])) ctx.save_for_backward(mask) ctx.mark_non_differentiable(geometry) return x, geometry @staticmethod def backward(ctx, grad_x, grad_geometry): (mask,) = ctx.saved_tensors # Since the operation is summing, we simply need to send gradient # to all elements that were part of the summation process. indices = torch.cumsum(mask, 0) indices[mask] -= 1 output_grad = grad_x[indices] return output_grad, None, None这段代码中有判断是否是同一体素的语句吗?
时间: 2024-03-31 13:33:25 浏览: 18
是的,这段代码中有判断是否是同一体素的语句。具体来说,在前向传递中,第12行的语句`mask[:-1] = ranks[1:] != ranks[:-1]`用于创建一个掩码`mask`,以指示哪些张量属于同一体素。这个掩码的长度为张量列表中所有张量的数量。`mask`中的每个元素对应于张量列表中的一个张量,如果该张量与前一个张量不属于同一体素,则该元素为True,否则为False。在接下来的代码中,使用这个掩码来提取属于同一体素的张量,然后计算每个体素中所有特征值的总和。