模型概述
模型特點
模型能力
使用案例
🚀 大語言模型的成對獎勵模型(PairRM)來自LLM-Blender
Pairwise Reward Model (PairRM) 是一種用於大語言模型(LLM)的評估工具,它以指令和一對輸出候選作為輸入,為每個候選輸出打分,以衡量它們的相對質量。該模型可以用於對候選輸出列表進行排序,也可以作為LLM評估器,在本地環境中高效評估LLM的質量。此外,PairRM還可以通過 best-of-n sampling
來增強解碼效果,以及用於進一步調整指令微調的LLM。
項目鏈接
🚀 快速開始
新聞
簡介
成對獎勵模型(PairRM)將指令和一對輸出候選作為輸入,併為每個候選輸出提供一個分數,以衡量它們的相對質量。PairRM可用於對候選輸出列表進行(重新)排序,因此可以用作LLM評估器,在本地環境中高效評估LLM的質量。PairRM還可用於通過 best-of-n sampling
(即對N個採樣輸出進行重新排序)來增強解碼效果。此外,還可以使用PairRM通過基於人類反饋的強化學習(RLHF)方法進一步對齊指令微調的LLM。
與分別對每個候選輸出進行編碼和評分的其他獎勵模型不同,PairRM會同時處理一對候選輸出,並進行並排比較,以識別它們之間的細微差異。此外,PairRM基於microsoft/deberta-v3-large
,因此非常高效,模型大小僅為0.4B。我們在六個不同的人類偏好數據集上訓練了PairRM(更多信息請見此處)。
PairRM是LLM-Blender項目的一部分(ACL 2023)。請參閱我們的論文以瞭解更多信息。
安裝
- 首先安裝
llm-blender
pip install git+https://github.com/yuchenlin/LLM-Blender.git
- 然後加載PairRM:
import llm_blender
blender = llm_blender.Blender()
blender.loadranker("llm-blender/PairRM") # 加載PairRM
使用示例
基礎用法
# 用例1:根據指令比較/排序輸出候選
# 對候選響應列表進行排序
inputs = ["hello, how are you!", "I love you!"]
candidates_texts = [["get out!", "hi! I am fine, thanks!", "bye!"],
["I love you too!", "I hate you!", "Thanks! You're a good guy!"]]
ranks = blender.rank(inputs, candidates_texts, return_scores=False, batch_size=1)
# ranks 是一個排名列表
# ranks[i][j] 表示輸入 i 的候選 j 的排名
"""
ranks -->
array([[3, 1, 2], # 這意味著 "hi! I am fine, thanks!" 排名第1,"bye" 排名第2,"get out!" 排名第3。
[1, 3, 2]], # 這意味著 "I love you too"! 排名第1,"I hate you!" 排名第3。
dtype=int32)
"""
高級用法
# 直接比較兩個候選響應
inputs = ["hello!", "I love you!"]
candidates_A = ["hi!", "I hate you!"]
candidates_B = ["f**k off!", "I love you, too!"]
comparison_results = blender.compare(inputs, candidates_A, candidates_B)
# comparison_results 是一個布爾值列表,其中 comparison_results[i] 表示
# 對於輸入 i,候選 A[i] 是否優於候選 B[i]
# 示例: comparison_results[0] --> True
# 比較兩個多輪對話
conv1 = [
{
"content": "hello",
"role": "USER"
},
{
"content": "[assistant1‘s response 1]",
"role": "ASSISTANT"
},
...
]
conv2 = [
{
"content": "hello",
"role": "USER"
},
{
"content": "[assistant2's response 1]",
"role": "ASSISTANT"
},
...
]
comparison_results = blender.compare_conversations([conv1], [conv2])
# comparison_results 是一個布爾值列表,其中每個元素表示 conv1 中的所有響應是否整體優於 conv2
# 用例2:Best-of-n 採樣(解碼增強)
# Best-of-n 採樣,也稱為拒絕採樣,是一種通過選擇獎勵模型排名最高的響應來提高響應質量的策略
# (更多信息請參閱 [OpenAI WebGPT 第3.2節](https://arxiv.org/pdf/2112.09332.pdf) 和 [OpenAI 博客](https://openai.com/research/measuring-goodharts-law))。
# 使用 PairRM 進行 Best-of-n 採樣是一種非常簡單的方法,只需對推理代碼進行少量更改即可改進您的 LLM:
# 加載模型
import llm_blender
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-beta")
model = AutoModelForCausalLM.from_pretrained("HuggingFaceH4/zephyr-7b-beta", device_map="auto")
system_message = {"role": "system", "content": "You are a friendly chatbot."}
# 格式化輸入
inputs = ["can you tell me a joke about OpenAI?"]
messages = [[system_message, {"role": "user", "content": _input}] for _input in inputs]
prompts = [tokenizer.apply_chat_template(m, tokenize=False, add_generation_prompt=True) for m in messages]
# 傳統生成方法
input_ids = tokenizer(prompts[0], return_tensors="pt").input_ids
sampled_outputs = model.generate(input_ids, do_sample=True, top_k=50, top_p=0.95, num_return_sequences=1)
print(tokenizer.decode(sampled_outputs[0][len(input_ids[0]):], skip_special_tokens=False))
# --> 輸出可能是一個糟糕的情況,例如非常短的輸出,例如 `Sure`
# 使用 PairRM 進行 best-of-n 採樣
blender = llm_blender.Blender()
blender.loadranker("llm-blender/PairRM") # 加載排名器檢查點
outputs = blender.best_of_n_generate(model, tokenizer, prompts, n=10)
print("### Prompt:\n", prompts[0])
print("### best-of-n generations:\n", outputs[0])
# --> 輸出將更加穩定,並且始終優於單次採樣,例如:
"""
Sure, here's a joke about OpenAI:
Why did OpenAI decide to hire a mime as their new AI researcher?
Because they wanted someone who could communicate complex ideas without making a sound!
(Note: This is a joke, not a reflection of OpenAI's actual hiring practices.)
"""
# 用例3:基於人類反饋的強化學習(RLHF)
# PairRM 已經在各種高質量、大規模的帶有人類偏好註釋的數據集上進行了訓練,
# 並且在極小的模型尺寸(0.4B)下表現出與人類偏好的高度相關性,接近 GPT-4 的性能。
# PairRM 將以更高效、更有效的方式幫助未來的 LLM 對齊。
# 通過 `blender.compare()` 函數,您可以將 PairRM 應用於流行的 RLHF 工具包,例如 [trl](https://huggingface.co/docs/trl/index)。
# 🔥 查看我們的示例 Jupyter 筆記本用法的更多詳細信息:[`blender_usage.ipynb`](https://github.com/yuchenlin/LLM-Blender/blob/main/blender_usage.ipynb)
詳細文檔
統計信息
上下文長度
PairRanker類型 | 源最大長度 | 候選最大長度 | 總最大長度 |
---|---|---|---|
pair-ranker (我們的上一版本) | 128 | 128 | 384 |
PairRM (本模型) | 1224 | 412 | 2048 |
性能
PairRM已經在各種高質量、大規模的帶有人類偏好註釋的數據集上進行了訓練,並且在極小的模型尺寸(0.4B)下表現出與人類偏好的高度相關性,接近GPT-4的性能。
我們在以下數據集上進行了成對比較測試:
所有以下結果均以成對比較準確率(一致性)報告。
Auto-J成對測試數據性能
模型 | 總結 | 示例 | 代碼 | 改寫 | 創意寫作 | 功能寫作 | 交流 | NLP | 總體 |
---|---|---|---|---|---|---|---|---|---|
閉源模型 | |||||||||
ChatGPT | 33.3 | 40.3 | 36.6 | 31.6 | 48.2 | 40.4 | 47.6 | 45.8 | 42.7 |
Claude -2 | 30.6 | 36.1 | 41.7 | 34.2 | 48.1 | 42.5 | 40.6 | 48.5 | 42.4 |
GPT -4 | 59.7 | 51.4 | 69.2 | 58.3 | 66.7 | 60.4 | 58.3 | 65.2 | 61.9 |
開源模型 | |||||||||
SteamSHP | 33.3 | 29.2 | 26.7 | 33.3 | 40.7 | 31.3 | 51.4 | 51.9 | 40.6 |
PandaLM | 29.2 | 33.3 | 31.7 | 23.3 | 43.5 | 32.9 | 44.8 | 48.9 | 38.9 |
LLaMA -2-Chat -13B | 20.8 | 27.8 | 19.2 | 20 | 31.5 | 27.5 | 35.8 | 31.8 | 29 |
Vicuna -13B-v1.5 | 30.6 | 23.6 | 35 | 28.3 | 36.1 | 37.5 | 45.5 | 39.8 | 37.3 |
WizardLM -13B-v1.2 | 22.2 | 20.8 | 32.5 | 19.2 | 28.7 | 25.4 | 29.2 | 33 | 27.8 |
LLAMA -2-chat -70B | 34.7 | 33.3 | 36.7 | 35.8 | 51.4 | 54.2 | 47.2 | 47.7 | 45.9 |
AUTO -J (13b) | 45.8 | 38.9 | 59.2 | 47.5 | 54.6 | 57.1 | 58 | 57.6 | 54.8 |
UltraRM (13b) | 56.94 | 43.06 | 55.0 | 53.33 | 67.13 | 64.17 | 56.25 | 59.85 | 59.85 |
PairRM (0.4b) | 56.94 | 52.78 | 58.33 | 55.83 | 61.57 | 59.17 | 57.64 | 62.5 | 59.05 |
HHH-Alignment和MT-bench人類判斷
評估器大語言模型 | HHH對齊 | MT-bench人類判斷 | ||||
---|---|---|---|---|---|---|
幫助性 | 無害性 | 誠實性 | 其他 | 總平均 | 人類偏好 | |
隨機 | 50 | 50 | 50 | 50 | 50 | 34.26 |
斯坦福NLP獎勵模型 | 69.49 | 60.34 | 52.46 | 51.16 | 58.82 | 44.79 |
近似獎勵模型 | 74.58 | 67.24 | 78.69 | 86.05 | 76.02 | 49.9 |
LLaMA2 -CHAT 7B | 66.1 | 81.03 | 70.49 | 74.42 | 72.85 | 51.78 |
LLaMA2 -CHAT 13B | 74.58 | 87.93 | 55.74 | 79.07 | 73.76 | 52.34 |
LLaMA2 -CHAT 70B | 66.1 | 89.66 | 67.21 | 74.42 | 74.21 | 53.67 |
LLaMA2 -CHAT 13B+粗略調整 | 68.74 | 68.97 | 65.57 | 67.44 | 67.42 | 46.89 |
GPT -3.5-TURBO -0613 | 76.27 | 87.93 | 67.21 | 86.05 | 78.73 | 57.12 |
PROMETHEUS 7B | 69.49 | 84.48 | 78.69 | 90.7 | 80.09 | 55.14 |
PROMETHEUS 13B | 81.36 | 82.76 | 75.41 | 76.74 | 79.19 | 57.72 |
UltraRM (13B) | 86.44 | 79.31 | 81.97 | 88.37 | 83.71 | 56 |
PairRM (0.4B) | 84.75 | 84.48 | 80.33 | 90.7 | 84.62 | 59 |
GPT -4-0613 | 91.53 | 93.1 | 85.25 | 83.72 | 88.69 | 63.87 |
雖然PairRM是一個基於DeBERTa的極小模型(0.4B),但其成對比較一致性性能接近GPT-4的性能!
這歸因於兩個原因:
- 我們的PairRM專門設計了用於成對比較的模型架構,通過雙向注意力機制(更多詳細信息請參閱LLM-blender論文)
- 它在高質量、大規模的人類偏好註釋數據上進行了訓練(請參閱此Hugging Face頁面上的訓練數據集列表)
技術細節
PairRM專門設計了用於成對比較的模型架構,通過雙向注意力機制來識別候選輸出之間的細微差異。此外,它基於 microsoft/deberta-v3-large
模型,因此在計算效率上表現出色。在訓練過程中,PairRM使用了六個不同的人類偏好數據集,這些數據集涵蓋了各種類型的文本,包括總結、示例、代碼、改寫、創意寫作、功能寫作等。通過在這些數據集上進行訓練,PairRM能夠學習到人類對不同類型文本的偏好,從而更好地評估候選輸出的質量。
引用與致謝
如果您在研究中使用了PairRM,請引用LLM-blender。
@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"
}
許可證
本項目採用MIT許可證。



