Skip to content

fix: Qwen3-TTS incorrect loss handling#9752

Open
FelysNeko wants to merge 1 commit into
modelscope:mainfrom
FelysNeko:main
Open

fix: Qwen3-TTS incorrect loss handling#9752
FelysNeko wants to merge 1 commit into
modelscope:mainfrom
FelysNeko:main

Conversation

@FelysNeko

Copy link
Copy Markdown
Contributor

PR type

  • Bug Fix
  • New Feature
  • Document Updates
  • More Models or Datasets Support

PR information

总共包含了两个修复和一处代码简化。

sub_talker_loss 没有被传下去

现象:Loss 极低(从 1 开始下降),但是完全无法克隆声音:no_sub_talker_loss.mp3

原代码把 sub_talker_loss 作为动态属性挂在 ModelOutput 上,再由 compute_sft_lossgetattr 读取:

outputs.sub_talker_loss = sub_talker_loss   # 动态属性
# compute_sft_loss:
sub_talker_loss = getattr(outputs, 'sub_talker_loss', None)   # → None

动态属性被 @can_return_tuple / 模型封装在跨边界时剥离,getattr 恒为 None,又被静默兜底成纯 talker loss,code_predictor 整个训练零梯度。

修复:把损失合并移进 tts_forward 内部。

标签 shift 错位

现象:在修复了上一条后,Loss 极高(sub_talker_loss 大约是 11),输出噪音,模型崩溃:double_shift.mp3

上游代码中 forward_sub_talker_finetune 内部调用 code_predictor.forward_finetune,传入的的 labels 是已经预移位一位过的 codec_ids[:, 1:]forward_finetune 中的实现会调用 ForCausalLMLoss 再次 shift,导致双重 shift 的情况。

修复:参考 QwenLM/Qwen3-TTS#278 ,但直接取 sub_talker_logits 手动计算 Loss。

移除手动计算 talker_loss

依然参考 QwenLM/Qwen3-TTS#278 ,并不需要手动计算 talker_loss

Experiment results

Paste your experiment result here(if needed).

SPEAKER_NAME='cyrene' \
CUDA_VISIBLE_DEVICES=0 \
nohup swift sft \
    --model Qwen/Qwen3-TTS-12Hz-0.6B-Base \
    --dataset '/root/autodl-tmp/audio/train/0/Chinese(PRC).jsonl' \
    --split_dataset_ratio 0.0 \
    --load_from_cache_file true \
    --tuner_type full \
    --torch_dtype bfloat16 \
    --num_train_epochs 4 \
    --per_device_train_batch_size 8 \
    --gradient_accumulation_steps 2 \
    --learning_rate 2.29e-5 \
    --weight_decay 0.01 \
    --adam_beta2 0.999 \
    --lr_scheduler_type cosine_with_min_lr \
    --lr_scheduler_kwargs '{"min_lr": 9.94e-6}' \
    --attn_impl 'flash_attention_2' \
    --save_steps 33550336 \
    --max_length 4096 \
    --output_dir /root/autodl-tmp/output/tts \
    --warmup_ratio 0.05 \
    --dataset_num_proc 1 \
    --dataloader_num_workers 4 \
    --save_only_model \
    > "$(date +%s).log" 2>&1 &

成功复刻昔涟:it_works.mp3

并且额外尝试了 examples/models/qwen3_tts/train.sh,能听得出来是芙宁娜:furina.mp3

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the loss calculation for the Qwen TTS model. The computation of the sub-talker loss and the total loss is moved from the template's compute_sft_loss method directly into the model's tts_forward method. This simplifies the template code and centralizes the loss computation within the model's forward pass. I have no feedback to provide as there are no review comments.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@Jintao-Huang

Copy link
Copy Markdown
Collaborator

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants