大数跨境

通义千问Qwen2.5-Coder 全系列来咯!强大、多样、实用!

通义千问Qwen2.5-Coder 全系列来咯!强大、多样、实用! 阿里云开发者
2024-11-12
35

通义千问发布Qwen2.5-Coder全系列,开源代码模型再升级

覆盖六大尺寸,32B旗舰模型代码能力追平GPT-4o,支持本地部署与微调

通义千问团队正式开源Qwen2.5-Coder全系列模型,涵盖0.5B、1.5B、3B、7B、14B、32B六种参数规模,全面满足开发者在不同场景下的需求。

其中,Qwen2.5-Coder-32B-Instruct作为旗舰模型,在多项主流代码生成基准(EvalPlus、LiveCodeBench、BigCodeBench)上达到开源模型SOTA水平,整体表现与GPT-4o相当。

在代码修复方面,该模型在Aider基准测试中得分为73.7,与GPT-4o持平;在多语言代码修复基准MdEval上得分75.2,位居开源模型首位。

在代码推理能力方面,Qwen2.5-Coder系列持续优化,32B模型较此前发布的7B版本进一步提升。

支持超过40种编程语言,在McEval多语言代码生成评测中得分为65.9,对Haskell、Racket等小众语言亦有出色表现,得益于预训练阶段的精细化数据清洗与配比策略。

为评估人类偏好对齐效果,团队构建内部代码偏好评测基准Code Arena,并采用GPT-4o作为裁判模型进行“A vs. B”对比测试,结果显示Qwen2.5-Coder-32B-Instruct在生成代码的可读性、规范性和实用性方面表现优异。

此次开源涵盖六个尺寸的Base与Instruct两类模型:Base模型适用于进一步微调训练,Instruct模型则为已对齐的对话版本,可直接用于代码辅助任务。

实验验证了Scaling Law在代码大模型中的有效性:随着参数规模增加,各尺寸模型在MBPP-3shot(Base模型评估)和最新LiveCodeBench题目(Instruct模型评估)上的表现呈稳定正相关趋势。

除3B模型采用“Research Only”许可外,其余模型(0.5B、1.5B、7B、14B、32B)均采用Apache 2.0许可证,允许商业使用与二次开发。

开发者可通过ModelScope平台获取模型:

模型链接:

https://modelscope.cn/collections/Qwen25-Coder-9d375446e8f5814a

模型Demo体验:

https://modelscope.cn/studios/Qwen/Qwen2.5-Coder-demo

Artifacts功能体验:

https://modelscope.cn/studios/Qwen/Qwen2.5-Coder-Artifacts

支持多种本地部署方式:

使用Transformers单卡运行量化版32B模型:

from modelscope import AutoModelForCausalLM, AutoTokenizer
model_name = "Qwen/Qwen2.5-Coder-32B-Instruct-GPTQ-Int4"
model = AutoModelForCausalLM.from_pretrained( model_name,
torch_dtype="auto",
device_map="auto" )
tokenizer = AutoTokenizer.from_pretrained(model_name)
prompt = "write a quick sort algorithm."
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]

通过Ollama一键运行GGUF格式模型:

#设置下启用
ollama serve
#ollama run ModelScope 任意 GGUF 模型
ollama run modelscope.cn/Qwen/Qwen2.5-32B-Instruct-GGUF

使用vLLM实现推理加速:

pip install vllm -U
export VLLM_USE_MODELSCOPE=True vllm serve Qwen/Qwen2.5-Coder-7B-Instruct
# 推理代码:
from openai import OpenAI
# Modify OpenAI's API key and API base to use vLLM's API server. openai_api_key = "EMPTY"
openai_api_base = "http://localhost:8000/v1"
client = OpenAI(
api_key=openai_api_key,
base_url=openai_api_base, )
completion = client.completions.create(model="Qwen/Qwen2.5-Coder-1.5B-Instruct",print("Completion result:", completion)
prompt="San Francisco is a")

支持使用ms-swift工具进行高效微调:

# 安装 ms-swift
git clone https://github.com/modelscope/ms-swift.git
cd swift
pip install -e .[llm]
# 微调脚本:
# Experimental environment: A10, 3090, V100, ... # 15GB GPU memory 
CUDA_VISIBLE_DEVICES=0 swift sft \
  --model_type qwen2_5-coder-3b-instruct \ 
  --model_id_or_path qwen/Qwen2.5-Coder-3B-Instruct \ 
  --dataset swift/self-cognition#500 \
              AI-ModelScope/Magpie-Qwen2-Pro-200K-Chinese#500 \
              AI-ModelScope/Magpie-Qwen2-Pro-200K-English#500 \ 
  --logging_steps 5 \
  --max_length 4096 \
  --learning_rate 1e-4 \
  --output_dir output \ 
  --lora_target_modules ALL \ 
  --model_name 小黄 'Xiao Huang' \ 
  --model_author 魔搭 ModelScope \ 
  --system 'You are a helpful assistant.'

微调后推理脚本:

# Experimental environment: A10, 3090, V100, ... # 直接推理
CUDA_VISIBLE_DEVICES=0 swift infer \
  --ckpt_dir output/qwen2_5-coder-3b-instruct/vx-xxx/checkpoint-xxx
# 使用 vLLM 进行推理加速 
CUDA_VISIBLE_DEVICES=0 swift infer \
  --ckpt_dir output/qwen2_5-coder-3b-instruct/vx-xxx/checkpoint-xxx \ 
  --infer_backend vllm 
  --max_model_len 8192 
  --merge_lora true

Qwen2.5-Coder在代码开发场景中的应用实践

探索Qwen2.5-Coder在Cursor、Artifacts与Interpreter中的实际表现

作为面向实用开发者的AI模型,Qwen2.5-Coder在代码助手、Artifacts生成和Interpreter执行等场景中展现出强大能力,为开发者提供开源且高效的编程辅助方案。

在代码编写方面,Qwen2.5-Coder可集成至Cursor等主流代码编辑工具,通过配置OpenAI兼容的API接口(包含URL与API Key),即可实现智能生成、编辑与补全功能,显著提升开发效率。

借助快捷指令(如Command+K),用户可实时体验其流畅的代码交互能力。

在Artifacts应用中,Qwen2.5-Coder支持通过提示词驱动的方式生成可视化内容,用户可通过克隆魔搭创空间项目实现本地部署。

git clone https://www.modelscope.cn/studios/Qwen/Qwen2.5-Coder-Artifacts.git cd Qwen2.5-Coder-Artifacts
pip install -r requirements.txt
pip install gradio
python app.py

在系统操作层面,结合Open Interpreter框架,Qwen2.5-Coder可在Mac环境下实现AI对计算机的直接控制。

安装命令如下:

pip install open-interpreter

配置Python环境参数后,即可启动交互式指令执行:

from interpreter import interpreter
interpreter.llm.api_base = "YOUR_BASE_URL"
interpreter.llm.api_key = "YOUR_API_KEY"
interpreter.llm.model = "openai/Qwen-Coder-32B-Instruct"
interpreter.chat("Can you set my system to light mode?")

以上实践展示了Qwen2.5-Coder在多种编程环境中的灵活性与实用性,助力开发者构建更智能的工作流。

【声明】内容源于网络
0
0
阿里云开发者
阿里巴巴官方技术号,关于阿里的技术创新均呈现于此。
内容 3834
粉丝 0
阿里云开发者 阿里巴巴官方技术号,关于阿里的技术创新均呈现于此。
总阅读101.8k
粉丝0
内容3.8k