Views
No views yet
1device = 0
2
3model = AutoModelForCausalLM.from_pretrained(script_args.preference_name_or_path,
4 torch_dtype=torch.bfloat16, attn_implementation="flash_attention_2").cuda()
5tokenizer = AutoTokenizer.from_pretrained(script_args.preference_name_or_path, use_fast=True)
6tokenizer_plain = AutoTokenizer.from_pretrained(script_args.preference_name_or_path, use_fast=True)
7tokenizer_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"
8
9prompt_template = "[CONTEXT] {context} [RESPONSE A] {response_A} [RESPONSE B] {response_B} \n"
10token_id_A = tokenizer.encode("A", add_special_tokens=False)
11token_id_B = tokenizer.encode("B", add_special_tokens=False)
12assert len(token_id_A) == 1 and len(token_id_B) == 1
13token_id_A = token_id_A[0]
14token_id_B = token_id_B[0]
15temperature = 1.0
16
17
18model.eval()
19response_chosen = "BBBB"
20response_rejected = "CCCC"
21
22## We can also handle multi-turn conversation.
23instruction = [{"role": "user", "content": ...},
24{"role": "assistant", "content": ...},
25{"role": "user", "content": ...},
26]
27context = tokenizer_plain.apply_chat_template(instruction, tokenize=False)
28responses = [response_chosen, response_rejected]
29probs_chosen = []
30
31for chosen_position in [0, 1]:
32 # we swap order to mitigate position bias
33 response_A = responses[chosen_position]
34 response_B = responses[1 - chosen_position]
35 prompt = prompt_template.format(context=context, response_A=response_A, response_B=response_B)
36 message = [
37 {"role": "user", "content": prompt},
38 ]
39
40 input_ids = tokenizer.encode(tokenizer.apply_chat_template(message, tokenize=False).replace(tokenizer.bos_token, ""), return_tensors='pt', add_special_tokens=False).cuda()
41
42 with torch.no_grad():
43 output = model(input_ids)
44 logit_A = output.logits[0, -1, token_id_A].item()
45 logit_B = output.logits[0, -1, token_id_B].item()
46 # take softmax to get the probability; using numpy
47 Z = np.exp(logit_A / temperature) + np.exp(logit_B / temperature)
48 logit_chosen = [logit_A, logit_B][chosen_position]
49 prob_chosen = np.exp(logit_chosen / temperature) / Z
50 probs_chosen.append(prob_chosen)
51
52avg_prob_chosen = np.mean(probs_chosen)
53correct = 0.5 if avg_prob_chosen == 0.5 else float(avg_prob_chosen > 0.5)
54print(correct)@misc{rlhflow,
title={RLHF Workflow: From Reward Modeling to Online RLHF},
author={Hanze Dong and Wei Xiong and Bo Pang and Haoxiang Wang and Han Zhao and Yingbo Zhou and Nan Jiang and Doyen Sahoo and Caiming Xiong and Tong Zhang},
year={2024},
eprint={2405.07863},
archivePrefix={arXiv},
primaryClass={cs.LG}
}@article{zhao2023slic,
title={Slic-hf: Sequence likelihood calibration with human feedback},
author={Zhao, Yao and Joshi, Rishabh and Liu, Tianqi and Khalman, Misha and Saleh, Mohammad and Liu, Peter J},
journal={arXiv preprint arXiv:2305.10425},
year={2023}
}