code: def _select_targets(output: Tensor, target: TargetType) -> Tensor:
      """
      IMPORTANT:
      please avoid patching this function. The existing behavior is very
      unpredictable. We should be more opinionated about the type and format of
      the target so that we can stop supporting some unpredictable cases.
      Or better, we should encourage users to wrapping their forward function to
      return the attr targets themselves, instead of passing target.
  
      This legacy function behaves based on
      - the type of target
      - if the target has the length of the output
  
      If the target is int or scalar tensor, the target is seen as the
      index of the last dimensions of every example in the output. E.g., if the
      output is of shape (Batch, ..., X, Y), the selected output will be (Batch, ..., X)
  
      If the target is tuple[int], the target is seens as the last indices of every
      example in the output. E.g., if the
      output is of shape (Batch, ..., X, Y, Z) and the target is tuple(y, z),
      the selected output will be (Batch, ..., X)
  
      If the target is a non-scalar tensor, it must be a 1D tensor of the output length
      and the output must be a 2D tensor. The target is then seen as the indices of the
      2nd dim of the output. E.g., if the output is of shape (Batch, X) and the target is
      in shape (X,), the selected output will be (Batch,)
  
      If the target is a list[int], it must has the same length as the output. The output
      must be a 2D tensor and each int element of the target is seen as the 2nd dim of it.
      E.g., if the output is of shape (Batch, X) and the target is [x1, x2, ...],
      the selected output will be (Batch,)
  
      If the target is a list[tuple], it must has the same length as the output. Each
      tuple element of the target is seen as the leading dim behind the batch dim
      of the output. E.g., if the output is of shape (Batch, X, Y, Z, ...) and
      the target is [(x1, y1), (x2, y2), ...], the selected output
      will be in shape (Batch, Z, ...)
      """
      if target is None:
          return output
  
      num_examples = output.shape[0]
      dims = len(output.shape)
      device = output.device
      if isinstance(target, (int, tuple)):
          return _verify_select_column(output, target)
      elif isinstance(target, torch.Tensor):
          if torch.numel(target) == 1 and isinstance(
              target.item(), int
          ):
              return _verify_select_column(
                  output, cast(int, target.item())
              )
          elif (
              len(target.shape) == 1
              and torch.numel(target) == num_examples
          ):
              assert (
                  dims == 2
              ), "Output must be 2D to select tensor of targets."
              return torch.gather(
                  output, 1, target.reshape(len(output), 1)
              ).squeeze(-1)
          else:
              raise AssertionError(
                  "Tensor target dimension %r is not valid. %r"
                  % (target.shape, output.shape)
              )
      elif isinstance(target, list):
          assert (
              len(target) == num_examples
          ), "Target list length does not match output!"
          if isinstance(target[0], int):
              assert (
                  dims == 2
              ), "Output must be 2D to select tensor of targets."
              return torch.gather(
                  output,
                  1,
                  torch.tensor(target, device=device).reshape(
                      len(output), 1
                  ),
              ).squeeze(-1)
          elif isinstance(target[0], tuple):
              return torch.stack(
                  [
                      output[(i,) + cast(Tuple, targ_elem)]
                      for i, targ_elem in enumerate(target)
                  ]
              )
          else:
              raise AssertionError(
                  f"Target element type {type(target[0])} in list is not valid."
              )
      else:
          raise AssertionError(
              f"Target type {type(target)} is not valid."
          )
  
imports: import typing
  from enum import Enum
  from functools import reduce
  from inspect import signature
  from typing import Any, Callable, cast, Dict, List, overload, Tuple, Union
  import numpy
  import torch
  from captum._utils.typing import BaselineType, Literal, TargetType, TensorOrTupleOfTensorsGeneric, TupleOrTensorOrBoolGeneric
  from torch import device, Tensor
  from torch.nn import Module
label_desc: Use image node_id 37 for calling a global function with example usage: _select_targets(output, target) and returns: output, _verify_select_column, _verify_select_column, torch, torch, torch