import torch


def kl_gauss(q_mu, q_logvar, mu=None, logvar=None):
    """
    KL divergence between two diagonal gaussians
    """
    if mu is None:
        mu = torch.zeros_like(q_mu)
    if logvar is None:
        logvar = torch.zeros_like(q_logvar)

    return -0.5 * (1 + q_logvar - logvar - (torch.pow(q_mu - mu, 2) + torch.exp(q_logvar)) / torch.exp(logvar))