Download modeling_gome.py from Prositron/gome: direct link, hf CLI and curl.
- Browser
- Download file 1.73 kB
-
https://huggingface.co/Prositron/gome/resolve/main/modeling_gome.py
- Command line
-
hf download hf://Prositron/gome/modeling_gome.py
-
curl -L -o modeling_gome.py https://huggingface.co/Prositron/gome/resolve/main/modeling_gome.py
1.73 kB
| import torch | |
| import torch.nn as nn | |
| from transformers import PreTrainedModel | |
| from transformers.modeling_outputs import CausalLMOutputWithPast | |
| from .configuration_gome import GomeConfig | |
| from .gome_model import Gome | |
| class GomeForCausalLM(PreTrainedModel): | |
| config_class = GomeConfig | |
| main_input_name = "input_ids" | |
| def __init__(self, config): | |
| super().__init__(config) | |
| self.model = Gome(config) | |
| # important for HF | |
| self.post_init() | |
| def forward( | |
| self, | |
| input_ids=None, | |
| attention_mask=None, | |
| labels=None, | |
| **kwargs | |
| ): | |
| """ | |
| HF expects: | |
| - input_ids | |
| - returns logits + optional loss | |
| """ | |
| logits, _ = self.model(input_ids) | |
| loss = None | |
| if labels is not None: | |
| # shift for causal LM | |
| shift_logits = logits[:, :-1, :].contiguous() | |
| shift_labels = labels[:, 1:].contiguous() | |
| loss_fct = nn.CrossEntropyLoss() | |
| loss = loss_fct( | |
| shift_logits.view(-1, shift_logits.size(-1)), | |
| shift_labels.view(-1) | |
| ) | |
| return CausalLMOutputWithPast( | |
| loss=loss, | |
| logits=logits, | |
| past_key_values=None, # not implemented yet | |
| ) | |
| def prepare_inputs_for_generation( | |
| self, | |
| input_ids, | |
| **kwargs | |
| ): | |
| """ | |
| This makes `.generate()` work. | |
| Minimal version (no caching yet). | |
| """ | |
| return {"input_ids": input_ids} | |
| def _reorder_cache(self, past, beam_idx): | |
| """ | |
| Required for beam search compatibility. | |
| We don’t use cache yet, so just return it. | |
| """ | |
| return past |