Stable-DiffCoder icon indicating copy to clipboard operation
Stable-DiffCoder copied to clipboard

How to fine-tune DiffCoder?

Open osyvokon opened this issue 7 months ago • 2 comments

Hi ByteDance! First of all, great job on Stable-DiffCoder! 🚀

I'm trying to understand the recommended way to fine-tune the model for a custom use case. As far as I understand, traditional auto-regressive frameworks like trl doesn't match the pretraining objective and won't work or, worse, will silently degrade performance.

Is there an official or recommended fine-tuning pipeline?

osyvokon avatar Apr 20 '26 10:04 osyvokon

Hi, thanks a lot for your interest in our work.

Unfortunately, we used an internal training framework, so we are not able to release the original training code. However, adapting existing diffusion-LLM training code to Stable-DiffCoder should be fairly straightforward as long as a few key details are handled correctly:

  1. Loss should only be computed on masked positions. Please distinguish this from the prompt mask used in standard SFT settings. For Stable-DiffCoder, only the positions that are actually diffusion-masked should contribute to the loss. For the linear mask-ratio sampling strategy, you can refer to the LLaDA guidelines here: https://github.com/ML-GSAI/LLaDA/blob/main/GUIDELINES.md

  2. Mask token ID matters. In Stable-DiffCoder, the mask token ID is 5.

  3. Labels are not shifted as in autoregressive training. Unlike standard AR training, labels in Stable-DiffCoder should not be shifted by one token. A [MASK] token is trained to predict the token at the same position, not the next token.

  4. The model architecture / masking setup should match block diffusion. Stable-DiffCoder uses the block-diffusion masking strategy: during input construction, a copy of the sequence is concatenated to the end; masking is applied only to the first copy; and loss is computed only on the masked positions in that first copy. The attention mask should follow the design illustrated in Figure 3 of this paper: https://arxiv.org/pdf/2503.09573

  5. The loss should be reweighted by the inverse mask ratio. For example, if only 1 out of 100 positions is masked, the loss should be multiplied by 100, and then divided by the total sequence length.

  6. For SFT on top of Stable-DiffCoder-Instruct, keeping the original system prompt is helpful. If you want to preserve instruct performance while continuing SFT from Stable-DiffCoder-Instruct, we recommend using the original system prompt / chat template from: https://huggingface.co/ByteDance-Seed/Stable-DiffCoder-8B-Instruct/blob/main/tokenizer_config.json

  7. For longer contexts, increasing max_position_embeddings is a reasonable option. Since the model uses a relatively large rope_theta, it tends to have fairly good extrapolation behavior, so directly extending to longer contexts can work reasonably well in practice.

If these points are implemented correctly, most training frameworks should be adaptable without too much difficulty.

We have not personally trained Stable-DiffCoder using an open-source framework, so we cannot endorse one as an official pipeline. But for reference, the following projects may be useful:

If you run into any other issues while adapting the training pipeline, I’d be very happy to help answer them.

Hope this helps.

Facico avatar Apr 21 '26 08:04 Facico

Thank you for the detailed answer, it's very helpful!

osyvokon avatar Apr 21 '26 14:04 osyvokon