Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: llama3
|
| 3 |
+
base_model: RLHFlow/pair-preference-model-LLaMA3-8B
|
| 4 |
+
library_name: transformers
|
| 5 |
+
pipeline_tag: text-generation
|
| 6 |
+
tags:
|
| 7 |
+
- llama
|
| 8 |
+
- conversational
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# pair-preference-model-LLaMA3-8B-GGUF
|
| 12 |
+
This is quantized version of [RLHFlow/pair-preference-model-LLaMA3-8B](https://huggingface.co/RLHFlow/pair-preference-model-LLaMA3-8B) created using llama.cpp
|
| 13 |
+
|
| 14 |
+
# Model Description
|
| 15 |
+
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).
|
| 16 |
+
|
| 17 |
+
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.
|
| 18 |
+
|
| 19 |
+
See our paper [RLHF Workflow: From Reward Modeling to Online RLHF](https://arxiv.org/abs/2405.07863) for more details of this model.
|
| 20 |
+
|
| 21 |
+
## Service the RM
|
| 22 |
+
|
| 23 |
+
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.
|
| 24 |
+
|
| 25 |
+
```python
|
| 26 |
+
device = 0
|
| 27 |
+
|
| 28 |
+
model = AutoModelForCausalLM.from_pretrained(script_args.preference_name_or_path,
|
| 29 |
+
torch_dtype=torch.bfloat16, attn_implementation="flash_attention_2").cuda()
|
| 30 |
+
tokenizer = AutoTokenizer.from_pretrained(script_args.preference_name_or_path, use_fast=True)
|
| 31 |
+
tokenizer_plain = AutoTokenizer.from_pretrained(script_args.preference_name_or_path, use_fast=True)
|
| 32 |
+
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"
|
| 33 |
+
|
| 34 |
+
prompt_template = "[CONTEXT] {context} [RESPONSE A] {response_A} [RESPONSE B] {response_B} \n"
|
| 35 |
+
token_id_A = tokenizer.encode("A", add_special_tokens=False)
|
| 36 |
+
token_id_B = tokenizer.encode("B", add_special_tokens=False)
|
| 37 |
+
assert len(token_id_A) == 1 and len(token_id_B) == 1
|
| 38 |
+
token_id_A = token_id_A[0]
|
| 39 |
+
token_id_B = token_id_B[0]
|
| 40 |
+
temperature = 1.0
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
model.eval()
|
| 44 |
+
response_chosen = "BBBB"
|
| 45 |
+
response_rejected = "CCCC"
|
| 46 |
+
|
| 47 |
+
## We can also handle multi-turn conversation.
|
| 48 |
+
instruction = [{"role": "user", "content": ...},
|
| 49 |
+
{"role": "assistant", "content": ...},
|
| 50 |
+
{"role": "user", "content": ...},
|
| 51 |
+
]
|
| 52 |
+
context = tokenizer_plain.apply_chat_template(instruction, tokenize=False)
|
| 53 |
+
responses = [response_chosen, response_rejected]
|
| 54 |
+
probs_chosen = []
|
| 55 |
+
|
| 56 |
+
for chosen_position in [0, 1]:
|
| 57 |
+
# we swap order to mitigate position bias
|
| 58 |
+
response_A = responses[chosen_position]
|
| 59 |
+
response_B = responses[1 - chosen_position]
|
| 60 |
+
prompt = prompt_template.format(context=context, response_A=response_A, response_B=response_B)
|
| 61 |
+
message = [
|
| 62 |
+
{"role": "user", "content": prompt},
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
input_ids = tokenizer.encode(tokenizer.apply_chat_template(message, tokenize=False).replace(tokenizer.bos_token, ""), return_tensors='pt', add_special_tokens=False).cuda()
|
| 66 |
+
|
| 67 |
+
with torch.no_grad():
|
| 68 |
+
output = model(input_ids)
|
| 69 |
+
logit_A = output.logits[0, -1, token_id_A].item()
|
| 70 |
+
logit_B = output.logits[0, -1, token_id_B].item()
|
| 71 |
+
# take softmax to get the probability; using numpy
|
| 72 |
+
Z = np.exp(logit_A / temperature) + np.exp(logit_B / temperature)
|
| 73 |
+
logit_chosen = [logit_A, logit_B][chosen_position]
|
| 74 |
+
prob_chosen = np.exp(logit_chosen / temperature) / Z
|
| 75 |
+
probs_chosen.append(prob_chosen)
|
| 76 |
+
|
| 77 |
+
avg_prob_chosen = np.mean(probs_chosen)
|
| 78 |
+
correct = 0.5 if avg_prob_chosen == 0.5 else float(avg_prob_chosen > 0.5)
|
| 79 |
+
print(correct)
|
| 80 |
+
```
|