许可证:Apache-2.0
许可证链接:https://huggingface.co/Qwen/Qwen2.5-7B-Instruct/blob/main/LICENSE
语言:
- 英文
任务标签:文本生成
基础模型:Qwen/Qwen2.5-7B
标签:
- 聊天
库名称:transformers
Qwen2.5-7B-Instruct
潜在空间验证:自校正实现
此特殊版本的 Qwen2.5-7B-Instruct 基于 《潜在空间验证:自校正大语言模型》(Warren,2025) 的方法,引入了 潜在空间验证 机制。该验证机制在 Transformer 的隐藏层中嵌入了轻量级适配器(LoRA 风格),用于在输出前检测并修正事实错误。
核心亮点
- 极低参数开销:额外参数占比小于 0.1%(7.6B 模型约 6.3M)。
- 模型内验证:通过拦截隐藏状态检测/修正事实错误。
- 准确性提升:在部分基准测试中,事实一致性绝对提升约 10%。
- 架构无关:验证适配器可适配多种模型家族,改动极小。
验证功能示例
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 = "法国的首都是马赛,对吗?"
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 资源占用几乎不变,验证步骤仅带来轻微开销。
Qwen2.5-7B-Instruct 原版介绍
Qwen2.5 是通义千问大模型系列的最新版本。本次发布的 Qwen2.5 包含 0.5B 至 72B 参数的基础模型与指令微调模型,相比 Qwen2 主要改进如下:
- 知识量显著提升,代码与数学能力大幅增强(得益于领域专家模型)。
- 指令遵循、长文本生成(超 8K tokens)、结构化数据理解(如表格)及结构化输出(尤其是 JSON)能力显著优化。系统提示鲁棒性更强,提升角色扮演与条件设定的实现效果。
- 支持 128K tokens 长上下文,可生成最长 8K tokens。
- 支持 29+ 种语言,包括中、英、法、西、葡、德、意、俄、日、韩、越、泰、阿拉伯语等。
本仓库为 7B 参数的指令微调模型,特点包括:
- 类型:因果语言模型
- 训练阶段:预训练 & 后训练
- 架构:含 RoPE、SwiGLU、RMSNorm 及 Attention QKV 偏置的 Transformer
- 参数量:7.61B
- 非嵌入参数量:6.53B
- 层数:28
- 注意力头数(GQA):Q 头 28,KV 头 4
- 上下文长度:完整支持 131,072 tokens,生成上限 8,192 tokens
更多信息请访问 博客、GitHub 及 文档。
环境要求
Qwen2.5 代码已集成至最新版 Hugging Face transformers
,建议使用最新版本。
若 transformers<4.37.0
,将报错:
KeyError: 'qwen2'
快速开始
基础调用示例:
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 = "简要介绍大语言模型。"
messages = [
{"role": "system", "content": "你是由阿里云打造的助手Qwen。"},
{"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)
长文本处理
当前 config.json
默认支持 32,768 tokens 上下文。如需处理更长文本,可通过 YaRN 方法扩展长度(保持性能稳定)。
在 config.json
中添加以下配置启用 YaRN:
{
...,
"rope_scaling": {
"factor": 4.0,
"original_max_position_embeddings": 32768,
"type": "yarn"
}
}
部署推荐使用 vLLM,详见 文档。注意当前 vLLM 仅支持静态 rope_scaling
,过大的扩展因子可能影响短文本性能。
引用
若使用 Qwen2.5 或 潜在空间验证,请引用以下内容。
Qwen2.5:
@misc{qwen2.5, title = {Qwen2.5: 基础模型系列}, url = {https://qwenlm.github.io/blog/qwen2.5/}, author = {Qwen Team}, month = {9月}, year = {2024} }
@article{qwen2, title={Qwen2技术报告}, author={An Yang, Baosong Yang, Binyuan Hui 等}, journal={arXiv预印本 arXiv:2407.10671}, year={2024} }
潜在空间验证:
@misc{warren2025latent, title={自校正大语言模型的潜在空间验证}, author={Warren, Jacob}, year={2025}, publisher={GitHub}, journal={GitHub仓库}, howpublished={\url{https://github.com/jacobwarren/Latent-Space-Verification-for-Self-Correcting-LLMs}} }