Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/source/en/model_doc/qwen3_5.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ print(tokenizer.decode(generated_ids[0], skip_special_tokens=True))
## Usage tips and notes

- Layers are hybrid: [`Qwen3_5TextConfig`]'s `layer_types` is a per-layer list of `"linear_attention"` or `"full_attention"` that encodes the 3:1 Gated DeltaNet / Gated Attention stack. The DeltaNet path (`Qwen3NextGatedDeltaNet`) needs the optional `causal_conv1d` (from [Dao-AILab](https://github.com/Dao-AILab/causal-conv1d)) and `fla` packages for its fast kernels — without them, the model silently falls back to slower and more memory hungry PyTorch ops.
- On NVIDIA GB10 (compute capability 12.1 / SM121) neither `causal_conv1d` nor `fla` ship an SM121 build, so the DeltaNet path always falls back to the slow PyTorch reference. Passing `use_kernels=True` (`pip install -U kernels`) to [`~PreTrainedModel.from_pretrained`] swaps the Gated DeltaNet conv1d and delta-rule cores for a compute-capability-gated Hub kernel ([`Atlas-Inference/gdn`](https://huggingface.co/kernels/Atlas-Inference/gdn)); every other GPU keeps the existing path. The kernel is numerically faithful to the fallback (identical greedy output) and speeds up prefill. Measured on `Qwen/Qwen3.6-27B` (bf16, GB10/SM121, 1024-token prompt, greedy decode of 256 tokens):

| `use_kernels` | TTFT (prefill) | Decode |
| --- | --- | --- |
| `False` (PyTorch fallback) | 1.66 s | 4.11 tok/s |
| `True` ([`Atlas-Inference/gdn`](https://huggingface.co/kernels/Atlas-Inference/gdn)) | 1.11 s (1.49x faster) | 4.14 tok/s |

Decode is unchanged because the single-token DeltaNet recurrence is memory-bandwidth-bound; the win is on the chunked-prefill core and grows with prompt length. Loading the mapped kernel currently requires `trust_remote_code=True` until `Atlas-Inference` is added to the trusted-kernels allowlist.
- Multimodal RoPE splits the head dimension into three components (temporal, height, width) via `mrope_section` on the text config. If you replace the rotary module, preserve this split or position encodings for image and video tokens will be misaligned.
- Use [`Qwen3_5ForCausalLM`] for text-only generation with [`Qwen3_5TextConfig`]; use [`Qwen3_5ForConditionalGeneration`] with the full [`Qwen3_5Config`] and a processor ([`~AutoProcessor.from_pretrained`]) to feed interleaved image/video + text via [`~ProcessorMixin.apply_chat_template`].

Expand Down
8 changes: 8 additions & 0 deletions docs/source/en/model_doc/qwen3_5_moe.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ print(tokenizer.decode(generated_ids[0], skip_special_tokens=True))
- For Qwen3.5-35B-A3B, the text config uses `hidden_size=2048` across 40 layers, 256 experts with 8 routed + 1 shared per token, and `moe_intermediate_size=512` — very different shapes from the dense Qwen3.5 checkpoints, so weights are not interchangeable.
- Native context is 262,144 tokens. To reach the advertised ~1M context, enable YaRN rope scaling via the config's `rope_scaling` field — plain loading gives you the native window only.
- As with Qwen3.5, linear-attention layers depend on optional `causal_conv1d` (from [Dao-AILab](https://github.com/Dao-AILab/causal-conv1d)). Without it, the model silently falls back to slower and more memory hungry PyTorch ops.
- On NVIDIA GB10 (compute capability 12.1 / SM121) `causal_conv1d` and `fla` have no SM121 build, so the Gated DeltaNet path always uses the slow PyTorch reference. Passing `use_kernels=True` (`pip install -U kernels`) to [`~PreTrainedModel.from_pretrained`] swaps it for the same compute-capability-gated Hub kernel as the dense variant ([`Atlas-Inference/gdn`](https://huggingface.co/kernels/Atlas-Inference/gdn), shared because `Qwen3_5MoeGatedDeltaNet` has the same core as `Qwen3_5GatedDeltaNet`); every other GPU keeps the existing path. The kernel is numerically faithful to the fallback (identical greedy output) and speeds up prefill. Measured on `Qwen/Qwen3.6-35B-A3B` (bf16, GB10/SM121, 1024-token prompt, greedy decode of 256 tokens):

| `use_kernels` | TTFT (prefill) | Decode |
| --- | --- | --- |
| `False` (PyTorch fallback) | 0.73 s | 16.3 tok/s |
| `True` ([`Atlas-Inference/gdn`](https://huggingface.co/kernels/Atlas-Inference/gdn)) | 0.53 s (1.38x faster) | 16.7 tok/s |

Decode is roughly flat because the single-token DeltaNet recurrence is memory-bandwidth-bound; the win is on the chunked-prefill core and grows with prompt length. Loading the mapped kernel currently requires `trust_remote_code=True` until `Atlas-Inference` is added to the trusted-kernels allowlist.

## Qwen3_5MoeConfig

Expand Down
14 changes: 14 additions & 0 deletions src/transformers/integrations/hub_kernels.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

if is_kernels_available():
from kernels import (
CUDAProperties,
Device,
FuncRepository,
LayerRepository,
Expand Down Expand Up @@ -119,6 +120,19 @@ def _build_kernel_mapping() -> dict:
# version=1,
# )
# },
# GB10/SM121 GDN fast path (no fla/causal_conv1d build there); dense and MoE share it.
"Qwen3_5GatedDeltaNet": {
Device(
type="cuda",
properties=CUDAProperties(min_capability=121, max_capability=121),
): LayerRepository(
repo_id="Atlas-Inference/gdn",
layer_name="Qwen3_5GatedDeltaNet",
revision="ef12347fc77d6ddf1cb72c0bd0af1c7d6cc69172",
# TODO: drop once Atlas-Inference is an allow-listed trusted publisher
trust_remote_code=True,
),
},
"SwiGLUMLP": {
"cuda": {
Mode.INFERENCE | Mode.TORCH_COMPILE: LayerRepository(
Expand Down
2 changes: 2 additions & 0 deletions src/transformers/models/qwen3_5/modeling_qwen3_5.py

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Ah ig the qwen 3 next variation is not included because of the different memory layout of the projections (fused vs split)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's right, Qwen3-Next uses fused projection weights (in_proj_qkvz, in_proj_ba) while Qwen3.5 deletes those and uses split ones (in_proj_qkv/z/b/a). Our kernel reuses the host module's projections, so it's written against the split layout

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, yes that makes sense!

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from ...activations import ACT2FN
from ...cache_utils import Cache, DynamicCache
from ...generation import GenerationMixin
from ...integrations import use_kernel_forward_from_hub
from ...masking_utils import create_causal_mask
from ...modeling_flash_attention_utils import FlashAttentionKwargs
from ...modeling_layers import (
Expand Down Expand Up @@ -368,6 +369,7 @@ def torch_recurrent_gated_delta_rule(
return core_attn_out, last_recurrent_state


@use_kernel_forward_from_hub("Qwen3_5GatedDeltaNet")
class Qwen3_5GatedDeltaNet(nn.Module):
def __init__(self, config: Qwen3_5Config, layer_idx: int):
super().__init__()
Expand Down
2 changes: 2 additions & 0 deletions src/transformers/models/qwen3_5/modular_qwen3_5.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from ... import initialization as init
from ...cache_utils import Cache, DynamicCache
from ...integrations import use_kernel_forward_from_hub
from ...masking_utils import create_causal_mask
from ...modeling_layers import (
GenericForSequenceClassification,
Expand Down Expand Up @@ -192,6 +193,7 @@ def compute_default_rope_parameters(
return inv_freq, attention_factor


@use_kernel_forward_from_hub("Qwen3_5GatedDeltaNet")
class Qwen3_5GatedDeltaNet(Qwen3NextGatedDeltaNet):
def __init__(self, config: Qwen3_5Config, layer_idx: int):
super().__init__(config, layer_idx)
Expand Down
3 changes: 2 additions & 1 deletion src/transformers/models/qwen3_5_moe/modeling_qwen3_5_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from ...activations import ACT2FN
from ...cache_utils import Cache, DynamicCache
from ...generation import GenerationMixin
from ...integrations import use_experts_implementation
from ...integrations import use_experts_implementation, use_kernel_forward_from_hub
from ...masking_utils import create_causal_mask
from ...modeling_flash_attention_utils import FlashAttentionKwargs
from ...modeling_layers import GradientCheckpointingLayer
Expand Down Expand Up @@ -366,6 +366,7 @@ def torch_recurrent_gated_delta_rule(
return core_attn_out, last_recurrent_state


@use_kernel_forward_from_hub("Qwen3_5GatedDeltaNet")
class Qwen3_5MoeGatedDeltaNet(nn.Module):
def __init__(self, config: Qwen3_5MoeConfig, layer_idx: int):
super().__init__()
Expand Down
3 changes: 3 additions & 0 deletions src/transformers/models/qwen3_5_moe/modular_qwen3_5_moe.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from huggingface_hub.dataclasses import strict

from ... import initialization as init
from ...integrations import use_kernel_forward_from_hub
from ...modeling_layers import GradientCheckpointingLayer
from ...modeling_outputs import BaseModelOutputWithPooling
from ...modeling_utils import PreTrainedModel
Expand Down Expand Up @@ -155,6 +156,8 @@ class Qwen3_5MoeTextRotaryEmbedding(Qwen3_5TextRotaryEmbedding):
pass


# Same GDN core as the dense variant, so it reuses the dense Hub kernel name.
@use_kernel_forward_from_hub("Qwen3_5GatedDeltaNet")
class Qwen3_5MoeGatedDeltaNet(Qwen3_5GatedDeltaNet):
pass

Expand Down
Loading