Upload folder using huggingface_hub
Browse files- model.safetensors +2 -2
- modeling_gpt2workshop.py +10 -18
model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7349b1235d0d8a942d19d02fda96277b26a06653742cbc55284be790322e79fc
|
| 3 |
+
size 494622496
|
modeling_gpt2workshop.py
CHANGED
|
@@ -83,6 +83,7 @@ class TransformerBlock(nn.Module):
|
|
| 83 |
|
| 84 |
class GPT2WorkshopForCausalLM(PreTrainedModel, GenerationMixin):
|
| 85 |
config_class = GPT2WorkshopConfig
|
|
|
|
| 86 |
|
| 87 |
def __init__(self, config):
|
| 88 |
super().__init__(config)
|
|
@@ -91,33 +92,24 @@ class GPT2WorkshopForCausalLM(PreTrainedModel, GenerationMixin):
|
|
| 91 |
self.embedding_dropout = nn.Dropout(config.dropout)
|
| 92 |
self.layers = nn.ModuleList([TransformerBlock(config) for _ in range(config.num_layers)])
|
| 93 |
self.final_norm = nn.RMSNorm(config.hidden_dim, eps=1e-6)
|
| 94 |
-
self.lm_head = nn.Linear(config.hidden_dim, config.vocab_size, bias=False)
|
| 95 |
-
self.lm_head.weight = self.token_embedding.weight
|
| 96 |
rope_cos, rope_sin = build_rope_cache(config.context_length, config.head_dim, device="cpu", theta=config.rope_theta)
|
| 97 |
self.register_buffer("rope_cos", rope_cos)
|
| 98 |
self.register_buffer("rope_sin", rope_sin)
|
| 99 |
-
self.post_init()
|
| 100 |
-
|
| 101 |
-
def get_output_embeddings(self):
|
| 102 |
-
return self.lm_head
|
| 103 |
-
|
| 104 |
-
def set_output_embeddings(self, new_embeddings):
|
| 105 |
-
self.lm_head = new_embeddings
|
| 106 |
-
|
| 107 |
-
def get_input_embeddings(self):
|
| 108 |
-
return self.token_embedding
|
| 109 |
-
|
| 110 |
-
def set_input_embeddings(self, new_embeddings):
|
| 111 |
-
self.token_embedding = new_embeddings
|
| 112 |
|
| 113 |
def forward(self, input_ids=None, attention_mask=None, labels=None, **kwargs):
|
| 114 |
x = self.embedding_dropout(self.token_embedding(input_ids))
|
| 115 |
for layer in self.layers:
|
| 116 |
x = layer(x, self.rope_cos, self.rope_sin)
|
| 117 |
x = self.final_norm(x)
|
| 118 |
-
logits =
|
| 119 |
-
logits = soft_cap_logits(logits, self.config.logit_soft_cap)
|
| 120 |
loss = None
|
| 121 |
if labels is not None:
|
| 122 |
loss = F.cross_entropy(logits.reshape(-1, logits.size(-1)), labels.reshape(-1), ignore_index=-100)
|
| 123 |
-
return CausalLMOutput(loss=loss, logits=logits)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
class GPT2WorkshopForCausalLM(PreTrainedModel, GenerationMixin):
|
| 85 |
config_class = GPT2WorkshopConfig
|
| 86 |
+
_tied_weights_keys = {}
|
| 87 |
|
| 88 |
def __init__(self, config):
|
| 89 |
super().__init__(config)
|
|
|
|
| 92 |
self.embedding_dropout = nn.Dropout(config.dropout)
|
| 93 |
self.layers = nn.ModuleList([TransformerBlock(config) for _ in range(config.num_layers)])
|
| 94 |
self.final_norm = nn.RMSNorm(config.hidden_dim, eps=1e-6)
|
|
|
|
|
|
|
| 95 |
rope_cos, rope_sin = build_rope_cache(config.context_length, config.head_dim, device="cpu", theta=config.rope_theta)
|
| 96 |
self.register_buffer("rope_cos", rope_cos)
|
| 97 |
self.register_buffer("rope_sin", rope_sin)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
def forward(self, input_ids=None, attention_mask=None, labels=None, **kwargs):
|
| 100 |
x = self.embedding_dropout(self.token_embedding(input_ids))
|
| 101 |
for layer in self.layers:
|
| 102 |
x = layer(x, self.rope_cos, self.rope_sin)
|
| 103 |
x = self.final_norm(x)
|
| 104 |
+
logits = soft_cap_logits(F.linear(x, self.token_embedding.weight), self.config.logit_soft_cap)
|
|
|
|
| 105 |
loss = None
|
| 106 |
if labels is not None:
|
| 107 |
loss = F.cross_entropy(logits.reshape(-1, logits.size(-1)), labels.reshape(-1), ignore_index=-100)
|
| 108 |
+
return CausalLMOutput(loss=loss, logits=logits)
|
| 109 |
+
|
| 110 |
+
def prepare_inputs_for_generation(self, input_ids, **kwargs):
|
| 111 |
+
return {"input_ids": input_ids}
|
| 112 |
+
|
| 113 |
+
@property
|
| 114 |
+
def all_tied_weights_keys(self):
|
| 115 |
+
return {}
|