🚀 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}} }