weqweasdas
commited on
Commit
•
e1d2459
1
Parent(s):
9a480b2
Update README.md
Browse files
README.md
CHANGED
@@ -1,4 +1,65 @@
|
|
1 |
---
|
2 |
{}
|
3 |
---
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
{}
|
3 |
---
|
4 |
+
This preference model is trained from [LLaMA3-8B-it](meta-llama/Meta-Llama-3-8B-Instruct) with the training script at [Reward Modeling](https://github.com/RLHFlow/RLHF-Reward-Modeling/tree/pm_dev/pair-pm).
|
5 |
+
|
6 |
+
The dataset is RLHFlow/pair_preference_model_dataset. It achieves Chat-98.6, Char-hard 65.8, Safety 89.6, and reasoning 94.9 in reward bench.
|
7 |
+
|
8 |
+
|
9 |
+
## Service the RM
|
10 |
+
|
11 |
+
Here is an example to use the Preference Model to rank a pair. For n>2 responses, it is recommened to use the tournament style ranking strategy to get the best response so that the complexity is linear in n.
|
12 |
+
|
13 |
+
```python
|
14 |
+
device = 0
|
15 |
+
|
16 |
+
model = AutoModelForCausalLM.from_pretrained(script_args.preference_name_or_path,
|
17 |
+
torch_dtype=torch.bfloat16, attn_implementation="flash_attention_2").cuda()
|
18 |
+
tokenizer = AutoTokenizer.from_pretrained(script_args.preference_name_or_path, use_fast=True)
|
19 |
+
tokenizer_plain = AutoTokenizer.from_pretrained(script_args.preference_name_or_path, use_fast=True)
|
20 |
+
tokenizer_plain.chat_template = "\n{% for message in messages %}{% if loop.index0 % 2 == 0 %}\n\n<turn> user\n {{ message['content'] }}{% else %}\n\n<turn> assistant\n {{ message['content'] }}{% endif %}{% endfor %}\n\n\n"
|
21 |
+
|
22 |
+
prompt_template = "[CONTEXT] {context} [RESPONSE A] {response_A} [RESPONSE B] {response_B} \n"
|
23 |
+
token_id_A = tokenizer.encode("A", add_special_tokens=False)
|
24 |
+
token_id_B = tokenizer.encode("B", add_special_tokens=False)
|
25 |
+
assert len(token_id_A) == 1 and len(token_id_B) == 1
|
26 |
+
token_id_A = token_id_A[0]
|
27 |
+
token_id_B = token_id_B[0]
|
28 |
+
temperature = 1.0
|
29 |
+
|
30 |
+
|
31 |
+
model.eval()
|
32 |
+
prompt = "AAAA"
|
33 |
+
response_chosen = "BBBB"
|
34 |
+
response_rejected = "CCCC"
|
35 |
+
|
36 |
+
instruction = [{"role": "user", "content": prompt}]
|
37 |
+
context = tokenizer_plain.apply_chat_template(instruction, tokenize=False)
|
38 |
+
responses = [response_chosen, response_rejected]
|
39 |
+
probs_chosen = []
|
40 |
+
|
41 |
+
for chosen_position in [0, 1]:
|
42 |
+
# we swap order to mitigate position bias
|
43 |
+
response_A = responses[chosen_position]
|
44 |
+
response_B = responses[1 - chosen_position]
|
45 |
+
prompt = prompt_template.format(context=context, response_A=response_A, response_B=response_B)
|
46 |
+
message = [
|
47 |
+
{"role": "user", "content": prompt},
|
48 |
+
]
|
49 |
+
|
50 |
+
input_ids = tokenizer.encode(tokenizer.apply_chat_template(message, tokenize=False).replace(tokenizer.bos_token, ""), return_tensors='pt', add_special_tokens=False).cuda()
|
51 |
+
|
52 |
+
with torch.no_grad():
|
53 |
+
output = model(input_ids)
|
54 |
+
logit_A = output.logits[0, -1, token_id_A].item()
|
55 |
+
logit_B = output.logits[0, -1, token_id_B].item()
|
56 |
+
# take softmax to get the probability; using numpy
|
57 |
+
Z = np.exp(logit_A / temperature) + np.exp(logit_B / temperature)
|
58 |
+
logit_chosen = [logit_A, logit_B][chosen_position]
|
59 |
+
prob_chosen = np.exp(logit_chosen / temperature) / Z
|
60 |
+
probs_chosen.append(prob_chosen)
|
61 |
+
|
62 |
+
avg_prob_chosen = np.mean(probs_chosen)
|
63 |
+
correct = 0.5 if avg_prob_chosen == 0.5 else float(avg_prob_chosen > 0.5)
|
64 |
+
print(correct)
|
65 |
+
```
|