-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate_fineweb.py
143 lines (115 loc) · 3.89 KB
/
evaluate_fineweb.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import torch
import math
import argparse
import pickle
from pathlib import Path
import re
from lit_gpt.model import GPT, Config
from lit_gpt.diffmodel import TransEncoder
from transformers import AutoTokenizer
import torch.nn.functional as F
from tqdm import tqdm
from evaluate_diff import set_seed
from safetensors.torch import load_file
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--type",
type=str,
required=True,
help="arm or mdm"
)
parser.add_argument(
"--model",
type=int,
required=True,
)
parser.add_argument(
"--ckpt-path",
type=str,
required=True
)
parser.add_argument(
"--fineweb",
type=str,
required=True
)
parser.add_argument(
"--batch-size",
type=int,
default=128
)
parser.add_argument(
"--mc-samples",
type=int,
default=1024
)
parser.add_argument(
"--seed",
type=int,
default=1234
)
args = parser.parse_args()
return args
def forward_process(batch, total_dim=32000, eps=1e-3):
b, l = batch.shape
t = torch.rand((b,), device=batch.device)
p_mask = (1 - eps) * t + eps
p_mask = p_mask[:, None].repeat(1, l)
mask_indices = torch.rand((b, l), device=batch.device) < p_mask
noisy_batch = torch.where(mask_indices, total_dim, batch)
return noisy_batch, mask_indices, p_mask
@torch.no_grad()
def get_loss_diff(model, input_ids):
noisy_input, mask_indices, p_mask = forward_process(input_ids)
with torch.cuda.amp.autocast(dtype=torch.bfloat16):
logits = model(noisy_input)
loss = F.cross_entropy(logits[mask_indices], input_ids[mask_indices], reduction='none') / p_mask[mask_indices]
loss = loss.sum() / (input_ids.shape[0] * input_ids.shape[1])
return loss
@torch.no_grad()
def get_loss_ar(model, input_ids):
target = input_ids[:, 1:].contiguous()
with torch.cuda.amp.autocast(dtype=torch.bfloat16):
logits = model(input_ids[:, :-1])
loss = F.cross_entropy(logits.view(-1, logits.shape[-1]), target.view(-1))
return loss
if __name__ == "__main__":
args = get_args()
set_seed(args.seed)
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model_name = f"Diff_LLaMA_{args.model}M"
config = Config.from_name(model_name)
tokenizer = AutoTokenizer.from_pretrained('TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T',
padding_side="right", use_fast=True)
if args.type == 'arm':
model = GPT(config).to(device)
elif args.type == 'mdm':
model = TransEncoder(config).to(device)
else:
raise NotImplementedError(args.type)
model.load_state_dict(load_file(args.ckpt_path))
pickle_filename = f'data/fineweb/{args.fineweb}.pkl'
print(f'load from: {pickle_filename}')
with open(pickle_filename, 'rb') as f:
loaded_tokens = pickle.load(f)
assert len(loaded_tokens) == 100 * 1024 * 2048
assert len(loaded_tokens) % (args.batch_size * 2048) == 0
num_iterations = len(loaded_tokens) // (args.batch_size * 2048)
losses = []
for index in tqdm(range(num_iterations)):
data_list = loaded_tokens[index * (args.batch_size * 2048): (index + 1) * (args.batch_size * 2048)]
data = torch.tensor(data_list).to(device)
data = data.view(args.batch_size, 2048)
if args.type == 'arm':
loss = get_loss_ar(model, data)
losses.append(loss.item())
elif args.type == 'mdm':
for _ in range(args.mc_samples): # mc number
loss = get_loss_diff(model, data)
losses.append(loss.item())
else:
raise NotImplementedError(args.type)
ppl = math.exp(sum(losses) / len(losses))
message = f'{args.ckpt_path}, {args.fineweb}, ppl={ppl}'
print(message)