# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 """Speech-LLM (SALM) GGUFs from a speechlm2 checkpoint: the decoder, a standard llama.cpp NemotronH GGUF, and next to it the encoder file (conversion/salm_encoder.py): the speech encoder and the CTC timestamp head. The decoder names its encoder file (nemo_speech.speech_model) and both carry the checkpoint's id (nemo_speech.checkpoint_id), so the runtime finds the pair from the decoder alone and refuses parts of different checkpoints. One encoder file serves every decoder profile of a checkpoint. The checkpoint's `llm.*` tensors (Nemotron-H MoE backbone with its MTP layer) go through llama.cpp's NemotronH converter to BF16 (salm_decoder_hf.py), then the patched llama-quantize rewrites the routed experts and the dense linears (attention, Mamba in/out, shared experts) by `profile`: nvfp4 routed experts NVFP4, dense linears BF16 (default; Blackwell runs the experts natively) iq4_nl routed experts IQ4_NL, dense linears Q8_0 (other GPUs, CPU, Vulkan, Metal; sized for 24 GB GPUs) q2_0 routed experts Q2_0 except those of the last Q2_KEPT_MOE_LAYERS MoE layers (IQ4_NL), dense linears, embeddings and the output head Q8_0 (the smallest decoder, for smaller GPUs) Otherwise embeddings, the output head and the norms stay BF16/F32. Q2_0 needs the scale fitting of patches/: stock llama.cpp rounds every block to three of its four levels, so the converter checks its output for the fourth. The speech encoder (conversion/pee.py) is F16 ("none" writes the decoder alone, for a second profile of a checkpoint whose encoder file exists) and the CTC timestamp head (conversion/ctc_timestamp.py) comes from the checkpoint when it carries one (ctc_timestamp.*). A checkpoint quantized with ModelOpt (hf_quant_config.json; NVFP4 and FP8 layers, Hugging Face NemotronH tensor names) keeps its weights: it converts to `nvfp4` only, the decoder straight from llama.cpp's converter without llama-quantize. NVFP4 tensors keep their blocks and scales, FP8 tensors become their BF16 values and a per-tensor scale (salm_decoder_hf.py), and the encoder file defaults to F32, which holds the FP8 encoder linears exactly. """ from __future__ import annotations import hashlib import json import os import shutil import struct import subprocess import sys from pathlib import Path from .s2s_components.voicechat_source import ( default_quantizer_path, ensure_llama_checkout, ensure_quantizer, ) DEFAULT_PROFILE = "nvfp4" PROFILES = ("nvfp4", "iq4_nl", "q2_0") DENSE_TYPE = { "nvfp4": "bf16", "iq4_nl": "q8_0", "q2_0": "q8_0", } # the dense linears of each profile Q2_KEPT_MOE_LAYERS = 5 # q2_0: the last MoE layers whose routed experts stay IQ4_NL Q2_0_BLOCK_BYTES = 18 # FP16 scale and 64 2-bit codes ENCODER_TYPES = ("f16", "f32", "none") DEFAULT_ENCODER = "f16" QUANTIZED_ENCODER = "f32" # the default for a ModelOpt checkpoint: its FP8 encoder linears exactly MODELOPT_ALGOS = {"NVFP4", "FP8"} # the ModelOpt layer formats a checkpoint may use ENCODER_FILE_KEY = "nemo_speech.speech_model" # the encoder file the decoder names CHECKPOINT_ID_KEY = "nemo_speech.checkpoint_id" EXPERT_TENSORS = "ffn_(up|down|gate)_exps" DENSE_TENSORS = ("ssm_(in|out)", "ffn_(up|down)_shexp", "attn_(q|k|v|output)") AUDIO_LOCATOR = "<|audio|>" def normalize_profile(profile: str) -> str: value = profile.lower() if value not in PROFILES: raise ValueError("salm --outtype must be " + ", ".join(PROFILES)) return value def backbone_config(source: Path) -> dict: """The LLM backbone's HF config: llm_backbone/config.json, else the SALM config's llm_config.""" path = source / "llm_backbone" / "config.json" if path.is_file(): return json.loads(path.read_text(encoding="utf-8")) config = json.loads((source / "config.json").read_text(encoding="utf-8")) if isinstance(config.get("llm_config"), dict): return config["llm_config"] raise RuntimeError(f"{source}: no llm_backbone/config.json and no llm_config in config.json") def modelopt_quantization(source: Path) -> dict | None: """The `quantization` section of a ModelOpt checkpoint's hf_quant_config.json, or None for an unquantized one. Raises for layer formats other than NVFP4 and FP8 or a quantized KV cache.""" path = source / "hf_quant_config.json" if not path.is_file(): return None config = json.loads(path.read_text(encoding="utf-8")) if (config.get("producer") or {}).get("name", "").lower() != "modelopt": raise RuntimeError(f"{path}: not a ModelOpt quantization config") quantization = config.get("quantization") or {} algos = { layer.get("quant_algo") for layer in (quantization.get("quantized_layers") or {}).values() if isinstance(layer, dict) } or {quantization.get("quant_algo")} if not algos <= MODELOPT_ALGOS: raise RuntimeError( f"{path}: unsupported ModelOpt formats {sorted(str(a) for a in algos - MODELOPT_ALGOS)}" ) if quantization.get("kv_cache_quant_algo"): raise RuntimeError(f"{path}: a quantized KV cache is not supported") return quantization def kept_expert_layers(profile: str, config: dict) -> list[int]: """The decoder blocks whose routed experts stay IQ4_NL: for q2_0 the last MoE layers of the main stack.""" if profile != "q2_0": return [] moe = [i for i, kind in enumerate(config.get("layers_block_type") or []) if kind == "moe"] if not moe: raise RuntimeError("the backbone config lists no MoE layers (layers_block_type)") return moe[-Q2_KEPT_MOE_LAYERS:] def stage(source: Path, staging: Path, weights: bool = True) -> None: """An HF-shaped directory for the converter: backbone config, the tokenizer without the audio locator (it has no embedding row) and, with `weights`, links to the checkpoint's safetensors.""" if staging.exists(): shutil.rmtree(staging) staging.mkdir(parents=True) config = backbone_config(source) if config.get("architectures") != ["NemotronHForCausalLM"]: raise RuntimeError(f"unsupported SALM backbone {config.get('architectures')}") (staging / "config.json").write_text(json.dumps(config, indent=2), encoding="utf-8") tokenizer = json.loads((source / "tokenizer.json").read_text(encoding="utf-8")) tokenizer["added_tokens"] = [ t for t in tokenizer.get("added_tokens", []) if t.get("content") != AUDIO_LOCATOR ] (staging / "tokenizer.json").write_text( json.dumps(tokenizer, ensure_ascii=False), encoding="utf-8" ) tokenizer_config = json.loads((source / "tokenizer_config.json").read_text(encoding="utf-8")) tokenizer_config.pop("audio_token", None) tokenizer_config.pop("model_specific_special_tokens", None) extra = tokenizer_config.get("extra_special_tokens") if isinstance(extra, dict): extra.pop("audio_token", None) decoder = tokenizer_config.get("added_tokens_decoder") or {} for key in [k for k, v in decoder.items() if v.get("content") == AUDIO_LOCATOR]: del decoder[key] (staging / "tokenizer_config.json").write_text( json.dumps(tokenizer_config, indent=2, ensure_ascii=False), encoding="utf-8" ) for name in ( "generation_config.json", "hf_quant_config.json", ): # the converter reads ModelOpt's config if (source / name).is_file(): shutil.copyfile(source / name, staging / name) if not weights: return files = sorted(source.glob("*.safetensors")) + sorted(source.glob("*.safetensors.index.json")) if not any(w.suffix == ".safetensors" for w in files): raise RuntimeError(f"{source}: no safetensors") for w in files: (staging / w.name).symlink_to(w.resolve()) def convert( source: Path, outfile: Path, profile: str = DEFAULT_PROFILE, llama_cpp: Path | None = None, quantizer: Path | None = None, auto_build_quantizer: bool = True, dry_run: bool = False, encoder: str | None = None, checkpoint_name: str | None = None, ) -> None: profile = normalize_profile(profile) dense = DENSE_TYPE[profile] quantized = modelopt_quantization(source) is not None if quantized and profile != "nvfp4": raise ValueError( f"{source} is quantized with ModelOpt and converts to --outtype nvfp4 only, keeping its weights; " f"convert {profile} from the BF16 checkpoint" ) encoder = encoder_type(profile, encoder, quantized) if encoder not in ENCODER_TYPES: raise ValueError("salm encoder type must be " + ", ".join(ENCODER_TYPES)) name = checkpoint_name or source.name repository = Path(__file__).resolve().parent.parent llama = ensure_llama_checkout(llama_cpp or repository / "llama.cpp", repository) quantize = None if ( not dry_run and not quantized ): # before the long conversion, so a missing quantizer fails fast quantize = ensure_quantizer( llama, quantizer, profile=profile, repo_root=repository, auto_build=auto_build_quantizer ) work = outfile.with_name(outfile.name + ".work") speech = outfile.with_name(encoder_file_name(name, encoder)) if encoder != "none" else None identity = checkpoint_id(source) if speech: # first: about a minute, and a bad checkpoint fails before the decoder's long run ctc_source = ctc_checkpoint(source) if dry_run: print( f"[convert-salm] would write the encoder file {speech.name}: the {encoder.upper()} encoder" + (f" and the CTC timestamp head of {ctc_source.name}" if ctc_source else "") ) else: from . import pee from .salm_encoder import write_encoder_file work.mkdir(parents=True, exist_ok=True) print(f"[convert-salm] encoder -> {encoder.upper()} GGUF") pee.convert(source, work / "encoder.gguf", "fp16" if encoder == "f16" else encoder) if ctc_source: from . import ctc_timestamp print(f"[convert-salm] CTC timestamp head of {ctc_source.name}") ctc_timestamp.convert(ctc_source, work / "ctc.gguf", name) write_encoder_file( work / "encoder.gguf", work / "ctc.gguf" if ctc_source else None, speech, name=f"{name} Encoder {encoder.upper()}", checkpoint_id=identity, ) staging = work / "hf" print(f"[convert-salm] staging the decoder of {source}") stage(source, staging) # a ModelOpt checkpoint's decoder comes straight from the converter (its weights are already quantized) decoder = ( outfile.with_name(outfile.name + ".writing") if quantized else work / "decoder-bf16.gguf" ) print( f"[convert-salm] decoder -> {'GGUF with the checkpoint weights' if quantized else 'BF16 GGUF'} {decoder}" ) paired = encoder if encoder != "none" else (QUANTIZED_ENCODER if quantized else DEFAULT_ENCODER) keys = decoder_keys(name, identity, paired) command = [ sys.executable, str(Path(__file__).with_name("salm_decoder_hf.py")), str(llama), str(staging), "--outfile", str(decoder), "--outtype", "bf16", "--model-name", f"{name} {profile.upper()}", *[arg for key in keys for arg in ("--nemo-speech-key", key)], *(["--dry-run"] if dry_run else []), ] try: subprocess.run(command, check=True) except BaseException: if quantized: decoder.unlink(missing_ok=True) raise if dry_run: # tensor mapping checked, nothing written shutil.rmtree(work, ignore_errors=True) return if quantized: decoder.replace(outfile) shutil.rmtree(work, ignore_errors=True) if speech: print(f"[convert-salm] {outfile.name} and its encoder file {speech.name}") return quantize_decoder( decoder, outfile, profile, quantize, dense, kept_expert_layers(profile, backbone_config(source)), llama, ) shutil.rmtree(work, ignore_errors=True) if speech: print(f"[convert-salm] {outfile.name} and its encoder file {speech.name}") def encoder_file_name(name: str, encoder: str = DEFAULT_ENCODER) -> str: """The encoder file's name: the checkpoint's name and the encoder type, the same for every decoder.""" return f"{name}-Encoder-{encoder.upper()}.gguf" def decoder_keys(name: str, identity: str, encoder: str = DEFAULT_ENCODER) -> list[str]: """The decoder's nemo_speech keys: the checkpoint id and the encoder file it pairs with, also when this run writes no encoder file (--salm-encoder none reuses the checkpoint's existing one).""" return [ f"{CHECKPOINT_ID_KEY}={identity}", f"{ENCODER_FILE_KEY}={encoder_file_name(name, encoder)}", ] def checkpoint_id(source: Path) -> str: """An id of the checkpoint's weights: the names, types and shapes of the tensors of its safetensors and the first and last 4 KiB of each tensor's data (a few MB read, against the whole file).""" digest = hashlib.sha256() for path in sorted(source.glob("*.safetensors")): with open(path, "rb") as f: (size,) = struct.unpack(" Path | None: """The checkpoint safetensors that carries the CTC timestamp head (ctc_timestamp.*), if any.""" from safetensors import safe_open for path in sorted(source.glob("*.safetensors")): with safe_open(str(path), framework="pt", device="cpu") as f: if (f.metadata() or {}).get("ctc_timestamp_format"): return path return None def encoder_type(profile: str, encoder: str | None = None, quantized: bool = False) -> str: """The encoder's type: F16 (every profile shares one encoder file), F32 by default for a ModelOpt checkpoint (its FP8 encoder linears exactly), or "none".""" normalize_profile(profile) return (encoder or (QUANTIZED_ENCODER if quantized else DEFAULT_ENCODER)).lower() def tensor_rules( profile: str, dense: str = "bf16", kept_layers: list[int] | None = None ) -> list[str]: """llama-quantize --tensor-type rules: they apply over a quantized base type and the first match wins, so the expert and dense rules come first and a catch-all keeps everything else BF16 (the base type is never used). `kept_layers`: blocks whose routed experts stay IQ4_NL (kept_expert_layers). """ rules = [] if kept_layers: rules.append(rf"blk\.({'|'.join(str(i) for i in kept_layers)})\.{EXPERT_TENSORS}=iq4_nl") rules.append(f"{EXPERT_TENSORS}={profile}") if dense != "bf16": rules.extend(f"{pattern}={dense}" for pattern in DENSE_TENSORS) if profile == "q2_0": rules += [r"^token_embd\.weight=q8_0", r"^output\.weight=q8_0"] return rules + [".=bf16"] def q2_0_uses_top_level(blocks: bytes) -> bool: """Whether Q2_0 blocks use code 3 (+2 x scale). Stock llama.cpp's rounding takes the scale from the block's largest magnitude and never reaches it; the fitted scales of patches/ do.""" import numpy as np raw = np.frombuffer(blocks, dtype=np.uint8) codes = raw[: raw.size - raw.size % Q2_0_BLOCK_BYTES].reshape(-1, Q2_0_BLOCK_BYTES)[:, 2:] shifts = np.array([0, 2, 4, 6], dtype=np.uint8) return bool((((codes[:, :, None] >> shifts) & 3) == 3).any()) def check_q2_0_rounding(path: Path, llama_cpp: Path, quantize: Path) -> None: """Fails when the first Q2_0 tensor of `path` was rounded by an unpatched llama-quantize (`quantize`). Reads it in a fresh interpreter with the pinned llama.cpp's gguf-py first on the path: an installed gguf may predate Q2_0. """ script = ( "import sys, gguf\n" "for t in gguf.GGUFReader(sys.argv[1]).tensors:\n" " if t.tensor_type == gguf.GGMLQuantizationType.Q2_0:\n" " sys.stdout.buffer.write(t.data.reshape(-1)[: int(sys.argv[2])].tobytes())\n" " break\n" ) env = dict(os.environ, PYTHONPATH=str(llama_cpp / "gguf-py")) blocks = subprocess.run( [sys.executable, "-c", script, str(path), str(Q2_0_BLOCK_BYTES * 65536)], env=env, check=True, capture_output=True, ).stdout if not blocks: raise RuntimeError(f"{path} has no Q2_0 tensor") if not q2_0_uses_top_level(blocks): managed = default_quantizer_path(llama_cpp) if Path(quantize).resolve() == managed: hint = f"remove {managed.parents[1]} so the converter rebuilds it from patches/" else: hint = ( "pass --llama-quantize a build of this repository's llama.cpp with patches/ applied" ) raise RuntimeError(f"{quantize} rounded Q2_0 without the fitted scales of patches/; {hint}") def quantize_decoder( bf16: Path, outfile: Path, profile: str, quantize: Path, dense: str = "bf16", kept_layers: list[int] | None = None, llama_cpp: Path | None = None, ) -> None: """A BF16 decoder GGUF with its routed experts in `profile` (IQ4_NL in `kept_layers`) and its dense linears in `dense`.""" threads = ( len(os.sched_getaffinity(0)) if hasattr(os, "sched_getaffinity") else os.cpu_count() or 8 ) temporary = outfile.with_name(outfile.name + ".quantizing") print(f"[convert-salm] routed experts -> {profile.upper()}, dense linears -> {dense.upper()}") command = [str(quantize)] for rule in tensor_rules(profile, dense, kept_layers): command += ["--tensor-type", rule] command += [str(bf16), str(temporary), "Q8_0", str(threads)] try: subprocess.run(command, check=True) if not temporary.is_file() or temporary.stat().st_size == 0: raise RuntimeError(f"llama-quantize did not produce {temporary}") if profile == "q2_0": check_q2_0_rounding( temporary, llama_cpp or Path(__file__).resolve().parents[1] / "llama.cpp", quantize ) temporary.replace(outfile) finally: temporary.unlink(missing_ok=True)