if it may happen, that you mask out everything (e.g. if you have multiple batches and some batchentries are completly masked out), you might want to do it like this:
batchsize = 32
datasize = 2
a = torch.randn(batchsize, datasize)
mask = (a > 0).float()
mask_sum = torch.sum(mask, dim=1)
mask_sum_modified = torch.clamp(mask_sum, min=1.0)
torch.sum(a * mask, dim=1) / mask_sum_modified