I don’t think your solution would work: the dimension you want to infer with view is not necessarily a multiple of hidden_size, since the size of masked_outputs is the number of non-zeros elements in the mask. In fact, this size can be anything depending on your data.
What you can do is to use the masks at the very step of your loss computation, and before this step, keep everything at the dimensions of your dec_outputs. Since in a seq2seq model your targets and outputs would have the same masks, you can simply do:
masked_outputs = torch.masked_select(dec_outputs, mask)
masked_targets = torch.masked_select(targets, mask)
loss = my_criterion(masked_outputs, masked_targets)