de</span>
</summary>
<pre><code class="python">&#34;&#34;&#34;
Miscellaneous utility functions.
&#34;&#34;&#34;

from functools import wraps

import numpy as np
import torch
import torch.nn.functional as F


def log_sum_exp_trick(x, log_w=None, dim=-1, keepdim=False):
    &#34;&#34;&#34;Compute log(sum(w * exp(x), dim=dim, keepdim=keepdim)) safely.

    Uses the logsumexp trick for the computation of this quantity.
    &#34;&#34;&#34;
    if log_w is None:
        log_w = torch.zeros_like(x)

    x = x + lo