def _prediction_loop(self, dataloader: DataLoader, description: str, task_name: str, mode: str, prediction_loss_only: Optional[bool]=None) -> PredictionOutput:
    '\n        Prediction/evaluation loop, shared by `evaluate()` and `predict()`.\n        Works both with or without labels.\n        '
    prediction_loss_only = (prediction_loss_only if (prediction_loss_only is not None) else self.prediction_loss_only)
    model = self.model
    if (self.args.n_gpu > 1):
        model = torch.nn.DataParallel(model)
    else:
        model = self.model
    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)
    eval_losses: List[float] = []
    preds: torch.Tensor = None
    label_ids: torch.Tensor = None
    model.eval()
    if is_tpu_available():
        dataloader = pl.ParallelLoader(dataloader, [self.args.device]).per_device_loader(self.args.device)
    for inputs in tqdm(dataloader, desc=description):
        has_labels = any(((inputs.get(k) is not None) for k in ['labels', 'lm_labels', 'masked_lm_labels']))
        for (k, v) in inputs.items():
            inputs[k] = v.to(self.args.device)
        with torch.no_grad():
            outputs = model(**inputs)
            if has_labels:
                (step_eval_loss, logits) = outputs[:2]
                eval_losses += [step_eval_loss.mean().item()]
            else:
                logits = outputs[0]
        if (not prediction_loss_only):
            if (preds is None):
                preds = logits.detach()
            else:
                preds = torch.cat((preds, logits.detach()), dim=0)
            if (inputs.get('labels') is not None):
                if (label_ids is None):
                    label_ids = inputs['labels'].detach()
                else:
                    label_ids = torch.cat((label_ids, inputs['labels'].detach()), dim=0)
    if (self.args.local_rank != (- 1)):
        if (preds is not None):
            preds = self.distributed_concat(preds, num_total_examples=self.num_examples(dataloader))
        if (label_ids is not None):
            label_ids = self.distributed_concat(label_ids, num_total_examples=self.num_examples(dataloader))
    elif is_tpu_available():
        if (preds is not None):
            preds = xm.mesh_reduce('eval_preds', preds, torch.cat)
        if (label_ids is not None):
            label_ids = xm.mesh_reduce('eval_label_ids', label_ids, torch.cat)
    if (preds is not None):
        preds = preds.cpu().numpy()
    if (label_ids is not None):
        label_ids = label_ids.cpu().numpy()
    if ((self.compute_metrics is not None) and (preds is not None) and (label_ids is not None)):
        metrics = self.compute_metrics(EvalPrediction(predictions=preds, label_ids=label_ids))
    else:
        metrics = {}
    if (len(eval_losses) > 0):
        metrics[f'{task_name}_{mode}_loss'] = np.mean(eval_losses)
    for key in list(metrics.keys()):
        if (not key.startswith(f'{task_name}_{mode}_')):
            metrics[f'{task_name}_{mode}_{key}'] = metrics.pop(key)
    return PredictionOutput(predictions=preds, label_ids=label_ids, metrics=metrics)
Prediction/evaluation loop, shared by `evaluate()` and `predict()`.
Works both with or without labels.
def _prediction_loop(self, dataloader: DataLoader, description: str, task_name: str, mode: str, prediction_loss_only: Optional[bool]=None) -> PredictionOutput:
    '\n        Prediction/evaluation loop, shared by `evaluate()` and `predict()`.\n        Works both with or without labels.\n        '
    prediction_loss_only = (prediction_loss_only if (prediction_loss_only is not None) else self.prediction_loss_only)
    model = self.model
    if (self.args.n_gpu > 1):
        model = torch.nn.DataParallel(model)
    else:
        model = self.model
    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)
    eval_losses: List[float] = []
    preds: torch.Tensor = None
    label_ids: torch.Tensor = None
    model.eval()
    if is_tpu_available():
        dataloader = pl.ParallelLoader(dataloader, [self.args.device]).per_device_loader(self.args.device)
    for inputs in tqdm(dataloader, desc=description):
        has_labels = any(((inputs.get(k) is not None) for k in ['labels', 'lm_labels', 'masked_lm_labels']))
        for (k, v) in inputs.items():
            inputs[k] = v.to(self.args.device)
        with torch.no_grad():
            outputs = model(**inputs)
            if has_labels:
                (step_eval_loss, logits) = outputs[:2]
                eval_losses += [step_eval_loss.mean().item()]
            else:
                logits = outputs[0]
        if (not prediction_loss_only):
            if (preds is None):
                preds = logits.detach()
            else:
                preds = torch.cat((preds, logits.detach()), dim=0)
            if (inputs.get('labels') is not None):
                if (label_ids is None):
                    label_ids = inputs['labels'].detach()
                else:
                    label_ids = torch.cat((label_ids, inputs['labels'].detach()), dim=0)
    if (self.args.local_rank != (- 1)):
        if (preds is not None):
            preds = self.distributed_concat(preds, num_total_examples=self.num_examples(dataloader))
        if (label_ids is not None):
            label_ids = self.distributed_concat(label_ids, num_total_examples=self.num_examples(dataloader))
    elif is_tpu_available():
        if (preds is not None):
            preds = xm.mesh_reduce('eval_preds', preds, torch.cat)
        if (label_ids is not None):
            label_ids = xm.mesh_reduce('eval_label_ids', label_ids, torch.cat)
    if (preds is not None):
        preds = preds.cpu().numpy()
    if (label_ids is not None):
        label_ids = label_ids.cpu().numpy()
    if ((self.compute_metrics is not None) and (preds is not None) and (label_ids is not None)):
        metrics = self.compute_metrics(EvalPrediction(predictions=preds, label_ids=label_ids))
    else:
        metrics = {}
    if (len(eval_losses) > 0):
        metrics[f'{task_name}_{mode}_loss'] = np.mean(eval_losses)
    for key in list(metrics.keys()):
        if (not key.startswith(f'{task_name}_{mode}_')):
            metrics[f'{task_name}_{mode}_{key}'] = metrics.pop(key)
    return PredictionOutput(predictions=preds, label_ids=label_ids, metrics=metrics)