# SPDX-FileCopyrightText: Copyright (c) 2026 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. import contextlib import errno import json import os import struct import tempfile from dataclasses import dataclass, field from pathlib import Path from typing import (TYPE_CHECKING, Any, Dict, Generic, Iterator, List, Optional, Sequence, Tuple, TypeVar) import filelock import torch import transformers from transformers.utils import HF_MODULES_CACHE from tensorrt_llm._torch.locality_domain.policy import LocalityDomainPolicy from tensorrt_llm._torch.pyexecutor.config_utils import ( get_glm5_next_layer_masks, get_glm5_next_num_attention_layers, get_kimi_linear_num_attention_layers, get_qwen3_hybrid_num_attention_layers, is_glm5_next, is_kimi_linear, is_nemotron_hybrid, is_qwen3_hybrid, is_qwen4_exp, load_pretrained_config) from tensorrt_llm._utils import (get_sm_version, is_sm_100f, is_sm_120f, torch_dtype_to_binding) from tensorrt_llm.bindings import LayerType as LayerTypeCpp from tensorrt_llm.functional import AllReduceStrategy # Callers import MoeLoadBalancerConfig from this module. from tensorrt_llm.llmapi.llm_args import MoeLoadBalancerConfig # noqa: F401 from tensorrt_llm.llmapi.llm_args import (CSA2SparseAttentionConfig, DeepSeekSparseAttentionConfig, DeepSeekV4SparseAttentionConfig, KvCacheConfig, MoeLoadBalancerConfigType, MultimodalConfig) from tensorrt_llm.logger import logger from tensorrt_llm.mapping import Mapping from tensorrt_llm.models.modeling_utils import QuantConfig from tensorrt_llm.models.quant_config_utils import \ update_quant_config_from_compressed_tensors from tensorrt_llm.quantization.mode import QuantAlgo from tensorrt_llm.quantization.modelopt_config import ( canonicalize_quant_algo, is_modelopt_quant_config, read_modelopt_quant_config, reject_checkpoint_nvfp4_plus, warn_if_inline_diverges) from tensorrt_llm.quantization.utils.fp4_utils import ( NVFP4_SF_VEC_SIZE, W4A16_NVFP4_LINEAR_SF_VEC_SIZES, nvfp4_scaling_vector_size) if TYPE_CHECKING: from tensorrt_llm.bindings import ModelConfig as ModelConfigCpp from tensorrt_llm.llmapi.llm_args import (DecodingBaseConfig, KvCacheCompressionConfig, LoraConfig, SparseAttentionConfig, SpeculativeConfig) TConfig = TypeVar("TConfig", bound=transformers.PretrainedConfig) # Python's errno module omits Linux's EBADHANDLE value. _LINUX_EBADHANDLE = 521 @dataclass(frozen=True) class KVCacheLayerSpec: """Model-provided, unsharded KV-cache geometry for one attention layer.""" head_dim: int num_kv_heads: int num_q_heads: int attention_window: Optional[int] = None def _get_kv_cache_layer_specs( model_config: object, ) -> Optional[Sequence[KVCacheLayerSpec]]: """Return concrete per-layer geometry from full or lightweight configs.""" layer_specs = getattr(model_config, "kv_cache_layer_specs", None) if not isinstance(layer_specs, Sequence): return None return layer_specs _DEEPSEEK_V4_ARCHITECTURES = {"DeepseekV4ForCausalLM"} # V4.1 shares V4's routed-MoE formats and backend choice, but needs its own # dense FP8 policy for both original and ModelOpt-converted checkpoints. _DEEPSEEK_V41_ARCHITECTURES = { "DeepseekV41ForCausalLM", "DeepseekV41ForConditionalGeneration", } # Shared by V4 and V4.1: both name the first routed expert of a MoE block # identically, and both encode the routed-MoE format in its dtype (I8 -> MXFP4, # U8 -> NVFP4). A block is ``layers.{i}`` or, for an MTP layer, ``mtp.{j}``. _DEEPSEEK_V4_BLOCK_ROUTED_EXPERT_WEIGHT = "{block}.ffn.experts.0.w1.weight" _DEEPSEEK_V4_ROUTED_EXPERT_WEIGHT = _DEEPSEEK_V4_BLOCK_ROUTED_EXPERT_WEIGHT.format( block="layers.0") _KIMI_K3_ARCHITECTURES = { "KimiK3ForConditionalGeneration", "KimiLinearForCausalLM", } _GLM5_NEXT_ARCHITECTURES = { "Glm5NextForCausalLM", "Glm5NextForConditionalGeneration", } _MINIMAX_M3_ARCHITECTURES = { "MiniMaxM3SparseForCausalLM", "MiniMaxM3SparseForConditionalGeneration", } def _is_lock_infra_error(exc: BaseException) -> bool: """Whether exc indicates broken lock infrastructure (not mere contention).""" if isinstance(exc, PermissionError): return True if isinstance(exc, OSError): # EEXIST: filelock's ensure_directory_exists() can lose the # mkdir(exist_ok=True) race on NFS when many ranks start at once # (the post-EEXIST is_dir() recheck sees a stale attribute cache). # An un-creatable lock dir is broken infra, not contention. return exc.errno in (errno.EACCES, errno.EPERM, errno.ENOLCK, errno.ESTALE, errno.EEXIST, _LINUX_EBADHANDLE) return False def _release_lock_ignoring_infra_errors(lock: "filelock.BaseFileLock") -> None: """Release ``lock``, downgrading broken-lock-infra errors to a warning. NFS can return ENOLCK/ESTALE from the unlock ``flock`` call itself (e.g. lock-daemon exhaustion when many ranks start simultaneously). The work the lock protected has already completed at release time, so crashing the process here would fail an otherwise healthy executor. """ try: lock.release() except (PermissionError, OSError) as e: if not _is_lock_infra_error(e): raise logger.warning(f"HF remote-code lock release failed ({e}), continuing") def _try_take_lock(lock: "filelock.BaseFileLock") -> bool: """Take ``lock`` without waiting; True if this process now holds it. Contention is reported as False rather than raised, so the caller can tell "someone else is already filling the cache" apart from "the lock itself is unusable", which surfaces as PermissionError/OSError. """ try: # timeout=0 rather than blocking=False: same immediate semantics, but # available in the older filelock releases huggingface-hub allows. lock.acquire(timeout=0) except filelock.Timeout: # filelock signals "not acquired" with Timeout. Nothing was waited on # here: another process simply holds the lock. return False return True @contextlib.contextmanager def hf_remote_code_lock(timeout: int = 10) -> Iterator[None]: """ Serialize only the first load of HuggingFace ``trust_remote_code`` files. ``transformers.dynamic_module_utils.get_cached_module_file`` publishes a checkpoint's .py files into the shared ``HF_MODULES_CACHE`` with a plain ``shutil.copy``, which truncates the destination before writing it. That copy takes no lock at all, and the ``threading.Lock`` transformers holds over the subsequent import does not span processes. So a rank can import a file another rank is still writing and observe it as empty: the module loads, but the class it should define is missing. Only writes are dangerous. Once the cache is complete, transformers compares each file against the checkpoint and copies nothing, so the remaining ranks can load concurrently. This is therefore a two-step gate rather than a queue: the first arrival fills the cache while holding the lock, and everyone else waits for it, steps aside, then loads in parallel. Cost is one fill plus one concurrent load, independent of the rank count. Every caller must share this one lock instead of defining its own: ``AutoTokenizer`` and ``AutoProcessor`` internally call ``AutoConfig.from_pretrained``, so config and processor loads write overlapping sets of files into the same directory. Args: timeout: Maximum time to wait for the filling rank, in seconds. """ # One lock file inside the cache directory it guards, so that every rank # sharing that cache contends on the same file. lock = filelock.FileLock(str( Path(HF_MODULES_CACHE) / "hf_remote_code.lock")) try: is_filler = _try_take_lock(lock) except (PermissionError, OSError) as e: # Broken lock infra (permissions or NFS errors): retry on a tempdir lock. if not _is_lock_infra_error(e): raise tmp_dir = Path(tempfile.gettempdir()) tmp_dir.mkdir(parents=True, exist_ok=True) lock = filelock.FileLock(str(tmp_dir / "hf_remote_code.lock")) try: is_filler = _try_take_lock(lock) except (PermissionError, OSError) as tmp_error: if not _is_lock_infra_error(tmp_error): raise logger.warning( "no usable HF remote-code lock, loading unserialized; a " "concurrent rank may import a partially written module and " "fail with AttributeError") yield return if is_filler: # Guard only the body's exit so caller exceptions propagate. try: yield finally: _release_lock_ignoring_infra_errors(lock) return # Another rank is filling the cache. Wait for it to finish, then release at # once: the cache is complete by then, so the waiting ranks load in # parallel instead of queueing behind one another. try: lock.acquire(timeout=timeout) except filelock.Timeout: # Unlike the probe above, this wait genuinely expired: the filling rank # is still holding the lock. logger.warning( f"HF remote-code cache still incomplete after {timeout}s, loading " "unserialized; this rank may import a partially written module " "and fail with AttributeError") except (PermissionError, OSError) as e: if not _is_lock_infra_error(e): raise logger.warning( f"waiting on the HF remote-code lock failed ({e}), loading " "unserialized") else: _release_lock_ignoring_infra_errors(lock) yield @dataclass(kw_only=True) class ModelConfig(Generic[TConfig]): pretrained_config: Optional[TConfig] = None mapping: Mapping = field(default_factory=Mapping) # Quantization configs quant_config: QuantConfig = field(default_factory=QuantConfig) # Per linear layer quantization in quant_cfg.json or hf_quant_config.json quant_config_dict: Optional[Dict[str, QuantConfig]] = None # Delay weights creation to DecoderModelForCausalLM.__post_init__ # to support mixed quantization. skip_create_weights_in_init: bool = False spec_config: Optional["DecodingBaseConfig"] = None # When False, the column-parallel LM head keeps its vocab-sharded output # instead of all-gathering to full vocab. Used for one-model speculative # draft models so greedy draft sampling can do a lighter TP gather. Defaults # to True to preserve behavior for every non-draft model. lm_head_gather_output: bool = True # Dtype of the logits written by the LM head GEMM (LlmArgs.lm_head_output_dtype). # None keeps the model dtype; torch.float32 writes the float32 accumulator # directly instead of rounding the logits to bf16/fp16 first. lm_head_output_dtype: Optional[torch.dtype] = None lora_config: Optional["LoraConfig"] = None sparse_attention_config: Optional["SparseAttentionConfig"] = None kv_cache_compression_config: Optional["KvCacheCompressionConfig"] = None is_generation: bool = True is_encoder_decoder: bool = False max_num_tokens: int = 8192 max_seq_len: Optional[int] = None moe_max_num_tokens: Optional[int] = None # Set in __post_init__; a normal field, not init=False, so that # dataclasses.replace() and copy.copy() carry it. _moe_max_num_tokens_is_default: Optional[bool] = field(default=None, repr=False, compare=False) moe_load_balancer: Optional[MoeLoadBalancerConfigType] = None attn_backend: str = 'TRTLLM' # Effective threshold requested for trtllm-gen MLA skip-correction. Zero # disables the optimization; hardware support is resolved by the backend. skip_correction_threshold: float = 0.0 moe_backend: str = 'CUTLASS' # options can be CUTLASS, TRTLLM # The backend the user requested, before AUTO resolution. moe_backend_requested: str = 'AUTO' # IF true, disables FC2+finalize fusion in CUTLASS MoE backend moe_disable_finalize_fusion: bool = False # If true, use low precision combine in MoE operations (only for NVFP4 quantization) use_low_precision_moe_combine: bool = False # NVFP4 GEMM backend configuration - list of backends to consider for auto-selection nvfp4_gemm_allowed_backends: List[str] = field( default_factory=lambda: ['cutlass', 'cublaslt', 'cutedsl', 'cuda_core']) allreduce_strategy: AllReduceStrategy = AllReduceStrategy.AUTO # Lossy: the model's two-shot MNNVL all-reduces fused with a residual # RMSNorm exchange E4M3 (AllReduceParams.fp8_payload). mnnvl_allreduce_fp8_payload: bool = False # Every tensor-parallel rank holds the whole embedding table. replicate_embedding: bool = False # Batch sizes of the generation CUDA graphs (empty without them). cuda_graph_batch_sizes: Tuple[int, ...] = () # If true, enable min-latency mode. Currently only used for Llama4. enable_min_latency: bool = False # Allow models to select op according to whether CUDA Graphs are used. use_cuda_graph: bool = False force_dynamic_quantization: bool = False # If true, use torch.compile for embedding layers. enable_torch_compile_for_embedding = False extra_attrs: Dict = field(default_factory=dict, repr=False, init=False) # Model-provided per-layer KV geometry. It is stored per config rather than # in extra_attrs because one-model drafters share extra_attrs with the # target while sizing their own KV caches. _kv_cache_layer_specs: Optional[Tuple[KVCacheLayerSpec, ...]] = field(default=None, repr=False, init=False) # cute dsl op configs use_cute_dsl_blockscaling_mm: bool = False use_cute_dsl_blockscaling_bmm: bool = False use_cute_dsl_bf16_bmm: bool = False use_cute_dsl_bf16_gemm: bool = False # locality domain execution policy (controls partitioned linear/MoE execution) locality_domain_policy: LocalityDomainPolicy = field( default_factory=LocalityDomainPolicy) _frozen: bool = field(default=False, init=False, repr=False) # If true, ONLY the vision encoder part of the full model is loaded/executed. mm_encoder_only: bool = False # If true, the multimodal encoder of a multimodal checkpoint is NOT # instantiated/loaded and the model serves text-only requests. This is # opt-in per model: each model implementation must honor this flag when # building its encoder (currently Mistral3 and the Qwen3-VL / Qwen3.5-VL # models); a model that does not check it simply ignores the flag (no-op). disable_mm_encoder: bool = False # Video pruning rate for VLM models (None = EVS disabled) video_pruning_rate: Optional[float] = None # Multimodal model configuration, e.g. vision encoder CUDA graph buckets. multimodal_config: MultimodalConfig | None = None # The modeling_v2 target class to build, or None for the built-in # implementation. Set by the model loader, read by `_resolve_class`. modeling_v2_target: Optional[type] = None @property def kv_cache_layer_specs(self) -> Optional[Tuple[KVCacheLayerSpec, ...]]: """Return model-provided per-layer KV-cache geometry, if any.""" return self._kv_cache_layer_specs def set_kv_cache_layer_specs( self, layer_specs: Sequence[KVCacheLayerSpec]) -> None: """Publish unsharded per-layer KV geometry to generic runtime code.""" self._kv_cache_layer_specs = tuple(layer_specs) def __setattr__(self, key, value): """ Prevent modification of frozen instance attributes. However, we allow modification of 'extra_attrs' attributes for torch.compile and 'pretrained_config' attributes for mutimodal models. 'quant_config' is allowed to be modified to set different quantization for VLM. '_kv_cache_layer_specs' is set by models that publish per-layer KV geometry. All the other attributes are frozen. This can be bypassed by manually setting '_frozen' to False. The design is to discourage modifying the attributes unintentionally. """ if self._frozen: if key not in ('_frozen', 'extra_attrs', 'pretrained_config', 'quant_config', '_kv_cache_layer_specs'): raise AttributeError( f"Cannot modify ModelConfig.'{key}' - instance is frozen") super().__setattr__(key, value) def __post_init__(self): # Fused expert reduction can change accumulation order between runs. if (os.getenv("FORCE_DETERMINISTIC", "0") == "1" or os.getenv("FORCE_MOE_KERNEL_DETERMINISTIC", "0") == "1"): self.moe_disable_finalize_fusion = True if (self.sparse_attention_config is None and getattr(self.pretrained_config, "model_type", None) in ("deepseek_v41", "deepseek_v41_text")): self.sparse_attention_config = CSA2SparseAttentionConfig() if self.pretrained_config and self.sparse_attention_config: # Sparse geometry can come from the checkpoint. Resolve it once so # cache allocation, CUDA-graph routing, and model layers all read # the same concrete values. self.sparse_attention_config = ( self.sparse_attention_config._resolve_checkpoint_defaults( self.pretrained_config)) if self.pretrained_config: self.is_encoder_decoder = self.is_encoder_decoder_model( self.pretrained_config) if self.pretrained_config and hasattr(self.pretrained_config, "architectures"): self.is_generation = self.is_generation_model( self.pretrained_config.architectures, mm_encoder_only=self.mm_encoder_only) def get_all_reduce_strategy(strategy: str = "AUTO"): maps = { "AUTO": AllReduceStrategy.AUTO, "NCCL": AllReduceStrategy.NCCL, "UB": AllReduceStrategy.UB, "MINLATENCY": AllReduceStrategy.MIN_LATENCY, "ONESHOT": AllReduceStrategy.ONESHOT, "TWOSHOT": AllReduceStrategy.TWOSHOT, "LOWPRECISION": AllReduceStrategy.LOWPRECISION, "MNNVL": AllReduceStrategy.MNNVL, "NCCL_SYMMETRIC": AllReduceStrategy.NCCL_SYMMETRIC, } key = strategy.upper() return maps[key] if key in maps else AllReduceStrategy.AUTO if isinstance(self.allreduce_strategy, str): self.allreduce_strategy = get_all_reduce_strategy( self.allreduce_strategy) # Set default moe_max_num_tokens if not specified # The maximum number of tokens in MoE are multiplied by DP size when attention DP is enabled # Record the provenance first: once filled in, a derived size is # indistinguishable from one a deployment configured to the same number. if self._moe_max_num_tokens_is_default is None: self._moe_max_num_tokens_is_default = self.moe_max_num_tokens is None if self.moe_max_num_tokens is None: self.moe_max_num_tokens = self.max_num_tokens * self.mapping.dp_size self.extra_attrs["locality_domain_policy"] = self.locality_domain_policy def is_moe_max_num_tokens_default(self) -> bool: """Whether ``moe_max_num_tokens`` was derived rather than configured. A MoE backend with a conservative workspace cap uses this to clamp only the derived size. A config rebuilt from another one's ``moe_max_num_tokens`` -- the draft configs in ``modeling_speculative.py`` -- reads as configured, which is safe: the target's first MoE layer has already capped the forwarded size. """ return bool(self._moe_max_num_tokens_is_default) @property def torch_dtype(self) -> torch.dtype: """Get the torch dtype of the model.""" # TODO: this is an assumption that a HF model is always in bfloat16 # We should figure out a better way to handle this if other models # start to not report dtype. return self.pretrained_config.torch_dtype or torch.bfloat16 @property def fuse_pos_embd(self): if self.attn_backend == 'TRTLLM': return True elif self.attn_backend == 'FLASHINFER': return False return False @property def enable_flash_mla(self): if self.attn_backend == 'TRTLLM': if getattr(self.pretrained_config, "kv_lora_rank", None) and getattr( self.pretrained_config, "qk_rope_head_dim", None): head_dim = self.pretrained_config.kv_lora_rank + self.pretrained_config.qk_rope_head_dim if head_dim == 576 and torch.cuda.get_device_capability() == ( 9, 0): return True return False def get_quant_config(self, name: Optional[str] = None) -> QuantConfig: if name is None or self.per_layer_quant_configs is None: return self.quant_config if name in self.per_layer_quant_configs: return self.per_layer_quant_configs[name] raise ValueError(f'quant config of {name} is not found') @staticmethod def is_encoder_decoder_model(pretrained_config: Optional[TConfig]) -> bool: if pretrained_config is None: return False text_config = pretrained_config get_text_config = getattr(pretrained_config, "get_text_config", None) if callable(get_text_config): text_config = get_text_config() elif hasattr(pretrained_config, "text_config"): text_config = pretrained_config.text_config return getattr(text_config, "is_encoder_decoder", False) @staticmethod def is_generation_model(model_architectures: Optional[List[str]], mm_encoder_only: bool = False) -> bool: if model_architectures is None: logger.warning( "Model architectures is None, default to is_generation_model=True" ) return True if mm_encoder_only: return False return model_architectures[0] not in [ "BertForSequenceClassification", "Qwen2ForProcessRewardModel", "Qwen2ForRewardModel", "LlamaForTextEmbedding", "Qwen3ForTextEmbedding" ] # TODO: should be 'not model_type == ModelType.ENCODER_ONLY' # once ModelType is used in pytorch flow. @staticmethod def resolve_moe_backend(moe_backend: str, architecture: str, quant_config: Optional[QuantConfig] = None) -> str: """Resolve AUTO moe_backend to a specific backend based on model architecture. **Not the implementation-selection entry point.** That is ``moe_resolution.resolve_moe_impl``, and the two run in different phases on different questions. This one turns the literal ``AUTO`` into a concrete backend name while the checkpoint is being read; the other turns a concrete backend name into an impl class while a layer is being built, by asking each candidate's ``can_implement``. The phases cannot be merged, and the reason is a genuine cycle rather than an accident of layering: several quant formats pick their ``quant_algo`` from the backend name (see ``get_mxfp4_quant_algo`` and ``load_hf_quant_config``), so a backend name is needed to finish building ``quant_config`` -- while ``resolve_moe_impl`` needs a finished ``quant_config`` to state the problem at all. Hence the deliberate two-step in ``from_pretrained``: an architecture-only hint first, then a quant-aware resolution once ``quant_config`` exists. What this must therefore never grow is capability knowledge. Every rule here is a *preference* ("on Blackwell we would rather run TRTLLM-Gen"), and preferences that turn out to be unservable are caught downstream, where ``resolve_moe_impl`` records the substitution in a ``MoEResolutionReport``. A "can it run" test added here would be a second copy of a gate that already exists in a ``can_implement``, and the two copies would drift. Args: moe_backend: The configured moe_backend (may be "AUTO") architecture: The model architecture name (e.g., "GptOssForCausalLM") quant_config: Optional quantization config for resolving quantized MoE checkpoints. Returns: Resolved backend name (never "AUTO") """ if moe_backend.upper() != "AUTO": return moe_backend # NVFP4_PLUS, which OVERRIDE_QUANT_ALGO selects for MXFP4 experts, runs # on the CuteDSL grouped GEMM only. if ModelConfig.override_quant_algo() == QuantAlgo.NVFP4_PLUS: return "CUTEDSL" if architecture in _KIMI_K3_ARCHITECTURES: return "TRTLLM" if (architecture in _DEEPSEEK_V4_ARCHITECTURES or architecture in _DEEPSEEK_V41_ARCHITECTURES): sm_version = get_sm_version() if 100 <= sm_version < 120: return "TRTLLM" if architecture in _GLM5_NEXT_ARCHITECTURES and is_sm_100f(): return "TRTLLM" is_w4a16_nvfp4 = (quant_config is not None and quant_config.quant_algo in (QuantAlgo.W4A16_NVFP4, "W4A16_NVFP4")) if is_w4a16_nvfp4: sm_version = get_sm_version() # CuteDslB12xW4a16Nvfp4FusedMoE on SM120/121 (reached through the # CUTEDSL family), MarlinFusedMoE on Hopper. Any # other SM falls through to CUTLASS, which dequantizes on the fly. if is_sm_120f(sm_version): return "CUTEDSL" if 90 <= sm_version < 100: return "MARLIN" if architecture == "GptOssForCausalLM": sm_version = get_sm_version() # Select the best performing backend based on SM version if 100 <= sm_version < 120: # Blackwell return "TRTLLM" elif 90 <= sm_version < 100: # Hopper return "TRITON" else: return "CUTLASS" # Fallback to CUTLASS for other SM versions (e.g., SM120) quant_algo = quant_config.quant_algo if quant_config is not None else None is_fp8_block_scales = quant_algo in (QuantAlgo.FP8_BLOCK_SCALES, "FP8_BLOCK_SCALES") if is_fp8_block_scales and is_sm_100f(): return "TRTLLM" return "CUTLASS" @staticmethod def load_modelopt_quant_config(quant_config_file, checkpoint_dir, moe_backend): with open(quant_config_file) as f: quant_config_dict = json.load(f) return ModelConfig._build_modelopt_quant_config( read_modelopt_quant_config(quant_config_dict), checkpoint_dir, moe_backend) @staticmethod def _validate_nvfp4_group_size(quant_config: QuantConfig, layer_name: Optional[str] = None) -> None: """Reject NVFP4-family ``group_size`` values no kernel can consume. ModelOpt records the scale block width in ``quantization.group_size``. The W4A4 NVFP4 GEMMs are hardware-bound to 16-element blocks, while the weight-only W4A16_NVFP4 path dequantizes in software and also accepts the 32-element blocks that ModelOpt's ``nvfp4_*_weight_only`` recipes may export. Any other width would only surface as a scale-shape mismatch deep inside weight loading, so fail here with the value. """ group_size = nvfp4_scaling_vector_size(quant_config) if quant_config.quant_algo == QuantAlgo.W4A16_NVFP4: supported = W4A16_NVFP4_LINEAR_SF_VEC_SIZES elif quant_config.quant_algo in (QuantAlgo.NVFP4, QuantAlgo.NVFP4_ARC): supported = (NVFP4_SF_VEC_SIZE, ) else: return if layer_name is not None and "experts" in layer_name.split("."): supported = (NVFP4_SF_VEC_SIZE, ) if group_size not in supported: where = f" for layer '{layer_name}'" if layer_name else "" raise ValueError( f"hf_quant_config.json declares group_size={group_size}{where} " f"with quant_algo={quant_config.quant_algo.name}, but " f"TensorRT-LLM supports {'/'.join(map(str, supported))}-element " "NVFP4 scale blocks for this algorithm (32-element blocks are " "only supported by the dense W4A16_NVFP4 Linear path).") @staticmethod def _build_modelopt_quant_config(json_quant_configs, checkpoint_dir, moe_backend): """Build (quant_config, layer_quant_config) from a normalized modelopt 'quantization' inner dict. ``json_quant_configs`` should be a dict as produced by :func:`read_modelopt_quant_config`. May be mutated in place via ``.update()`` when overlaying ``quant_cfg.json``. """ quant_config = QuantConfig() layer_quant_config = None # ``canonicalize_quant_algo`` is applied again here (and per layer # below) because the ``quant_cfg.json`` overlay merged in for # MIXED_PRECISION bypasses ``read_modelopt_quant_config``. quant_config.quant_algo = ( QuantAlgo(canonicalize_quant_algo(json_quant_configs['quant_algo'])) if json_quant_configs.get('quant_algo') is not None else None) quant_config.kv_cache_quant_algo = ( QuantAlgo(json_quant_configs['kv_cache_quant_algo']) if json_quant_configs.get('kv_cache_quant_algo') is not None else None) quant_config.group_size = json_quant_configs.get('group_size', None) quant_config.exclude_modules = json_quant_configs.get( 'exclude_modules', None) # AWQ-specific extras; only override defaults when present in JSON. if 'has_zero_point' in json_quant_configs: quant_config.has_zero_point = json_quant_configs['has_zero_point'] if 'pre_quant_scale' in json_quant_configs: quant_config.pre_quant_scale = json_quant_configs['pre_quant_scale'] if quant_config.quant_algo == QuantAlgo.MIXED_PRECISION: json_extended_quant_configs: dict = {} # See tests/unittest/llmapi/test_llm_quant.py try: mixed_quant_config_file = transformers.utils.hub.cached_file( checkpoint_dir, 'quant_cfg.json') with open(mixed_quant_config_file) as fm: json_extended_quant_configs = json.load(fm) except Exception: logger.info( "No quant_cfg.json found for layer quant info, using hf_quant_config.json." ) json_quant_configs.update(json_extended_quant_configs) # kv_cache_quant_algo is global regardless of MIXED_PRECISION kv_cache_quant_algo = (QuantAlgo( json_quant_configs['kv_cache_quant_algo']) if json_quant_configs.get('kv_cache_quant_algo') is not None else None) mixed_quant_configs = json_quant_configs.get( 'quantized_layers', None) if (kv_quant_lhs := json_extended_quant_configs.get( "kv_cache_quant_algo", None)) is not None and ( kv_quant_rhs := quant_config.kv_cache_quant_algo) is not None: if kv_quant_lhs != kv_quant_rhs: raise RuntimeError( f"The kvcache config in 'quant_cfg.json', {kv_quant_lhs}," f"is different from 'hf_quant_config.json', {kv_quant_rhs}!" ) quant_config.kv_cache_quant_algo = kv_cache_quant_algo quant_config.group_size = json_quant_configs.get( 'group_size', quant_config.group_size) quant_config.exclude_modules = json_quant_configs.get( 'exclude_modules', quant_config.exclude_modules) for layer in mixed_quant_configs: layer_cfg = mixed_quant_configs[layer] config = QuantConfig() config.kv_cache_quant_algo = kv_cache_quant_algo reject_checkpoint_nvfp4_plus(layer_cfg['quant_algo'], f"Quantized layer {layer}") config.quant_algo = QuantAlgo( canonicalize_quant_algo(layer_cfg['quant_algo'])) config.group_size = layer_cfg.get('group_size', None) ModelConfig._validate_nvfp4_group_size(config, layer) # AWQ-specific extras emitted by modelopt per-layer. if 'has_zero_point' in layer_cfg: config.has_zero_point = layer_cfg['has_zero_point'] if 'pre_quant_scale' in layer_cfg: config.pre_quant_scale = layer_cfg['pre_quant_scale'] mixed_quant_configs[layer] = config layer_quant_config = mixed_quant_configs elif quant_config.quant_algo == QuantAlgo.FP8_BLOCK_SCALES: if quant_config.group_size is None: quant_config.group_size = 128 ModelConfig._validate_nvfp4_group_size(quant_config) if (moe_backend == 'TRTLLM' and quant_config.quant_algo == QuantAlgo.FP8_BLOCK_SCALES and quant_config.exclude_modules is None): quant_config.exclude_modules = [ "*kv_b_proj*", "*k_b_proj*", "*eh_proj" ] return quant_config, layer_quant_config @staticmethod def require_nvfp4_plus_moe_backend( moe_backend: Optional[str], backend_setting: str = "moe_config.backend") -> None: """Reject an explicit MoE backend that cannot run NVFP4_PLUS experts. Only the CuteDSL grouped GEMM implements NVFP4_PLUS, and AUTO resolves to it under the override (``resolve_moe_backend``). """ if moe_backend is None or str(moe_backend).upper() in ("AUTO", "CUTEDSL"): return raise ValueError( "OVERRIDE_QUANT_ALGO=NVFP4_PLUS runs MXFP4 experts on the CuteDSL " f"MoE only, but {backend_setting} is {moe_backend}; set " f"{backend_setting}: CUTEDSL or leave it AUTO.") @staticmethod def get_mxfp4_quant_algo(moe_backend, is_dynamic_quant=False): quant_algo = ModelConfig.override_quant_algo() if quant_algo == QuantAlgo.NVFP4_PLUS: ModelConfig.require_nvfp4_plus_moe_backend(moe_backend) if quant_algo is None and not is_dynamic_quant: if get_sm_version() >= 100: if moe_backend == 'TRITON': return QuantAlgo.W4A8_MXFP4_FP8 else: return QuantAlgo.W4A8_MXFP4_MXFP8 else: return QuantAlgo.W4A16_MXFP4 else: return quant_algo @staticmethod def load_hf_quant_config(hf_quant_config, moe_backend, checkpoint_dir=None): quant_config = QuantConfig() layer_quant_config = None # Route inline modelopt configs (legacy or flat) to the modelopt builder. if is_modelopt_quant_config(hf_quant_config): return ModelConfig._build_modelopt_quant_config( read_modelopt_quant_config(hf_quant_config), checkpoint_dir, moe_backend) # Read exclude_modules from HF config if present (HF format module names) hf_exclude_modules = hf_quant_config.get('modules_to_not_convert', None) # FP8 ckpt: DeepSeek V3 style (weight_block_size) or # per-tensor static activation scale style (activation_scheme="static", # e.g. Ministral / Pixtral). if hf_quant_config.get("quant_method") == "fp8" and ( hf_quant_config.get("weight_block_size") or hf_quant_config.get("activation_scheme") == "static"): quant_config.quant_algo = QuantAlgo.FP8_BLOCK_SCALES block_size = hf_quant_config.get("weight_block_size", [128, 128]) assert tuple(block_size) == ( 128, 128), "FP8_BLOCK_SCALES only supports block_size=(128,128)" quant_config.group_size = block_size[0] # Set default exclude_modules for FP8_BLOCK_SCALES # kv_b_proj must always be excluded: FP8 128x128 block boundaries # don't necessarily align with per-head dim boundaries (e.g. GLM-5 # has qk_nope_head_dim=192), so the scale tensor cannot be cleanly # reshaped per-head. The dequant path handles this correctly. default_exclude = ["*kv_b_proj*", "*k_b_proj*", "*eh_proj"] # Merge HF config's modules_to_not_convert with default exclude_modules if hf_exclude_modules is not None: quant_config.exclude_modules = list( dict.fromkeys(hf_exclude_modules + default_exclude)) else: quant_config.exclude_modules = default_exclude # MXFP4 checkpoints. elif hf_quant_config.get("quant_method") == "mxfp4": quant_config.quant_algo = ModelConfig.get_mxfp4_quant_algo( moe_backend) quant_config.group_size = 32 # Default exclude_modules for MXFP4 (TRTLLM internal format) default_exclude = [ 'block.*.attn.out', 'block.*.mlp.gate', 'block.*.attn.qkv', 'embedding', 'unembedding' ] # Merge HF config's modules_to_not_convert with default exclude_modules if hf_exclude_modules is not None: quant_config.exclude_modules = list( dict.fromkeys(hf_exclude_modules + default_exclude)) else: quant_config.exclude_modules = default_exclude # MXFP8 checkpoints (e4m3 weights + UE8M0 1x32 block scales, dynamic MXFP8 acts). elif hf_quant_config.get("quant_method") == "mxfp8": quant_config.quant_algo = QuantAlgo.MXFP8 block_size = hf_quant_config.get("weight_block_size", [1, 32]) # MXFP8 uses 1x32 blocks along the K dim; group_size is the K block (32). assert tuple(block_size) == (1, 32), ( f"MXFP8 only supports weight_block_size=[1,32], got {block_size}" ) quant_config.group_size = block_size[1] # Layers the producer left in BF16. ignored = hf_quant_config.get("ignored_layers", []) if hf_exclude_modules is not None: quant_config.exclude_modules = list( dict.fromkeys(hf_exclude_modules + ignored)) else: quant_config.exclude_modules = list(ignored) # NOTE: This is for llm-compressor's quantized checkpoints. elif hf_quant_config.get("quant_method") == "compressed-tensors": update_quant_config_from_compressed_tensors(quant_config, hf_quant_config) elif hf_quant_config.get("quant_method") == "nvfp4": quant_config.quant_algo = QuantAlgo.NVFP4 group_size = hf_quant_config.get("group_size", 16) assert group_size == 16, "NVFP4 only supports group_size=16" quant_config.group_size = group_size default_exclude = ['*.mlp.gate', 'lm_head'] # Merge HF config's modules_to_not_convert with default exclude_modules if hf_exclude_modules is not None: quant_config.exclude_modules = list( dict.fromkeys(hf_exclude_modules + default_exclude)) else: quant_config.exclude_modules = default_exclude return quant_config, layer_quant_config @staticmethod def _read_safetensors_header(path: Path) -> Dict[str, Any]: with open(path, "rb") as f: header_size = struct.unpack(" dict[str, dict[str, Any]]: """Return the safetensors header entry of each named tensor found. A tensor is looked up first in the shard that ``model.safetensors.index.json`` assigns it, then in every shard in sorted order. Each shard header is read at most once. """ checkpoint_path = Path(checkpoint_dir) weight_map = {} index_path = checkpoint_path / "model.safetensors.index.json" if index_path.exists(): with open(index_path) as f: weight_map = json.load(f).get("weight_map", {}) headers: dict[Path, dict[str, Any]] = {} def read_header(path: Path) -> dict[str, Any]: if path not in headers: headers[path] = ModelConfig._read_safetensors_header(path) return headers[path] found = {} for tensor_name in tensor_names: shard_name = weight_map.get(tensor_name) if shard_name is None: continue shard_path = checkpoint_path / shard_name if shard_path.exists(): header = read_header(shard_path) if tensor_name in header: found[tensor_name] = header[tensor_name] missing = [name for name in tensor_names if name not in found] for shard_path in sorted(checkpoint_path.glob("*.safetensors")): if not missing: break header = read_header(shard_path) for tensor_name in missing: if tensor_name in header: found[tensor_name] = header[tensor_name] missing = [name for name in missing if name not in found] return found @staticmethod def _get_safetensors_header_for_tensor(checkpoint_dir: str, tensor_name: str) -> Optional[Dict]: return ModelConfig._get_safetensors_headers_for_tensors( checkpoint_dir, [tensor_name]).get(tensor_name) @staticmethod def _deepseek_v4_routed_moe_layout_of( tensor_info: dict[str, Any] | None) -> str | None: """Routed-MoE format encoded by a first routed expert weight's header.""" if tensor_info is None: return None dtype = tensor_info.get("dtype") shape = tensor_info.get("shape", []) if dtype == "I8" and len(shape) == 2: return "mxfp4" if dtype == "U8": return "nvfp4" return None @staticmethod def _detect_deepseek_v4_routed_moe_layout( checkpoint_dir: str) -> Optional[str]: return ModelConfig._deepseek_v4_routed_moe_layout_of( ModelConfig._get_safetensors_header_for_tensor( checkpoint_dir, _DEEPSEEK_V4_ROUTED_EXPERT_WEIGHT)) @staticmethod def _detect_deepseek_v4_routed_moe_block_layouts( checkpoint_dir: str, blocks: Sequence[str]) -> dict[str, str | None]: """Routed-MoE format of each checkpoint block, e.g. ``mtp.0``. A block maps to ``None`` when its first routed expert weight is absent or is stored in neither the MXFP4 nor the NVFP4 layout. """ weight_names = { block: _DEEPSEEK_V4_BLOCK_ROUTED_EXPERT_WEIGHT.format(block=block) for block in blocks } headers = ModelConfig._get_safetensors_headers_for_tensors( checkpoint_dir, list(weight_names.values())) return { block: ModelConfig._deepseek_v4_routed_moe_layout_of(headers.get(name)) for block, name in weight_names.items() } @staticmethod def _is_deepseek_v4_base_checkpoint(checkpoint_dir: str) -> bool: tensor_info = ModelConfig._get_safetensors_header_for_tensor( checkpoint_dir, _DEEPSEEK_V4_ROUTED_EXPERT_WEIGHT) if tensor_info is None: return False return ModelConfig._detect_deepseek_v4_routed_moe_layout( checkpoint_dir) not in ("mxfp4", "nvfp4") @staticmethod def _build_deepseek_v41_quant_config(hf_quant_config: dict) -> QuantConfig: """Build the global quant policy for a DeepSeek-V4.1 checkpoint. Grouped output and shared-expert GEMMs use ``FP8_BLOCK_SCALES``. CSA2 Linear projections retain native block-32 FP8 weights; routed experts retain their separately detected quantization policy. """ block_size = tuple(hf_quant_config.get("weight_block_size") or ()) if not block_size: return QuantConfig() # ModelOpt metadata describes the experts, not the dense FP8 weights. # Do not let its producer field redispatch this view to ModelOpt. as_128 = { "quant_method": "fp8", "weight_block_size": [128, 128], "modules_to_not_convert": hf_quant_config.get("modules_to_not_convert"), } if block_size != (128, 128): logger.info("DeepSeek-V4.1: grouped output and shared-expert FP8 " "weights are requantized from " f"{list(block_size)} to [128, 128] at load.") return ModelConfig.load_hf_quant_config(as_128, 'TRTLLM')[0] @staticmethod def _has_deepseek_v4_layer_only_modelopt_quant_config( quant_config_file: str) -> bool: with open(quant_config_file) as f: quant_config_dict = json.load(f) quantization_config = quant_config_dict.get('quantization', {}) return (quantization_config.get('quant_algo', None) is None and quantization_config.get('quantized_layers', None) is not None) @staticmethod def _set_deepseek_v4_routed_moe_quant_config(pretrained_config, checkpoint_dir: str, moe_backend: str, layer_quant_config, spec_config=None, require_layout: bool = False): layout = ModelConfig._detect_deepseek_v4_routed_moe_layout( checkpoint_dir) if layout not in ("mxfp4", "nvfp4"): if require_layout: raise ValueError( "DeepSeek-V4 checkpoint has layer-specific quantized_layers " "in hf_quant_config.json, but the routed MoE layout could " "not be detected from safetensors metadata. Expected " f"{_DEEPSEEK_V4_ROUTED_EXPERT_WEIGHT} to use dtype I8 " "for MXFP4 or U8 for NVFP4.") return layer_quant_config def build_experts_quant_config(layout_name: str) -> QuantConfig: experts_quant_config = QuantConfig() if layout_name == "mxfp4": experts_quant_config.quant_algo = ( ModelConfig.get_mxfp4_quant_algo(moe_backend)) experts_quant_config.group_size = 32 else: experts_quant_config.quant_algo = QuantAlgo.NVFP4 experts_quant_config.group_size = 16 experts_quant_config.exclude_modules = [ 'block.*.attn.out', 'block.*.mlp.gate', 'block.*.attn.qkv', 'embedding', 'unembedding' ] return experts_quant_config experts_quant_configs = {layout: build_experts_quant_config(layout)} logger.info( "Detected DeepSeek-V4 routed MoE %s checkpoint layout; using " "%s for routed experts.", layout.upper(), experts_quant_configs[layout].quant_algo) if layer_quant_config is None: layer_quant_config = {} else: layer_quant_config = dict(layer_quant_config) # MoE layer ``i`` reads checkpoint block ``layers.{i}``. MTP layer ``j`` # is MoE layer ``num_hidden_layers + j`` and, like the weight loader, # reads block ``mtp.{j % num_nextn_predict_layers}`` of the checkpoint. num_mtp_layers = 0 if (spec_config is not None and spec_config.spec_dec_mode.is_mtp_one_model()): num_mtp_layers = spec_config.num_nextn_predict_layers num_checkpoint_mtp_blocks = getattr( pretrained_config, "num_nextn_predict_layers", None) or 1 blocks = [ f"layers.{layer_idx}" for layer_idx in range(pretrained_config.num_hidden_layers) ] + [ f"mtp.{mtp_idx % num_checkpoint_mtp_blocks}" for mtp_idx in range(num_mtp_layers) ] block_layouts = ModelConfig._detect_deepseek_v4_routed_moe_block_layouts( checkpoint_dir, sorted(set(blocks))) for layer_idx, block in enumerate(blocks): # A block whose own layout cannot be read keeps the layers.0 one. block_layout = block_layouts[block] or layout if block_layout not in experts_quant_configs: experts_quant_configs[block_layout] = ( build_experts_quant_config(block_layout)) if block_layout != layout: logger.info( "DeepSeek-V4 routed MoE block %s has the %s checkpoint " "layout; using %s for the routed experts of MoE layer %d.", block, block_layout.upper(), experts_quant_configs[block_layout].quant_algo, layer_idx) layer_quant_config[f"model.layers.{layer_idx}.mlp.experts"] = ( experts_quant_configs[block_layout]) return layer_quant_config @staticmethod def _set_minimax_m3_layer_quant_config(pretrained_config, layer_quant_config): """Normalize the Minimax M3 MIXED_PRECISION per-layer quant config. Two fix-ups are applied: 1. Strip the ``language_model.`` prefix from every per-layer key. The M3 VL checkpoint stores keys like ``language_model.model.layers.0.self_attn.o_proj -> MXFP8`` in ``hf_quant_config.json``, but the TRT-LLM module tree names the text decoder ``model.layers.0.self_attn.o_proj`` (no ``language_model.`` prefix -- the loader strips it). ``apply_layerwise_quant_config`` matches *standalone* Linears (e.g. ``o_proj``, ``down_proj``) with an **exact** ``name == key`` comparison, so the prefixed keys never match and those layers silently fall back to the global ``MIXED_PRECISION`` config -> loaded unquantized (MXFP8 ``weight_scale`` dropped) -> the attention/MLP output magnitude explodes. Stripping the prefix makes the exact match succeed. (Fused qkv/gate_up Linears and the Attention wrapper use substring matches and happened to work regardless.) 2. Inject a single coarse ``model.layers.N.block_sparse_moe.experts`` entry per MoE layer so ``MiniMaxM3MoE._get_experts_quant_config`` can select the NVFP4 backend for the routed experts (the fine-grained per-linear NVFP4 expert keys can't be used directly). Does nothing when there is no per-layer config (e.g. the uniform MXFP8 or a BF16 checkpoint). """ from tensorrt_llm.models.modeling_utils import QuantAlgo if layer_quant_config is None: return layer_quant_config # (1) Strip the ``language_model.`` prefix so exact-match per-layer # quant assignment works for standalone base Linears. _LM_PREFIX = "language_model." layer_quant_config = { (k[len(_LM_PREFIX):] if k.startswith(_LM_PREFIX) else k): v for k, v in layer_quant_config.items() } # (2) Inject coarse NVFP4 expert entries (only when routed experts are # NVFP4). has_nvfp4_experts = any( "block_sparse_moe.experts" in k and isinstance(v, QuantConfig) and v.quant_algo == QuantAlgo.NVFP4 for k, v in layer_quant_config.items()) if not has_nvfp4_experts: return layer_quant_config experts_quant_config = QuantConfig() experts_quant_config.quant_algo = QuantAlgo.NVFP4 # TODO: remove the hardcoded group_size and read it from the per-linear # NVFP4 expert entries in hf_quant_config.json instead. 16 is correct # for standard NVFP4 today, but this is a latent bug if a checkpoint # ever ships a different group size. experts_quant_config.group_size = 16 text_config = getattr(pretrained_config, "text_config", pretrained_config) if isinstance(text_config, dict): moe_layer_freq = text_config.get("moe_layer_freq", []) else: moe_layer_freq = getattr(text_config, "moe_layer_freq", []) for layer_idx, freq in enumerate(moe_layer_freq): if int(freq) != 0: layer_quant_config[ f"model.layers.{layer_idx}.block_sparse_moe.experts"] = experts_quant_config logger.info( "Detected Minimax M3 NVFP4 routed MoE checkpoint; using NVFP4 " "for routed experts.") return layer_quant_config @staticmethod def load_quant_config_from_dtypes_json(dtypes_json_file, moe_backend: str): quant_config = QuantConfig() layer_quant_config = None # Keyed by module name in checkpoint order; a set would order the # names by string hash, which varies between processes. exclude_modules: dict[str, None] = {} has_mxfp4 = False is_dynamic_quant = False with open(dtypes_json_file) as f: dtypes_json = json.load(f) for layer, dtype in dtypes_json.items(): if layer.endswith("weight"): if dtype == "BF16" or dtype == "FP16": names = layer.split(".") exclude_modules.setdefault('.'.join(names[:-1])) elif dtype == "MXFP4": # This is the path for the fp8 checkpoint which requires dynamic quantization. is_dynamic_quant = True has_mxfp4 = True elif layer.endswith("weight.blocks"): scale_name = layer.replace("weight.blocks", "weight.scales") scale_dtype = dtypes_json.get(scale_name, None) assert scale_dtype == "UE8" is_dynamic_quant = False has_mxfp4 = True if has_mxfp4: quant_config.quant_algo = ModelConfig.get_mxfp4_quant_algo( moe_backend, is_dynamic_quant) quant_config.group_size = 32 quant_config.exclude_modules = list(exclude_modules) logger.info(f"Setting quant_config: {quant_config}") return quant_config, layer_quant_config @staticmethod def override_quant_algo(): new_algo = os.environ.get("OVERRIDE_QUANT_ALGO", None) supported_algos = { "W4A16_MXFP4": QuantAlgo.W4A16_MXFP4, "W4A8_MXFP4_MXFP8": QuantAlgo.W4A8_MXFP4_MXFP8, "W4A8_MXFP4_FP8": QuantAlgo.W4A8_MXFP4_FP8, # MXFP4 weights with E8M0 scales converted to UE5M3 at load, and # E2M1 activations with UE5M3 block scales (Rubin SM107 only). "NVFP4_PLUS": QuantAlgo.NVFP4_PLUS, } if new_algo is not None: if new_algo.upper() in supported_algos: return supported_algos[new_algo.upper()] else: logger.warning( f"Unsupported quant algo: {new_algo}, supported algos: {supported_algos.keys()}" ) return None @classmethod def from_pretrained(cls, checkpoint_dir: str, trust_remote_code=False, **kwargs): def update_sparse_attention_indexer_config(pretrained_config, kwargs): sparse_attention_config = kwargs.get('sparse_attention_config') if sparse_attention_config: index_n_heads = sparse_attention_config.index_n_heads or pretrained_config.index_n_heads index_head_dim = sparse_attention_config.index_head_dim or pretrained_config.index_head_dim # index_topk needs an explicit-set check rather than `or`: the # DeepSeekV4SparseAttentionConfig default (512) is truthy, so a # plain `or` shadows the checkpoint's index_topk (e.g. Pro's # 1024) whenever the user did not set it. Mirror the window_size # handling below and consult model_fields_set. (index_n_heads / # index_head_dim stay on `or` since their defaults are None.) if 'index_topk' in sparse_attention_config.model_fields_set: index_topk = sparse_attention_config.index_topk else: index_topk = pretrained_config.index_topk indexer_max_chunk_size = sparse_attention_config.indexer_max_chunk_size skip_indexer_for_short_seqs = sparse_attention_config.skip_indexer_for_short_seqs # Pass-through DSA tuning flags so user-set values survive the # V4 sparse_attention_config rebuild below. The V3.2 path # already threads these explicitly (lines 723-727); without # this block the V4 rebuild silently drops any user override # back to subclass defaults (e.g., enable_heuristic_topk=False # even when the user set it to True in --extra_llm_api_options). use_cute_dsl_topk = sparse_attention_config.use_cute_dsl_topk use_cute_dsl_paged_mqa_logits = sparse_attention_config.use_cute_dsl_paged_mqa_logits q_split_threshold = sparse_attention_config.q_split_threshold indexer_rope_interleave = sparse_attention_config.indexer_rope_interleave enable_heuristic_topk = sparse_attention_config.enable_heuristic_topk use_self_sampling_topk = sparse_attention_config.use_self_sampling_topk use_gvr_emission = sparse_attention_config.use_gvr_emission indexer_k_dtype = sparse_attention_config.indexer_k_dtype else: index_n_heads = pretrained_config.index_n_heads index_head_dim = pretrained_config.index_head_dim index_topk = pretrained_config.index_topk indexer_max_chunk_size = None skip_indexer_for_short_seqs = True # Defaults match DeepSeekV4SparseAttentionConfig field defaults. use_cute_dsl_topk = False use_cute_dsl_paged_mqa_logits = DeepSeekV4SparseAttentionConfig.model_fields[ 'use_cute_dsl_paged_mqa_logits'].default q_split_threshold = 8192 indexer_rope_interleave = False enable_heuristic_topk = False use_self_sampling_topk = True use_gvr_emission = False default_sparse_attention_config = DeepSeekV4SparseAttentionConfig( ) indexer_k_dtype = default_sparse_attention_config.indexer_k_dtype indexer_config = {} indexer_config['index_n_heads'] = index_n_heads indexer_config['index_head_dim'] = index_head_dim indexer_config['index_topk'] = index_topk indexer_config['indexer_max_chunk_size'] = indexer_max_chunk_size indexer_config[ 'skip_indexer_for_short_seqs'] = skip_indexer_for_short_seqs indexer_config['use_cute_dsl_topk'] = use_cute_dsl_topk indexer_config[ 'use_cute_dsl_paged_mqa_logits'] = use_cute_dsl_paged_mqa_logits indexer_config['q_split_threshold'] = q_split_threshold indexer_config['indexer_rope_interleave'] = indexer_rope_interleave indexer_config['enable_heuristic_topk'] = enable_heuristic_topk indexer_config['use_self_sampling_topk'] = use_self_sampling_topk indexer_config['use_gvr_emission'] = use_gvr_emission indexer_config['indexer_k_dtype'] = indexer_k_dtype return indexer_config # Use file lock to prevent race conditions when multiple processes # try to import/cache the same remote model config file with hf_remote_code_lock(): # When handling the case where model_format is TLLM_ENGINE # send cyclic requests to the NONE URL. if checkpoint_dir is not None: pretrained_config = load_pretrained_config( checkpoint_dir, trust_remote_code=trust_remote_code, **kwargs, ) # transformers 5 leaves `_name_or_path` empty when the config # JSON does not carry one, so a config loaded from a local # directory no longer remembers where it came from. Record it: # a model whose weights need a sibling file from the checkpoint # (DeepSeek-V4.1's Engram needs the checkpoint's own tokenizer to # derive its hash multipliers) has no other way to find it. if not getattr(pretrained_config, '_name_or_path', None): pretrained_config._name_or_path = str(checkpoint_dir) if pretrained_config.architectures[0] in [ "DeepseekV32ForCausalLM", "GlmMoeDsaForCausalLM" ]: sparse_attention_config = kwargs.get( 'sparse_attention_config') indexer_rope_interleave = getattr( pretrained_config, 'indexer_rope_interleave', False) if sparse_attention_config: index_n_heads = sparse_attention_config.index_n_heads or pretrained_config.index_n_heads index_head_dim = sparse_attention_config.index_head_dim or pretrained_config.index_head_dim # Explicit-set check (see V4 path above): only honor a # user-provided index_topk; otherwise take the # checkpoint value rather than a truthy subclass default. if 'index_topk' in sparse_attention_config.model_fields_set: index_topk = sparse_attention_config.index_topk else: index_topk = pretrained_config.index_topk indexer_max_chunk_size = sparse_attention_config.indexer_max_chunk_size skip_indexer_for_short_seqs = sparse_attention_config.skip_indexer_for_short_seqs use_cute_dsl_topk = sparse_attention_config.use_cute_dsl_topk use_cute_dsl_paged_mqa_logits = sparse_attention_config.use_cute_dsl_paged_mqa_logits q_split_threshold = sparse_attention_config.q_split_threshold enable_heuristic_topk = sparse_attention_config.enable_heuristic_topk use_self_sampling_topk = sparse_attention_config.use_self_sampling_topk use_gvr_emission = sparse_attention_config.use_gvr_emission indexer_k_dtype = sparse_attention_config.indexer_k_dtype index_share_for_mtp_iteration = sparse_attention_config.index_share_for_mtp_iteration else: index_n_heads = pretrained_config.index_n_heads index_head_dim = pretrained_config.index_head_dim index_topk = pretrained_config.index_topk indexer_max_chunk_size = None skip_indexer_for_short_seqs = True use_cute_dsl_topk = False use_cute_dsl_paged_mqa_logits = DeepSeekSparseAttentionConfig.model_fields[ 'use_cute_dsl_paged_mqa_logits'].default q_split_threshold = 8192 enable_heuristic_topk = False use_self_sampling_topk = True use_gvr_emission = False indexer_k_dtype = "fp8" index_share_for_mtp_iteration = None kwargs[ 'sparse_attention_config'] = DeepSeekSparseAttentionConfig( index_n_heads=index_n_heads, index_head_dim=index_head_dim, index_topk=index_topk, indexer_max_chunk_size=indexer_max_chunk_size, skip_indexer_for_short_seqs= skip_indexer_for_short_seqs, use_cute_dsl_topk=use_cute_dsl_topk, use_cute_dsl_paged_mqa_logits= use_cute_dsl_paged_mqa_logits, q_split_threshold=q_split_threshold, indexer_rope_interleave=indexer_rope_interleave, enable_heuristic_topk=enable_heuristic_topk, use_self_sampling_topk=use_self_sampling_topk, use_gvr_emission=use_gvr_emission, indexer_k_dtype=indexer_k_dtype, index_share_for_mtp_iteration= index_share_for_mtp_iteration) elif pretrained_config.architectures[ 0] in _DEEPSEEK_V41_ARCHITECTURES: sparse_config = kwargs.get('sparse_attention_config') if sparse_config is not None and not isinstance( sparse_config, CSA2SparseAttentionConfig): raise ValueError( "DeepSeek-V4.1 requires CSA2 sparse attention") kwargs[ 'sparse_attention_config'] = sparse_config or CSA2SparseAttentionConfig( ) elif pretrained_config.architectures[ 0] == "DeepseekV4ForCausalLM": if cls._is_deepseek_v4_base_checkpoint(checkpoint_dir): logger.warning( "Support for DeepSeek-V4 Base checkpoints is " "experimental. For better supported behavior, use " "a DeepSeek-V4 Instruct checkpoint.") indexer_config = update_sparse_attention_indexer_config( pretrained_config, kwargs) checkpoint_compress_ratios = getattr( pretrained_config, 'compress_ratios', None) num_base_layers = pretrained_config.num_hidden_layers spec_config = kwargs.get('spec_config', None) # ``num_nextn_predict_layers`` is MTP-specific (only read on # the is_mtp_one_model path). Only set it on configs that # actually declare the field; other DeepSeek-V4 spec modes # (e.g. DSpark, which carries its own draft stage count) do # not, and a blind setattr would fail pydantic validation. if (spec_config is not None and 'num_nextn_predict_layers' in type(spec_config).model_fields and spec_config.num_nextn_predict_layers is None): spec_config.num_nextn_predict_layers = getattr( pretrained_config, 'num_nextn_predict_layers', 1) mtp_enabled = (spec_config is not None and spec_config.spec_dec_mode.is_mtp_one_model()) sparse_attention_config = kwargs.get( 'sparse_attention_config') checkpoint_window_size = getattr(pretrained_config, 'window_size', None) if checkpoint_window_size is None: checkpoint_window_size = getattr( pretrained_config, 'sliding_window', None) if sparse_attention_config: compress_ratios = sparse_attention_config.compress_ratios window_size = sparse_attention_config.window_size if 'window_size' not in sparse_attention_config.model_fields_set: window_size = checkpoint_window_size else: compress_ratios = checkpoint_compress_ratios window_size = checkpoint_window_size if (checkpoint_compress_ratios is not None and (compress_ratios is None or len(checkpoint_compress_ratios) > len(compress_ratios))): compress_ratios = checkpoint_compress_ratios if window_size is None: window_size = checkpoint_window_size if window_size is None: window_size = pretrained_config.sliding_window # DeepSeek-V4 needs explicit per-layer compress ratios. They # must come from the checkpoint config or a user override; we # intentionally do not synthesize a default list (it would # silently change sparse-attention semantics). Fail fast with # an actionable message instead of letting the normalization # below raise an opaque TypeError on None. if compress_ratios is None: raise ValueError( "DeepSeek-V4 requires per-layer `compress_ratios`, " "but none were found in the checkpoint config and " "none were provided via `sparse_attention_config`. " "Set `compress_ratios` in the model's config.json, or " "pass `sparse_attention_config=" "DeepSeekV4SparseAttentionConfig(compress_ratios=[...])`" " in --extra_llm_api_options.") # Normalize checkpoint-facing ratio 0 (SWA-only/uncompressed) # to 1 internally so cache allocation math works. The # external config keeps the original semantics. compress_ratios = [ ratio if ratio > 0 else 1 for ratio in compress_ratios ] # Only synthesize ratios for extra MTP layers. The base # model ratios must come from the checkpoint or an # explicit user override; padding a short default list for # non-MTP changes sparse attention semantics. if mtp_enabled: mtp_num_layers = spec_config.num_nextn_predict_layers total_layers = num_base_layers + mtp_num_layers if len(compress_ratios) < total_layers: compress_ratios = list(compress_ratios) + [1] * ( total_layers - len(compress_ratios)) indexer_k_dtype = indexer_config.pop('indexer_k_dtype') kwargs[ 'sparse_attention_config'] = DeepSeekV4SparseAttentionConfig( compress_ratios=compress_ratios, window_size=window_size, indexer_k_dtype=indexer_k_dtype, **indexer_config) else: raise ValueError( "checkpoint_dir is None. Cannot load model config without a valid checkpoint directory." ) # Get cached file from path or repo id, return None if not exists. def cached_file(path_or_repo_id, file_name): try: return transformers.utils.hub.cached_file( path_or_repo_id, file_name) except OSError: return None # Some checkpoints lack torch_dtype, populate with dtype dtype = getattr(pretrained_config, 'dtype', None) # For composite VLM configs the dtype lives inside ``text_config`` # because the top-level config has no ``dtype`` field. if dtype is None: text_config = getattr(pretrained_config, 'text_config', None) if text_config is not None: dtype = getattr(text_config, 'dtype', None) pretrained_config.torch_dtype = dtype # Prior to transformers 5, composite configs (e.g. Qwen2_5_VLConfig) delegated attribute # lookups to their text sub-config, so accesses like `config.vocab_size` / # `config.hidden_size` resolved transparently. # 5.x removed that delegation, so eagerly mirror the text sub-config onto the top-level # config to keep downstream consumers working. _mirror_text_subconfig_attrs(pretrained_config) # Apply model_kwargs to override config parameters if provided model_kwargs = kwargs.pop('model_kwargs', None) if model_kwargs: def _recursive_update_config(config: transformers.PretrainedConfig, update_dict: Dict[str, Any]): """ Recursively update a PretrainedConfig object with values from update_dict. Args: config: PretrainedConfig object to update update_dict: Dictionary with values to update in the config """ for key, value_new in update_dict.items(): target_value = getattr(config, key, None) # Handle nested PretrainedConfig objects when value is a dict if isinstance(value_new, dict) and isinstance( target_value, transformers.PretrainedConfig): # Recursively update the nested config logger.info( f"Recursively updating nested config: {key}") _recursive_update_config(target_value, value_new) elif (key in ["torch_dtype", "dtype"] and isinstance(value_new, str) and value_new != "auto"): # check special handling of torch_dtype (DEPRECATED!) and dtype keys to ensure we # use the correct torch.dtype object instead of a string. dtype = getattr(torch, value_new) assert isinstance(dtype, torch.dtype), f"Invalid {dtype=}" setattr(config, key, dtype) logger.info( f"Applied model_kwargs: {key}={dtype} (previous value: {target_value})" ) else: # Direct update for simple values setattr(config, key, value_new) logger.info( f"Applied model_kwargs: {key}={value_new} (previous value: {target_value})" ) _recursive_update_config(pretrained_config, model_kwargs) quant_config = QuantConfig() layer_quant_config = None require_deepseek_v4_routed_moe_layout = False requested_moe_backend = kwargs.get('moe_backend', 'AUTO') architecture = pretrained_config.architectures[ 0] if pretrained_config.architectures else "" hf_quant_config = getattr(pretrained_config, "quantization_config", None) has_deepseek_v41_dense_fp8 = ( architecture in _DEEPSEEK_V41_ARCHITECTURES and hf_quant_config is not None and hf_quant_config.get("quant_method") == "fp8" and bool(hf_quant_config.get("weight_block_size"))) # Use an architecture-only backend hint for quant config parsing. Some # quant formats choose the quant_algo from the backend name, so the final # quant-aware AUTO resolution happens after quant_config is loaded. moe_backend_hint = cls.resolve_moe_backend(requested_moe_backend, architecture) # quantized ckpt in modelopt format if quant_config_file := cached_file(checkpoint_dir, 'hf_quant_config.json'): with open(quant_config_file) as f: normalized = read_modelopt_quant_config(json.load(f)) # The file is authoritative; warn if the inline copy disagrees. # Done before _build_modelopt_quant_config since the builder may # mutate ``normalized`` via ``.update`` from quant_cfg.json. warn_if_inline_diverges( normalized, getattr(pretrained_config, "quantization_config", None), source_file="hf_quant_config.json", ) if architecture in _DEEPSEEK_V4_ARCHITECTURES: require_deepseek_v4_routed_moe_layout = ( cls._has_deepseek_v4_layer_only_modelopt_quant_config( quant_config_file)) quant_config, layer_quant_config = cls._build_modelopt_quant_config( normalized, checkpoint_dir, moe_backend_hint) if (quant_config.quant_algo is None and hf_quant_config is not None and not has_deepseek_v41_dense_fp8): hf_quant_config, hf_layer_quant_config = cls.load_hf_quant_config( hf_quant_config, moe_backend_hint, checkpoint_dir=checkpoint_dir) if hf_quant_config.quant_algo is not None: logger.info( "Using quantization_config from config.json as global " "quantization because hf_quant_config.json does not set " "a global quant_algo.") quant_config = hf_quant_config if hf_layer_quant_config is not None: if layer_quant_config is None: layer_quant_config = hf_layer_quant_config else: layer_quant_config = { **hf_layer_quant_config, **layer_quant_config, } # DeepSeek-V4.1: 32x32 block-scaled dense FP8, which the generic # FP8_BLOCK_SCALES path cannot represent. Must precede the generic # branch below, which would assert on the block size. elif (architecture in _DEEPSEEK_V41_ARCHITECTURES and hf_quant_config is not None): if is_modelopt_quant_config(hf_quant_config): quant_config, layer_quant_config = cls.load_hf_quant_config( hf_quant_config, moe_backend_hint, checkpoint_dir=checkpoint_dir) else: quant_config = cls._build_deepseek_v41_quant_config( hf_quant_config) # quantized ckpt in other formats elif getattr(pretrained_config, "quantization_config", None) is not None: hf_quant_config = pretrained_config.quantization_config quant_config, layer_quant_config = cls.load_hf_quant_config( hf_quant_config, moe_backend_hint, checkpoint_dir=checkpoint_dir) elif quant_config_file := cached_file(checkpoint_dir, 'dtypes.json'): quant_config, layer_quant_config = cls.load_quant_config_from_dtypes_json( quant_config_file, moe_backend_hint) if (has_deepseek_v41_dense_fp8 and quant_config.quant_algo in (None, QuantAlgo.MIXED_PRECISION)): # Preserve the parsed expert and KV policies; only dense weights # use the V4.1 remapper's FP8 block-128 layout. dense_quant_config = cls._build_deepseek_v41_quant_config( hf_quant_config) dense_quant_config.kv_cache_quant_algo = quant_config.kv_cache_quant_algo quant_config = dense_quant_config kwargs['moe_backend'] = cls.resolve_moe_backend( requested_moe_backend, architecture, quant_config=quant_config, ) kwargs['moe_backend_requested'] = requested_moe_backend # V4.1 names the routed experts exactly as V4 does and encodes MXFP4 the # same way (I8 storage), so the detector and setter apply verbatim. if (architecture in _DEEPSEEK_V4_ARCHITECTURES or architecture in _DEEPSEEK_V41_ARCHITECTURES): layer_quant_config = cls._set_deepseek_v4_routed_moe_quant_config( pretrained_config, checkpoint_dir, kwargs['moe_backend'], layer_quant_config, kwargs.get('spec_config', None), require_layout=require_deepseek_v4_routed_moe_layout) if architecture in _MINIMAX_M3_ARCHITECTURES: layer_quant_config = cls._set_minimax_m3_layer_quant_config( pretrained_config, layer_quant_config) model_config = cls(pretrained_config=pretrained_config, quant_config=quant_config, quant_config_dict=layer_quant_config, **kwargs) model_config._frozen = True return model_config def get_bindings_model_config( self, is_disagg: bool = False, tokens_per_block: Optional[int] = None, kv_cache_config: Optional[KvCacheConfig] = None, spec_config: Optional['SpeculativeConfig'] = None, ) -> "ModelConfigCpp": """ This method is used to construct the bindings config for the model. Currently it assumes that an engine has been created. Args: tokens_per_block: The number of tokens per block. Please note that in PyTorch flow tokens_per_block is not available in the model config, instead it is defined in the executor config. Returns: The bindings model config. """ # TODO smor- this isn't robust, and currently tested for LlamaConfig only # TODO smor- currently assuming no rnn layers, no MOE from tensorrt_llm.bindings import ModelConfig as ModelConfigCpp # Attention DP should not shard attention heads; use attn_tp_size=1 in that case # so downstream KV calculations see the full (non-partitioned) head count. attn_tp_size = self.mapping.attn_tp_size if not self.mapping.enable_attention_dp else 1 attn_cp_size = self.mapping.attn_cp_size def ceil_div(a, b): return (a + b - 1) // b num_heads = ceil_div(self.pretrained_config.num_attention_heads, attn_tp_size * attn_cp_size) hidden_size = ceil_div(self.pretrained_config.hidden_size, attn_tp_size) num_layers = self.pretrained_config.num_hidden_layers num_attention_layers = self.get_num_attention_layers() if (self.spec_config is not None and self.spec_config.spec_dec_mode.is_mtp_one_model()): assert self.spec_config.num_nextn_predict_layers is not None, ( "num_nextn_predict_layers must be set from model config before building ModelConfig. " "Ensure update_spec_config_from_model_config() has been called." ) num_layers += self.spec_config.num_nextn_predict_layers num_attention_layers += self.spec_config.num_nextn_predict_layers model_config_cpp = ModelConfigCpp( vocab_size=self.pretrained_config.vocab_size, num_layers=num_layers, num_attention_layers=num_attention_layers, num_rnn_layers=0, num_heads=num_heads, hidden_size=hidden_size, data_type=torch_dtype_to_binding( self.pretrained_config.torch_dtype)) # For kv cache size calculation: set tokens_per_block if tokens_per_block is None: logger.warning( f"tokens_per_block is not set, using default value {model_config_cpp.tokens_per_block}" ) else: model_config_cpp.tokens_per_block = tokens_per_block num_key_value_heads = getattr(self.pretrained_config, "num_key_value_heads", num_heads) if isinstance(num_key_value_heads, (list, tuple)): # Per-layer KV heads (e.g., Nemotron-NAS, variable GQA models) num_kv_heads_per_layer = [ ceil_div(kv_heads, attn_tp_size * attn_cp_size) for kv_heads in num_key_value_heads ] model_config_cpp.num_kv_heads_per_layer = num_kv_heads_per_layer else: num_kv_heads = ceil_div(num_key_value_heads, attn_tp_size * attn_cp_size) model_config_cpp.set_num_kv_heads(num_kv_heads) # For hybrid models (e.g., Nemotron-H with Mamba + Attention), LoRA can be applied # to non-attention layers (e.g., Mamba in_proj/out_proj). Set num_lora_layers to # total layers so the C++ LoRA validation accepts all layer indices. if is_nemotron_hybrid(self.pretrained_config): model_config_cpp.set_num_lora_layers(num_layers) mlp_hidden_size = None if self.pretrained_config.intermediate_size is not None: mlp_hidden_size = ceil_div(self.pretrained_config.intermediate_size, self.mapping.tp_size) else: # TODO: once tensorrt_llm._torch.AutoConfig is implemented, the following logic # should be moved to tensorrt_llm._torch.AutoConfig of the relevant modeling_xxx file if hasattr(self.pretrained_config, "architectures" ) and self.pretrained_config.architectures is not None: architectures = self.pretrained_config.architectures if len(architectures ) == 1 and architectures[0] == "DeciLMForCausalLM": mlp_hidden_size = ceil_div(self._infer_nemotron_ffn_mult(), self.mapping.tp_size) else: raise ValueError( f"Inferring mlp hidden size for model architecture: {architectures} isn't supported yet" ) if mlp_hidden_size is None: raise ValueError( f"Failed to infer mlp hidden size for model: {self.pretrained_config.model_type}" ) # For kv cache size calculation: set size_per_head head_dim_names = ["head_size", "head_dim"] head_size = None for head_dim_name in head_dim_names: if hasattr(self.pretrained_config, head_dim_name): value = getattr(self.pretrained_config, head_dim_name) if value is not None: head_size = value break if head_size is None: assert hidden_size % num_heads == 0, ( f"hidden_size ({hidden_size}) must be divisible by num_heads ({num_heads})" ) calculated_head_size = hidden_size // num_heads logger.warning( f"head_size/head_dim is not set or None, using default value {calculated_head_size}" ) head_size = calculated_head_size model_config_cpp.mlp_hidden_size = mlp_hidden_size model_config_cpp.size_per_head = head_size # NOTE: this method is not robust, for Gemma3ForCausalLM only layer_types = self.get_layer_types() if layer_types is not None: model_config_cpp.layer_types = layer_types return model_config_cpp def _infer_nemotron_ffn_mult(self): # TODO smor: this is a hack to support Nemotron-Super-49B-v1 with LoRA, tracked by TRTLLM-5045 ticket # Nemotron-NAS has variable ffn_mult for each layer, we need to find the maximum # so that we don't set a too small mlp_hidden_size. This solution leads to a memory # consumption that is higher than required. biggest_ffn_mult = max([ (x.ffn.ffn_mult if x.ffn.ffn_mult is not None else 0) for x in self.pretrained_config.block_configs ]) from tensorrt_llm._torch.models.modeling_nemotron_nas import \ _ffn_mult_to_intermediate_size mlp_hidden_size = _ffn_mult_to_intermediate_size( biggest_ffn_mult, self.pretrained_config.hidden_size) return mlp_hidden_size def get_layer_types(self) -> Optional[List[LayerTypeCpp]]: """ This method is a hack to support the effort to switch to KvCacheManagerCpp. Currently, it is only tested for Gemma3ForCausalLM. For other models, it will return None. """ if self.pretrained_config.architectures[0] in ["Gemma3ForCausalLM"]: logger.debug( f"Setting layer types for {self.pretrained_config.architectures}" ) return [ LayerTypeCpp.ATTENTION, ] * self.pretrained_config.num_hidden_layers else: return None def get_num_attention_layers(self) -> int: """Number of full-attention layers in the model. Pure model property: independent of which KV cache manager will run. For non-hybrid models this equals num_hidden_layers. For hybrid Mamba models (Nemotron-hybrid, Qwen3-hybrid, Qwen4-Exp) it returns only the attention count derived from the layer pattern; mamba layers are reported by ``get_num_mamba_layers``. """ cfg = self.pretrained_config if is_nemotron_hybrid(cfg): return cfg.hybrid_override_pattern.count("*") if is_qwen3_hybrid(cfg) or is_qwen4_exp(cfg): return get_qwen3_hybrid_num_attention_layers(cfg) if is_kimi_linear(cfg): return get_kimi_linear_num_attention_layers(cfg) if is_glm5_next(cfg): return get_glm5_next_num_attention_layers(cfg) return cfg.num_hidden_layers def get_num_mamba_layers(self) -> int: """Number of Mamba / linear-attention layers (0 for non-hybrid).""" cfg = self.pretrained_config if is_nemotron_hybrid(cfg): return cfg.hybrid_override_pattern.count("M") if is_qwen3_hybrid(cfg) or is_qwen4_exp(cfg): return cfg.num_hidden_layers - get_qwen3_hybrid_num_attention_layers( cfg) if is_kimi_linear(cfg): return cfg.num_hidden_layers - get_kimi_linear_num_attention_layers( cfg) if is_glm5_next(cfg): # From the literal layer_types list: the composite config may not # carry num_hidden_layers at the top level before normalization. _, kda_mask = get_glm5_next_layer_masks(cfg) return sum(kda_mask) return 0 def _mirror_text_subconfig_attrs( pretrained_config: transformers.PretrainedConfig) -> None: """Mirror text sub-config attributes onto the parent config. Composite configs (e.g. Qwen2_5_VLConfig) keep text-side fields like `vocab_size`, `hidden_size`, `num_attention_heads`, etc. inside a `text_config` sub-config. Prior to transformers 5, the parent config delegated attribute lookups there automatically; that delegation was removed in 5.x. Copying the sub-config's attributes onto the parent keeps downstream code (which accesses these on the top-level config) working without having to learn about the composite layout. """ text_config = getattr(pretrained_config, "text_config", None) if text_config is None or not isinstance(text_config, transformers.PretrainedConfig): return for key, value in vars(text_config).items(): if key.startswith("_"): continue try: getattr(pretrained_config, key) except AttributeError: setattr(pretrained_config, key, value)