File size: 2,166 Bytes
76389c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json,math,random,time
from pathlib import Path
import torch
from tokenizers import Tokenizer
from model import Config,DiChat10M
root=Path(__file__).resolve().parent
torch.manual_seed(20261028);random.seed(20261028)
torch.set_num_threads(4)
ck=torch.load(root/'latest_big.pt',map_location='cpu',weights_only=False)
tok=Tokenizer.from_file(str(root/'tokenizer_big.json'))
model=DiChat10M(Config(**ck['config']))
model.load_state_dict(ck['model'])
device='cuda' if torch.cuda.is_available() else 'cpu'
model=model.to(device).eval()
print('CHECKPOINT',ck['step'],'DEVICE',device,flush=True)
def score_corpus(name,windows=64,context=256,batch=4):
 ids=tok.encode((root/name).read_text(encoding='utf-8')).ids
 rng=random.Random(20261028)
 starts=[rng.randrange(0,len(ids)-context-1) for _ in range(windows)]
 total_loss=0;correct=0;total=0
 with torch.inference_mode():
  for j in range(0,windows,batch):
   sub=starts[j:j+batch]
   x=torch.tensor([ids[i:i+context] for i in sub],device=device)
   y=torch.tensor([ids[i+1:i+context+1] for i in sub],device=device)
   logits=model(x).float()
   total_loss+=torch.nn.functional.cross_entropy(logits.reshape(-1,logits.size(-1)),y.reshape(-1),reduction='sum').item()
   correct+=(logits.argmax(-1)==y).sum().item();total+=y.numel()
 loss=total_loss/total
 return {'file':name,'total_tokens_in_file':len(ids),'evaluated_tokens':total,'loss_nats':round(loss,5),'perplexity':round(math.exp(loss),3),'next_token_accuracy':round(correct/total,5)}
out={'model':'DiChat Mini 10M Base','checkpoint_step':ck['step'],'parameters':sum(p.numel() for p in model.parameters()),'vocab_size':tok.get_vocab_size(),'context':model.config.context,'method':'64 fixed-seed random windows per split, context 256, no training, overlapping windows possible','results':{}}
for name in ['val_stories.txt','test_stories.txt']:
 t=time.time();out['results'][name]=score_corpus(name);print(name,json.dumps(out['results'][name],ensure_ascii=False),'seconds',round(time.time()-t,1),flush=True)
(root/'benchmark_base_v01.json').write_text(json.dumps(out,ensure_ascii=False,indent=2),encoding='utf-8')
print('SAVED benchmark_base_v01.json',flush=True)