Mask 3-D tensor with variable length masks

Suppose I have a tensor of shape (B, L, E) and a boolean mask of shape (B, L).

I’m trying to pluck the unmasked sections from the 3-D tensor and place them into another tensor of shape (B, D, E) (where D is smaller than or equal to L). I’ve looked into the gather and scatter operations but they’ve so far been hard to get my head around :sweat: and it’s not obvious how I would deal with padding since masks might not necessarily have the same number of zeros.

Any ideas/directions I could follow? Thanks!!