🚀 Qwen2.5-7B-Instruct
Qwen2.5-7B-Instruct是基於Qwen2.5大語言模型微調的指令模型,通過潛在空間驗證機制,能有效檢測和糾正事實性錯誤,提升輸出的準確性。同時,Qwen2.5本身在知識儲備、編碼和數學能力、指令遵循等多方面有顯著提升。
🚀 快速開始
以下是使用標準Qwen2.5管道的最小代碼片段:
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "Qwen/Qwen2.5-7B-Instruct"
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
prompt = "Give me a short introduction to large language model."
messages = [
{"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
{"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
generated_ids = model.generate(
**model_inputs,
max_new_tokens=512
)
generated_ids = [
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
]
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
print(response)
✨ 主要特性
潛在空間驗證版本特性
- 極小的參數開銷:額外參數少於0.1%(對於76億參數的模型,約為630萬)。
- 模型內驗證:該方法攔截隱藏狀態以檢測/糾正事實性錯誤。
- 提高準確性:在某些基準測試中,事實一致性絕對提升高達約10%。
- 架構無關性:驗證適配器可以在幾乎不做更改的情況下應用於各種模型家族。
原始Qwen2.5-7B-Instruct模型特性
- 知識與能力提升:由於在編碼和數學領域的專業專家模型,顯著增加了知識儲備,並大大提高了編碼和數學能力。
- 指令遵循與文本生成:在指令遵循、長文本生成(超過8K令牌)、理解結構化數據(如表格)和生成結構化輸出(尤其是JSON)方面有顯著改進。對系統提示的多樣性更具彈性,增強了聊天機器人的角色扮演實現和條件設置。
- 長上下文支持:支持長達128K令牌的上下文,並可生成多達8K令牌。
- 多語言支持:支持超過29種語言,包括中文、英語、法語、西班牙語、葡萄牙語、德語、意大利語、俄語、日語、韓語、越南語、泰語、阿拉伯語等。
📦 安裝指南
Qwen2.5的代碼包含在最新的Hugging Face transformers
中。建議使用最新版本的 transformers
。
使用 transformers<4.37.0
時,會遇到以下錯誤:
KeyError: 'qwen2'
💻 使用示例
基礎用法
from latent_verification import load_verification_model
from transformers import AutoTokenizer
verified_model_name = "YourCustomOrg/Qwen2.5-7B-Instruct-Verification"
model = load_verification_model(verified_model_name)
tokenizer = AutoTokenizer.from_pretrained(verified_model_name)
prompt = "The capital of France is Marseilles, correct?"
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(inputs["input_ids"], max_new_tokens=50)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
在這個代碼片段中:
load_verification_model
確保驗證適配器被初始化並處於激活狀態。
- 任何前向傳播都會自動應用潛在空間檢查,修改隱藏狀態以減少事實不一致性。
高級用法
如果你有一個指令模型,並希望應用潛在空間驗證:
from transformers import AutoModelForCausalLM
from latent_verification import create_verification_model
base_model_name = "Qwen/Qwen2.5-7B-Instruct"
base_model = AutoModelForCausalLM.from_pretrained(base_model_name)
verified_model = create_verification_model(
base_model=base_model,
adapter_locations=[2, 5, 8, 11, 14, 17, 20, 27],
bottleneck_size=64,
enable_cross_layer=True
)
verified_model.save_pretrained("YourCustomOrg/Qwen2.5-7B-Instruct-Verification")
然後可以將增強驗證的模型上傳到Hugging Face,或使用 load_verification_model
在本地加載。
📚 詳細文檔
評估與性能
詳細的評估結果請參閱 潛在空間驗證論文。有關GPU內存和吞吐量基準測試,請參閱 此處。
驗證機制可以在許多任務中將事實可靠性提高約10%,同時保持甚至增強基礎模型的流暢性。實際上,整體GPU佔用幾乎相同,只是驗證步驟有少量開銷。
處理長文本
當前的 config.json
設置為上下文長度最大為32768令牌。為了處理超過32768令牌的輸入,使用 YaRN 方法進行長度外推,該方法在長文本上保持了強大的性能。
對於支持的框架,可以在 config.json
中添加以下代碼片段以啟用YaRN:
{
...,
"rope_scaling": {
"factor": 4.0,
"original_max_position_embeddings": 32768,
"type": "yarn"
}
}
部署時,建議使用 vLLM。使用詳情請參閱 文檔。請注意,當前的vLLM僅支持靜態 rope_scaling
,如果啟用非常大的因子,可能會影響較短文本的性能。
模型信息
屬性 |
詳情 |
模型類型 |
因果語言模型 |
訓練階段 |
預訓練和後訓練 |
架構 |
帶有RoPE、SwiGLU、RMSNorm和注意力QKV偏置的transformers |
參數數量 |
76.1億 |
非嵌入參數數量 |
65.3億 |
層數 |
28 |
注意力頭數量(GQA) |
Q為28,KV為4 |
上下文長度 |
完整的131072令牌,可生成多達8192令牌 |
🔧 技術細節
潛在空間驗證機制
該特殊版本的 Qwen2.5-7B-Instruct 結合了基於 "Latent-Space Verification for Self-Correcting LLMs" (Warren, 2025) 中描述方法的 潛在空間驗證。驗證機制將輕量級適配器(LoRA風格)嵌入到變壓器的隱藏層中,以在事實性不準確信息出現在輸出之前檢測並糾正它們。
長文本處理技術
使用 YaRN 方法進行長度外推,以處理超過32768令牌的輸入,並在長文本上保持強大的性能。
📄 許可證
本項目採用 Apache-2.0 許可證。
📖 引用
如果您認為我們的工作有幫助,請隨意同時引用 Qwen2.5 和 潛在空間驗證。
Qwen2.5:
@misc{qwen2.5, title = {Qwen2.5: A Party of Foundation Models}, url = {https://qwenlm.github.io/blog/qwen2.5/}, author = {Qwen Team}, month = {September}, year = {2024} }
@article{qwen2, title={Qwen2 Technical Report}, author={An Yang and Baosong Yang and Binyuan Hui and et al.}, journal={arXiv preprint arXiv:2407.10671}, year={2024} }
潛在空間驗證:
@misc{warren2025latent, title={Latent-Space Verification for Self-Correcting LLMs}, author={Warren, Jacob}, year={2025}, publisher={GitHub}, journal={GitHub repository}, howpublished={\url{https://github.com/jacobwarren/Latent-Space-Verification-for-Self-Correcting-LLMs}} }