code: def reduce(
      tensor: Tensor,
      pattern: str,
      reduction: Reduction,
      **axes_lengths: int,
  ) -> Tensor:
      if isinstance(tensor, list):
          if len(tensor) == 0:
              raise TypeError(
                  "Einops can't be applied to an empty list"
              )
          xp = tensor[0].__array_namespace__()
          tensor = xp.stack(tensor)
      else:
          xp = tensor.__array_namespace__()
      try:
          hashable_axes_lengths = tuple(axes_lengths.items())
          recipe = _prepare_transformation_recipe(
              pattern,
              reduction,
              axes_names=tuple(axes_lengths),
              ndim=tensor.ndim,
          )
          return _apply_recipe_array_api(
              xp,
              recipe=recipe,
              tensor=tensor,
              reduction_type=reduction,
              axes_lengths=hashable_axes_lengths,
          )
      except EinopsError as e:
          message = ' Error while processing {}-reduction pattern "{}".'.format(
              reduction, pattern
          )
          if not isinstance(tensor, list):
              message += "\n Input tensor shape: {}. ".format(
                  tensor.shape
              )
          else:
              message += "\n Input is list. "
          message += "Additional info: {}.".format(axes_lengths)
          raise EinopsError(message + "\n {}".format(e))
  
file_path: einops/einops/array_api.py
imports: from typing import List, Tuple, Sequence
  from .einops import Tensor, Reduction, EinopsError, _prepare_transformation_recipe, _apply_recipe_array_api
  from .packing import analyze_pattern, prod
label_desc: Use image node_id 1 for calling a global function with example usage: reduce(tensor, pattern, reduction) and returns: _apply_recipe_array_api