Ah OK, then I misunderstood your question, sorry.
In that case you could create your mask vector (with zeros and ones) and multiply it with the loss.
Something like this should work:
model = nn.Conv2d(3, 1, 3, 1, 1)
x = torch.randn(1, 3, 5, 5)
mask = torch.empty(1, 1, 5, 5).random_(2)
target = torch.empty(1, 1, 5, 5).random_(2)
criterion = nn.BCELoss(reduction='none')

output = torch.sigmoid(model(x))
loss = criterion(output, target)
loss = loss * mask
loss.mean().backward()