Views
No views yet
1python inference.py \
2 --embed_pt data/emb/prism/V2.pt \
3 --meta_json data/emb/prism/V2.json \
4 --ckpt path/to/checkpoint.pt \
5 --dataset PRISM \
6 --seen_train_limit -1 \
7 --unseen_train_limit -1 \
8 --hidden_layers 2 \
9 --inner_lr 1e-3 \
10 --eval_inner_epochs 1 \
11 --val_ratio 0.9 \
12 --score_threshold -1 \
13 --seed 42 \
14 --device cuda:0shared_weight only),1import torch
2from copy import deepcopy
3from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
5from utils import bt_loss
6from train import MRM
7from inference import load_ckpt_into_model
8
9
10@torch.no_grad()
11def encode_pairs(model, tokenizer, pairs, device="cuda"):
12 model.eval()
13 ch, rj = [], []
14 for ex in pairs:
15 conv = ex["prompt"]
16 for key, buf in [("chosen", ch), ("rejected", rj)]:
17 ids = tokenizer.apply_chat_template(
18 conv + [{"role": "assistant", "content": ex[key]}],
19 tokenize=True, return_tensors="pt"
20 ).to(device)
21 out = model(ids, output_hidden_states=True)
22 buf.append(out.hidden_states[-1][0, -1].float().cpu())
23 return torch.stack(ch), torch.stack(rj)
24
25
26def adapt_single_user(base_model, support_ch, support_rj, inner_lr=1e-3, inner_epochs=5, device="cuda"):
27 model = deepcopy(base_model).to(device).train()
28 opt = torch.optim.Adam([model.shared_weight], lr=inner_lr)
29 support_ch, support_rj = support_ch.to(device), support_rj.to(device)
30 for _ in range(inner_epochs):
31 opt.zero_grad()
32 loss = bt_loss(model(support_ch), model(support_rj))
33 loss.backward()
34 opt.step()
35 return model.eval()
36
37
38@torch.no_grad()
39def infer_on_pairs(model, ch, rj, device="cuda"):
40 return model(ch.to(device)), model(rj.to(device))
41
42
43device = "cuda" if torch.cuda.is_available() else "cpu"
44
45MODEL_PATH = "Skywork/Skywork-Reward-V2-Llama-3.1-8B"
46tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
47llm = AutoModelForSequenceClassification.from_pretrained(
48 MODEL_PATH, num_labels=1, torch_dtype=torch.bfloat16, device_map=device
49)
50
51CKPT_PATH = "ckpt/model.pt"
52mrm = MRM(in_dim=4096, hidden_sizes=[2], use_bias=False)
53load_ckpt_into_model(mrm, CKPT_PATH, device)
54
55support_pairs = [
56 {
57 "prompt": [{"role": "user", "content": "TL;DR this post: I tried waking up at 5am for a month and tracked my productivity."}],
58 "chosen": "Waking up early helped at first, but long-term productivity depended more on sleep quality than wake-up time.",
59 "rejected": "The post is about waking up early and productivity.",
60 },
61 {
62 "prompt": [{"role": "user", "content": "Summarize the main point: I switched from iPhone to Android after 10 years."}],
63 "chosen": "The author values customization and battery life more than ecosystem lock-in, which motivated the switch.",
64 "rejected": "The author bought a new phone.",
65 },
66]
67
68sup_ch, sup_rj = encode_pairs(llm, tokenizer, support_pairs, device)
69user_mrm = adapt_single_user(mrm, sup_ch, sup_rj, device=device)
70
71test_pairs = [
72 {
73 "prompt": [{"role": "user", "content": "TL;DR: I quit my job to freelance and here is what I learned in 6 months."}],
74 "chosen": "Freelancing offers flexibility but requires strong self-discipline and financial planning to be sustainable.",
75 "rejected": "The author talks about quitting a job and freelancing.",
76 }
77]
78
79test_ch, test_rj = encode_pairs(llm, tokenizer, test_pairs, device)
80s_ch, s_rj = infer_on_pairs(user_mrm, test_ch, test_rj, device)
81
82print("reward(chosen) =", s_ch.tolist())
83print("reward(rejected)=", s_rj.tolist())
841@inproceedings{cai2026MRM,
2 title={One Adapts to Any: Meta Reward Modeling for Personalized LLM Alignment},
3 author={Hongru Cai and Yongqi Li and Tiezheng Yu and Fengbin Zhu and Wenjie Wang and Fuli Feng and Wenjie Li},
4 booktitle={Proceedings of the 49th International ACM SIGIR Conference on Research and Development in Information Retrieval},
5 series={SIGIR '26},
6 year={2026}
7}