Views
No views yet
1import re
2import torch
3from transformers import AutoTokenizer, pipeline
4
5model_path = "Vidushee/Qwen3-32B-BT-RewardModel"
6tokenizer = AutoTokenizer.from_pretrained(model_path)
7
8rm_pipe = pipeline(
9 "sentiment-analysis",
10 model=model_path,
11 device=0,
12 tokenizer=tokenizer,
13 model_kwargs={"torch_dtype": torch.bfloat16, "attn_implementation": "flash_attention_2"},
14 truncation=True,
15 max_length=12288,
16)
17
18pipe_kwargs = {
19 "return_all_scores": True,
20 "function_to_apply": "none",
21 "batch_size": 1,
22}
23
24# Format your conversation
25chat = [
26 {"role": "user", "content": "Your paper context here"},
27 {"role": "assistant", "content": "Question to score"},
28]
29
30text = tokenizer.apply_chat_template(
31 chat, tokenize=False, add_generation_prompt=False, enable_thinking=False
32)
33# Strip empty think blocks that Qwen3 inserts even with enable_thinking=False
34text = re.sub(r"<think>\s*</think>\s*", "", text)
35# Strip trailing newline so reward pools from <|im_end|>
36text = text.rstrip("\n")
37
38outputs = rm_pipe([text], **pipe_kwargs)
39reward = outputs[0][0]["score"]
40print(f"Reward: {reward}")1# Score chosen vs rejected responses
2chosen_chat = [
3 {"role": "user", "content": "Paper context..."},
4 {"role": "assistant", "content": "Good question about the paper"},
5]
6rejected_chat = [
7 {"role": "user", "content": "Paper context..."},
8 {"role": "assistant", "content": "Bad question about the paper"},
9]
10
11def format_text(messages):
12 text = tokenizer.apply_chat_template(
13 messages, tokenize=False, add_generation_prompt=False, enable_thinking=False
14 )
15 text = re.sub(r"<think>\s*</think>\s*", "", text)
16 return text.rstrip("\n")
17
18outputs = rm_pipe([format_text(chosen_chat), format_text(rejected_chat)], **pipe_kwargs)
19chosen_reward = outputs[0][0]["score"]
20rejected_reward = outputs[1][0]["score"]
21
22print(f"Chosen reward: {chosen_reward:.4f}")
23print(f"Rejected reward: {rejected_reward:.4f}")
24print(f"Chosen is better: {chosen_reward > rejected_reward}")