luxfer / runtime /training /validation.py
mdxss's picture
Publish Luxfer early beta with runnable weights and language metadata
7b6f2cc verified
Raw History Blame Contribute Delete
797 Bytes
"""Response-token-weighted teacher-forced loss; not a translation quality score."""
import torch
@torch.no_grad()
def evaluate_nll(model,examples,device):
was_training=model.training
model.eval()
total=0.0; count=0
try:
for ids,labels in examples:
tokens=int((labels[:,1:]!=-100).sum())
if not tokens: continue
loss=model(ids.to(device),labels=labels.to(device),use_cache=False).loss
if not torch.isfinite(loss): raise RuntimeError('Nonfinite validation loss')
total+=loss.item()*tokens; count+=tokens
finally:
model.train(was_training)
if count==0: raise ValueError('No supervised validation tokens')
return {'nll':total/count,'tokens':count,'precision':'model parameter dtype; no autocast'}