00.前言
今天,通义千问Qwen团队正式开源推出 Qwen3,这是 Qwen 系列大型语言模型的最新成员。最新的Qwen3系列模型具备双模推理能力(深入思考/快速响应)、支持119种语言及方言,并强化了Agent功能与代码执行能力,全面满足复杂问题处理与全球化应用需求。
其中,旗舰模型 Qwen3-235B-A22B 在代码、数学、通用能力等基准测试中,与 DeepSeek-R1、o1、o3-mini、Grok-3 和 Gemini-2.5-Pro 等顶级模型相比,表现出极具竞争力的结果。此外,小型 MoE 模型 Qwen3-30B-A3B 的激活参数数量是 QwQ-32B 的 10%,表现更胜一筹,甚至像 Qwen3-4B 这样的小模型也能匹敌 Qwen2.5-72B-Instruct 的性能。
编辑
编辑
本次Qwen3开源了两个 MoE 模型的权重:Qwen3-235B-A22B,一个拥有 2350 多亿总参数和 220 多亿激活参数的大模型,以及Qwen3-30B-A3B,一个拥有约 300 亿总参数和 30 亿激活参数的小型 MoE 模型。此外,六个 Dense 模型也已开源,包括 Qwen3-32B、Qwen3-14B、Qwen3-8B、Qwen3-4B、Qwen3-1.7B 和 Qwen3-0.6B,均在 Apache 2.0 许可下开源。
Github:
https://github.com/QwenLM/Qwen3
Blog:
https://qwenlm.github.io/zh/blog/qwen3/
模型合集:
https://www.modelscope.cn/collections/Qwen3-9743180bdc6b48
创空间体验:
https://www.modelscope.cn/studios/Qwen/qwen3-chat-demo
模型亮点小编敲黑板:
Qwen3 模型支持两种思考模式:
- 思考模式:在这种模式下,模型会逐步推理,经过深思熟虑后给出最终答案,适合需要深入思考的复杂问题。
- 非思考模式:在此模式中,模型提供快速、近乎即时响应,适用于对速度要求高于深度的简单问题。
多语言
Qwen3 模型支持 119 种语言和方言,其中包括印欧语系、汉藏语系、亚非语系、南岛语系、德拉威语、突厥语系 、壮侗语系、乌拉尔语系、南亚语系等等。这一广泛的多语言能力为国际应用开辟了新的可能性,让全球用户都能受益于这些模型的强大功能。
增强的 Agent 能力
优化了 Qwen3 模型的 Agent 和 代码能力,同时也加强了对 MCP 的支持(后文附使用Qwen3系列模型与MCP结合的实战教程)
01.推理&部署
Transformers
在 transformers 中使用 Qwen3-30B-A3B :
from modelscope import AutoModelForCausalLM, AutoTokenizer model_name = "Qwen/Qwen3-30B-A3B" # load the tokenizer and the model tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained( model_name, torch_dtype="auto", device_map="auto" ) # prepare the model input prompt = "Give me a short introduction to large language model." messages = [ {"role": "user", "content": prompt} ] text = tokenizer.apply_chat_template( messages, tokenize=False, add_generation_prompt=True, enable_thinking=True # Switch between thinking and non-thinking modes. Default is True. ) model_inputs = tokenizer([text], return_tensors="pt").to(model.device) # conduct text completion generated_ids = model.generate( **model_inputs, max_new_tokens=32768 ) output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist() # parsing thinking content try: # rindex finding 151668 (</think>) index = len(output_ids) - output_ids[::-1].index(151668) except ValueError: index = 0 thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n") content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n") print("thinking content:", thinking_content) print("content:", content)
禁用思考模式,只需对参数 enable_thinking 进行如下修改:
text = tokenizer.apply_chat_template( messages, tokenize=False, add_generation_prompt=True, enable_thinking=False # True is the default value for enable_thinking. )
多工具部署
开发者朋友们可以使用 sglang>=0.4.6.post1 或 vllm>=0.8.4 来创建一个与 OpenAI API 兼容的 API endpoint:
- SGLang:
SGLANG_USE_MODELSCOPE=1 python -m sglang.launch_server --model-path Qwen/Qwen3-32B --reasoning-parser qwen3
- vLLM:
VLLM_USE_MODELSCOPE=1 vllm serve Qwen/Qwen3-32B --enable-reasoning --reasoning-parser deepseek_r1
要禁用思考模式,可以移除参数 --reasoning-parser(以及 --enable-reasoning)
如果用于本地开发,可以通过运行简单的命令 ollama run qwen3:30b-a3b 来使用 ollama 与模型进行交互。您也可以使用 LMStudio 或者 llama.cpp 以及 ktransformers 等代码库进行本地开发
- Ollama:
ollama run modelscope.cn/unsloth/Qwen3-8B-GGUF
Ollama默认是thinking模式,如果需要切换到非thinking模式,在prompt后拼接上/no_think 即可。此外Ollama请确保升级到新版本(v0.6.6或以上)。
编辑
添加图片注释,不超过 140 字(可选)
使用魔搭API-Inference直接调用
魔搭平台的API-Inference,也第一时间为Qwen3系列模型提供了支持。魔搭的用户可通过API调用的方式,直接使用。具体API-Inference的使用方式可参见各个模型页面(例如 https://www.modelscope.cn/models/Qwen/Qwen3-32B)说明:
编辑
或者参见API-Inference文档:https://www.modelscope.cn/docs/model-service/API-Inference/intro。值得特别说明的是,Qwen3系列模型可自由切换思考与普通模式,在API接口上,通过extra_body的参数来控制。默认enable_thinking配置打开,可按需关闭。在开启思考模式的时候,还可以通过thinking_budget参数,来限制思考的长度(一般推荐thinking_budget不要配置过小,以4096以上为宜)。
调用示例:
from openai import OpenAI client = OpenAI( base_url='https://api-inference.modelscope.cn/v1/', api_key='MODELSCOPE_SDK_TOKEN', # ModelScope Token ) # set extra_body for thinking control extra_body = { # enable thinking, set to False to disable "enable_thinking": True, # use thinking_budget to contorl num of tokens used for thinking # "thinking_budget": 4096 } response = client.chat.completions.create( model='Qwen/Qwen3-32B', # ModelScope Model-Id messages=[ { 'role': 'user', 'content': '9.9和9.11谁大' } ], stream=True, extra_body=extra_body ) done_thinking = False for chunk in response: thinking_chunk = chunk.choices[0].delta.reasoning_content answer_chunk = chunk.choices[0].delta.content if thinking_chunk != '': print(thinking_chunk, end='', flush=True) elif answer_chunk != '': if not done_thinking: print('\n\n === Final Answer ===\n') done_thinking = True print(answer_chunk, end='', flush=True)
输出结果:
嗯,我现在要比较9.9和9.11哪个更大。首先,我应该先理解这两个数字的结构。9.9是一个小数,而9.11看起来也是一个小数,不过可能有些人可能会误解为日期或者其他形式?不过题目里明确说是比较数值大小,所以应该按照数学上的小数来处理。 首先,我需要确认这两个数的小数点位置。9.9是九又十分之九,也就是9 + 0.9 = 9.9。而9.11则是九又百分之一十一,也就是9 + 0.11 = 9.11。这时候,我需要比较这两个小数的大小。通常比较小数的方法是从左到右逐位比较,直到找到不同的数字为止。 首先比较整数部分,两个数都是9,所以这部分相等。接下来比较小数部分。第一个数是0.9,第二个数是0.11。这时候需要注意小数位数的问题。0.9可以看作0.90,因为小数末尾的零不改变数值的大小。这样两个小数就变成了0.90和0.11。现在比较0.90和0.11的话,第一位小数是9和1,显然9比1大,所以0.90大于0.11,因此整个数9.9(即9.90)比9.11大。 ====================Final Answer==================== 9.9和9.11的比较可以通过以下步骤进行: 1. **整数部分**:两者均为9,相等。 2. **小数部分**: - 9.9可写为 **9.90**(补零不改变数值)。 - 比较十分位:9.90的十分位是 **9**,9.11的十分位是 **1**。 - 由于 $9 > 1$,因此 **9.90 > 9.11**。 **结论**: $$ \boxed{9.9 > 9.11} $$
感谢阿里云百炼平台提供背后算力支持
使用ModelScope MCP实验场集成
Qwen3系列模型提供了对于智能体和工具调用更好的能力支持,能够在在思考和非思考模式下精确集成外部工具。我们也第一时间将多个Qwen3模型集成到了ModelScope MCP实验场,提供Qwen3在MCP场景上的工具调用能力的体验。
编辑
愉快的让Qwen3帮我们订个出行前的准备:
编辑
02.模型微调
我们介绍使用ms-swift对Qwen/Qwen3-8B进行SFT/GRPO以及使用Megatron-SWIFT对Qwen/Qwen3-30B-A3B进行SFT。ms-swift是魔搭社区官方提供的大模型与多模态大模型训练部署框架。
ms-swift开源地址:
https://github.com/modelscope/ms-swift
我们将展示可运行的微调demo,并给出自定义数据集的格式。
在开始微调之前,请确保您的环境已准备妥当。
git clone https://github.com/modelscope/ms-swift.git cd ms-swift pip install -e . pip install liger-kernel transformers -U
SFT
对Qwen3-8B进行训练的脚本如下,在ModelScope提供的免费GPU Notebook中即可运行:
# 训练显存:22GB # 你可以指定`--dataset AI-ModelScope/alpaca-gpt4-data-zh`来跑通实验 CUDA_VISIBLE_DEVICES=0 \ swift sft \ --model Qwen/Qwen3-8B \ --train_type lora \ --dataset '<dataset-path>' \ --torch_dtype bfloat16 \ --num_train_epochs 1 \ --per_device_train_batch_size 1 \ --per_device_eval_batch_size 1 \ --learning_rate 1e-4 \ --lora_rank 8 \ --lora_alpha 32 \ --target_modules all-linear \ --gradient_accumulation_steps 4 \ --eval_steps 50 \ --save_steps 50 \ --save_total_limit 2 \ --logging_steps 5 \ --max_length 2048 \ --output_dir output \ --warmup_ratio 0.05 \ --dataloader_num_workers 4 \ --packing true \ --user_liger_kernel true
自定义数据集格式如下(system字段可选),指定`--dataset <dataset_path>`即可:
{"messages": [{"role": "user", "content": "浙江的省会在哪?"}, {"role": "assistant", "content": "<think>\nxxx\n</think>\n\n浙江的省会在杭州。"}]}
GRPO
以Qwen3-8B为例,下面使用ms-swift框架对进行GRPO训练
使用AI-MO/NuminaMath-TIR作为数据集,并使用accuracy函数计算模型回答的准确率奖励, 计算奖励需要安装以下环境
pip install math_verify==0.5.2
自定义数据集格式与SFT类似,其中assistant部分不必需。如果使用accuracy奖励,则需要solution列来计算准确率。
# llm {"messages": [{"role": "system", "content": "You are a useful and harmless assistant"}, {"role": "user", "content": "Tell me tomorrow's weather"}]} {"messages": [{"role": "system", "content": "You are a useful and harmless math calculator"}, {"role": "user", "content": "What is 1 + 1?"}, {"role": "assistant", "content": "It equals 2"}, {"role": "user", "content": "What about adding 1?"}]} {"messages": [{"role": "user", "content": "What is your name?"}]} # mllm {"messages": [{"role": "user", "content": "<image>What is the difference between the two images?"}], "images": ["/xxx/x.jpg"]} {"messages": [{"role": "user", "content": "<image><image>What is the difference between the two images?"}], "images": ["/xxx/y.jpg", "/xxx/z.png"]}
也可以使用自定义的奖励函数/奖励模型进行训练,数据集中的列会传到奖励函数的**kwargs中,自定义奖励函数的例子参考swift/examples/train/grpo/plugin/plugin.py
--external_plugins examples/train/grpo/plugin/plugin.py \ --reward_funcs external_math_acc external_math_format \ --reward_model AI-ModelScope/Skywork-Reward-Llama-3.1-8B-v0.2
在训练过程中,我们使用vLLM来加速采样过程。设置num_infer_workers=8,我们为每个device都部署一个vLLM engine来加速采样过程。
训练脚本
# 70G*8 CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 \ NPROC_PER_NODE=8 \ swift rlhf \ --rlhf_type grpo \ --model Qwen/Qwen3-8B \ --train_type full \ --dataset AI-MO/NuminaMath-TIR \ --torch_dtype bfloat16 \ --num_train_epochs 1 \ --per_device_train_batch_size 2 \ --per_device_eval_batch_size 2 \ --learning_rate 1e-6 \ --save_total_limit 2 \ --logging_steps 5 \ --output_dir output \ --gradient_accumulation_steps 1 \ --warmup_ratio 0.05 \ --dataloader_num_workers 4 \ --max_completion_length 4096 \ --vllm_max_model_len 8192 \ --reward_funcs accuracy \ --num_generations 16 \ --use_vllm true \ --vllm_gpu_memory_utilization 0.4 \ --sleep_level 1 \ --offload_model true \ --offload_optimizer true \ --gc_collect_after_offload true \ --deepspeed zero3 \ --num_infer_workers 8 \ --tensor_parallel_size 1 \ --temperature 1.0 \ --top_p 0.85 \ --report_to wandb \ --log_completions true \ --overlong_filter true
MoE训练(Megatron-SWIFT)
ms-swift引入了Megatron的并行技术来加速大模型的训练,包括数据并行、张量并行、流水线并行、序列并行,上下文并行,专家并行。支持Qwen3、Qwen3-MoE、Qwen2.5、Llama3、Deepseek-R1蒸馏系等模型的预训练和微调。
对于环境准备(镜像)和HF与MCore模型权重的转换,可以参考Megatron-SWIFT训练文档,这里不详细展开:https://swift.readthedocs.io/zh-cn/latest/Instruction/Megatron-SWIFT%E8%AE%AD%E7%BB%83.html
我们使用DLC启动训练命令,训练环境是2机8 * 80GiB A800:
# https://help.aliyun.com/zh/pai/user-guide/general-environment-variables # 请确保两个节点的保存权重路径相同 NNODES=$WORLD_SIZE \ NODE_RANK=$RANK \ megatron sft \ --load Qwen3-30B-A3B-Base-mcore \ --dataset 'liucong/Chinese-DeepSeek-R1-Distill-data-110k-SFT' \ --tensor_model_parallel_size 2 \ --expert_model_parallel_size 8 \ --moe_grouped_gemm true \ --moe_shared_expert_overlap true \ --moe_aux_loss_coeff 0.01 \ --micro_batch_size 1 \ --global_batch_size 16 \ --packing true \ --recompute_granularity full \ --recompute_method uniform \ --recompute_num_layers 1 \ --train_iters 2000 \ --eval_iters 50 \ --finetune true \ --cross_entropy_loss_fusion true \ --lr 1e-5 \ --lr_warmup_iters 100 \ --min_lr 1e-6 \ --save megatron_output/Qwen3-30B-A3B-Base \ --eval_interval 200 \ --save_interval 200 \ --max_length 8192 \ --num_workers 8 \ --dataset_num_proc 8 \ --no_save_optim true \ --no_save_rng true \ --sequence_parallel true \ --use_flash_attn true
训练loss图(部分):
编辑
添加图片注释,不超过 140 字(可选)
自定义数据集格式与`swift sft`相同,可以在本文上方找到,指定`--dataset <dataset_path>`即可。
使用`megatron sft`和`swift sft`进行Qwen3-30B-A3B模型全参数训练速度/显存占用对比如下:
Megatron-LM |
DeepSpeed-ZeRO2 |
DeepSpeed-ZeRO3 |
|
训练速度 |
9.6s/it |
- |
91.2s/it |
显存占用 |
16*60GiB |
OOM |
16*80GiB |
点击链接,即可跳转模型体验~