@albanD OK I will try to make a small code sample. But might be a bit later.
One workaround I found from internet is to use below code , it will not cause Nan.
lengths = (1 - x_mask).sum(1)
return torch.cat([torch.max(i[:l], dim=0)[0].view(1,-1) for i,l in zip(x, lengths)], dim=0)