|
| 1 | +import os.path |
| 2 | +import torch |
| 3 | +import torch.nn as nn |
| 4 | + |
| 5 | + |
| 6 | +def eval_model(model, model_name, tokenizer, tasks=["lambada_openai", "hellaswag", "winogrande", "piqa"], eval_bs=32): |
| 7 | + try: |
| 8 | + from intel_extension_for_transformers.llm.evaluation.lm_eval import evaluate as lm_evaluate |
| 9 | + print("evaluation with itrex lm-eval", flush=True) |
| 10 | + |
| 11 | + if str(model.device) == "cpu": |
| 12 | + model = model.to(torch.bfloat16) |
| 13 | + dtype = 'bfloat16' |
| 14 | + else: |
| 15 | + model = model.half() |
| 16 | + dtype = 'float16' |
| 17 | + model.eval() |
| 18 | + results = lm_evaluate(model="hf-causal", |
| 19 | + model_args=f'pretrained="{model_name}",tokenizer="{model_name}",dtype={dtype}', |
| 20 | + user_model=model, |
| 21 | + tasks=tasks, |
| 22 | + device=str(model.device), |
| 23 | + batch_size=eval_bs) |
| 24 | + |
| 25 | + except: |
| 26 | + print("evaluation with official lm-eval", flush=True) |
| 27 | + from lm_eval.evaluator import simple_evaluate |
| 28 | + import json |
| 29 | + import shutil |
| 30 | + |
| 31 | + ##save model |
| 32 | + output_dir = "./tmp_signround" |
| 33 | + if os.path.exists(output_dir): |
| 34 | + shutil.rmtree(output_dir) |
| 35 | + if output_dir is not None: |
| 36 | + model.save_pretrained(output_dir) |
| 37 | + tokenizer.save_pretrained(output_dir) |
| 38 | + if str(model.device) == "cpu": |
| 39 | + dtype = 'bfloat16' |
| 40 | + else: |
| 41 | + dtype = 'float16' |
| 42 | + results = simple_evaluate(model="hf-causal", |
| 43 | + model_args=f'pretrained="{output_dir}",tokenizer="{output_dir}",dtype={dtype}', |
| 44 | + tasks=tasks, |
| 45 | + device=str(model.device), |
| 46 | + batch_size=eval_bs, |
| 47 | + no_cache=True) |
| 48 | + dumped = json.dumps(results, indent=2) |
| 49 | + print(dumped) |
| 50 | + |
| 51 | + if os.path.exists(output_dir): |
| 52 | + shutil.rmtree(output_dir) |
| 53 | + |
| 54 | + @torch.no_grad() |
| 55 | + def eval_same_with_gptq(model, testenc, dev): |
| 56 | + print('Evaluating ...', flush=True) |
| 57 | + # model.eval() |
| 58 | + model.to(dev) |
| 59 | + |
| 60 | + testenc = testenc.input_ids |
| 61 | + nsamples = testenc.numel() // model.seqlen |
| 62 | + |
| 63 | + use_cache = model.config.use_cache |
| 64 | + model.config.use_cache = False |
| 65 | + |
| 66 | + testenc = testenc.to(dev) |
| 67 | + nlls = [] |
| 68 | + for i in range(nsamples): |
| 69 | + batch = testenc[:, (i * model.seqlen):((i + 1) * model.seqlen)].to(dev) |
| 70 | + lm_logits = model(batch).logits |
| 71 | + shift_logits = lm_logits[:, :-1, :].contiguous() |
| 72 | + shift_labels = testenc[ |
| 73 | + :, (i * model.seqlen):((i + 1) * model.seqlen) |
| 74 | + ][:, 1:] |
| 75 | + loss_fct = nn.CrossEntropyLoss() |
| 76 | + loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1)) |
| 77 | + neg_log_likelihood = loss.float() * model.seqlen |
| 78 | + nlls.append(neg_log_likelihood) |
| 79 | + ppl = torch.exp(torch.stack(nlls).sum() / (nsamples * model.seqlen)) |
| 80 | + print(ppl.item()) |
| 81 | + |
| 82 | + model.config.use_cache = use_cache |
| 83 | + return ppl.item() |
| 84 | + |
| 85 | + datasets = ['wikitext2', 'ptb-new', 'c4-new'] |
| 86 | + |
| 87 | + from gptq_data_loader import get_loaders |
| 88 | + for dataset in datasets: |
| 89 | + dataloader, testloader = get_loaders( |
| 90 | + dataset, seed=0, model=model_name, seqlen=model.seqlen |
| 91 | + ) |
| 92 | + print(dataset, flush=True) |
| 93 | + ppl = eval_same_with_gptq(model, testloader, str(model.device)) |
| 94 | + results.update({dataset: ppl}) |
0 commit comments