大模型实战训练营(XJTU)-作业一 代码助手模型
模型介绍
模型名称
- 中文名:基于qwen-7B的代码助手
- 英文名:Code assistant based on qwen-7B
模型简介
通义千问-7B(Qwen-7B)是阿里云研发的通义千问大模型系列的70亿参数规模的模型。Qwen-7B是基于Transformer的大语言模型, 在超大规模的预训练数据上进行训练得到。预训练数据类型多样,覆盖广泛,包括大量网络文本、专业书籍、代码等。同时,在Qwen-7B的基础上,我们使用对齐机制打造了基于大语言模型的AI助手Qwen-7B-Chat。相较于最初开源的Qwen-7B模型,我们现已将预训练模型和Chat模型更新到效果更优的版本。
本实验通过ModelScope提供的轻量级微调工具SWIFT(Scalable lightWeight Infrastructure for Fine-Tuning),对Qwen-7B-Chat进行微调,实现以下功能:
- 代码生成能力
- 给出⼀个询问(e.g."使⽤python写快排."),模型可以给出正确的示例代码,以及适当的代码注释,输出的代码需要被```环绕
- ⽀持多种变成语言(e.g. python, C, C++等).
- 自我认知能力:询问它是谁, 它的开发者是谁, 可以正确的进⾏回答.
- 多轮对话能力
实验环境
ModelScope社区提供的的免费算力平台
硬件
- CPU:Intel(R) Xeon(R) Platinum 8369B CPU @ 2.90GHz(8核 32GB)
- GPU:A10(24G)
软件
- python 3.8及以上版本
- pytorch 1.12及以上版本,推荐2.0及以上版本
- 建议使用CUDA 11.4及以上(GPU用户、flash-attention用户等需考虑此选项)
- python 3.8 and above
- pytorch 1.12 and above, 2.0 and above are recommended
- CUDA 11.4 and above are recommended (this is for GPU users, flash-attention users, etc.)
- SWIFT 1.5.1
训练方法
本实验通过ModelScope提供的轻量级微调工具SWIFT(Scalable lightWeight Infrastructure for Fine-Tuning),对Qwen-7B-Chat进行微调
- 微调模型:qwen/Qwen-7B-Chat
- 微调数据集:AI-ModelScope/alpaca-gpt4-data-en,AI-ModelScope/alpaca-gpt4-data-zh,codefuse-ai/Evol-instruction-66k
- 微调参数:
# 微调模型参数
model_type=ModelType.qwen_7b_chat,
# 微调数据集参数
dataset=[DatasetName.codefuse_evol_instruction_zh,DatasetName.alpaca_zh, DatasetName.alpaca_en],
train_dataset_sample=10000,
# 存储目录参数
output_dir='output',
# 自我认知参数
lora_target_modules='ALL',
self_cognition_sample=500,
model_name=['基于qwen-7B的代码助手', 'Code assistant based on qwen-7B'],
model_author=['于子毅', 'YZY'],
# 训练参数
eval_steps=100,
quantization_bit=4,
gradient_accumulation_steps =16,
warmup_ratio=0.03,
logging_steps=10,
示例代码
模型微调
# LLM微调
import os
import torch
from swift.llm import DatasetName, ModelType
from swift.llm import SftArguments, sft_main
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
torch.cuda.empty_cache()
# 设置微调参数
sft_args = SftArguments(
model_type=ModelType.qwen_7b_chat,
dataset=[DatasetName.codefuse_evol_instruction_zh,DatasetName.alpaca_zh, DatasetName.alpaca_en],
train_dataset_sample=10000,
eval_steps=100,
logging_steps=10,
output_dir='output',
lora_target_modules='ALL',
self_cognition_sample=500,
model_name=['基于qwen-7B的代码助手', 'Code assistant based on qwen-7B'],
model_author=['于子毅', 'YZY'],
quantization_bit=4,
gradient_accumulation_steps =16,
warmup_ratio=0.03,
)
output = sft_main(sft_args)
best_model_checkpoint = output['best_model_checkpoint']
print(f'best_model_checkpoint: {best_model_checkpoint}')
torch.cuda.empty_cache()
模型推理
# LLM推理
import os
from swift.llm import InferArguments, merge_lora_main, infer_main
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
infer_args = InferArguments(
ckpt_dir=best_model_checkpoint,
eval_human=True)
# merge_lora_main(infer_args)
result = infer_main(infer_args)
torch.cuda.empty_cache()
模型上传
# 模型上传
from modelscope.hub.api import HubApi
YOUR_ACCESS_TOKEN ="###" # 自己的SDK令牌
api = HubApi()
api.login(YOUR_ACCESS_TOKEN)
api.push_model(
model_id="flashyzy/qwen-7B-chat-code",
model_dir=best_model_checkpoint # 本地模型目录,要求目录中必须包含configuration.json
)
推理效果
在image文件夹下面"自我认知.png","代码及多轮对话能力"
评论