import torch
from typing import Optional


def _weighted_mean(
    tensor: torch.Tensor,
    weight: Optional[torch.Tensor],
) -> torch.Tensor:
    """
    Compute weighted mean.

    :param tensor:
        The tensor.
    :param weight:
        An optional weight.

    :return:
        The (weighted) mean. If weight is None, uniform weights are assumed.
    """
    tensor = tensor.float()
    if weight is None:
        return tensor.mean()
    return (tensor * weight).sum() / weight.sum()