Hi Richard,
Thanks for the solution! I also thought about the cat operation, but I am not sure if there is some build-in function can do this efficiently without for loop since masking should be a very common function.
I also tried pack_padded_sequence and pad_packed_sequence, but they require the index matrix “I” is sorted, which is not flexible enough.