-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_generation.py
60 lines (43 loc) · 1.79 KB
/
test_generation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# %%
import torch
from torch.utils.data import DataLoader
from datasets import load_dataset
from transformers import AutoTokenizer
from continous_diffusion import DiffusionModel
# Load the TinyStories dataset
dataset = load_dataset("roneneldan/TinyStories")
tokenizer = AutoTokenizer.from_pretrained("google-bert/bert-base-uncased") # or any suitable tokenizer
if tokenizer.pad_token is None:
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
vocab_size=tokenizer.vocab_size+1
print(f"vocab_size: {tokenizer.vocab_size}")
# Preprocess the dataset
def tokenize_function(examples):
return tokenizer(examples["text"], padding="max_length", truncation=True, max_length=128)
tokenized_datasets = dataset.map(tokenize_function, batched=True)
tokenized_datasets.set_format("torch")
device="cuda" if torch.cuda.is_available() else "cpu"
# %%
model=DiffusionModel(embed_dim=256,
qkv_dim=4096,
num_heads=8,
cond_dim=64,
n_blocks=32,
vocab_size=vocab_size,
device=device
)
# Assuming state_dict is the loaded state dictionary and the prefix to remove is "_orig_mod."
state_dict=torch.load('checkpoints/139.96M_parameters_ep4.pt')
adjusted_state_dict = {key.replace("_orig_mod.", ""): value for key, value in state_dict.items()}
# Now try loading the adjusted state dictionary
model.load_state_dict(adjusted_state_dict)
print(f"n parameters:{model.n_parameters/1e6}M")
# %%
out_embeddings=model.generate(1,128,1000,device=device)
from torch.distributions.categorical import Categorical
logits=model.un_embedder(out_embeddings)
distrubution=Categorical(logits=logits)
sample=distrubution.sample()
# sample=logits.argmax(dim=-1)
tokenizer.batch_decode(sample)
# %%