Download modeling_my_gpt.py from dev-das/sarcastic-model: direct link, hf CLI and curl.
- Browser
- Download file 1.47 kB
-
https://huggingface.co/dev-das/sarcastic-model/resolve/main/modeling_my_gpt.py
- Command line
-
hf download hf://dev-das/sarcastic-model/modeling_my_gpt.py
-
curl -L -o modeling_my_gpt.py https://huggingface.co/dev-das/sarcastic-model/resolve/main/modeling_my_gpt.py
1.47 kB
| import torch | |
| import torch.nn as nn | |
| from transformers import PreTrainedModel | |
| from transformers.modeling_outputs import CausalLMOutput | |
| from transformers.generation import GenerationMixin | |
| from .configuration_my_gpt import MyGPTConfig | |
| from .untrained_model import GPTModel | |
| import os | |
| class MyGPTForCausalLM(PreTrainedModel, GenerationMixin): | |
| config_class = MyGPTConfig | |
| main_input_name = "input_ids" | |
| def __init__(self, config): | |
| super().__init__(config) | |
| # Import your original GPTModel | |
| self.model = GPTModel({ | |
| "vocab_size": config.vocab_size, | |
| "context_length": config.max_position_embeddings, | |
| "emb_dim": config.hidden_size, | |
| "n_heads": config.num_attention_heads, | |
| "n_layers": config.num_hidden_layers, | |
| "drop_rate": config.drop_rate, | |
| "qkv_bias": config.qkv_bias | |
| }) | |
| self.post_init() | |
| def forward(self, input_ids, labels=None, **kwargs): | |
| logits = self.model(input_ids) | |
| loss = None | |
| if labels is not None: | |
| 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 CausalLMOutput( | |
| loss=loss, | |
| logits=logits, | |
| ) | |