Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -1,10 +1,26 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
0-layer transformer described in [A Mathematical Framework for Transformer Circuits](https://transformer-circuits.pub/2021/framework/index.html).Load with ```python
|
| 2 |
+
class ZeroLayerTransformer(nn.Module, PyTorchModelHubMixin):
|
| 3 |
+
def __init__(self, config):
|
| 4 |
+
super().__init__()
|
| 5 |
+
self.config = config
|
| 6 |
+
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size)
|
| 7 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
| 8 |
+
|
| 9 |
+
def forward(self, input_ids=None, attention_mask=None, labels=None, **kwargs):
|
| 10 |
+
hidden_states = self.embed_tokens(input_ids)
|
| 11 |
+
logits = self.lm_head(hidden_states)
|
| 12 |
+
|
| 13 |
+
loss = None
|
| 14 |
+
if labels is not None:
|
| 15 |
+
shift_logits = logits[..., :-1, :].contiguous()
|
| 16 |
+
shift_labels = labels[..., 1:].contiguous()
|
| 17 |
+
loss_fct = nn.CrossEntropyLoss()
|
| 18 |
+
loss = loss_fct(
|
| 19 |
+
shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1)
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
return {"loss": loss, "logits": logits}
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
```
|
| 26 |
+
model = ZeroLayerTransformer.from_pretrained('Butanium/simple-stories-zero-layer-simple-transformer')
|