code: def kl_div(
      input: Union[ivy.Array, ivy.NativeArray],
      target: Union[ivy.Array, ivy.NativeArray],
      /,
      *,
      reduction: Optional[str] = "mean",
      log_target=False,
      out: Optional[ivy.Array] = None,
  ) -> ivy.Array:
      """Compute the Kullback-Leibler divergence loss between two input tensors
      (conventionally, probability distributions).
  
      Parameters
      ----------
      input : array_like
          Tensor of arbitrary shape in log-probabilities
      target : array_like
          Tensor of the same shape as input. See log_target for
          the target’s interpretation
      reduction : {'mean', 'sum', 'batchmean', 'none'}, optional
          Type of reduction to apply to the output. Default is 'mean'.
      log_target : bool
          A flag indicating whether target is passed in the log space.
          It is recommended to pass certain distributions (like softmax)
          in the log space to avoid numerical issues caused by explicit log.
          Default: False
  
      Returns
      -------
      ret : array
          The Kullback-Leibler divergence loss between the two input tensors.
  
      Examples
      --------
      >>> input = ivy.array([[0.2, 0.8], [0.5, 0.5]])
      >>> target = ivy.array([[0.6, 0.4], [0.3, 0.7]])
      >>> ivy.kl_div(input, target)
      ivy.array(-0.555969)
  
      >>> input = ivy.array([[0.2, 0.8], [0.5, 0.5]])
      >>> target = ivy.array([[0.6, 0.4], [0.3, 0.7]])
      >>> ivy.kl_div(input, target, reduction='sum')
      ivy.array(-2.223876)
  
      >>> input = ivy.array([[0.2, 0.8], [0.5, 0.5]])
      >>> target = ivy.array([[0.6, 0.4], [0.3, 0.7]])
      >>> ivy.kl_div(input, target, reduction='batchmean')
      ivy.array(-1.111938)
  
      >>> input = ivy.array([0.2, 0.8], [0.5, 0.5])
      >>> target = ivy.array([0.6, 0.4], [0.3, 0.7])
      >>> ivy.kl_div(input, target, reduction='none')
      ivy.array([[-0.42649534, -0.68651628],
                  [-0.51119184, -0.59967244]])
      """
      if not log_target:  # default
          loss_pointwise = target * (ivy.log(target) - input)
      else:
          loss_pointwise = ivy.exp(target) * (target - input)
  
      if reduction == "mean":  # default
          loss = ivy.mean(loss_pointwise)
      elif reduction == "batchmean":  # mathematically correct
          loss = ivy.sum(loss_pointwise) / input.shape[0]
      elif reduction == "sum":
          loss = ivy.sum(loss_pointwise)
      else:  # reduction == "none"
          loss = loss_pointwise
      return ivy.inplace_update(out, loss) if out is not None else loss
  
file_path: ivy/ivy/functional/ivy/experimental/losses.py
imports: from typing import Union, Optional
  import ivy
  from ivy.func_wrapper import handle_nestable, inputs_to_ivy_arrays, handle_array_like_without_promotion, handle_array_function, to_native_arrays_and_back
  from ivy.utils.exceptions import handle_exceptions
label_desc: Use image node_id 6 for calling a global function with example usage: kl_div() and returns: unknown