Views
No views yet


1conda create --name xtuner-env python=3.10 -y
2conda activate xtuner-envpip install 'xtuner[deepspeed]'==0.2.0pip install 'git+https://github.com/InternLM/xtuner.git@main#egg=xtuner[deepspeed]'1data = [
2 {
3 "prompt": [{"role": "user", "content": "What is the capital of China?"}],
4 "reference": [{"role": "assistant", "content": "Beijing."}],
5 "output": [{"role": "assistant", "content": "Beijing."}]
6 },
7 {
8 "prompt": [{"role": "user", "content": "What is the capital of China?"}],
9 "reference": [{"role": "assistant", "content": "Beijing."}],
10 "output": [{"role": "assistant", "content": "Shanghai."}]
11 }
12]1from transformers import AutoModel, AutoTokenizer
2from xtuner.utils import RewardModelClient
3
4model_name = 'internlm/POLAR-1_8B'
5
6model = AutoModel.from_pretrained(
7 model_name,
8 device_map="cuda",
9 trust_remote_code=True
10)
11tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
12
13client = RewardModelClient(model_name)
14encoded_data = client.encode(data)
15batch = tokenizer(encoded_data, return_tensors='pt', padding=True).to('cuda')
16outputs = model(**batch)
17rewards = outputs[0].squeeze(-1).cpu().tolist()
18print(rewards)lmdeploy serve api_server internlm/POLAR-1_8B --backend pytorch --server-port 300001from xtuner.utils import RewardModelClient
2
3client = RewardModelClient("internlm/POLAR-1_8B",
4 server_type="lmdeploy",
5 server_address="127.0.0.1:30000")
6
7# Request rewards directly
8rewards = client(data)
9print(rewards)
10
11# First encode data and then get rewards via the request function.
12encoded_data = client.encode(data)
13rewards = client.lmdeploy_request_reward(encoded_data)
14print(rewards)python3 -m sglang.launch_server --model internlm/POLAR-1_8B --trust-remote-code --is-embedding --dp 4 --tp 2 --mem-fraction-static 0.9 --port 300001from xtuner.utils import RewardModelClient
2
3client = RewardModelClient("internlm/POLAR-1_8B",
4 server_type="sglang",
5 server_address="127.0.0.1:30000")
6
7# Request rewards directly
8rewards = client(data)
9print(rewards)
10
11# First encode data and then get rewards via the request function.
12encoded_data = client.encode(data)
13rewards = client.sglang_request_reward(encoded_data)
14print(rewards)vllm serve internlm/POLAR-1_8B --task=reward --trust-remote-code --tensor-parallel-size=2 --port 300001from xtuner.utils import RewardModelClient
2
3client = RewardModelClient("internlm/POLAR-1_8B",
4 server_type="vllm",
5 server_address="127.0.0.1:30000")
6
7# Request rewards directly
8rewards = client(data)
9print(rewards)
10
11# First encode data and then get rewards via the request function.
12encoded_data = client.encode(data)
13rewards = client.vllm_request_reward(encoded_data)
14print(rewards)train.jsonl file, formatted as follows:1{
2 "prompt": [{"role": "user", "content": "What is the capital of China?"}],
3 "reference": [{"role": "assistant", "content": "Beijing."}],
4 "chosen": [{"role": "assistant", "content": "Beijing."}],
5 "rejected": [{"role": "assistant", "content": "Shanghai."}]
6}xtuner train ${CONFIG_FILE_PATH}1# On a single GPU
2xtuner train /path/to/POLAR_1_8B_full_varlenattn_custom_dataset.py --deepspeed deepspeed_zero2
3
4# On multiple GPUs
5NPROC_PER_NODE=${GPU_NUM} xtuner train /path/to/POLAR_1_8B_full_varlenattn_custom_dataset.py --deepspeed deepspeed_zero2--deepspeed means using DeepSpeed to optimize the training. Xtuner comes with several integrated strategies including ZeRO-1, ZeRO-2, and ZeRO-3. If you wish to disable this feature, simply remove this argument.xtuner convert pth_to_hf ${CONFIG_FILE_PATH} ${PTH} ${SAVE_PATH}1from xtuner.utils import RewardModelClient
2
3prompt = "How many 'r's are there in the word 'strawberry'?"
4reference = "There are 3 'r's in the word 'strawberry'. Here's how we can count them: 's', 't', 'r', 'a', 'w', 'b', 'e', 'r', 'r', 'y'. So, the answer is 3."
5outputs = [
6 # Same as the reference response.
7 "There are 3 'r's in the word 'strawberry'. Here's how we can count them: 's', 't', 'r', 'a', 'w', 'b', 'e', 'r', 'r', 'y'. So, the answer is 3.",
8 # Correct answer with correct thoughts.
9 "Let's count the 'r's in 'strawberry': 's', 't', 'r', 'a', 'w', 'b', 'e', 'r', 'r', 'y'. There are three 'r's, so the answer is three.",
10 # Wrong answer with wrong thoughts.
11 "Let's count the 'r's in 'strawberry': 's', 't', 'r', 'a', 'w', 'b', 'e', 'r', 'r', 'y'. There are two 'r's, so the answer is two.",
12 # Wrong answer with correct thoughts.
13 "Let's count the 'r's in 'strawberry': 's', 't', 'r', 'a', 'w', 'b', 'e', 'r', 'r', 'y'. There are three 'r's, so the answer is two.",
14 # Correct answer with wrong thoughts.
15 "Let's count the 'r's in 'strawberry': 's', 't', 'r', 'a', 'w', 'b', 'e', 'r', 'r', 'y'. There are two 'r's, so the answer is three.",
16 # Correct answer without thoughts.
17 "There are 3 'r's in the word 'strawberry'.",
18 # Wrong answer without thoughts.
19 "There are 2 'r's in the word 'strawberry'.",
20]
21data = [{"prompt": prompt, "reference": reference, "output": output} for output in outputs]
22
23client = RewardModelClient("internlm/POLAR-7B", server_type="sglang", server_address="127.0.0.1:30000")
24rewards = client(data)
25
26sorted_res = sorted(zip(outputs, rewards), key=lambda x: x[1], reverse=True)
27
28for output, reward in sorted_res:
29 print(f"Output: {output}
30Reward: {reward}
31")1Output: There are 3 'r's in the word 'strawberry'. Here's how we can count them: 's', 't', 'r', 'a', 'w', 'b', 'e', 'r', 'r', 'y'. So, the answer is 3.
2Reward: 0.054595947265625
3
4Output: Let's count the 'r's in 'strawberry': 's', 't', 'r', 'a', 'w', 'b', 'e', 'r', 'r', 'y'. There are three 'r's, so the answer is three.
5Reward: -2.005859375
6
7Output: There are 3 'r's in the word 'strawberry'.
8Reward: -6.70703125
9
10Output: Let's count the 'r's in 'strawberry': 's', 't', 'r', 'a', 'w', 'b', 'e', 'r', 'r', 'y'. There are two 'r's, so the answer is three.
11Reward: -7.10546875
12
13Output: Let's count the 'r's in 'strawberry': 's', 't', 'r', 'a', 'w', 'b', 'e', 'r', 'r', 'y'. There are three 'r's, so the answer is two.
14Reward: -7.1328125
15
16Output: Let's count the 'r's in 'strawberry': 's', 't', 'r', 'a', 'w', 'b', 'e', 'r', 'r', 'y'. There are two 'r's, so the answer is two.
17Reward: -8.46875
18
19Output: There are 2 'r's in the word 'strawberry'.
20Reward: -10.82031251from xtuner.utils import RewardModelClient
2
3prompt = "Summarize the first book of Frank Herbert’s Dune in one witty short sentence."
4reference = "Royal teen discovers that life’s a beach—minus the ocean, plus spice, giant sandworms and deadly politics."
5outputs = [
6 # Same as the reference response.
7 "Royal teen discovers that life’s a beach—minus the ocean, plus spice, giant sandworms and deadly politics.",
8 # Closely resembles the reference response but includes factual errors.
9 "Royal teen discovers that life’s a beach—minus the ocean, plus magic, dark wizards and deadly politics.",
10 # A distinct yet concise and witty summary that draws analogies from other dramas—markedly different from the reference response.
11 "Young noble’s move to desert planet turns into galactic Game of Thrones with fewer dragons, more worms.",
12 # A concise summary, but lacking wit—fails to meet the requirement.
13 "A noble family’s fall sparks a young heir’s rise as a leader on a harsh desert planet governed by prophecy and survival.",
14 # A witty summary, but overly long—fails to meet the requirement.
15 "Paul Atreides loses his father, gains prophetic powers, learns to ride a sandworm, leads a holy war, and discovers that being the chosen one comes with a lot of blood, sand, and questionable decisions.",
16 # A concise and witty summary that draws from multiple Dune books rather than just the first—fails to follow the instruction.
17 "Boy gets planet, becomes god, loses soul — family drama ensues across galaxies."
18]
19data = [{"prompt": prompt, "reference": reference, "output": output} for output in outputs]
20
21client = RewardModelClient("internlm/POLAR-7B", server_type="sglang", server_address="127.0.0.1:30000")
22rewards = client(data)
23
24sorted_res = sorted(zip(outputs, rewards), key=lambda x: x[1], reverse=True)
25
26for output, reward in sorted_res:
27 print(f"Output: {output}
28Reward: {reward}
29")1Output: Royal teen discovers that life’s a beach—minus the ocean, plus spice, giant sandworms and deadly politics.
2Reward: 0.466552734375
3
4Output: Young noble’s move to desert planet turns into galactic Game of Thrones with fewer dragons, more worms.
5Reward: -6.91796875
6
7Output: Royal teen discovers that life’s a beach—minus the ocean, plus magic, dark wizards and deadly politics.
8Reward: -7.70703125
9
10Output: Paul Atreides loses his father, gains prophetic powers, learns to ride a sandworm, leads a holy war, and discovers that being the chosen one comes with a lot of blood, sand, and questionable decisions.
11Reward: -8.4296875
12
13Output: A noble family’s fall sparks a young heir’s rise as a leader on a harsh desert planet governed by prophecy and survival.
14Reward: -8.6484375
15
16Output: Boy gets planet, becomes god, loses soul — family drama ensues across galaxies.
17Reward: -10.359375@article{dou2025pretrained,
title={Pre-Trained Policy Discriminators are General Reward Models},
author={Dou, Shihan and Liu, Shichun and Yang, Yuming and Zou, Yicheng and Zhou, Yunhua and Xing, Shuhao and Huang, Chenhao and Ge, Qiming and Song, Demin and Lv, Haijun and others},
journal={arXiv preprint arXiv:2507.05197},
year={2025}
}