DiChat-Mini-10M-Base / benchmark_tinystories.py
DiChat-models's picture
Initial DiChat Mini 10M Base release
76389c9 verified
Raw History Blame Contribute Delete
2.13 kB
import json,math,random,time
from pathlib import Path
import torch
from tokenizers import Tokenizer
from huggingface_hub import hf_hub_download
from model import Config,DiChat10M
root=Path(__file__).resolve().parent
source='roneneldan/TinyStories'
filename='TinyStoriesV2-GPT4-valid.txt'
print('DOWNLOADING/LOCATING',source,filename,flush=True)
path=hf_hub_download(repo_id=source,repo_type='dataset',filename=filename)
text=Path(path).read_text(encoding='utf-8')
print('VALIDATION CHARS',len(text),flush=True)
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()
ids=tok.encode(text).ids
rng=random.Random(20261028)
context=256;windows=128;batch=4
starts=[rng.randrange(0,len(ids)-context-1) for _ in range(windows)]
loss_sum=0;correct=0;count=0
print('START',len(ids),'tokens',flush=True)
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()
loss_sum+=torch.nn.functional.cross_entropy(logits.reshape(-1,logits.size(-1)),y.reshape(-1),reduction='sum').item()
correct+=(logits.argmax(-1)==y).sum().item();count+=y.numel()
loss=loss_sum/count
out={'dataset':source,'file':filename,'model':'DiChat Mini 10M Base','step':ck['step'],'language':'English','model_training_language':'predominantly Russian','tokenizer':'DiChat 4096 ByteLevel BPE','context':context,'windows':windows,'evaluated_tokens':count,'dataset_tokens':len(ids),'loss_nats':loss,'perplexity':math.exp(loss),'next_token_accuracy':correct/count,'method':'128 random fixed-seed windows; possible overlap; not original TinyStories paper evaluation protocol'}
(root/'benchmark_tinystories_v01.json').write_text(json.dumps(out,ensure_ascii=False,indent=2),encoding='utf-8')
print('RESULT',json.dumps(out,ensure_ascii=False),flush=True)