I was sceptical about the performance of torch.gather
so I searched for similar questions with numpy and found this post.
Similar solution from NumPy to Pytorch
I took the solution from @Andy L and translated it into pytorch. However, take it with a grain of salt, because I don't know how the strides work:
from numpy.lib.stride_tricks import as_strided
# NumPy solution:
def custom_roll(arr, r_tup):
m = np.asarray(r_tup)
arr_roll = arr[:, [*range(arr.shape[1]),*range(arr.shape[1]-1)]].copy() #need `copy`
#print(arr_roll)
strd_0, strd_1 = arr_roll.strides
#print(strd_0, strd_1)
n = arr.shape[1]
result = as_strided(arr_roll, (*arr.shape, n), (strd_0 ,strd_1, strd_1))
return result[np.arange(arr.shape[0]), (n-m)%n]
# Translated to PyTorch
def pcustom_roll(arr, r_tup):
m = torch.tensor(r_tup)
arr_roll = arr[:, [*range(arr.shape[1]),*range(arr.shape[1]-1)]].clone() #need `copy`
#print(arr_roll)
strd_0, strd_1 = arr_roll.stride()
#print(strd_0, strd_1)
n = arr.shape[1]
result = torch.as_strided(arr_roll, (*arr.shape, n), (strd_0 ,strd_1, strd_1))
return result[torch.arange(arr.shape[0]), (n-m)%n]
Here is also the solution from @Daniel M as plug and play.
def roll_by_gather(mat,dim, shifts: torch.LongTensor):
# assumes 2D array
n_rows, n_cols = mat.shape
if dim==0:
#print(mat)
arange1 = torch.arange(n_rows).view((n_rows, 1)).repeat((1, n_cols))
#print(arange1)
arange2 = (arange1 - shifts) % n_rows
#print(arange2)
return torch.gather(mat, 0, arange2)
elif dim==1:
arange1 = torch.arange(n_cols).view(( 1,n_cols)).repeat((n_rows,1))
#print(arange1)
arange2 = (arange1 - shifts) % n_cols
#print(arange2)
return torch.gather(mat, 1, arange2)
Benchmarking
First, I ran the methods on CPU.
Surprisingly, the gather
solution from above is the fastest:
n_cols = 10000
n_rows = 100
shifts = torch.randint(-100,100,size=[n_rows,1])
data = torch.arange(n_rows*n_cols).reshape(n_rows,n_cols)
npdata = np.arange(n_rows*n_cols).reshape(n_rows,n_cols)
npshifts = shifts.numpy()
%timeit roll_by_gather(data,1,shifts)
%timeit pcustom_roll(data,shifts)
%timeit custom_roll(npdata,npshifts)
>> 2.41 ms ± 68.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
>> 90.4 ms ± 882 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
>> 247 ms ± 6.08 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Running the code on GPU shows similar results:
%timeit roll_by_gather(data,shifts)
%timeit pcustom_roll(data,shifts)
131 µs ± 6.79 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
3.29 ms ± 46.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
(Note: You need torch.arange(...,device='cuda:0')
within the roll_by_gather
method)