def calc_mean_std(feat, eps=1e-5):
    """
    Calculate channel-wise mean and standard deviation for the input features and preserve the dimensions
    Args:
        feat: the latent feature of shape [B, C, H, W]
        eps: a small value to prevent calculation error of variance

    Returns:
    Channel-wise mean and standard deviation of the input features
    """
    size = feat.size()
    assert (len(size) == 4)
    N, C = size[:2]
    feat_var = feat.view(N, C, -1).var(dim=2) + eps
    feat_std = feat_var.sqrt().view(N, C, 1, 1)
    feat_mean = feat.view(N, C, -1).mean(dim=2).view(N, C, 1, 1)
    return feat_mean, feat_std