Skip to content

vllm.model_executor.models.qwen3_dflash

Classes:

Functions:

DFlashQwen3Attention

Bases: Module

Attention for DFlash speculative decoding.

Context KVs are pre-inserted into the KV cache before the forward pass. This layer handles only query tokens via standard attention. Adapted from Qwen3Attention.

Methods:

  • forward –

    DFlash attention assumes that the KV cache is already populated

forward(positions, hidden_states)

DFlash attention assumes that the KV cache is already populated with the context K/V from the target model's hidden states. This forward op computes attention for the query tokens only. See also: precompute_and_store_context_kv

DFlashQwen3ForCausalLM

Bases: Qwen3ForCausalLM

Methods:

_read_mask_embedding()

Checks for an override mask embedding in mask_embedding.pt and returns it.

Some checkpoints ship a separately-trained mask embedding for the mask token, which we use to overwrite the embedding for mask_token_id. This helper checks for the file, loads the pytorch tensor, and returns the embedding to use.

Returns None if the override file is not present.

context_kv_modules()

The modules whose weights combine_hidden_states and precompute_and_store_context_kv read once loaded. With SpeculativeConfig.prefill_context_kv_only, the other draft weights are freed (see DFlashSpeculator.free_unused_weights).

get_draft_attn_causal()

Per-layer attention causality, aligned with get_draft_kv_cache_layer_names.

precompute_and_store_context_kv(context_states, context_positions, context_slot_mapping=None)

Precompute projected + RoPE'd K/V and write to cache.

DFlashQwen3Model

Bases: Module

Methods:

_apply_context_rope(all_k, context_positions)

Apply RoPE in place to the context K of every layer, one fused call.

_build_fused_kv_buffers()

Build fused weight buffers for precompute_and_store_context_kv.

Must be called after weights are loaded. Stacks the KV-projection weights, K-norm weights, and RoPE parameters from every attention layer so that precompute_and_store_context_kv can run one fused GEMM for all layers at once. Also aliases the weight of the hidden_norm.

_load_context_kv(layer_idx, shard_id, param_name, weight)

Load this rank's rows of a layer's K or V checkpoint tensor into the MXFP8 context projection, sharded as the layer's QKV projection does.

_make_mxfp8_context_kv_proj(layers_attn)

One MXFP8 GEMM over the K/V rows of every layer's QKV projection.

MXFP8 block scales are per output row, so it holds the K/V rows of the checkpoint's weights and scales as-is. They are loaded from the checkpoint's K/V tensors (load_weights), so the projection goes through post-load processing and weight reloads like any other layer.

_project_context_kv_token_major(context_states, num_ctx, num_layers, num_kv_heads, head_dim)

Context K/V of every layer, [num_ctx, L, 2, nkv, hd].

context_kv_modules()

The modules whose weights the context K/V path (combine_hidden_states and precompute_and_store_context_kv) reads once loaded. A subclass that changes that path must override this.

precompute_and_store_context_kv(context_states, context_positions, context_slot_mapping=None)

Precompute K/V for context states write them into each layer's KV cache.

Input context states are projected to K/V, normed, and have RoPE applied. Since the context shape is different than the query shape, we can't rely on the regular forward pass to apply torch.compile and CUDA graphs to this section. As such, this function is optimized to minimize the number of torch ops present: we use fused vLLM kernels for RMSNorm and RoPE, fuse the GEMM into one large projection, and avoid cloning buffers (with .contiguous()) where possible.

When context_slot_mapping is None (e.g. during dummy_run) only the computation runs, and no K/V is written to cache.

_dflash_layer_causal(config, layer_idx)

Resolve explicit causality before falling back to legacy layer defaults.

_load_context_kv_rows(param, loaded_weight, row_offset)

Weight loader of the MXFP8 context K/V projection: one layer's K or V rows, at their offset in the stacked projection.

_reads_raw_weights(projs)

Whether post-load .weight is plain (not quantized, not CPU-repacked).

_resolve_layer_attention(config, layer_idx)

Resolve (sliding_window, causal) for one DFlash draft layer.

+----------------------+-------------------------+--------------------------------+ | Config | layer_type | *causal | +======================+=========================+================================+ | layer_types | SWA if use_swa | True if layer_types[i]=SWA | | | else layer_types[i] | else False | +----------------------+-------------------------+--------------------------------+ | layer_types=None | SWA | False | | + use_swa=True | | | +----------------------+-------------------------+--------------------------------+ | layer_types=None | Full | False | | + use_swa=False | | | +----------------------+-------------------------+--------------------------------+ * If dflash_config.causal is set, its value overrides causal for all layers.

This is to support a varied ecosystem of checkpoints, including: - XiaomiMiMo/MiMo-V2.5-Pro-FP4-DFlash (sets "use_swa", assumes non-causal) - z-lab/gemma-4-31B-it-DFlash (has mixed layer types, assumes causal only for SWA) - z-lab/Qwen3.5-9B-DFlash ("standard" DFlash, all full attn, assumes non-causal)

dflash_has_any_non_causal(config)

Whether the draft needs a non-causal-capable backend, resolved from config (config mirror of the model's get_draft_attn_causal, usable pre-build).

dflash_has_any_sliding(config)

Whether any draft layer uses sliding-window attention, resolved from config (config mirror of _resolve_layer_attention, usable pre-build).