Views
No views yet
<Response i + 1> {response}1import os
2os.environ["CUDA_VISIBLE_DEVICES"] = "0"
3from llm_blender.pair_ranker.pairrm import DebertaV2PairRM
4from transformers import AutoTokenizer
5from typing import List
6pairrm = DebertaV2PairRM.from_pretrained("maywell/Better-PairRM", device_map="cuda:0").eval()
7tokenizer = AutoTokenizer.from_pretrained("maywell/Better-PairRM")
8source_prefix = "<|source|>"
9cand1_prefix = "<|candidate1|>"
10cand2_prefix = "<|candidate2|>"
11inputs = ["hello!", "I love you!"]
12candidates_A = ["hi!", "I hate you!"]
13candidates_B = ["f**k off!", "I love you, too!"]
14def tokenize_pair(sources:List[str], candidate1s:List[str], candidate2s:List[str], source_max_length=2030, candidate_max_length=670):
15 ids = []
16 assert len(sources) == len(candidate1s) == len(candidate2s)
17 max_length = source_max_length + 2 * candidate_max_length
18 for i in range(len(sources)):
19 source_ids = tokenizer.encode(source_prefix + sources[i], max_length=source_max_length, truncation=True)
20 candidate_max_length = (max_length - len(source_ids)) // 2
21 candidate1_ids = tokenizer.encode(cand1_prefix + candidate1s[i], max_length=candidate_max_length, truncation=True)
22 candidate2_ids = tokenizer.encode(cand2_prefix + candidate2s[i], max_length=candidate_max_length, truncation=True)
23 ids.append(source_ids + candidate1_ids + candidate2_ids)
24 encodings = tokenizer.pad({"input_ids": ids}, return_tensors="pt", padding="max_length", max_length=max_length)
25 return encodings
26
27encodings = tokenize_pair(inputs, candidates_A, candidates_B)
28encodings = {k:v.to(pairrm.device) for k,v in encodings.items()}
29outputs = pairrm(**encodings)
30logits = outputs.logits.tolist()
31comparison_results = outputs.logits > 0
32print(logits)
33print(comparison_results)1import jinja2
2from transformers import AutoTokenizer
3
4tokenizer = AutoTokenizer.from_pretrained("microsoft/deberta-v3-large")
5
6def truncate_texts(text, max_length, truncate_side):
7 tokenizer.truncation_side = truncate_side
8 tokens = tokenizer.encode(text, add_special_tokens=False, max_length=max_length)
9 truncated_text = tokenizer.decode(tokens, skip_special_tokens=True)
10 return truncated_text
11
12MY_JINJA_TEMPLATE = """{% for message in messages -%}
13{% if message['role'] == 'user' -%}
14USER: {{ message['content']|trim -}}
15{% if not loop.last -%}
16
17
18{% endif %}
19{% elif message['role'] == 'assistant' -%}
20ASSISTANT: {{ message['content']|trim -}}
21{% if not loop.last -%}
22
23
24{% endif %}
25{% elif message['role'] == 'user_context' -%}
26USER: {{ message['content']|trim -}}
27{% if not loop.last -%}
28
29
30{% endif %}
31{% elif message['role'] == 'system' -%}
32SYSTEM MESSAGE: {{ message['content']|trim -}}
33{% if not loop.last -%}
34
35
36{% endif %}
37{% endif %}
38{% endfor -%}
39{% if add_generation_prompt and messages[-1]['role'] != 'assistant' -%}
40ASSISTANT: {% endif -%}"""
41
42my_jinja2_env = jinja2.Environment()
43my_jinja2_template = my_jinja2_env.from_string(MY_JINJA_TEMPLATE)
44
45def tokenize_conv_pair(convAs: List[str], convBs: List[str]):
46
47 # check conversations correctness
48 assert len(convAs) == len(convBs), "Number of conversations must be the same"
49 for c_a, c_b in zip(convAs, convBs):
50 assert len(c_a) == len(c_b), "Number of turns in each conversation must be the same"
51 assert all([c_a[i]['content'] == c_b[i]['content'] for i in range(0, len(c_a), 2)]), "USER turns must be the same"
52
53 inputs = [
54 truncate_texts(my_jinja2_template.render(messages=x[:-1], add_generation_prompt=True), 2030, "left") for x in convAs
55 ]
56 cand1_texts = [
57 truncate_texts(x[-1]['content'], 670, "right") for x in convAs
58 ]
59 cand2_texts = [
60 truncate_texts(x[-1]['content'], 670, "right") for x in convBs
61 ]
62 encodings = tokenize_pair(inputs, cand1_texts, cand2_texts)
63 return encodings| PairRanker type | Source max length | Candidate max length | Total max length |
|---|---|---|---|
| pair-ranker | 128 | 128 | 384 |
| PairRM | 1224 | 412 | 2048 |
| Better-PairRM (This model) | 2030 | 670 | 3370 |
| Metric | llm-blender/PairRM-hf | maywell/Better-PairRM |
|---|---|---|
| model | llm-blender/PairRM-hf | maywell/Better-PairRM |
| model_type | Custom Classifier | Custom Classifier |
| alpacaeval-length | 0.758 | 0.863 |
| alpacaeval-hard | 0.979 | 1.000 |
| alpacaeval-easy | 0.970 | 0.990 |
| donotanswer | 0.360 | 0.522 |
| hep-cpp | 0.628 | 0.646 |
| hep-go | 0.689 | 0.713 |
| hep-java | 0.628 | 0.713 |
| hep-js | 0.604 | 0.707 |
| hep-python | 0.646 | 0.713 |
| hep-rust | 0.652 | 0.726 |
| llmbar-adver-GPTInst | 0.304 | 0.141 |
| llmbar-adver-GPTOut | 0.596 | 0.447 |
| llmbar-adver-manual | 0.500 | 0.261 |
| llmbar-adver-neighbor | 0.433 | 0.276 |
| llmbar-natural | 0.800 | 0.720 |
| math-prm | 0.333 | 0.295 |
| mt-bench-hard | 0.649 | 0.703 |
| mt-bench-med | 0.900 | 1.000 |
| mt-bench-easy | 0.964 | 0.929 |
| refusals-dangerous | 0.080 | 0.730 |
| refusals-offensive | 0.010 | 0.940 |
| xstest-should-refuse | 0.370 | 0.968 |
| xstest-should-respond | 0.952 | 0.876 |
| average | 0.600 | 0.690 |
Note - llmbar test score is bit weird across all models on Reward-Bench
@inproceedings{llm-blender-2023,
title = "LLM-Blender: Ensembling Large Language Models with Pairwise Comparison and Generative Fusion",
author = "Jiang, Dongfu and Ren, Xiang and Lin, Bill Yuchen",
booktitle = "Proceedings of the 61th Annual Meeting of the Association for Computational Linguistics (ACL 2023)",
year = "2023"
}