Usually you would create the target mask before the training so that you wouldn’t need gradients for this operation.
Could you explain your use case a bit and how you would like to use these gradients for the mask?