hf importable
Browse files- __pycache__/dataset.cpython-312.pyc +0 -0
- __pycache__/model.cpython-312.pyc +0 -0
- __pycache__/modeling_perklm.cpython-312.pyc +0 -0
- hf.py +21 -0
- modeling_perklm.py +56 -0
- perklm_hf/config.json +15 -0
- perklm_hf/model.safetensors +3 -0
__pycache__/dataset.cpython-312.pyc
ADDED
|
Binary file (4.18 kB). View file
|
|
|
__pycache__/model.cpython-312.pyc
ADDED
|
Binary file (21.1 kB). View file
|
|
|
__pycache__/modeling_perklm.cpython-312.pyc
ADDED
|
Binary file (3.48 kB). View file
|
|
|
hf.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import GPT2TokenizerFast
|
| 3 |
+
from modeling_perklm import PerkLMConfig, PerkLM
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
checkpoint = torch.load('perklm.pt', map_location = 'cpu', weights_only = True)
|
| 7 |
+
old_config = checkpoint['config']
|
| 8 |
+
|
| 9 |
+
hf_config = PerkLMConfig(d_model = old_config['model']['d_model'],
|
| 10 |
+
n_heads = old_config['model']['n_heads'],
|
| 11 |
+
n_layers = old_config['model']['n_layers'],
|
| 12 |
+
d_ff = old_config['model']['d_ff'],
|
| 13 |
+
maxt = old_config['model']['maxt'],
|
| 14 |
+
dropout = old_config['model']['dropout'],
|
| 15 |
+
tokenizer_path = 'tokenizer')
|
| 16 |
+
|
| 17 |
+
model = PerkLM(hf_config)
|
| 18 |
+
model.transformer.load_state_dict(checkpoint['model_state_dict'])
|
| 19 |
+
|
| 20 |
+
model.save_pretrained('perklm_hf')
|
| 21 |
+
hf_config.save_pretrained('perklm_hf')
|
modeling_perklm.py
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import PreTrainedModel, PretrainedConfig, GPT2TokenizerFast, \
|
| 3 |
+
AutoConfig, AutoModelForCausalLM
|
| 4 |
+
from transformers.modeling_outputs import CausalLMOutput
|
| 5 |
+
from model import FriendsTransformer
|
| 6 |
+
|
| 7 |
+
class PerkLMConfig(PretrainedConfig):
|
| 8 |
+
model_type = "perklm"
|
| 9 |
+
|
| 10 |
+
def __init__(self, d_model = 512, n_heads = 8, n_layers = 6,
|
| 11 |
+
d_ff = 2048, maxt = 512, dropout = 0.1, tokenizer_path = None, **kwargs):
|
| 12 |
+
super().__init__(**kwargs)
|
| 13 |
+
self.d_model = d_model
|
| 14 |
+
self.n_heads = n_heads
|
| 15 |
+
self.n_layers = n_layers
|
| 16 |
+
self.d_ff = d_ff
|
| 17 |
+
self.maxt = maxt
|
| 18 |
+
self.dropout = dropout
|
| 19 |
+
self.tokenizer_path = tokenizer_path
|
| 20 |
+
|
| 21 |
+
class PerkLM(PreTrainedModel):
|
| 22 |
+
config_class = PerkLMConfig
|
| 23 |
+
_tied_weights_keys = ["transformer.lm_head.weight"]
|
| 24 |
+
|
| 25 |
+
def __init__(self, config):
|
| 26 |
+
super().__init__(config)
|
| 27 |
+
tokenizer = GPT2TokenizerFast.from_pretrained(config.tokenizer_path)
|
| 28 |
+
self.transformer = FriendsTransformer(d_model = config.d_model,
|
| 29 |
+
n_heads = config.n_heads,
|
| 30 |
+
n_layers = config.n_layers,
|
| 31 |
+
d_ff = config.d_ff,
|
| 32 |
+
dropout = config.dropout,
|
| 33 |
+
maxt = config.maxt,
|
| 34 |
+
tokenizer = tokenizer)
|
| 35 |
+
self.post_init()
|
| 36 |
+
|
| 37 |
+
def forward(self, input_ids, attention_mask = None,
|
| 38 |
+
responder = None, labels = None, **kwargs):
|
| 39 |
+
batch = {'input_ids': input_ids,
|
| 40 |
+
'attention_mask': attention_mask if attention_mask is not None \
|
| 41 |
+
else torch.ones_like(input_ids),
|
| 42 |
+
'responder': responder}
|
| 43 |
+
logits = self.transformer(batch)
|
| 44 |
+
|
| 45 |
+
loss = None
|
| 46 |
+
if labels is not None:
|
| 47 |
+
loss = torch.nn.functional.cross_entropy(logits[:, :-1].reshape(-1, logits.size(-1)),
|
| 48 |
+
labels[:, 1:].reshape(-1), ignore_index = -100)
|
| 49 |
+
|
| 50 |
+
return CausalLMOutput(loss = loss, logits = logits)
|
| 51 |
+
|
| 52 |
+
def tie_weights(self):
|
| 53 |
+
self.transformer.lm_head.weight = self.transformer.embedder.embedding.weight
|
| 54 |
+
|
| 55 |
+
AutoConfig.register("perklm", PerkLMConfig)
|
| 56 |
+
AutoModelForCausalLM.register(PerkLMConfig, PerkLM)
|
perklm_hf/config.json
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"PerkLM"
|
| 4 |
+
],
|
| 5 |
+
"d_ff": 2048,
|
| 6 |
+
"d_model": 512,
|
| 7 |
+
"dropout": 0.1,
|
| 8 |
+
"dtype": "float32",
|
| 9 |
+
"maxt": 512,
|
| 10 |
+
"model_type": "perklm",
|
| 11 |
+
"n_heads": 8,
|
| 12 |
+
"n_layers": 6,
|
| 13 |
+
"tokenizer_path": "tokenizer",
|
| 14 |
+
"transformers_version": "4.57.3"
|
| 15 |
+
}
|
perklm_hf/model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7ae6b37968cc9a69d3231e709f51ea32d5d9ad4d917ff3685126e7ca93c18413
|
| 3 |
+
size 179394920
|