Repository: EleutherAI/lm-evaluation-harness
Issue: Fail to reproduce the perplexity of Llama-2 7B on wikitext
Root cause: Algorithmic Error

Hi, when I use the command for evaluating Llama-2 7B on wikitext2:
lm_eval --model hf     --model_args pretrained=meta-llama/Llama-2-7b-hf     --tasks wikitext     --device cuda:0     --batch_size 1
The result is 
![image](https://github.com/user-attachments/assets/6f9385e5-ca6b-4d5a-a0db-aaeb573978b4)
However, the fp16 result I saw in many papers is 5.47. Another confusing point is that the other tasks like piqa, winogrande, arc-e, arc-c ... I can get the exact same results as the papers reported. Thanks!

Hi! Can you provide a source? I'll check

Thanks for your reply! The source for Llama-2 7B on wikitext2 is from many SOTA quantization works:
https://arxiv.org/pdf/2306.00978 (page 7)
https://github.com/qwopqwop200/GPTQ-for-LLaMa (Llama-1 only)
https://arxiv.org/pdf/2308.13137 (page 7)
They all report PPL 5.68 for Llama-1-7b and 5.47 for Llama-2-7b as a FP16 baseline, which are far from 8.7071 as I tried in lm-eval
Thank you in advance

> Hi, when I use the command for evaluating Llama-2 7B on wikitext2: lm_eval --model hf --model_args pretrained=meta-llama/Llama-2-7b-hf --tasks wikitext --device cuda:0 --batch_size 1 The result is ![image](https://private-user-images.githubusercontent.com/58928842/367597346-6f9385e5-ca6b-4d5a-a0db-aaeb573978b4.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjczNTI5NjEsIm5iZiI6MTcyNzM1MjY2MSwicGF0aCI6Ii81ODkyODg0Mi8zNjc1OTczNDYtNmY5Mzg1ZTUtY2E2Yi00ZDVhLWEwZGItYWFlYjU3Mzk3OGI0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA5MjYlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwOTI2VDEyMTEwMVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTVhNGMyMmQ1ZDQyZmQ0NWExYWQ2M2MzNDFlNGJmYjg2M2FkZWQ4MGRlMDM5MWY2Yzk3YmQ0NjU0M2FhMGU0MWEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.BC1LmLk2HBJsL6Bu8esKwk5osPxAfZ2tMFuiwdJap8g) However, the fp16 result I saw in many papers is 5.47. Another confusing point is that the other tasks like piqa, winogrande, arc-e, arc-c ... I can get the exact same results as the papers reported. Thanks!

Hello, how did you test zero-shot tasks like piqa, arc...，here is my result, compared to result in paper spinquant, it seems something wrong. btw, what's difference between acc and acc norm? I don't know which one to compare with 
![image](https://github.com/user-attachments/assets/be1df71b-db7e-402b-b7a4-45b7012b5f4e)
![image](https://github.com/user-attachments/assets/81022bb2-2f91-4d19-b32c-bd308ed7d14a)



For me, I use the codebase of GPTQ or AWQ to run the wikitext evaluation.

I think the baseline in GPTQ or AWQ for wikitext2 is correct. However, lm-eval contains most of the datasets so I want to use it for wikitext2. But the result is confusing since the other tasks like common sense are correct, only wiki2 fails to align with other repos.

Is it possible that the metric here for wikitext2 is different from what is used in other codebase? Since all paper reports mostly the same FP16 baseline for Llama2-7b on wikitext2 (5.47)

> I think the baseline in GPTQ or AWQ for wikitext2 is correct. However, lm-eval contains most of the datasets so I want to use it for wikitext2. But the result is confusing since the other tasks like common sense are correct, only wiki2 fails to align with other repos.

Indeed. So AWQ splits the evaluation of wikitext from lm-eval, and so does QuaRot. See [AWQ](https://github.com/mit-han-lab/llm-awq/blob/main/awq/entry.py#L255) and [QuaRot](https://github.com/spcl/QuaRot/blob/main/fake_quant/data_utils.py#L5). In this way, you can reproduce the PPL resutls in most paper (5.47 for Llama2-7b and 5.69 for Llama-7b).
Therefore, I choose to manually load the dataset and process the PPL calculations on wikitext and C4. 

For other tasks, lm-eval is good.

> > I think the baseline in GPTQ or AWQ for wikitext2 is correct. However, lm-eval contains most of the datasets so I want to use it for wikitext2. But the result is confusing since the other tasks like common sense are correct, only wiki2 fails to align with other repos.
> 
> Indeed. So AWQ splits the evaluation of wikitext from lm-eval, and so does QuaRot. See [AWQ](https://github.com/mit-han-lab/llm-awq/blob/main/awq/entry.py#L255) and [QuaRot](https://github.com/spcl/QuaRot/blob/main/fake_quant/data_utils.py#L5). In this way, you can reproduce the PPL resutls in most paper (5.47 for Llama2-7b and 5.69 for Llama-7b). Therefore, I choose to manually load the dataset and process the PPL calculations on wikitext and C4.
> 
> For other tasks, lm-eval is good.

Thanks. Do you mean manually load the dataset and process the PPL calculations on wikitext and C4 in lm-eval (change the code in lm-eval)?

> > > I think the baseline in GPTQ or AWQ for wikitext2 is correct. However, lm-eval contains most of the datasets so I want to use it for wikitext2. But the result is confusing since the other tasks like common sense are correct, only wiki2 fails to align with other repos.
> > 
> > 
> > Indeed. So AWQ splits the evaluation of wikitext from lm-eval, and so does QuaRot. See [AWQ](https://github.com/mit-han-lab/llm-awq/blob/main/awq/entry.py#L255) and [QuaRot](https://github.com/spcl/QuaRot/blob/main/fake_quant/data_utils.py#L5). In this way, you can reproduce the PPL resutls in most paper (5.47 for Llama2-7b and 5.69 for Llama-7b). Therefore, I choose to manually load the dataset and process the PPL calculations on wikitext and C4.
> > For other tasks, lm-eval is good.
> 
> Thanks. Do you mean manually load the dataset and process the PPL calculations on wikitext and C4 in lm-eval (change the code in lm-eval)?

Yes. Just refer to the implementation of AWQ and QuaRot. 

Maybe there is a better way:)

Thanks a lot! I'll try that

Hi @baberabb, did you get a chance to look at this? I run into similar problems. We([PyTorch/ExecuTorch](https://github.com/pytorch/executorch)) use lm_eval as well but observed discrepancies for the wikitext2 perplexity for llama models.

Hi! Sorry about the delay. Looked into it, and there are a couple of differences:
1. Normalization: We normalize the perplexity by word count:
https://github.com/EleutherAI/lm-evaluation-harness/blob/0845b588303f1f59af98dd1c5bdbd78a9e75a1e2/lm_eval/tasks/wikitext/preprocess_wikitext.py#L42
and not by token count as, for example, used by [llm-awq](https://github.com/mit-han-lab/llm-awq/blob/3665e1abbf04139aa037254ff4ff3f261bd68a40/awq/entry.py#L280). Both are valid, but lm-eval has traditionally reported tokenizer agnostic metrics. For details see [Appendix A3](https://arxiv.org/pdf/2405.14782)
2. The wikitext dataset used here is document level (`EleutherAI/wikitext_document_level`) while AWQ and QuaRot use an aggregated [dataset](https://github.com/mit-han-lab/llm-awq/blob/3665e1abbf04139aa037254ff4ff3f261bd68a40/awq/entry.py#L257). This means:
   - Our implementation creates non-overlapping [chunks](https://huggingface.co/docs/transformers/en/perplexity) of size `model_length` for each document separately and then reports an aggregate measure of these document-level perplexities.
   - The other implementation concatenates all the text in the dataset into a single long sequence before creating the overlapping chunks.
   
Made a fork [here](https://github.com/baberabb/lm-evaluation-harness/tree/wikitext-tokens) to report token normalized perplexity (for `Llama-2-7b-hf`) instead of word-level, as well as using the aggregated dataset. The result is `5.4775` as in the other sources. Command used was:

`lm_eval --model hf --model_args pretrained=meta-llama/Llama-2-7b-hf,max_length=2048 --tasks wikitext --batch_size 8`

Hope this helps!

Thanks @baberabb for investigating on this! This is really helpful and makes a lot of sense.

Looked at your fork, looks like it would require tokenizer if we want to report perplexity normalized by token count. Going forward, it seems that we won't be able to fix the difference discussed in this issue since they are caused by two different implementations and both are valid. Is it possible to let lm_eval report perplexity normalized by token count?