modelscope / modelscope/ms-swift

【qwen3.5,3.6 series】Tool call generation interrupted after training: `</think><|im_end|>` or `<tool_call><|im_end|>`

Open
#9,234 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
Python
Stars
15.7k
Forks
1.7k
Avg merge
1d 16h
Merged PRs (30d)
136

Description

Checklist / 检查清单
  • I have searched existing issues, and this is a new question or discussion topic. / 我已经搜索过现有的 issues,确认这是一个新的问题与讨论。
Question Description / 问题描述

Summary

After fine-tuning MoE models (Qwen3.5-35B-A3B, Qwen3.6-35B-A3B, Qwen3.5-122B-A10B) using ms-swift's Megatron SFT — both full fine-tune and LoRA — the trained models frequently fail to emit a valid <tool_call> block during multi-turn agentic inference. Instead, generation terminates prematurely in one of two patterns:

Pattern 1: </think> is immediately followed by <|im_end|>, skipping the tool call entirely.

<think>
... (reasoning content) ...
</think><|im_end|>

Pattern 2: <tool_call> is opened but immediately closed by <|im_end|> with no content.

<think>
... (reasoning content) ...
</think>
<tool_call><|im_end|>

Both patterns cause the agentic loop to stall. Across evaluations of 100 test samples per model, roughly 18–76% of samples are affected depending on the model and training configuration. The issue reproduces across all three model sizes and both training strategies (full / LoRA).


Environment

Item Version
ms-swift 4.1.0.dev0
SGLang 0.5.10rc0
SGLang Kernel 0.4.0
Training backend Megatron (megatron sft)
Models Qwen3.5-35B-A3B, Qwen3.6-35B-A3B, Qwen3.5-122B-A10B
Training type Full fine-tune & LoRA

Training Script

PYTORCH_CUDA_ALLOC_CONF='expandable_segments:True' \
NNODES=$WORLD_SIZE \
NODE_RANK=$RANK \
megatron sft \
    --model /path/to/model \
    --save_safetensors true \
    --dataset /path/to/dataset/ \
    --load_from_cache_file true \
    --split_dataset_ratio 0.01 \
    --tuner_type full \
    --tensor_model_parallel_size 2 \
    --pipeline_model_parallel_size 1 \
    --context_parallel_size 8 \
    --expert_model_parallel_size 8 \
    --moe_permute_fusion true \
    --moe_grouped_gemm true \
    --moe_shared_expert_overlap true \
    --moe_aux_loss_coeff 1e-3 \
    --micro_batch_size 1 \
    --global_batch_size 32 \
    --recompute_granularity full \
    --recompute_method uniform \
    --recompute_num_layers 1 \
    --num_train_epochs 3 \
    --group_by_length true \
    --finetune true \
    --freeze_llm false \
    --freeze_vit true \
    --freeze_aligner true \
    --cross_entropy_loss_fusion true \
    --lr 1e-5 \
    --lr_warmup_fraction 0.05 \
    --min_lr 1e-6 \
    --eval_steps 2000 \
    --save_steps 100 \
    --max_length 49152 \
    --dataloader_num_workers 4 \
    --dataset_num_proc 16 \
    --no_save_optim true \
    --no_save_rng true \
    --sequence_parallel true \
    --moe_expert_capacity_factor 2 \
    --optimizer_cpu_offload true \
    --use_precision_aware_optimizer true \
    --optimizer_offload_fraction 0.62 \
    --attention_backend flash \
    --padding_free false

Inference Script

python -m sglang.launch_server \
    --model-path $MODEL_PATH \
    --tp-size 8 \
    --mem-fraction-static 0.8 \
    --context-length 262144 \
    --reasoning-parser qwen3 \
    --tool-call-parser qwen3_coder

Additional Notes

  • The issue occurs with both full fine-tune and LoRA, ruling out LoRA-specific rank/target module issues.
  • The issue occurs across three model sizes (35B-A3B, 35B-A3B Qwen3.6, 122B-A10B), suggesting it is not model-architecture-specific.
  • Training data contains correctly formatted tool call examples with <tool_call>...</tool_call> sequences following </think>.
  • Base models (before fine-tuning) do not exhibit this behavior (tool calls are generated normally).

Any guidance on whether this is a known issue with Megatron SFT + MoE models, or a data preprocessing / template configuration problem, would be greatly appreciated.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the failure with the provided Megatron SFT and SGLang launch commands across the listed Qwen models and training modes. Compare the training data's tool-call formatting and preprocessing or template configuration with the generated output; done means the fine-tuned model consistently emits a complete valid tool-call block in multi-turn inference.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
ai, machine-learning
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.