Views
No views yet
| Models | Score | Chat | Chat Hard | Safety | Reasoning |
|---|---|---|---|---|---|
| InternLM2-20B-Reward | 89.5 | 98.6 | 74.1 | 89.4 | 95.7 |
| InternLM2-7B-Reward | 86.6 | 98.6 | 66.7 | 88.3 | 92.8 |
| InternLM2-1.8B-Reward | 80.6 | 95.0 | 58.1 | 81.8 | 87.4 |
1import torch
2from transformers import AutoModel, AutoTokenizer
3
4model = AutoModel.from_pretrained(
5 "internlm/internlm2-7b-reward",
6 device_map="cuda",
7 torch_dtype=torch.float16,
8 trust_remote_code=True,
9)
10tokenizer = AutoTokenizer.from_pretrained("internlm/internlm2-7b-reward", trust_remote_code=True)
11
12chat_1 = [
13 {"role": "user", "content": "Hello! What's your name?"},
14 {"role": "assistant", "content": "My name is InternLM2! A helpful AI assistant. What can I do for you?"}
15]
16chat_2 = [
17 {"role": "user", "content": "Hello! What's your name?"},
18 {"role": "assistant", "content": "I have no idea."}
19]
20
21
22# get reward score for a single chat
23score1 = model.get_score(tokenizer, chat_1)
24score2 = model.get_score(tokenizer, chat_2)
25print("score1: ", score1)
26print("score2: ", score2)
27# >>> score1: 0.767578125
28# >>> score2: -2.22265625
29
30
31# batch inference, get multiple scores at once
32scores = model.get_scores(tokenizer, [chat_1, chat_2])
33print("scores: ", scores)
34# >>> scores: [0.767578125, -2.22265625]
35
36
37# compare whether chat_1 is better than chat_2
38compare_res = model.compare(tokenizer, chat_1, chat_2)
39print("compare_res: ", compare_res)
40# >>> compare_res: True
41
42
43# rank multiple chats, it will return the ranking index of each chat
44# the chat with the highest score will have ranking index as 0
45rank_res = model.rank(tokenizer, [chat_1, chat_2])
46print("rank_res: ", rank_res) # lower index means higher score
47# >>> rank_res: [0, 1] 1import torch
2from transformers import AutoModel, AutoTokenizer
3
4# prepare the llm model and tokenizer
5llm = AutoModel.from_pretrained(
6 "internlm/internlm2-chat-7b",
7 device_map="cuda",
8 torch_dtype=torch.float16,
9 trust_remote_code=True,
10)
11llm_tokenizer = AutoTokenizer.from_pretrained("internlm/internlm2-chat-7b", trust_remote_code=True)
12
13# prepare the reward model and tokenizer
14reward = AutoModel.from_pretrained(
15 "internlm/internlm2-7b-reward",
16 device_map="cuda",
17 torch_dtype=torch.float16,
18 trust_remote_code=True,
19)
20reward_tokenizer = AutoTokenizer.from_pretrained("internlm/internlm2-7b-reward", trust_remote_code=True)
21
22# prepare the chat prompt
23prompt = "Write an article about the artificial intelligence revolution."
24messages = [
25 {"role": "system", "content": "You are a helpful assistant."},
26 {"role": "user", "content": prompt}
27]
28text = llm_tokenizer.apply_chat_template(
29 messages,
30 tokenize=False,
31 add_generation_prompt=True
32)
33model_inputs = llm_tokenizer([text], return_tensors="pt").to("cuda")
34
35# generate best of N candidates
36num_candidates = 10 # N=10
37candidates = []
38
39outputs = llm.generate(
40 **model_inputs,
41 max_new_tokens=512,
42 num_return_sequences=num_candidates,
43 pad_token_id=llm_tokenizer.eos_token_id,
44 do_sample=True,
45 top_k=50,
46 top_p=0.95,
47 temperature=0.8,
48)
49outputs = outputs[:, model_inputs["input_ids"].shape[1]:]
50for i in range(num_candidates):
51 candidate = llm_tokenizer.decode(outputs[i], skip_special_tokens=True)
52 candidates.append(messages + [{"role": "assistant", "content": candidate}])
53
54rank_indices = reward.rank(reward_tokenizer, candidates)
55sorted_candidates = sorted(zip(rank_indices, candidates), key=lambda x: x[0])
56
57## print the ranked candidates
58# for i, (rank_index, candidate) in enumerate(sorted_candidates):
59# print(f"------------Rank {i}------------: \n{candidate[-1]['content']}")
60
61# print the best response
62best_response = sorted_candidates[0][1][-1]['content']
63print(best_response)@misc{cai2024internlm2,
title={InternLM2 Technical Report},
author={Zheng Cai and Maosong Cao and Haojiong Chen and Kai Chen and Keyu Chen and Xin Chen and Xun Chen and Zehui Chen and Zhi Chen and Pei Chu and Xiaoyi Dong and Haodong Duan and Qi Fan and Zhaoye Fei and Yang Gao and Jiaye Ge and Chenya Gu and Yuzhe Gu and Tao Gui and Aijia Guo and Qipeng Guo and Conghui He and Yingfan Hu and Ting Huang and Tao Jiang and Penglong Jiao and Zhenjiang Jin and Zhikai Lei and Jiaxing Li and Jingwen Li and Linyang Li and Shuaibin Li and Wei Li and Yining Li and Hongwei Liu and Jiangning Liu and Jiawei Hong and Kaiwen Liu and Kuikun Liu and Xiaoran Liu and Chengqi Lv and Haijun Lv and Kai Lv and Li Ma and Runyuan Ma and Zerun Ma and Wenchang Ning and Linke Ouyang and Jiantao Qiu and Yuan Qu and Fukai Shang and Yunfan Shao and Demin Song and Zifan Song and Zhihao Sui and Peng Sun and Yu Sun and Huanze Tang and Bin Wang and Guoteng Wang and Jiaqi Wang and Jiayu Wang and Rui Wang and Yudong Wang and Ziyi Wang and Xingjian Wei and Qizhen Weng and Fan Wu and Yingtong Xiong and Chao Xu and Ruiliang Xu and Hang Yan and Yirong Yan and Xiaogui Yang and Haochen Ye and Huaiyuan Ying and Jia Yu and Jing Yu and Yuhang Zang and Chuyu Zhang and Li Zhang and Pan Zhang and Peng Zhang and Ruijie Zhang and Shuo Zhang and Songyang Zhang and Wenjian Zhang and Wenwei Zhang and Xingcheng Zhang and Xinyue Zhang and Hui Zhao and Qian Zhao and Xiaomeng Zhao and Fengzhe Zhou and Zaida Zhou and Jingming Zhuo and Yicheng Zou and Xipeng Qiu and Yu Qiao and Dahua Lin},
year={2024},
eprint={2403.17297},
archivePrefix={arXiv},
primaryClass={cs.CL}
}| Models | Score | Chat | Chat Hard | Safety | Reasoning |
|---|---|---|---|---|---|
| InternLM2-20B-Reward | 89.5 | 98.6 | 74.1 | 89.4 | 95.7 |
| InternLM2-7B-Reward | 86.6 | 98.6 | 66.7 | 88.3 | 92.8 |
| InternLM2-1.8B-Reward | 80.6 | 95.0 | 58.1 | 81.8 | 87.4 |
1import torch
2from transformers import AutoModel, AutoTokenizer
3
4model = AutoModel.from_pretrained(
5 "internlm/internlm2-7b-reward",
6 device_map="cuda",
7 torch_dtype=torch.float16,
8 trust_remote_code=True,
9)
10tokenizer = AutoTokenizer.from_pretrained("internlm/internlm2-7b-reward", trust_remote_code=True)
11
12chat_1 = [
13 {"role": "user", "content": "Hello! What's your name?"},
14 {"role": "assistant", "content": "My name is InternLM2! A helpful AI assistant. What can I do for you?"}
15]
16chat_2 = [
17 {"role": "user", "content": "Hello! What's your name?"},
18 {"role": "assistant", "content": "I have no idea."}
19]
20
21
22# 获取单个对话的奖励分数
23score1 = model.get_score(tokenizer, chat_1)
24score2 = model.get_score(tokenizer, chat_2)
25print("score1: ", score1)
26print("score2: ", score2)
27# >>> score1: 0.767578125
28# >>> score2: -2.22265625
29
30
31# 批量推理,一次获取多个分数
32scores = model.get_scores(tokenizer, [chat_1, chat_2])
33print("scores: ", scores)
34# >>> scores: [0.767578125, -2.22265625]
35
36
37# 比较 chat_1 是否比 chat_2 更好
38compare_res = model.compare(tokenizer, chat_1, chat_2)
39print("compare_res: ", compare_res)
40# >>> compare_res: True
41
42
43# 排名多个对话,它将返回每个对话的排名序号
44# 分数最高的对话排名序号为 0
45rank_res = model.rank(tokenizer, [chat_1, chat_2])
46print("rank_res: ", rank_res) # 排名序号越低表示分数越高
47# >>> rank_res: [0, 1] 1import torch
2from transformers import AutoModel, AutoTokenizer
3
4# 准备语言模型和分词器
5llm = AutoModel.from_pretrained(
6 "internlm/internlm2-chat-7b",
7 device_map="cuda",
8 torch_dtype=torch.float16,
9 trust_remote_code=True,
10)
11llm_tokenizer = AutoTokenizer.from_pretrained("internlm/internlm2-chat-7b", trust_remote_code=True)
12
13# 准备奖励模型和分词器
14reward = AutoModel.from_pretrained(
15 "internlm/internlm2-7b-reward",
16 device_map="cuda",
17 torch_dtype=torch.float16,
18 trust_remote_code=True,
19)
20reward_tokenizer = AutoTokenizer.from_pretrained("internlm/internlm2-7b-reward", trust_remote_code=True)
21
22# 准备提示词
23prompt = "Write an article about the artificial intelligence revolution."
24messages = [
25 {"role": "system", "content": "You are a helpful assistant."},
26 {"role": "user", "content": prompt}
27]
28text = llm_tokenizer.apply_chat_template(
29 messages,
30 tokenize=False,
31 add_generation_prompt=True
32)
33model_inputs = llm_tokenizer([text], return_tensors="pt").to("cuda")
34
35# 生成 N 个候选
36num_candidates = 10 # N=10
37candidates = []
38
39outputs = llm.generate(
40 **model_inputs,
41 max_new_tokens=512,
42 num_return_sequences=num_candidates,
43 pad_token_id=llm_tokenizer.eos_token_id,
44 do_sample=True,
45 top_k=50,
46 top_p=0.95,
47 temperature=0.8,
48)
49outputs = outputs[:, model_inputs["input_ids"].shape[1]:]
50
51
52for i in range(num_candidates):
53 candidate = llm_tokenizer.decode(outputs[i], skip_special_tokens=True)
54 candidates.append(messages + [{"role": "assistant", "content": candidate}])
55
56rank_indices = reward.rank(reward_tokenizer, candidates)
57sorted_candidates = sorted(zip(rank_indices, candidates), key=lambda x: x[0])
58
59## 打印排序后的候选
60# for i, (rank_index, candidate) in enumerate(sorted_candidates):
61# print(f"------------Rank {i}------------: \n{candidate[-1]['content']}")
62
63# 打印最佳回答
64best_response = sorted_candidates[0][1][-1]['content']
65print(best_response)@misc{cai2024internlm2,
title={InternLM2 Technical Report},
author={Zheng Cai and Maosong Cao and Haojiong Chen and Kai Chen and Keyu Chen and Xin Chen and Xun Chen and Zehui Chen and Zhi Chen and Pei Chu and Xiaoyi Dong and Haodong Duan and Qi Fan and Zhaoye Fei and Yang Gao and Jiaye Ge and Chenya Gu and Yuzhe Gu and Tao Gui and Aijia Guo and Qipeng Guo and Conghui He and Yingfan Hu and Ting Huang and Tao Jiang and Penglong Jiao and Zhenjiang Jin and Zhikai Lei and Jiaxing Li and Jingwen Li and Linyang Li and Shuaibin Li and Wei Li and Yining Li and Hongwei Liu and Jiangning Liu and Jiawei Hong and Kaiwen Liu and Kuikun Liu and Xiaoran Liu and Chengqi Lv and Haijun Lv and Kai Lv and Li Ma and Runyuan Ma and Zerun Ma and Wenchang Ning and Linke Ouyang and Jiantao Qiu and Yuan Qu and Fukai Shang and Yunfan Shao and Demin Song and Zifan Song and Zhihao Sui and Peng Sun and Yu Sun and Huanze Tang and Bin Wang and Guoteng Wang and Jiaqi Wang and Jiayu Wang and Rui Wang and Yudong Wang and Ziyi Wang and Xingjian Wei and Qizhen Weng and Fan Wu and Yingtong Xiong and Chao Xu and Ruiliang Xu and Hang Yan and Yirong Yan and Xiaogui Yang and Haochen Ye and Huaiyuan Ying and Jia Yu and Jing Yu and Yuhang Zang and Chuyu Zhang and Li Zhang and Pan Zhang and Peng Zhang and Ruijie Zhang and Shuo Zhang and Songyang Zhang and Wenjian Zhang and Wenwei Zhang and Xingcheng Zhang and Xinyue Zhang and Hui Zhao and Qian Zhao and Xiaomeng Zhao and Fengzhe Zhou and Zaida Zhou and Jingming Zhuo and Yicheng Zou and Xipeng Qiu and Yu Qiao and Dahua Lin},
year={2024},
eprint={2403.17297},
archivePrefix={arXiv},
primaryClass={cs.CL}
}