I need to compute softmax for a two dimensional matrix w, batch * seq_length. Sequences have different length, and they are denoted by a mask matrix mask_d, also of size batch * seq_length.
I have written the following code, however, it runs into all nan after a couple of iterations. Is there a better way to implement this, or is there an existing SoftMax implementation in PyTorch that can handle a batch of sequences of variable length by mask, and is numerically stable?

w_max = torch.max(w, 1)[0]
w_max = w_max.expand_as(w)
w_max.data[w_max.data < 0] = 0
w = torch.exp(w - w_max)
w_sum = torch.sum(w * mask_d, 1)
w_sum = w_sum.expand_as(w)
w = w / w_sum * mask_d