Perform an evaluation step on `model` using `inputs`. Subclass and override to inject custom behavior. Args: model (`nn.Module`): The model to evaluate. inputs (`Dict[str, Union[torch.Tensor, Any]]`): The inputs and t
(
self,
model: nn.Module,
inputs: Dict[str, Union[torch.Tensor, Any]],
prediction_loss_only: bool,
ignore_keys: Optional[List[str]] = None,
)
| 44 | return (loss, outputs) if return_outputs else loss |
| 45 | |
| 46 | def prediction_step( |
| 47 | self, |
| 48 | model: nn.Module, |
| 49 | inputs: Dict[str, Union[torch.Tensor, Any]], |
| 50 | prediction_loss_only: bool, |
| 51 | ignore_keys: Optional[List[str]] = None, |
| 52 | ) -> Tuple[Optional[torch.Tensor], Optional[torch.Tensor], Optional[torch.Tensor]]: |
| 53 | """ |
| 54 | Perform an evaluation step on `model` using `inputs`. |
| 55 | |
| 56 | Subclass and override to inject custom behavior. |
| 57 | |
| 58 | Args: |
| 59 | model (`nn.Module`): |
| 60 | The model to evaluate. |
| 61 | inputs (`Dict[str, Union[torch.Tensor, Any]]`): |
| 62 | The inputs and targets of the model. |
| 63 | |
| 64 | The dictionary will be unpacked before being fed to the model. Most models expect the targets under the |
| 65 | argument `labels`. Check your model's documentation for all accepted arguments. |
| 66 | prediction_loss_only (`bool`): |
| 67 | Whether or not to return the loss only. |
| 68 | ignore_keys (`List[str]`, *optional*): |
| 69 | A list of keys in the output of your model (if it is a dictionary) that should be ignored when |
| 70 | gathering predictions. |
| 71 | |
| 72 | Return: |
| 73 | Tuple[Optional[torch.Tensor], Optional[torch.Tensor], Optional[torch.Tensor]]: A tuple with the loss, |
| 74 | logits and labels (each being optional). |
| 75 | """ |
| 76 | has_labels = ( |
| 77 | False |
| 78 | if len(self.label_names) == 0 |
| 79 | else all(inputs.get(k) is not None for k in self.label_names) |
| 80 | ) |
| 81 | # For CLIP-like models capable of returning loss values. |
| 82 | # If `return_loss` is not specified or being `None` in `inputs`, we check if the default value of `return_loss` |
| 83 | # is `True` in `model.forward`. |
| 84 | return_loss = inputs.get("return_loss", None) |
| 85 | if return_loss is None: |
| 86 | return_loss = self.can_return_loss |
| 87 | loss_without_labels = ( |
| 88 | True if len(self.label_names) == 0 and return_loss else False |
| 89 | ) |
| 90 | |
| 91 | inputs = self._prepare_inputs(inputs) |
| 92 | if ignore_keys is None: |
| 93 | if hasattr(self.model, "config"): |
| 94 | ignore_keys = getattr( |
| 95 | self.model.config, "keys_to_ignore_at_inference", [] |
| 96 | ) |
| 97 | else: |
| 98 | ignore_keys = [] |
| 99 | |
| 100 | # labels may be popped when computing the loss (label smoothing for instance) so we grab them first. |
| 101 | if has_labels or loss_without_labels: |
| 102 | labels = nested_detach(tuple(inputs.get(name) |
| 103 | for name in self.label_names)) |
nothing calls this directly
no test coverage detected