import torch, math def compute_perplexity(model, tok, texts): losses=[] device="cpu" model.eval() with torch.no_grad(): for t in texts: if not t.strip(): continue inp = tok(t, return_tensors="pt", truncation=True).to(device) out = model(**inp, labels=inp["input_ids"]) losses.append(out.loss.item()) return math.exp(sum(losses)/len(losses))