r""" labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss
(
self,
input_ids: Optional[torch.LongTensor] = None,
attention_mask: Optional[torch.Tensor] = None,
position_ids: Optional[torch.LongTensor] = None,
past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None,
inputs_embeds: Optional[torch.FloatTensor] = None,
labels: Optional[torch.LongTensor] = None,
use_cache: Optional[bool] = None,
output_attentions: Optional[bool] = None,
output_hidden_states: Optional[bool] = None,
return_dict: Optional[bool] = None,
)
| 1407 | |
| 1408 | @add_start_docstrings_to_model_forward(LLAMA_INPUTS_DOCSTRING) |
| 1409 | def forward( |
| 1410 | self, |
| 1411 | input_ids: Optional[torch.LongTensor] = None, |
| 1412 | attention_mask: Optional[torch.Tensor] = None, |
| 1413 | position_ids: Optional[torch.LongTensor] = None, |
| 1414 | past_key_values: Optional[Union[Cache, List[torch.FloatTensor]]] = None, |
| 1415 | inputs_embeds: Optional[torch.FloatTensor] = None, |
| 1416 | labels: Optional[torch.LongTensor] = None, |
| 1417 | use_cache: Optional[bool] = None, |
| 1418 | output_attentions: Optional[bool] = None, |
| 1419 | output_hidden_states: Optional[bool] = None, |
| 1420 | return_dict: Optional[bool] = None, |
| 1421 | ) -> Union[Tuple, SequenceClassifierOutputWithPast]: |
| 1422 | r""" |
| 1423 | labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): |
| 1424 | Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., |
| 1425 | config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If |
| 1426 | `config.num_labels > 1` a classification loss is computed (Cross-Entropy). |
| 1427 | """ |
| 1428 | return_dict = return_dict if return_dict is not None else self.config.use_return_dict |
| 1429 | |
| 1430 | transformer_outputs = self.model( |
| 1431 | input_ids, |
| 1432 | attention_mask=attention_mask, |
| 1433 | position_ids=position_ids, |
| 1434 | past_key_values=past_key_values, |
| 1435 | inputs_embeds=inputs_embeds, |
| 1436 | use_cache=use_cache, |
| 1437 | output_attentions=output_attentions, |
| 1438 | output_hidden_states=output_hidden_states, |
| 1439 | return_dict=return_dict, |
| 1440 | ) |
| 1441 | hidden_states = transformer_outputs[0] |
| 1442 | logits = self.score(hidden_states) |
| 1443 | |
| 1444 | if input_ids is not None: |
| 1445 | batch_size = input_ids.shape[0] |
| 1446 | else: |
| 1447 | batch_size = inputs_embeds.shape[0] |
| 1448 | |
| 1449 | if self.config.pad_token_id is None and batch_size != 1: |
| 1450 | raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.") |
| 1451 | if self.config.pad_token_id is None: |
| 1452 | sequence_lengths = -1 |
| 1453 | else: |
| 1454 | if input_ids is not None: |
| 1455 | # if no pad token found, use modulo instead of reverse indexing for ONNX compatibility |
| 1456 | sequence_lengths = torch.eq(input_ids, self.config.pad_token_id).int().argmax(-1) - 1 |
| 1457 | sequence_lengths = sequence_lengths % input_ids.shape[-1] |
| 1458 | sequence_lengths = sequence_lengths.to(logits.device) |
| 1459 | else: |
| 1460 | sequence_lengths = -1 |
| 1461 | |
| 1462 | pooled_logits = logits[torch.arange(batch_size, device=logits.device), sequence_lengths] |
| 1463 | |
| 1464 | loss = None |
| 1465 | if labels is not None: |
| 1466 | labels = labels.to(logits.device) |
nothing calls this directly
no outgoing calls
no test coverage detected