def kld_approx(q_word_log_probs, p_word_log_probs, mask):
    """
    Computes an approximate KLD over summaries, where history is sampled
    but the actual support is summed over. Distributions are assumed to be
    categorical.

    :param q_word_log_probs: [summs_nr, seq_len, vocab_size]
                             log probabilities over all words (except first <S>)
                             assigned by the summarizer (q) distribution.
    :param p_word_log_probs [summs_nr, seq_len, vocab_size]
                            log probabilities assigned by the prior
    :param mask: [summs_nr, seq_len]
    """
    q_word_probs = q_word_log_probs.exp()

    kld = q_word_probs * (q_word_log_probs - p_word_log_probs)

    kld = kld.sum(-1)  # summing over vocabulary
    kld = (kld * mask).sum(-1)  # summing over sequences

    return kld