diff --git a/docs/source/en/model_doc/qwen3_5.md b/docs/source/en/model_doc/qwen3_5.md index 2f72c6ad107c..fee3d922ea41 100644 --- a/docs/source/en/model_doc/qwen3_5.md +++ b/docs/source/en/model_doc/qwen3_5.md @@ -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`]. diff --git a/docs/source/en/model_doc/qwen3_5_moe.md b/docs/source/en/model_doc/qwen3_5_moe.md index fb3e8d806364..dc4b22bccec6 100644 --- a/docs/source/en/model_doc/qwen3_5_moe.md +++ b/docs/source/en/model_doc/qwen3_5_moe.md @@ -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 diff --git a/src/transformers/integrations/hub_kernels.py b/src/transformers/integrations/hub_kernels.py index 3d3df4a62e2b..a3ebc9b8f1f3 100644 --- a/src/transformers/integrations/hub_kernels.py +++ b/src/transformers/integrations/hub_kernels.py @@ -55,6 +55,7 @@ if is_kernels_available(): from kernels import ( + CUDAProperties, Device, FuncRepository, LayerRepository, @@ -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( diff --git a/src/transformers/models/qwen3_5/modeling_qwen3_5.py b/src/transformers/models/qwen3_5/modeling_qwen3_5.py index 109b5ca5a2fc..21a8032e34f4 100644 --- a/src/transformers/models/qwen3_5/modeling_qwen3_5.py +++ b/src/transformers/models/qwen3_5/modeling_qwen3_5.py @@ -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 ( @@ -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__() diff --git a/src/transformers/models/qwen3_5/modular_qwen3_5.py b/src/transformers/models/qwen3_5/modular_qwen3_5.py index 91457c58f3ce..6a49a4e8f602 100644 --- a/src/transformers/models/qwen3_5/modular_qwen3_5.py +++ b/src/transformers/models/qwen3_5/modular_qwen3_5.py @@ -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, @@ -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) diff --git a/src/transformers/models/qwen3_5_moe/modeling_qwen3_5_moe.py b/src/transformers/models/qwen3_5_moe/modeling_qwen3_5_moe.py index cbfbbed86f51..8249194c7f61 100644 --- a/src/transformers/models/qwen3_5_moe/modeling_qwen3_5_moe.py +++ b/src/transformers/models/qwen3_5_moe/modeling_qwen3_5_moe.py @@ -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 @@ -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__() diff --git a/src/transformers/models/qwen3_5_moe/modular_qwen3_5_moe.py b/src/transformers/models/qwen3_5_moe/modular_qwen3_5_moe.py index c55bfe59fe49..4c6d8259e74e 100644 --- a/src/transformers/models/qwen3_5_moe/modular_qwen3_5_moe.py +++ b/src/transformers/models/qwen3_5_moe/modular_qwen3_5_moe.py @@ -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 @@ -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