| 1203 | GEMMA2_START_DOCSTRING, |
| 1204 | ) |
| 1205 | class Gemma2ForTokenClassification(Gemma2PreTrainedModel): |
| 1206 | def __init__(self, config): |
| 1207 | super().__init__(config) |
| 1208 | self.num_labels = config.num_labels |
| 1209 | self.model = Gemma2Model(config) |
| 1210 | if getattr(config, "classifier_dropout", None) is not None: |
| 1211 | classifier_dropout = config.classifier_dropout |
| 1212 | elif getattr(config, "hidden_dropout", None) is not None: |
| 1213 | classifier_dropout = config.hidden_dropout |
| 1214 | else: |
| 1215 | classifier_dropout = 0.1 |
| 1216 | self.dropout = nn.Dropout(classifier_dropout) |
| 1217 | self.score = nn.Linear(config.hidden_size, config.num_labels) |
| 1218 | |
| 1219 | # Initialize weights and apply final processing |
| 1220 | self.post_init() |
| 1221 | |
| 1222 | def get_input_embeddings(self): |
| 1223 | return self.model.embed_tokens |
| 1224 | |
| 1225 | def set_input_embeddings(self, value): |
| 1226 | self.model.embed_tokens = value |
| 1227 | |
| 1228 | @add_start_docstrings_to_model_forward(GEMMA2_INPUTS_DOCSTRING) |
| 1229 | @add_code_sample_docstrings( |
| 1230 | checkpoint=_CHECKPOINT_FOR_DOC, |
| 1231 | output_type=TokenClassifierOutput, |
| 1232 | config_class=_CONFIG_FOR_DOC, |
| 1233 | ) |
| 1234 | def forward( |
| 1235 | self, |
| 1236 | input_ids: Optional[torch.LongTensor] = None, |
| 1237 | attention_mask: Optional[torch.Tensor] = None, |
| 1238 | position_ids: Optional[torch.LongTensor] = None, |
| 1239 | past_key_values: Optional[List[torch.FloatTensor]] = None, |
| 1240 | inputs_embeds: Optional[torch.FloatTensor] = None, |
| 1241 | labels: Optional[torch.LongTensor] = None, |
| 1242 | use_cache: Optional[bool] = None, |
| 1243 | output_attentions: Optional[bool] = None, |
| 1244 | output_hidden_states: Optional[bool] = None, |
| 1245 | return_dict: Optional[bool] = None, |
| 1246 | ) -> Union[Tuple, TokenClassifierOutput]: |
| 1247 | r""" |
| 1248 | labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*): |
| 1249 | Labels for computing the sequence classification/regression loss. Indices should be in `[0, ..., |
| 1250 | config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If |
| 1251 | `config.num_labels > 1` a classification loss is computed (Cross-Entropy). |
| 1252 | """ |
| 1253 | return_dict = return_dict if return_dict is not None else self.config.use_return_dict |
| 1254 | |
| 1255 | outputs = self.model( |
| 1256 | input_ids, |
| 1257 | attention_mask=attention_mask, |
| 1258 | position_ids=position_ids, |
| 1259 | past_key_values=past_key_values, |
| 1260 | inputs_embeds=inputs_embeds, |
| 1261 | use_cache=use_cache, |
| 1262 | output_attentions=output_attentions, |
nothing calls this directly
no outgoing calls
no test coverage detected