Qwen2.5是由通义千问团队打造的最新一代大型语言模型,它在Qwen2的基础上进行了多项重大升级。新模型不仅参数规模从0.5B扩展至72B,而且在知识储备、指令遵循、长文本处理、结构化数据理解以及多语言支持等方面均有显著提升。本文将深入探讨Qwen2.5的关键特性,并详细介绍如何利用openMind工具套件和LLaMA Factory框架在Ascend平台上进行高效推理和微调。
Qwen2.5的核心优势
Qwen2.5的卓越性能得益于以下几个方面的创新:
- 知识量大幅提升: 通过引入在编程和数学领域的专家模型,Qwen2.5在相关任务上的表现得到了显著增强。这意味着模型能够更准确地理解和生成代码,解决复杂的数学问题。
- 指令遵循和长文本生成能力增强: Qwen2.5能够更好地理解和执行复杂的指令,生成超过8K token的长文本。这为创作高质量的文章、报告和故事提供了强大的支持。
- 结构化数据处理能力提升: 新模型能够更好地理解和生成结构化数据,例如表格和JSON。这使得模型在处理商业报表、科学数据等方面具有更高的效率。
- 多语言支持: Qwen2.5支持超过29种语言,包括中文、英语、法语、西班牙语、葡萄牙语、德语、意大利语、俄语、日语、韩语、越南语、泰语和阿拉伯语等。这使得模型能够服务于全球范围内的用户。
- 超长上下文处理能力: Qwen2.5支持高达128K token的上下文窗口,并能生成最多8K token的文本。这使得模型能够更好地理解长篇文档,并生成连贯、一致的回复。
环境配置
在开始使用Qwen2.5之前,需要配置相应的环境。以下是在Ascend平台上配置环境的步骤:
安装Ascend CANN Toolkit和Kernels:
首先,需要安装Ascend CANN Toolkit和Kernels。可以参考安装教程,或者使用以下命令:
# 请替换URL为CANN版本和设备型号对应的URL # 安装CANN Toolkit wget https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/Milan-ASL/Milan-ASL%20V100R001C17SPC701/Ascend-cann-toolkit_8.0.RC1.alpha001_linux-"$(uname -i)".run bash Ascend-cann-toolkit_8.0.RC1.alpha001_linux-"$(uname -i)".run --install # 安装CANN Kernels wget https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/Milan-ASL/Milan-ASL%20V100R001C17SPC701/Ascend-cann-kernels-910b_8.0.RC1.alpha001_linux.run bash Ascend-cann-kernels-910b_8.0.RC1.alpha001_linux.run --install # 设置环境变量 source /usr/local/Ascend/ascend-toolkit/set_env.sh
安装openMind Library以及openMind Hub Client:
安装openMind Hub Client
pip install openmind_hub
安装openMind Library,并安装PyTorch框架及其依赖。
pip install openmind[pt]
更详细的安装信息请参考openMind官方的环境安装章节。
安装llama-factory:
git clone https://github.com/hiyouga/LLaMA-Factory.git cd LLaMA-Factory pip install -e ".[torch-npu,metrics]"
模型获取
Qwen2.5-7B模型系列由社区开发者在魔乐社区贡献,包括:
- Qwen2.5-7B:modelers.cn/models/AI-R…
- Qwen2.5-7B-Instruct:modelers.cn/models/AI-R…
可以通过Git从魔乐社区下载模型的repo,以Qwen2.5-7B-Instruct为例:
首先保证已安装git-lfs(https://git-lfs.com)
git lfs install
git clone https://modelers.cn/AI-Research/Qwen2.5-7B-Instruct.git
模型推理
用户可以使用openMind Library或者LLaMa Factory进行模型推理,以Qwen2.5-7B-Instruct为例,具体如下:
使用openMind Library进行模型推理
新建推理脚本 inference_qwen2.5_7b_chat.py ,推理脚本内容为:
import argparse from openmind import AutoModelForCausalLM, AutoTokenizer from openmind_hub import snapshot_download def parse_args(): parser = argparse.ArgumentParser() parser.add_argument( "--model_name_or_path", type=str, help="Path to model", default=None, ) args = parser.parse_args() return args def main(): args = parse_args() if args.model_name_or_path: model_path = args.model_name_or_path else: model_path = snapshot_download("AI-Research/Qwen2.5-7B-Instruct", revision="main", resume_download=True, ignore_patterns=[" .h5", " .ot", "*.mspack"]) model = AutoModelForCausalLM.from_pretrained( model_path, torch_dtype="auto", device_map="auto" ) tokenizer = AutoTokenizer.from_pretrained(model_path) prompt = '你是谁' messages = [ {"role": "system", "content": "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) if __name__ == "__main__": main()
执行推理脚本:
python inference_qwen2.5_7b_chat.py
推理结果如下:
使用LLaMa Factory与模型交互
在LLaMa Factory路径下新建examples/inference/qwen2.5_7b_chat.yaml推理配置文件,文件内容为:
model_name_or_path: xxx # 当前仅支持本地加载,填写Qwen2.5-7B-Instruct本地权重路径 template: qwen
使用以下命令与模型进行交互:
llamafactory-cli chat examples/inference/qwen2.5_7b_chat.yaml
交互结果如下:
模型微调
数据集
使用Llama-Factory集成的identity数据集
微调
新建examples/train_lora/qwen2.5_7b_lora_sft.yaml 微调配置文件,微调配置文件如下:
### model model_name_or_path: xxx/xxx # 预训练模型路径 ### method stage: sft do_train: true finetuning_type: lora lora_target: all ### dataset dataset: identity template: qwen cutoff_len: 1024 max_samples: 1000 overwrite_cache: true preprocessing_num_workers: 16 ### output output_dir: ./saves/qwen2.5_7b/lora/sft logging_steps: 10 save_steps: 500 plot_loss: true overwrite_output_dir: true ### train per_device_train_batch_size: 1 gradient_accumulation_steps: 8 learning_rate: 1.0e-4 num_train_epochs: 10.0 lr_scheduler_type: cosine warmup_ratio: 0.1 bf16: true ddp_timeout: 180000000 ### eval val_size: 0.1 per_device_eval_batch_size: 1 eval_strategy: steps eval_steps: 500
使用以下命令进行微调:
llamafactory-cli train examples/train_lora/qwen2.5_7b_lora_sft.yaml
微调可视化
训练Loss可视化:
微调后推理
模型推理
修改examples/inference/qwen2.5_7b_chat.yaml推理配置文件,文件内容为:
model_name_or_path: xxx # 当前仅支持本地加载,填写Qwen2.5-7B-Instruct本地权重路径 adapter_name_or_path: ./saves/qwen2.5_7b/lora/sft template: qwen
使用以下命令进行推理:
llamafactory-cli chat examples/inference/qwen2.5_7b_chat.yaml
推理结果
总结
Qwen2.5作为新一代大型语言模型,在知识储备、指令遵循、长文本处理和多语言支持等方面都表现出了强大的能力。通过openMind应用使能套件,开发者可以更高效地进行模型调优和推理。Qwen2.5与openMind的结合,为AI应用的开发带来了新的可能性。