code: def _inf_max_helper(
      max_fun: Callable[[Tensor], Tensor],
      x: Tensor,
      dim: Union[int, Tuple[int, ...]],
      keepdim: bool,
  ) -> Tensor:
      """Helper function that generalizes the treatment of infinities for approximations
      to the maximum operator, i.e., `max(X, dim, keepdim)`. At the point of writing of
      this function, it is used to define `logsumexp` and `fatmax`.
  
      Args:
          max_fun: The function that is used to smoothly penalize the difference of an
              element to the true maximum.
          x: The Tensor on which to compute the smooth approximation to the maximum.
          dim: The dimension(s) to reduce over.
          keepdim: Whether to keep the reduced dimension. Defaults to False.
  
      Returns:
          The Tensor representing the smooth approximation to the maximum over the
          specified dimensions.
      """
      M = x.amax(dim=dim, keepdim=True)
      is_inf_max = torch.logical_and(
          *torch.broadcast_tensors(M.isinf(), x == M)
      )
      has_inf_max = _any(is_inf_max, dim=dim, keepdim=True)
  
      y_inf = x.masked_fill(~is_inf_max, 0.0)
      M_no_inf = M.masked_fill(M.isinf(), 0.0)
      y_no_inf = x.masked_fill(has_inf_max, 0.0) - M_no_inf
  
      res = torch.where(
          has_inf_max,
          y_inf.sum(dim=dim, keepdim=True),
          M_no_inf + max_fun(y_no_inf, dim=dim, keepdim=True),
      )
      # NOTE: Using `sum` instead of `squeeze` because PyTorch < 2.0 does not support
      # tuple `dim` arguments. `sum` and `squeeze` are equivalent here because the
      # `dim` dimensions have length one after the reductions in the previous lines.
      # TODO: Replace `sum` with `squeeze` once PyTorch >= 2.0 is required.
      return res if keepdim else res.sum(dim=dim)
  
imports: from __future__ import annotations
  import math
  from typing import Callable, Tuple, Union
  import torch
  from botorch.exceptions import UnsupportedError
  from botorch.utils.constants import get_constants_like
  from torch import finfo, Tensor
  from torch.nn.functional import softplus
label_desc: Use image node_id 13 for calling a global function with example usage: _inf_max_helper(max_fun, x, dim, keepdim) and returns: unknown