docstr: Prediction/evaluation loop, shared by :obj:`Trainer.evaluate()` and :obj:`Trainer.predict()`.
  Works both with or without labels.
docstr_tok: ["Prediction\/evaluation","loop",",","shared","by",":","obj",":","`","Trainer.evaluate","(",")","`","and",":","obj",":","`","Trainer.predict","(",")","`",".","Works","both","with","or","without","labels","."]
code: def _prediction_loop(
      self,
      dataloader: DataLoader,
      description: str,
      prediction_loss_only: Optional[bool] = None,
      metric_key_prefix: str = "eval",
  ) -> PredictionOutput:
      """
      Prediction/evaluation loop, shared by :obj:`Trainer.evaluate()` and :obj:`Trainer.predict()`.
      Works both with or without labels.
      """
  
      if not isinstance(dataloader.dataset, collections.abc.Sized):
          raise ValueError("dataset must implement __len__")
      prediction_loss_only = (
          prediction_loss_only
          if prediction_loss_only is not None
          else self.args.prediction_loss_only
      )
  
      model = self.model
      # multi-gpu eval
      if self.args.n_gpu > 1:
          model = torch.nn.DataParallel(model)
      else:
          model = self.model
      # Note: in torch.distributed mode, there's no point in wrapping the model
      # inside a DistributedDataParallel as we'll be under `no_grad` anyways.
  
      batch_size = dataloader.batch_size
      logger.info("***** Running %s *****", description)
      logger.info("  Num examples = %d", self.num_examples(dataloader))
      logger.info("  Batch size = %d", batch_size)
      logger.info("  Decode mode = %s", self.args.decode_mode)
      eval_losses: List[float] = []
      model.eval()
  
      metric = ParsingMetric()
  
      for inputs in tqdm(dataloader, desc=description):
          for k, v in inputs.items():
              inputs[k] = v.to(self.args.device)
  
          with torch.no_grad():
              step_eval_loss, rel_preds, arc_preds = model(
                  **inputs, return_dict=False
              )
  
              eval_losses += [step_eval_loss.mean().item()]
  
          mask = inputs["labels_arcs"].ne(
              self.model.config.pad_token_id
          )
          predictions_arcs = torch.argmax(arc_preds, dim=-1)[mask]
  
          labels_arcs = inputs["labels_arcs"][mask]
  
          predictions_rels, labels_rels = (
              rel_preds[mask],
              inputs["labels_rels"][mask],
          )
          predictions_rels = predictions_rels[
              torch.arange(len(labels_arcs)), labels_arcs
          ]
          predictions_rels = torch.argmax(predictions_rels, dim=-1)
  
          metric.add(
              labels_arcs,
              labels_rels,
              predictions_arcs,
              predictions_rels,
          )
  
      results = metric.get_metric()
      results[f"{metric_key_prefix}_loss"] = np.mean(eval_losses)
  
      # Prefix all keys with metric_key_prefix + '_'
      for key in list(results.keys()):
          if not key.startswith(f"{metric_key_prefix}_"):
              results[f"{metric_key_prefix}_{key}"] = results.pop(key)
  
      # Add predictions_rels to output, even though we are only interested in the metrics
      return PredictionOutput(
          predictions=predictions_rels, label_ids=None, metrics=results
      )
  
imports: import collections
  import logging
  import os
  from dataclasses import dataclass, field
  from typing import Callable, Dict, List, Optional, Tuple, Union
  import numpy
  import torch
  from torch.utils.data import DataLoader
  from torch.utils.data.dataset import Dataset
  from tqdm import tqdm
  from adapters import AdapterTrainer
  from transformers import DataCollator, EvalPrediction, PreTrainedModel, PreTrainedTokenizerBase, Trainer, TrainerCallback, TrainingArguments, is_torch_tpu_available
  from transformers.trainer_utils import PredictionOutput
label_desc: Use image node_id 5 for calling the DependencyParsingTrainer obj's underlying member method code with example usage: obj._prediction_loop(dataloader, description, prediction_loss_only, metric_key_prefix) and returns: PredictionOutput