Hey

For my work I need to compute the softmax of certain masked tensors. However, due to the masking some of the normaliser elements might be 0, which will produce NaNs in the result and gradients.

I have implemented an alternative by making a small extension to autograd, but I was wondering if torch gurus out there knew of a better solution.
Also, regardless of a better solution, is there some way I could optimise my approach?

Code for my approach:

``````class MaskedSoftmaxNormalisation(torch.autograd.Function):
"""
Performs the normalisation step of the softmax, but accounting for a masked tensor, whose normaliser
might have 0 values. This way we avoid the operation yielding 'NaN' values in the forward and backward passes.
"""

@staticmethod
"""
Performs the forward pass of the masked softmax normalisation. Avoids 'NaN' values, caused by masking, in
the result of the softmax.

:param ctx              : Pytorch's autograd context variable.
:param normaliser       : The normalising factors tensor that will be used to normalise masked_exp_tensor.

:return: The 'NaN' free normalised masked_exp_tensor (the result of the entire softmax function).
"""

# Due to the masking some normaliser values might be 0. Where the normaliser is 0, we replace it by some other
# value so that there are no 'NaN' values after the normalisation.
normaliser[normaliser <= 0.] = 1

# Saves in context the tensors required for the backward pass.

# Compute the masked softmax tensor. All entries where the corresponding normaliser values have been changed
# to 1 will themselves be 0, so the result will be correctly masked.

return result

@staticmethod
"""
Performs the backward pass of the masked softmax normalisation. Avoids 'NaN' values, caused by masking, in
the gradients of the softmax's input tensors.

:param ctx        : Pytorch's autograd context variable.
:param grad_result: The computed gradient of the result of the forward pass.

:return: The 'NaN' free (when caused by masking) gradients of the inputs to the softmax normalisation step.
"""

# Retrieve the necessary elements from 'context'.

"""
Computes the softmax of a tensor, along a given dimension, 'dim', and taking into account elements that are meant
to be masked, according to a specific mask. This avoids mask caused 'NaN' values in both the result of the softmax

:param tensor: The tensor on which the softmax function is to be applied.
:param mask  : The mask that is associated with the input tensor.
:param dim   : the dimension over which the softmax will be computed.

:return: The correctly softmaxed tensor, accounting for masked elements.
"""

# Compute the exponential of the tensor.
exp_tensor = torch.exp(tensor)

# Mask the exponentiated tensor, so that entries that are meant to be masked do not contribute to the normaliser.