… my math with the selecting tensor is not right, but perhaps that makes a little more sense of what I’m trying to do.  The naive way is
tmp = []
for b_i, l in enumerate(lengths):
    tmp.append(a[b_i, l-1, :])
torch.stack(tmp)