| 1390 | LLAMA_START_DOCSTRING, |
| 1391 | ) |
| 1392 | class LlamaForSequenceClassification(LlamaPreTrainedModel): |
| 1393 | def __init__(self, config): |
| 1394 | super().__init__(config) |
| 1395 | self.num_labels = config.num_labels |
| 1396 | self.model = LlamaModel(config) |
| 1397 | self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False) |
| 1398 | |
| 1399 | # Initialize weights and apply final processing |
| 1400 | self.post_init() |
| 1401 | |
| 1402 | def get_input_embeddings(self): |
| 1403 | return self.model.embed_tokens |
| 1404 | |
| 1405 | def set_input_embeddings(self, value): |
| 1406 | self.model.embed_tokens = value |
| 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: |
nothing calls this directly
no outgoing calls
no test coverage detected