It seems not easy for current pytorch. Because current operations only accept scalars as the parameters instead of tensor. Something like torch.range(tensor start, tensor end) sovles your problem. You can also refer to https://github.com/pytorch/nestedtensor (which is still experimental)