Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Longbench: NV code to ipex-llm #11662

Merged
merged 14 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions python/llm/dev/benchmark/LongBench/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# LongBench Benchmark Test

LongBench Benchmark allows users to test LongBench benchmark and record them in some json files. Users can provide models and related information in `config.yaml` and `config` directory.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LongBench is the first benchmark for bilingual, multitask, and comprehensive assessment of long context understanding capabilities of large language models. This benchmark implementation is adapted from xxxxx(https://github.com/THUDM/LongBench) and xxxx(https://github.com/FasterDecoding/SnapKV/tree/main/experiments/LongBench)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this line.


Before running, make sure to have [ipex-llm](../../../../../README.md) installed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move to env preparation.


## Dependencies
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Environment Preparation


```bash
pip install omegaconf
pip install datasets
pip install jieba
pip install fuzzywuzzy
pip install rouge
```

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add Load Data section
You can download and load the LongBench data through the Hugging Face datasets (🤗 HF Repo):

from datasets import load_dataset

datasets = ["narrativeqa", "qasper", "multifieldqa_en", "multifieldqa_zh", "hotpotqa", "2wikimqa", "musique", \
            "dureader", "gov_report", "qmsum", "multi_news", "vcsum", "trec", "triviaqa", "samsum", "lsht", \
            "passage_count", "passage_retrieval_en", "passage_retrieval_zh", "lcc", "repobench-p"]

for dataset in datasets:
    data = load_dataset('THUDM/LongBench', dataset, split='test')
    data = load_dataset('THUDM/LongBench', f"{dataset}_e", split='test')

## config

### `config.yaml`

Config YAML file has following format

```yaml
# The name of the models you want to test
model_name:
# - "mistral-7B-instruct-v0.2"
- "llama2-7b-chat-4k"
# - "chatglm4-9b"
# - "qwen2-7b-instruct"

# whether or not to test the full-kv score
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whether test the full-kv

full_kv: True
# whether or not to open optimize_model
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether apply model optimization

optimize_model: True
# dtype of the model
dtype: 'fp16'
# low bit of the model
low_bit: 'sym_int4'
# whether or not to use the 'e' version of the datasets
e: False

# the compress kv configs you want to test
compress_kv:
- "ablation_c512_w32_k7_maxpool"
- "ablation_c1024_w32_k7_maxpool"

# the datasets you want to test
datasets:
- "multi_news"
- "qasper"
- "hotpotqa"
- "trec"
- "passage_count"
- "lcc"
# - "multifieldqa_zh"
# - "dureader"
# - "vcsum"
# - "lsht"
# - "passage_retrieval_zh"

```

### The `config` dir

Some json files is saved in the `config` dir. It can be divided into three kinds: about models, about datasets, and about compress-kv.

#### About Models

- `model2path.json`: This file saves the path to the models.

- `model2maxlen.json`: This file saves the max length of the prompts of each model.

#### About datasets

- `dataset2maxlen.json`: The max length of the outputs of the models of each dataset.

- `dataset2prompt.json`: The format of prompts of each dataset.

#### About compress-kv

The rest json files are about compress-kv.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest JSON files are compress-kv test configurations.


## Run

There are two python files for users' call.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Configure the config.yaml and run pred.py and you can obtain the output of the model under pred/ folder corresponding to the model name.
  2. Run the evaluation code eval.py, you can get the evaluation results on all datasets in result.json.


- `pred.py`: This script will give the output of the models configged in the `config.yaml`

- `eval.py`: This script calculates the score of each case.

> [!Note]
>
> To test the models and get the score in a row, please run `test_and_eval.sh`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add citation section

@article{bai2023longbench,
  title={LongBench: A Bilingual, Multitask Benchmark for Long Context Understanding},
  author={Bai, Yushi and Lv, Xin and Zhang, Jiajie and Lyu, Hongchang and Tang, Jiankai and Huang, Zhidian and Du, Zhengxiao and Liu, Xiao and Zeng, Aohan and Hou, Lei and Dong, Yuxiao and Tang, Jie and Li, Juanzi},
  journal={arXiv preprint arXiv:2308.14508},
  year={2023}
}

29 changes: 29 additions & 0 deletions python/llm/dev/benchmark/LongBench/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
model_name:
# - "mistral-7B-instruct-v0.2"
- "llama2-7b-chat-4k"
# - "chatglm4-9b"
# - "qwen2-7b-instruct"

full_kv: True
optimize_model: True
dtype: 'fp16'
low_bit: 'sym_int4'

e: False

compress_kv:
- "ablation_c512_w32_k7_maxpool"
- "ablation_c1024_w32_k7_maxpool"

datasets:
- "multi_news"
- "qasper"
- "hotpotqa"
- "trec"
- "passage_count"
- "lcc"
# - "multifieldqa_zh"
# - "dureader"
# - "vcsum"
# - "lsht"
# - "passage_retrieval_zh"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"window_sizes": 32,
"default_max_capacity_prompts": 1024,
"specific_max_capcity_prompts": {},
"kernel_sizes": 7,
"pooling": "maxpool"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"window_sizes": 32,
"default_max_capacity_prompts": 2048,
"specific_max_capcity_prompts": {},
"kernel_sizes": 7,
"pooling": "maxpool"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"window_sizes": 32,
"default_max_capacity_prompts": 4096,
"specific_max_capcity_prompts": {},
"kernel_sizes": 7,
"pooling": "maxpool"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"window_sizes": 32,
"default_max_capacity_prompts": 512,
"specific_max_capcity_prompts": {},
"kernel_sizes": 7,
"pooling": "maxpool"
}
23 changes: 23 additions & 0 deletions python/llm/dev/benchmark/LongBench/config/dataset2maxlen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"narrativeqa": 128,
"qasper": 128,
"multifieldqa_en": 64,
"multifieldqa_zh": 64,
"hotpotqa": 32,
"2wikimqa": 32,
"musique": 32,
"dureader": 128,
"gov_report": 512,
"qmsum": 512,
"multi_news": 512,
"vcsum": 512,
"trec": 64,
"triviaqa": 32,
"samsum": 128,
"lsht": 64,
"passage_count": 32,
"passage_retrieval_en": 32,
"passage_retrieval_zh": 32,
"lcc": 64,
"repobench-p": 64
}
23 changes: 23 additions & 0 deletions python/llm/dev/benchmark/LongBench/config/dataset2prompt.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"narrativeqa": "You are given a story, which can be either a novel or a movie script, and a question. Answer the question asconcisely as you can, using a single phrase if possible. Do not provide any explanation.\n\nStory: {context}\n\nNow, answer the question based on the story asconcisely as you can, using a single phrase if possible. Do not provide any explanation.\n\nQuestion: {input}\n\nAnswer:",
"qasper": "You are given a scientific article and a question. Answer the question as concisely as you can, using a single phrase or sentence if possible. If the question cannot be answered based on the information in the article, write \"unanswerable\". If the question is a yes/no question, answer \"yes\", \"no\", or \"unanswerable\". Do not provide any explanation.\n\nArticle: {context}\n\n Answer the question based on the above article as concisely as you can, using a single phrase or sentence if possible. If the question cannot be answered based on the information in the article, write \"unanswerable\". If the question is a yes/no question, answer \"yes\", \"no\", or \"unanswerable\". Do not provide any explanation.\n\nQuestion: {input}\n\nAnswer:",
"multifieldqa_en": "Read the following text and answer briefly.\n\n{context}\n\nNow, answer the following question based on the above text, only give me the answer and do not output any other words.\n\nQuestion: {input}\nAnswer:",
"multifieldqa_zh": "阅读以下文字并用中文简短回答:\n\n{context}\n\n现在请基于上面的文章回答下面的问题,只告诉我答案,不要输出任何其他字词。\n\n问题:{input}\n回答:",
"hotpotqa": "Answer the question based on the given passages. Only give me the answer and do not output any other words.\n\nThe following are given passages.\n{context}\n\nAnswer the question based on the given passages. Only give me the answer and do not output any other words.\n\nQuestion: {input}\nAnswer:",
"2wikimqa": "Answer the question based on the given passages. Only give me the answer and do not output any other words.\n\nThe following are given passages.\n{context}\n\nAnswer the question based on the given passages. Only give me the answer and do not output any other words.\n\nQuestion: {input}\nAnswer:",
"musique": "Answer the question based on the given passages. Only give me the answer and do not output any other words.\n\nThe following are given passages.\n{context}\n\nAnswer the question based on the given passages. Only give me the answer and do not output any other words.\n\nQuestion: {input}\nAnswer:",
"dureader": "请基于给定的文章回答下述问题。\n\n文章:{context}\n\n请基于上述文章回答下面的问题。\n\n问题:{input}\n回答:",
"gov_report": "You are given a report by a government agency. Write a one-page summary of the report.\n\nReport:\n{context}\n\nNow, write a one-page summary of the report.\n\nSummary:",
"qmsum": "You are given a meeting transcript and a query containing a question or instruction. Answer the query in one or more sentences.\n\nTranscript:\n{context}\n\nNow, answer the query based on the above meeting transcript in one or more sentences.\n\nQuery: {input}\nAnswer:",
"multi_news": "You are given several news passages. Write a one-page summary of all news. \n\nNews:\n{context}\n\nNow, write a one-page summary of all the news.\n\nSummary:",
"vcsum": "下面有一段会议记录,请你阅读后,写一段总结,总结会议的内容。\n会议记录:\n{context}\n\n会议总结:",
"trec": "Please determine the type of the question below. Here are some examples of questions.\n\n{context}\n{input}",
"triviaqa": "Answer the question based on the given passage. Only give me the answer and do not output any other words. The following are some examples.\n\n{context}\n\n{input}",
"samsum": "Summarize the dialogue into a few short sentences. The following are some examples.\n\n{context}\n\n{input}",
"lsht": "请判断给定新闻的类别,下面是一些例子。\n\n{context}\n{input}",
"passage_count": "There are some paragraphs below sourced from Wikipedia. Some of them may be duplicates. Please carefully read these paragraphs and determine how many unique paragraphs there are after removing duplicates. In other words, how many non-repeating paragraphs are there in total?\n\n{context}\n\nPlease enter the final count of unique paragraphs after removing duplicates. The output format should only contain the number, such as 1, 2, 3, and so on.\n\nThe final answer is: ",
"passage_retrieval_en": "Here are 30 paragraphs from Wikipedia, along with an abstract. Please determine which paragraph the abstract is from.\n\n{context}\n\nThe following is an abstract.\n\n{input}\n\nPlease enter the number of the paragraph that the abstract is from. The answer format must be like \"Paragraph 1\", \"Paragraph 2\", etc.\n\nThe answer is: ",
"passage_retrieval_zh": "以下是若干段落文字,以及其中一个段落的摘要。请确定给定的摘要出自哪一段。\n\n{context}\n\n下面是一个摘要\n\n{input}\n\n请输入摘要所属段落的编号。答案格式必须是\"段落1\",\"段落2\"等格式\n\n答案是:",
"lcc": "Please complete the code given below. \n{context}Next line of code:\n",
"repobench-p": "Please complete the code given below. \n{context}{input}Next line of code:\n"
}
18 changes: 18 additions & 0 deletions python/llm/dev/benchmark/LongBench/config/model2maxlen.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
cyita marked this conversation as resolved.
Show resolved Hide resolved
"llama2-7b-chat-4k": 4096,
"longchat-v1.5-7b-32k": 4096,
"xgen-7b-8k": 4096,
"internlm-7b-8k": 4096,
"chatglm2-6b": 4096,
"chatglm2-6b-32k": 4096,
"chatglm3-6b-32k": 4096,
"chatglm4-9b": 4096,
"vicuna-v1.5-7b-16k": 4096,
"mistral-7B-instruct-v0.2": 4096,
"mistral-7B-instruct-v0.1": 4096,
"mixtral-8x7B-instruct-v0.1": 4096,
"llama-2-7B-32k-instruct": 4096,
"lwm-text-chat-1m": 4096,
"lwm-text-1m": 4096,
"qwen2-7b-instruct": 4096
}
18 changes: 18 additions & 0 deletions python/llm/dev/benchmark/LongBench/config/model2path.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"llama2-7b-chat-4k": "meta-llama/Llama-2-7b-chat-hf",
"longchat-v1.5-7b-32k": "lmsys/longchat-7b-v1.5-32k",
"xgen-7b-8k": "Salesforce/xgen-7b-8k-inst",
"internlm-7b-8k": "internlm/internlm-chat-7b-8k",
"chatglm2-6b": "THUDM/chatglm2-6b",
"chatglm2-6b-32k": "THUDM/chatglm2-6b-32k",
"chatglm3-6b-32k": "THUDM/chatglm3-6b-32k",
"chatglm4-9b": "THUDM/glm-4-9b-chat",
"vicuna-v1.5-7b-16k": "lmsys/vicuna-7b-v1.5-16k",
"mistral-7B-instruct-v0.2": "mistralai/Mistral-7B-Instruct-v0.2",
"mistral-7B-instruct-v0.1": "mistralai/Mistral-7B-Instruct-v0.1",
"mixtral-8x7B-instruct-v0.1": "mistralai/Mixtral-8x7B-Instruct-v0.1",
"llama-2-7B-32k-instruct": "togethercomputer/Llama-2-7B-32K-Instruct",
"lwm-text-chat-1m": "LargeWorldModel/LWM-Text-Chat-1M",
"lwm-text-1m": "LargeWorldModel/LWM-Text-1M",
"qwen2-7b-instruct": "Qwen/Qwen2-7B-Instruct"
}
130 changes: 130 additions & 0 deletions python/llm/dev/benchmark/LongBench/eval.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import os
import json
import argparse
import numpy as np

current_dir = os.path.dirname(os.path.realpath(__file__))

from metrics import (
qa_f1_score,
rouge_zh_score,
qa_f1_zh_score,
rouge_score,
classification_score,
retrieval_score,
retrieval_zh_score,
count_score,
code_sim_score,
)

dataset2metric = {
"narrativeqa": qa_f1_score,
"qasper": qa_f1_score,
"multifieldqa_en": qa_f1_score,
"multifieldqa_zh": qa_f1_zh_score,
"hotpotqa": qa_f1_score,
"2wikimqa": qa_f1_score,
"musique": qa_f1_score,
"dureader": rouge_zh_score,
"gov_report": rouge_score,
"qmsum": rouge_score,
"multi_news": rouge_score,
"vcsum": rouge_zh_score,
"trec": classification_score,
"triviaqa": qa_f1_score,
"samsum": rouge_score,
"lsht": classification_score,
"passage_retrieval_en": retrieval_score,
"passage_count": count_score,
"passage_retrieval_zh": retrieval_zh_score,
"lcc": code_sim_score,
"repobench-p": code_sim_score,
}

def parse_args(args=None):
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, default=None)
parser.add_argument('--e', action='store_true', help="Evaluate on LongBench-E")
return parser.parse_args(args)

def scorer_e(dataset, predictions, answers, lengths, all_classes):
scores = {"0-4k": [], "4-8k": [], "8k+": []}
for (prediction, ground_truths, length) in zip(predictions, answers, lengths):
score = 0.
if dataset in ["trec", "triviaqa", "samsum", "lsht"]:
prediction = prediction.lstrip('\n').split('\n')[0]
for ground_truth in ground_truths:
score = max(score, dataset2metric[dataset](prediction, ground_truth, all_classes=all_classes))
if length < 4000:
scores["0-4k"].append(score)
elif length < 8000:
scores["4-8k"].append(score)
else:
scores["8k+"].append(score)
for key in scores.keys():
scores[key] = round(100 * np.mean(scores[key]), 2)
return scores

def scorer(dataset, predictions, answers, all_classes):
total_score = 0.
for (prediction, ground_truths) in zip(predictions, answers):
score = 0.
if dataset in ["trec", "triviaqa", "samsum", "lsht"]:
prediction = prediction.lstrip('\n').split('\n')[0]
for ground_truth in ground_truths:
score = max(score, dataset2metric[dataset](prediction, ground_truth, all_classes=all_classes))
total_score += score
return round(100 * total_score / len(predictions), 2)


def result_path_range(full_kv: bool, configs: list[str], model_name: str, fa_name: str):
if full_kv:
yield f"{fa_name}/{model_name}"

for config in configs:
yield f"{fa_name}/{model_name}_{config}"


if __name__ == '__main__':
from omegaconf import OmegaConf
conf = OmegaConf.load(f'{current_dir}/config.yaml')

model_names = conf['model_name'] if OmegaConf.is_list(conf['model_name']) else [conf['model_name']]
full_kv = conf['full_kv']
ees = conf['e'] if OmegaConf.is_list(conf['e']) else [conf['e']]
compresskv_configs = conf['compress_kv'] if OmegaConf.is_list(conf['compress_kv']) else [conf['compress_kv']]

model2maxlen = json.load(open(f"{current_dir}/config/model2maxlen.json", "r"))

for model_name in model_names:
max_length = model2maxlen[model_name]
for e in ees:
fa_dir_name = f"pred_{'e_' if e else ''}{max_length}"
for path in result_path_range(full_kv, compresskv_configs, model_name, fa_dir_name):
scores = dict()
all_files = os.listdir(path)
print("Evaluating on:", all_files)
for filename in all_files:
if not filename.endswith("jsonl"):
continue
predictions, answers, lengths = [], [], []
dataset = filename.split('.')[0]
with open(f"{path}/{filename}", "r", encoding="utf-8") as f:
for line in f:
data = json.loads(line)
predictions.append(data["pred"])
answers.append(data["answers"])
all_classes = data["all_classes"]
if "length" in data:
lengths.append(data["length"])
if e:
score = scorer_e(dataset, predictions, answers, lengths, all_classes)
else:
score = scorer(dataset, predictions, answers, all_classes)
if dataset == 'qasper':
score_e = scorer_e(dataset, predictions, answers, lengths, all_classes)
scores[dataset] = score

out_path = f"{path}/result.json"
with open(out_path, "w") as f:
json.dump(scores, f, ensure_ascii=False, indent=4)
Loading
Loading