# SPDX-FileCopyrightText: Copyright (c) 2023-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """Modify state_dict and config for exporting speculative decoding in official format.""" import json import re import shutil import tempfile from abc import ABC, abstractmethod from contextlib import ExitStack from copy import deepcopy from pathlib import Path import torch import torch.nn as nn from safetensors import safe_open from safetensors.torch import save_file from modelopt.torch.utils.plugins.hf_checkpoint_utils import indexed_weight_map from ..quant_aware_conversion import revert_quant_config_names from .hf_spec_configs import kimik2_eagle_template_config, llama_eagle_template_config ALL_SPEC_MODES = ["eagle", "dflash"] def _get_rope_theta(config, default=None): """Get RoPE theta from either legacy or Transformers 5 config fields. ``rope_parameters`` is checked FIRST. A config can carry both fields with different values: Transformers 5 stores the real base under ``rope_parameters`` while the class default (10000.0 for Qwen3) may still be visible as a top-level ``rope_theta``. Reading ``rope_theta`` first silently exports a draft whose RoPE base is 100x off the target's, which breaks serving because DFlash injects the target's KV into every draft layer. """ # Transformers 5 stores this under rope_parameters (and exposes the same # data through rope_scaling for backwards compatibility). for attr in ("rope_parameters", "rope_scaling"): rope_config = getattr(config, attr, None) if isinstance(rope_config, dict) and rope_config.get("rope_theta") is not None: return rope_config["rope_theta"] rope_theta = getattr(config, "rope_theta", None) if rope_theta is not None: return rope_theta return default LLAMA_EAGLE_SINGLE_LAYER = { "required": { "layers.0.self_attn.q_proj", "layers.0.self_attn.k_proj", "layers.0.self_attn.v_proj", "layers.0.self_attn.o_proj", "layers.0.mlp.gate_proj", "layers.0.mlp.up_proj", "layers.0.mlp.down_proj", "layers.0.hidden_norm", "layers.0.input_layernorm", "layers.0.post_attention_layernorm", "norm", "fc", }, "optional": {"d2t", "lm_head"}, } KIMIK2_EAGLE_SINGLE_LAYER = { "required": { "layers.0.self_attn.kv_a_layernorm", "layers.0.self_attn.q_a_layernorm", "layers.0.self_attn.q_a_proj", "layers.0.self_attn.q_b_proj", "layers.0.self_attn.kv_a_proj_with_mqa", "layers.0.self_attn.kv_b_proj", "layers.0.self_attn.o_proj", "layers.0.mlp.gate_proj", "layers.0.mlp.up_proj", "layers.0.mlp.down_proj", "layers.0.hidden_norm", "layers.0.input_layernorm", "layers.0.post_attention_layernorm", "norm", "fc", }, "optional": { "d2t", "lm_head", }, } def has_spec_opt(model: nn.Module): """Check if the model has speculative decoding optimization.""" opt_modes = getattr(model, "_modelopt_state", []) return any(mode[0] in ALL_SPEC_MODES for mode in opt_modes) def has_quant_opt(model: nn.Module): """Check if the model has quantization optimization.""" opt_modes = getattr(model, "_modelopt_state", []) return any(mode[0] == "quantize" for mode in opt_modes) class SpeculativeDecodingExporter(ABC): """Export a modelopt speculative decoding checkpoint to deployment format.""" def __init__(self, model: nn.Module): """Initialize the SpeculativeDecodingExporter.""" self.model = model @abstractmethod def export( self, export_dir: Path | str, dtype: torch.dtype | None = None, ): """Export the model to the deployment format.""" raise NotImplementedError("Subclasses must implement this method.") class NativeMTPExporter(SpeculativeDecodingExporter): """Stream a saved MTP checkpoint into its adapter's native deployment layout.""" def __init__(self, training_checkpoint: str | Path, base_model: str | Path, *, device="cpu"): """Keep checkpoint paths so export never materializes the complete MTP model.""" self.training_checkpoint = Path(training_checkpoint) self.base_model = Path(base_model).resolve() self.device = device def export(self, export_dir: Path | str, dtype: torch.dtype | None = None): """Write only native MTP tensors and the original model configuration.""" # Speculative plugins import this module; the streaming writer imports unified export. from modelopt.torch.speculative.mtp import NativeMTPCheckpointError, get_native_mtp_adapter from ..unified_export_hf_streaming import _StreamingShardWriter if dtype is not None: raise ValueError("Native MTP export preserves native tensor dtypes; omit dtype.") output = Path(export_dir).absolute() if output.exists(): raise FileExistsError(f"Native MTP export destination already exists: {output}") config = json.loads((self.training_checkpoint / "config.json").read_text()) if not isinstance(config.get("mtp_boost"), dict): raise NativeMTPCheckpointError("Expected an MTP boosting training checkpoint") adapter = get_native_mtp_adapter( self.base_model, config["mtp_boost"].get("mtp_adapter", "auto") ) mtp = adapter.load_mtp(self.base_model, dtype=torch.bfloat16, device="meta") expected = mtp.state_dict() trained_map = indexed_weight_map(self.training_checkpoint) trained_keys = { key.removeprefix("eagle_module.") for key in trained_map if key.startswith("eagle_module.") } if trained_keys != set(expected): raise NativeMTPCheckpointError( "Expected a complete HF MTP training checkpoint matching the native model" ) with ExitStack() as stack, torch.no_grad(): handles = { name: stack.enter_context( safe_open(str(self.training_checkpoint / name), framework="pt") ) for name in {trained_map["eagle_module." + key] for key in expected} } state = {} for key, parameter in expected.items(): saved_key = "eagle_module." + key tensor = handles[trained_map[saved_key]].get_slice(saved_key) if tensor.get_shape() != list(parameter.shape) or tensor.get_dtype() not in ( "BF16", "F32", ): raise NativeMTPCheckpointError(f"Incompatible trained MTP tensor: {saved_key}") state[key] = tensor output.parent.mkdir(parents=True, exist_ok=True) with tempfile.TemporaryDirectory(prefix=".mtp-export-", dir=output.parent) as temporary: staging = Path(temporary) writer = _StreamingShardWriter(staging, max_shard_size=1_000_000_000) for name, tensor in adapter.export_tensors( self.base_model, mtp, state, device=self.device ): writer.add(name, tensor.detach().cpu().contiguous()) writer.finalize() shutil.copy2(self.base_model / "config.json", staging / "config.json") if output.exists(): raise FileExistsError(output) staging.rename(output) class EagleExporter(SpeculativeDecodingExporter): """Draft model exporter for Eagle.""" def __init__(self, model: nn.Module): """Initialize the EagleExporter.""" super().__init__(model) self.eagle_decoder_type = model.eagle_config.eagle_decoder_type self.num_hidden_layers = model.eagle_config.num_hidden_layers def _check_valid_sd(self, export_sd: dict): """Check the export state dict is valid, otherwise raise Exception.""" expected_keys_single_layer = { "llama": LLAMA_EAGLE_SINGLE_LAYER, "kimik2": KIMIK2_EAGLE_SINGLE_LAYER, }[self.eagle_decoder_type] # fc and hidden_norm are only present when use_aux_hidden_state=True use_aux = getattr(self.model.eagle_config, "use_aux_hidden_state", False) aux_only_keys = {"fc", "layers.0.hidden_norm"} required_keys = set(expected_keys_single_layer["required"]) if not use_aux: required_keys -= aux_only_keys # Check that export sd has required keys for key in required_keys: assert f"{key}.weight" in export_sd, f"Missing required key: {key}.weight" for i in range(1, self.num_hidden_layers): for key in required_keys - { "layers.0.hidden_norm", "layers.0.input_layernorm", "norm", "fc", }: assert f"{key}.weight".replace("layers.0", f"layers.{i}") in export_sd, ( f"Missing required key: {key}.weight" ) # Check that export sd has no unexpected keys # Note that quantized eagle are allowed to have scales allowed_keys_single_layer = ( expected_keys_single_layer["required"] | expected_keys_single_layer["optional"] ) for key in export_sd: assert ( re.sub(r"layers\.\d+\.", "layers.0.", key.rsplit(".", 1)[0]) in allowed_keys_single_layer ), f"Unexpected key: {key}" def _extract_state_dict(self, full_state_dict: dict): """Extract and return eagle state dict in deployment format.""" export_sd = {} for key in full_state_dict: if "eagle_module" in key: export_key = key.replace("eagle_module.", "") export_sd[export_key] = full_state_dict[key].clone() # Use base model's lm head if draft model doesn't have one if "lm_head.weight" not in export_sd: export_sd["lm_head.weight"] = full_state_dict["lm_head.weight"] self._check_valid_sd(export_sd) return export_sd def _export_config(self): """Export config.json in deployment format.""" template_config: dict = { "llama": llama_eagle_template_config, "kimik2": kimik2_eagle_template_config, }[self.model.eagle_config.eagle_decoder_type] template_config = deepcopy(template_config) def _get_config_from_draft_or_base(key: str, model: nn.Module): if getattr(model.eagle_config, key, None) is not None: return getattr(model.eagle_config, key) elif getattr(model.config, key, None) is not None: return getattr(model.config, key) else: return None for key in template_config: value = template_config[key] if isinstance(value, dict): # for eagle config, we find it in model.eagle_config for sub_key in value: if value[sub_key] is None: value[sub_key] = _get_config_from_draft_or_base(sub_key, self.model) elif value is None: # First, we try to load from eagle config. new_value = _get_config_from_draft_or_base(key, self.model) # If the value is a torch.dtype, we convert to string for serialization. if isinstance(new_value, torch.dtype): new_value = str(new_value).replace("torch.", "") template_config[key] = new_value # Inject export rope scaling override when training rope_type is "default". rope_cfg = self.model.eagle_config.rope_scaling or {} training_rope_type = rope_cfg.get("rope_type") or rope_cfg.get("type") eagle_export_rope_scaling = getattr(self.model, "eagle_export_rope_scaling", None) if eagle_export_rope_scaling and training_rope_type == "default": template_config["rope_scaling"] = eagle_export_rope_scaling # In transformers 5.x, rope_theta is under rope_scaling, not the main config. # Always source from the training rope config (rope_theta is not in export overrides). if template_config.get("rope_theta") is None and rope_cfg: template_config["rope_theta"] = rope_cfg.get("rope_theta") return template_config def _export_lora(self, export_dir: Path, full_sd: dict): """Export base model LoRA adapter weights alongside the eagle module artifacts.""" from peft import LoraConfig lora_sd = {k: v for k, v in full_sd.items() if ".lora_A." in k or ".lora_B." in k} if not lora_sd: raise RuntimeError( "No LoRA adapter tensors found in the model state dict. " "Ensure eagle_base_lora=True and the model was converted with LoRA adapters." ) # Reformat keys to match peft's standard adapter file format: # 1. Strip the adapter name (e.g. ".default") — peft re-inserts it on load. # 2. Add "base_model.model." prefix — peft expects this in saved adapters. lora_sd = { re.sub(r"(lora_[AB])\.\w+\.weight$", r"\1.weight", k): v for k, v in lora_sd.items() } lora_sd = {f"base_model.model.{k}": v for k, v in lora_sd.items()} save_file(lora_sd, export_dir / "adapter_model.safetensors") # Infer target modules from the exported LoRA keys (e.g., "q_proj", "v_proj") # Keys are like: base_model.model.model.layers.0.self_attn.q_proj.lora_A.weight target_modules = sorted({k.split(".")[-3] for k in lora_sd if ".lora_A." in k}) lora_config = LoraConfig( r=self.model.eagle_base_lora_rank, lora_alpha=self.model.eagle_base_lora_alpha, target_modules=target_modules, bias="none", ) with open(export_dir / "adapter_config.json", "w") as f: json.dump( lora_config.to_dict(), f, indent=4, default=lambda o: sorted(o) if isinstance(o, set) else o, ) def export( self, export_dir: Path | str, dtype: torch.dtype | None = None, ): """Export the model to the deployment format.""" # Make export dir export_dir = Path(export_dir) export_dir.mkdir(parents=True, exist_ok=True) # Export quantized modules if has_quant_opt(self.model): from ..unified_export_hf import _export_transformers_checkpoint full_sd, hf_quant_config = _export_transformers_checkpoint(self.model, dtype) else: full_sd, hf_quant_config = self.model.state_dict(), None # Export state dict drafter_sd = self._extract_state_dict(full_sd) save_file(drafter_sd, f"{export_dir}/model.safetensors") # Export config drafter_config = self._export_config() if hf_quant_config is not None: drafter_config["quantization_config"] = hf_quant_config with open(f"{export_dir}/config.json", "w") as file: json.dump(drafter_config, file, indent=4) # Export hf_quant_config for backward compatibility if hf_quant_config is not None: with open(f"{export_dir}/hf_quant_config.json", "w") as file: json.dump(hf_quant_config, file, indent=4) # Export LoRA adapter weights separately if getattr(self.model, "eagle_base_lora", False): self._export_lora(export_dir, full_sd) class EagleMedusaExporter(EagleExporter): """Draft model exporter for EagleMedusa.""" def __init__(self, model: nn.Module): """Initialize the EagleMedusaExporter.""" super().__init__(model) self.parallel_draft_step = model.eagle_config.parallel_draft_step self.parallel_draft_heads_num_layers = model.eagle_config.parallel_draft_heads_num_layers # NOTE: tmp: bypassing format check for parallel draft self._check_valid_sd = lambda *args, **kwargs: None def _extract_state_dict(self, full_state_dict: dict): """Extract the state dict of the draft model in deployment format.""" export_sd = super()._extract_state_dict(full_state_dict) if self.parallel_draft_step <= 1: return export_sd for i in range(self.parallel_draft_step - 1): for j in range(self.parallel_draft_heads_num_layers): export_sd[f"parallel_draft_heads.{i}.medusa_layers.{j}.linear.weight"] = ( export_sd.pop(f"parallel_draft_heads.medusa_heads.{i}.{j}.linear.weight") ) if f"parallel_draft_heads.medusa_heads.{i}.{j}.linear.bias" in export_sd: export_sd[f"parallel_draft_heads.{i}.medusa_layers.{j}.linear.bias"] = ( export_sd.pop(f"parallel_draft_heads.medusa_heads.{i}.{j}.linear.bias") ) return export_sd class DFlashExporter(SpeculativeDecodingExporter): """Draft model exporter for DFlash. Exports in z-lab compatible format: - model.safetensors: draft module weights (no prefix) - config.json: Qwen3-style config with dflash_config field """ def _map_export_name(self, name: str) -> str: return name.split("dflash_module.", 1)[-1] def _extract_state_dict(self, full_state_dict: dict): """Extract DFlash module weights, stripping the dflash_module prefix.""" export_sd = {} for key, value in full_state_dict.items(): if "dflash_module." in key: export_key = self._map_export_name(key) # Skip rotary embedding buffers (not needed, recomputed) if "rotary_emb" in export_key: continue export_sd[export_key] = value.clone() return export_sd def _export_config(self): """Build config.json matching z-lab DFlash format.""" model = self.model base_config = ( getattr(model.config, "text_config", None) or getattr(model.config, "llm_config", None) or model.config ) draft_config = model.dflash_config config = { "architectures": ["DFlashDraftModel"], # DFlash draft uses Qwen3 architecture regardless of base model type. # Use "qwen3" as default; base model types like "qwen3_5_text" are not # recognized by vLLM for draft model loading. "model_type": "qwen3", "block_size": model.dflash_block_size, "dflash_config": { "mask_token_id": model.mask_token_id, "target_layer_ids": list(model.target_layer_ids), }, # Architecture dimensions "hidden_size": getattr(draft_config, "hidden_size", base_config.hidden_size), "num_hidden_layers": draft_config.num_hidden_layers, "num_attention_heads": getattr( draft_config, "num_attention_heads", base_config.num_attention_heads ), "num_key_value_heads": getattr( draft_config, "num_key_value_heads", base_config.num_key_value_heads ), "head_dim": getattr( draft_config, "head_dim", base_config.hidden_size // base_config.num_attention_heads, ), "intermediate_size": getattr( draft_config, "intermediate_size", base_config.intermediate_size ), "hidden_act": getattr(draft_config, "hidden_act", "silu"), "rms_norm_eps": getattr(draft_config, "rms_norm_eps", 1e-6), "vocab_size": base_config.vocab_size, "max_position_embeddings": getattr(base_config, "max_position_embeddings", 32768), "initializer_range": getattr(base_config, "initializer_range", 0.02), "attention_bias": getattr(draft_config, "attention_bias", False), "attention_dropout": getattr(draft_config, "attention_dropout", 0.0), # Inherit the target's RoPE base: DFlash injects target KV into every draft # layer, so their RoPE bases must match. Transformers 5 stores rope_theta # in rope_parameters rather than a top-level config attribute. "rope_theta": _get_rope_theta(base_config, _get_rope_theta(draft_config, 1000000.0)), # YaRN long-context scaling is injected below (see the rope_scaling block). "rope_scaling": None, "tie_word_embeddings": False, # config.dtype is None, not absent, when unset, so a getattr default never applies. "torch_dtype": str(getattr(base_config, "dtype", None) or torch.bfloat16).replace( "torch.", "" ), "num_target_layers": base_config.num_hidden_layers, } # Add layer_types if present (Qwen3-style) if hasattr(draft_config, "layer_types"): config["layer_types"] = draft_config.layer_types else: config["layer_types"] = ["full_attention"] * draft_config.num_hidden_layers # Sliding-window attention: all draft layers use SWA. vLLM's # _resolve_layer_attention reads dflash_config.use_swa + swa_window_size; with # layer_types left all "full_attention" it applies a sliding window to every draft # layer (window from swa_window_size / top-level sliding_window). swa_window = getattr(self.model, "dflash_swa_window_size", None) if swa_window is not None: config["sliding_window"] = swa_window config["dflash_config"].update( { "use_swa": True, "swa_window_size": swa_window, } ) # Block-internal attention pattern. Emitted unconditionally (not just under SWA): # vLLM's _dflash_layer_causal treats dflash_config.causal as an all-layer override, # and its default differs per layer type, so writing it explicitly is what keeps # inference consistent with how the draft was actually trained. config["dflash_config"]["causal"] = ( getattr(self.model, "dflash_draft_attention", "bidirectional") == "causal" ) # Learnable per-head attention sink. vLLM reads dflash_config.attention_sink_bias to # decide whether to build the sink parameter and pass it to its attention kernel. if getattr(self.model, "dflash_attention_sink", False): config["dflash_config"]["attention_sink_bias"] = True config["attention_sink_bias"] = True # Inject the export-time YaRN rope_scaling from the dflash_export_rope_scaling # config field (empty dict disables). Mirrors eagle's eagle_export_rope_scaling. export_rope_scaling = getattr(self.model, "dflash_export_rope_scaling", None) if export_rope_scaling: config["rope_scaling"] = export_rope_scaling return config def export(self, export_dir: Path | str, dtype: torch.dtype | None = None): """Export the DFlash draft model to deployment format.""" export_dir = Path(export_dir) export_dir.mkdir(parents=True, exist_ok=True) # Export quantized modules if applicable if has_quant_opt(self.model): from ..unified_export_hf import _export_transformers_checkpoint module_names = tuple(name for name, _ in self.model.named_modules()) full_sd, hf_quant_config = _export_transformers_checkpoint(self.model, dtype) if hf_quant_config is not None: revert_quant_config_names( hf_quant_config.get("quantization", {}), self._map_export_name, module_names=module_names, ) else: full_sd, hf_quant_config = self.model.state_dict(), None # Export state dict drafter_sd = self._extract_state_dict(full_sd) assert drafter_sd, "No dflash_module weights found in state dict" if dtype is not None and hf_quant_config is None: drafter_sd = {k: v.to(dtype) for k, v in drafter_sd.items()} save_file(drafter_sd, f"{export_dir}/model.safetensors") # Export config drafter_config = self._export_config() # Update torch_dtype to match actual exported weights if dtype is not None: drafter_config["torch_dtype"] = str(dtype).replace("torch.", "") if hf_quant_config is not None: drafter_config["quantization_config"] = hf_quant_config with open(f"{export_dir}/config.json", "w") as f: json.dump(drafter_config, f, indent=2) if hf_quant_config is not None: with open(f"{export_dir}/hf_quant_config.json", "w") as f: json.dump(hf_quant_config, f, indent=2) print( f"Exported DFlash draft model: {len(drafter_sd)} tensors, " f"config keys: {list(drafter_config.keys())[:5]}..." ) class DominoExporter(DFlashExporter): """Draft model exporter for Domino (DFlash backbone + causal correction head). Same z-lab-compatible format as DFlash, plus the Domino head weights (``prefix_gru.*`` / ``embed_proj.*``, already captured by the inherited ``dflash_module.`` stripping) and the extra config fields the loader needs to rebuild the head (``projector_type``, ``emb_dim``, ``gru_hidden_dim``, ``pure_draft_prefix_len``, ``shift_label``). """ def _export_config(self): """Extend the DFlash config with the Domino head fields.""" config = super()._export_config() draft_config = self.model.dflash_config # Read off the built head rather than the config dict, so the export describes the # head these weights belong to. head = self.model.dflash_module # Mirror the reference checkpoint: emb_dim also appears at the top level. config["emb_dim"] = head.emb_dim config["dflash_config"].update( { "projector_type": getattr(draft_config, "projector_type", "domino"), "shift_label": bool(self.model._label_shift), "pure_draft_prefix_len": head.pure_draft_prefix_len, "gru_hidden_dim": head.gru_hidden_dim, "emb_dim": head.emb_dim, } ) return config class LiLiCorrExporter(DFlashExporter): """Draft model exporter for LiLiCorr (DFlash backbone + candidate-lattice reranker). Same z-lab-compatible format as DFlash, plus the reranker weights (``lilicorr.*``, already captured by the inherited ``dflash_module.`` stripping) and the config fields the serving loader rebuilds the head from. Every geometry field must be emitted: ``lilicorr_logit_scale`` and ``lilicorr_vector_eps`` change the score without changing any tensor shape, so a default guessed in their absence loads cleanly and scores a different function. """ def _export_config(self): """Extend the DFlash config with the LiLiCorr head fields.""" config = super()._export_config() draft_config = self.model.dflash_config head = self.model.dflash_module.lilicorr config["architectures"] = ["LiLiCorrDraftModel"] config["dflash_config"].update( { "projector_type": getattr(draft_config, "projector_type", "lilicorr"), "lilicorr_enabled": True, # Read off the built head rather than the config dict, so the exported # geometry is the geometry of the weights in the same directory. "lilicorr_candidate_topk": head.candidate_topk, "lilicorr_hidden_size": head.hidden_size, "lilicorr_num_layers": len(head.layers), "lilicorr_num_heads": head.num_heads, "lilicorr_mlp_ratio": head.mlp_ratio, "lilicorr_factor_dim": head.factor_dim, "lilicorr_vector_eps": head.vector_eps, "lilicorr_logit_scale": head.logit_scale, } ) # The serving loader builds the conv wrappers from geometry, not from the tensors: # without these two keys it defaults both to 0 and drops the conv tensors silently. conv = getattr(self.model.dflash_module.layers[0], "attention_conv", None) # `taps` is absent on the no-op sublayer wrapper, which is what a conv-free draft # carries, so its presence is the test for whether convolutions were installed. taps = getattr(conv, "taps", None) if conv is not None and taps: config["dflash_config"].update( { "conv_kernel_size": taps, "conv_group_size": conv.group_size, } ) return config class DSparkExporter(DFlashExporter): """Draft model exporter for DSpark (DFlash backbone + sequential Markov head). Vanilla heads use Qwen3DSparkModel to select vLLM's loader. Gated and recurrent heads keep the native layout because that runtime does not implement those variants. """ def _map_export_name(self, name: str) -> str: is_draft = "dflash_module." in name name = super()._map_export_name(name) if is_draft and self.model.dflash_module.markov_head_type == "vanilla": prefixes = { "markov_w1": "markov_head.markov_w1", "markov_w2": "markov_head.markov_w2", "confidence_proj": "confidence_head.proj", } for old_prefix, new_prefix in prefixes.items(): if name == old_prefix or name.startswith(old_prefix + "."): return new_prefix + name[len(old_prefix) :] return name def _export_config(self): """Extend the DFlash config with the DSpark head fields.""" config = super()._export_config() draft_config = self.model.dflash_config # Read off the built head, not the config dict: DSparkModule casts markov_rank to int # and lower-cases markov_head_type, and the export must describe what was trained. head = self.model.dflash_module config["dflash_config"].update( { "projector_type": getattr(draft_config, "projector_type", "dspark"), "shift_label": bool(self.model._label_shift), "markov_rank": head.markov_rank, "markov_head_type": head.markov_head_type, "use_confidence_head": head.use_confidence_head, } ) if config["dflash_config"]["markov_head_type"] == "vanilla": config["architectures"] = ["Qwen3DSparkModel"] # vLLM 0.32's DSparkSpeculator samples the anchor for next-token-aligned drafts. # Qwen3DSparkModel's confidence head also consumes ModelOpt's Markov features. config.update( { "mask_token_id": config["dflash_config"]["mask_token_id"], "target_layer_ids": config["dflash_config"]["target_layer_ids"], "markov_rank": config["dflash_config"]["markov_rank"], "sample_from_anchor": bool(self.model._label_shift), "enable_confidence_head": config["dflash_config"]["use_confidence_head"], "confidence_head_with_markov": config["dflash_config"]["use_confidence_head"], } ) return config class DFlash2Exporter(DFlashExporter): """Draft model exporter for DFlash2 (DFlash backbone + convolutions + selector). Same z-lab-compatible format as DFlash, plus the DFlash2 weights (``layers.*.attention_conv.*`` / ``layers.*.mlp_conv.*`` / ``candidate_selector.*``, already captured by the inherited ``dflash_module.`` stripping) and the config fields the SGLang/vLLM ``DFlash2DraftModel`` loader needs to rebuild them (``conv_kernel_size``, ``conv_group_size``, ``selector_rank``, ``selector_top_k``). The architecture name is what selects the DFlash2 serving path: a checkpoint declaring ``DFlashDraftModel`` loads as a plain DFlash draft and would silently ignore the convolutions and the selector. """ def _export_config(self): """Extend the DFlash config with the DFlash2 architecture fields.""" config = super()._export_config() draft_config = self.model.dflash_config config["architectures"] = ["DFlash2DraftModel"] # Present because HFDFlash2Model.modify validates them at convert time. config["dflash_config"].update( { "projector_type": getattr(draft_config, "projector_type", "dflash2"), "conv_kernel_size": draft_config.conv_kernel_size, "conv_group_size": draft_config.conv_group_size, "selector_rank": draft_config.selector_rank, "selector_top_k": draft_config.selector_top_k, # The published DFlash2 checkpoints carry block_size inside # dflash_config; the DFlash loader reads it from the top level. # Emit both so either contract resolves to the same value. "block_size": config["block_size"], } ) # vLLM reads is_causal from the TOP level, and the published DFlash2 checkpoints # state it explicitly rather than leaving it to be inferred from layer_types. # Mirror dflash_config.causal, which DFlashExporter writes unconditionally from # dflash_draft_attention; no parent writes a top-level is_causal. config["is_causal"] = config["dflash_config"]["causal"] return config