What I want is fairly simple: a MSE loss function, but able to mask some items:
def masked_mse_loss(a, b, mask):
    sum2 = 0.0
    num = 0
    for i in len(range(a)):
        if mask[i] == 1:
            sum2 += (a[i] - b[i]) ** 2.0
            num += 1
    return sum2 / num

Due to backward issue, I believe such a straightforward implementation would not make pytorch happy, but I have no idea on how to make it in correct way.