image848×727 39.9 KB

For each batch, there will be 4 loops of computation (line number 1,2,3,6) that I think is the main reason for slow forward.
You can try not to do so much loop operation by using tensor operations. For example, in line 4 and 5, maybe you can use
mask = ((coords.float() - center[None].repeat(coords.shape[0], 1).float()) ** 2).sum(dim=1) ** 0.5 <= radius ** 2

that compute all you need by tensor instead of a loop.
Same concept can be applied to the other loop operations.