Skip to content

Qwen3.8-Flash-Next: the n-gram shift and the PLE short convolution ignore packed-sequence boundaries, so padding-free training mixes tokens across samples #51572

Done

Description

@albertvillanova

System Info

  • transformers version: 5.24.0
  • Platform: Linux-6.8.0-1066-aws-x86_64-with-glibc2.35
  • Python version: 3.13.9
  • Huggingface_hub version: 2.5.0
  • Safetensors version: 0.8.0
  • Accelerate version: 1.16.0
  • Accelerate config: not found
  • DeepSpeed version: not installed
  • PyTorch version (accelerator?): 2.14.1+cu130 (CUDA)
  • Using distributed or parallel set-up in script?: no
  • Using GPU in script?: yes
  • GPU type: NVIDIA H100 80GB HBM3

Same result on main (5.25.0.dev0).

Who can help?

@vasqu @3outeille

Reproduction

With packed sequences (DataCollatorWithFlattening(return_flash_attn_kwargs=True, return_seq_idx=True), use_cache=False), the log-probs of a sequence depend on the sequence packed before it, on its first tokens. Tiny random Qwen3.8-Flash-Next (one linear + one indexed attention layer), with its PLE layer (n-gram embedding + PLE short convolution) on or off:

import torch
from transformers import AutoConfig, AutoModelForImageTextToText, DataCollatorWithFlattening


# Tiny Qwen3.8-Flash-Next (one linear + one indexed attention layer), with its n-gram/PLE layer on or off
text = {
    "hidden_size": 32, "num_attention_heads": 4, "linear_num_key_heads": 2, "linear_num_value_heads": 4, "linear_key_head_dim": 16, "linear_value_head_dim": 16, "num_key_value_heads": 2, "num_experts": 4, "num_experts_per_tok": 2,
    "moe_intermediate_size": 32, "shared_expert_intermediate_size": 32, "hc_lowrank": 8, "ple_embed_dim": 32,
    "ngram_vocab_size_base": 1000, "split_ngram_parts": 2, "indexer_n_heads": 2, "indexer_head_dim": 64,
    "num_hidden_layers": 2, "layer_types": ["linear_attention", "indexed_attention"], "ple_layer_ids": [1],
}
vision = {"hidden_size": 16, "depth": 1, "num_heads": 2, "intermediate_size": 32, "out_hidden_size": 32}
collator = DataCollatorWithFlattening(return_flash_attn_kwargs=True, return_seq_idx=True, return_tensors="pt")
doc = list(range(100, 124))  # the document we score
for ple_layer_ids in ([1], []):
    config = AutoConfig.from_pretrained("Qwen/Qwen3.8-Flash-Next", text_config={**text, "ple_layer_ids": ple_layer_ids}, vision_config=vision)
    torch.manual_seed(0)
    model = AutoModelForImageTextToText.from_config(config).to("cuda", torch.bfloat16).eval()

    def logps(prefix):  # `doc` packed after `prefix`
        batch = collator([{"input_ids": prefix}, {"input_ids": doc}])
        batch = {k: v.cuda() if torch.is_tensor(v) else v for k, v in batch.items() if k != "labels"}
        with torch.no_grad():
            return model(**batch, use_cache=False).logits[0, len(prefix):].float().log_softmax(-1)

    diff = (logps(list(range(200, 220))) - logps(list(range(300, 320)))).abs().amax(-1)
    print(f"ple_layer_ids={ple_layer_ids}: max |Δlogp| of the same document after two prefixes, per position: {[round(x, 4) for x in diff[:6].tolist()]} ...")
ple_layer_ids=[1]: max |Δlogp| of the same document after two prefixes, per position: [0.0209, 0.0123, 0.0078, 0.0105, 0.0054, 0.0037] ...
ple_layer_ids=[]: max |Δlogp| of the same document after two prefixes, per position: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ...

Without the PLE layer the difference is exactly 0: attention and gated DeltaNet keep packed sequences apart. Two steps of the PLE layer don't:

  • Qwen4ExpTextNGramEmbedding._shift_right_ignore_eos resets the n-gram context only at eos_token_id (<|endoftext|>), so the first tokens of a packed sequence read the previous sequence's last tokens.
  • Qwen4ExpTextPLELayer._short_conv (dilated conv1d) doesn't take packed-sequence boundaries into account, so it reads the previous sequence's last short_conv_state_len positions.

Since #51273 both receive the forward's kwargs (which carry seq_idx and cu_seq_lens_q for packed batches), but neither uses them.

This affects fine-tuning Qwen/Qwen3.8-Flash-Next with packing (e.g. TRL's SFTTrainer(padding_free=True)), where chat samples end with <|im_end|>, not <|endoftext|>. Similar to #51205 (Inkling short convolutions).

Expected behavior

A packed sequence's outputs don't depend on the other sequences in the pack, as for attention and gated DeltaNet (difference 0 in the reproduction above).

Activity

  1. vasqu commented on Dec 1, 2026

    @vasqu
    Collaborator

    Hmm so the PLE is the whole issue 😭

    Re 1. this feels fairly doable, for 2. we would need to update the kernels conv1d to support dilation tbh (cc @kashif if you are interested)

  2. albertvillanova commented on Dec 2, 2026

    @albertvillanova
    MemberAuthor

    The n-gram shift part is fixed by #51576. The PLE short conv part needs dilation in causal_conv1d_fn: huggingface/kernels-community#1404, then a follow-up here.

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

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions