I think you can just add a batch dimension of 1 into your mask and let broadcasting do the magic. 
If you don’t want the gradient to be backpropagated to the input, you might need to detach it with mask * x.detach().