I usually just register a hook to the variable that needs masking:
        def hook(grad):
            mask =  Variable(matrix_of_ones_and_zeros)
            return grad * mask

        Var.register_hook(hook)

This way, you can also ensure that unwanted gradients are not propogated during backprop.