Archived
Document Spark support and add reference differential
This commit is contained in:
1 parent
1483a1ae17
commit
125854df79
3 files changed
+229
No files matched your search
@@ -34,6 +34,7 @@ To support the development of radiance: [Patreon](https://patreon.com/StillDeadC
|
||||
| Sage-A1.5 FP8, DFlash2 drafter, vision | `sage-a1.5-fp8-df2.rad` (34 GiB) | [download](https://huggingface.co/Aijuus/sage-a1.5-fp8-df2) | `sage-a1.5-df2.yaml` |
|
||||
| MiniCPM5-2B FP8, DSpark drafter | `minicpm5-2b-fp8.rad` (3 GiB) | [download](https://huggingface.co/StillDeadcode/minicpm5-2b-fp8) | `minicpm5-2b.yaml` |
|
||||
| MiniCPM5-2B bf16, DSpark drafter | `minicpm5-2b-bf16.rad` (5 GiB) | [download](https://huggingface.co/StillDeadcode/minicpm5-2b-bf16) | `minicpm5-2b-bf16.yaml` |
|
||||
| Spark X2.6 4B bf16: thinking and tool calls, one card | `spark-x2.6-4b-bf16.rad` (8.3 GiB) | [convert](#convert-a-checkpoint) | — |
|
||||
| Gemma 4 31B: GPTQ int4 trunk, int8 activations, MTP, vision | `gemma4-31b-w4a8.rad` (20 GiB) | [download](https://huggingface.co/keptn/gemma4-31b-w4a8) | `gemma4-31b-w4a8.yaml` |
|
||||
| Gemma 4 31B w5: GPTQ int5 trunk (a scale and a zero point per 128 weights), MTP, vision | `gemma4-31b-w5.rad` (23 GiB) | [download](https://huggingface.co/keptn/gemma4-31b-w5) | `gemma4-31b-w5.yaml` |
|
||||
| Gemma 4 31B heretic-ara (refusals removed): GPTQ int4 trunk, int8 activations, MTP, vision | `gemma4-31b-heretic-ara-w4a8.rad` (20 GiB) | [download](https://huggingface.co/keptn/gemma4-31b-heretic-ara-w4a8) | `gemma4-31b-heretic-ara-w4a8.yaml` |
|
||||
@@ -282,6 +283,8 @@ budgets, long context, speculative decoding and operating a server.
|
||||
`data/recipes/` names. Download checkpoints with `scripts/hfget.sh <org/repo> <dir>`.
|
||||
|
||||
```sh
|
||||
# Spark X2.6 uses the checkpoint's BF16 weights, tokenizer and chat template; no recipe required.
|
||||
build/bin/rad-convert /srv/models/hf/XHToken-Spark-X2.6-4B -o spark-x2.6-4b-bf16.rad --max-ctx 4096
|
||||
build/bin/rad-convert /srv/models/hf/Qwen/Qwen3.8-27B -o qwen3.8-27b-bf16.rad \
|
||||
--recipe data/recipes/q38-27b-bf16.recipe
|
||||
build/bin/rad-convert /srv/models/hf/Qwen/Qwen3.8-Flash-Next -o qwen3.8-flash-next-bf16.rad --max-ctx 4096
|
||||
@@ -320,6 +323,11 @@ reads it: NVFP4 (compressed-tensors' and NVIDIA ModelOpt's) to the MXFP4 kernels
|
||||
Flash-Next's int4 block projections to int8. [docs/GUIDE.md](docs/GUIDE.md) names the recipes of record
|
||||
with their `rad-convert` commands.
|
||||
|
||||
Spark X2.6 currently runs at `--tp 1` without a drafter or quantisation recipe. Its checkpoint's
|
||||
chat template enables thinking by default; replies return reasoning in `reasoning_content`. Set
|
||||
`chat_template_kwargs.enable_thinking` to `false` for a non-thinking request. Tool calls use the
|
||||
checkpoint's tagged format and are returned as OpenAI `tool_calls`.
|
||||
|
||||
## Test
|
||||
|
||||
```sh
|
||||
|
||||
@@ -20,6 +20,7 @@ plugin claims.
|
||||
| `arch/glm5next` | `glm5next` | *(empty)* | GLM-5.3-Flash from AMD's Quark MXFP4 export: four mHC residual streams over 34 KDA and 11 sparse latent-attention (MLA) layers with a DSA indexer, 288 MXFP4 experts at top-8 under sigmoid routing, block-FP8 attention and dense layers at f32 scales, each weight served as stored; its MTP layer is a serial drafter. Pictures through its vision tower (`arch/common/rad_block_glm_vision.h`); bf16 KV |
|
||||
| `arch/bailing_hybrid` | `bailinghybrid` | *(empty)* | Ling-3.0-flash and its fine-tunes (Ling-3.0-flash-Sante) from the bf16 checkpoint: 35 KDA and 7 dense latent-attention (MLA) layers with a rotary part and a head-wise gate, 512 experts at top-8 under group-limited sigmoid routing with an ungated shared expert, the routed experts as the recipe stores them (`data/recipes/ling3-w4a8-rtn.recipe`); its MTP layer is a serial drafter; bf16 KV |
|
||||
| `arch/lfm2_bf16` | `lfm2` | *(empty)* | LiquidAI's LFM2 and LFM2-VL, bf16, one rank |
|
||||
| `arch/spark25_bf16` | `spark25` | *(empty)* | Spark X2.6, bf16 checkpoint, mixed sliding/full attention, one rank ([geometry](#spark25-spark-x26)) |
|
||||
| `arch/embgemma2` | `embeddinggemma2` | *(empty)* | EmbeddingGemma 2: a bidirectional encoder with Gemma 4's vision tower and audio conformer, pooled to one vector |
|
||||
| `arch/nomicbert` | `nomicbert` | *(empty)* | nomic-embed-text-v2-moe: a bidirectional post-norm encoder, 12 layers with 8 experts at top 2 on every second layer, mean-pooled to one vector an input |
|
||||
| `arch/nemotron35_embed` | `mistral3` | *(empty)* | Nemotron 3.5 Embed 1B/8B and Rerank 8B: native BF16 bidirectional text/image retrieval with a Pixtral tower, TP1 ([geometry and contracts](#nemotron35_embed-embedding-models)) |
|
||||
@@ -70,6 +71,41 @@ comes first in one and second in the other. The tables, by plugin:
|
||||
|
||||
---
|
||||
|
||||
# `spark25`: Spark X2.6
|
||||
|
||||
`model_type: spark2_5` / `Spark2_5ForCausalLM`. The 4B checkpoint has 36 layers, hidden size
|
||||
2560, 16 query heads and 4 KV heads at head dimension 256, a 10240-wide gated MLP and a tied
|
||||
131072-token embedding. It has no q/k norms or projection biases. The residual is accumulated in
|
||||
f32; norms write bf16 before the projections.
|
||||
|
||||
The checkpoint's fused Q/K/V matrix is split by rows in that order at conversion; gate and up are
|
||||
joined in that order. The MLP uses **exact erf GELU**, not `gelu_mul`'s tanh approximation. Each
|
||||
attention head's output is multiplied by its own sigmoid gate before the output projection.
|
||||
`layer_types` is authoritative: sliding layers use window 512 and full-head NeoX rotary at theta
|
||||
10000; full layers use 64 rotary dimensions at theta 5000000. Both use the full KV group, with the
|
||||
window applied by the local attention op, so prefix caching remains valid.
|
||||
|
||||
The initial implementation serves BF16 at TP1, tied embeddings, headwise sigmoid gates and the
|
||||
checkpoint's own rotary parameters. It does not implement DSpark, quantisation recipes, other gate
|
||||
activations or rotary-scaling overrides. The tokenizer and its template come from the checkpoint;
|
||||
the architecture declares the `<think>`/`</think>` reasoning markers and the
|
||||
`<tool_call>name<arg_key>...` tool-call format.
|
||||
|
||||
For a CPU differential against the reference implementation's `configuration_spark.py` and
|
||||
`modeling_spark.py`, with Python's torch, transformers and safetensors installed:
|
||||
|
||||
```sh
|
||||
python3 tests/spark25_reference.py --reference /path/to/XHToken-Spark-X2.6-4B --build build
|
||||
SPARK25_REFERENCE_TEMPLATE=/path/to/XHToken-Spark-X2.6-4B/chat_template.jinja \
|
||||
build/tests/spark25_chat_test
|
||||
```
|
||||
|
||||
The first builds a small deterministic checkpoint and compares prefill, chunked prefill and cached
|
||||
decode against the reference without a GPU. The second also exercises the checkpoint's actual chat
|
||||
template, which is not redistributed in this tree.
|
||||
|
||||
---
|
||||
|
||||
# `llama`: the plain decoder
|
||||
|
||||
`model_type: llama` with `LlamaForCausalLM`. Every layer is full attention; unlike `qwen35_fp8`
|
||||
|
||||
@@ -0,0 +1,185 @@
|
||||
#!/usr/bin/env python3
|
||||
"""CPU-only differential test against a Spark checkpoint's modeling_spark.py.
|
||||
|
||||
Requires torch, transformers and safetensors. It creates a tiny two-layer random checkpoint,
|
||||
converts and serves it on the host, then compares prefill and cached decode with the HF reference.
|
||||
No model weights are copied from the reference checkpoint, and no GPU is visible to either process.
|
||||
"""
|
||||
import argparse
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import socket
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
import types
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
# Set before importing torch or launching a Radiance child, even on a HIP-enabled build.
|
||||
os.environ.update(ROCR_VISIBLE_DEVICES="GPU-no-such-device", HIP_VISIBLE_DEVICES="",
|
||||
CUDA_VISIBLE_DEVICES="", OMP_NUM_THREADS="2", OMP_WAIT_POLICY="passive")
|
||||
|
||||
|
||||
def load_reference(path):
|
||||
pkg = types.ModuleType("spark25_reference")
|
||||
pkg.__path__ = [str(path)]
|
||||
sys.modules[pkg.__name__] = pkg
|
||||
for name in ("configuration_spark", "modeling_spark"):
|
||||
spec = importlib.util.spec_from_file_location(pkg.__name__ + "." + name, path / (name + ".py"))
|
||||
if spec is None or spec.loader is None:
|
||||
raise RuntimeError("cannot load " + name)
|
||||
mod = importlib.util.module_from_spec(spec)
|
||||
sys.modules[spec.name] = mod
|
||||
spec.loader.exec_module(mod)
|
||||
return sys.modules[pkg.__name__ + ".configuration_spark"], sys.modules[pkg.__name__ + ".modeling_spark"]
|
||||
|
||||
|
||||
def write_tokenizer(path):
|
||||
def symbol(b):
|
||||
def printable(c):
|
||||
return 33 <= c <= 126 or 161 <= c <= 172 or 174 <= c <= 255
|
||||
return chr(b if printable(b) else 256 + sum(not printable(c) for c in range(b)))
|
||||
vocab = {symbol(b): b for b in range(256)}
|
||||
added = []
|
||||
for i, s in enumerate(("<|endoftext|>", "<|im_start|>", "<|im_end|>")):
|
||||
vocab[s] = 256 + i
|
||||
added.append(dict(id=256 + i, content=s, single_word=False, lstrip=False,
|
||||
rstrip=False, normalized=False, special=True))
|
||||
bytelevel = dict(type="ByteLevel", add_prefix_space=False, trim_offsets=False, use_regex=True)
|
||||
tok = dict(version="1.0", added_tokens=added, normalizer=None, pre_tokenizer=bytelevel,
|
||||
post_processor=None, decoder=bytelevel,
|
||||
model=dict(type="BPE", dropout=None, unk_token=None, byte_fallback=False,
|
||||
vocab=vocab, merges=[]))
|
||||
(path / "tokenizer.json").write_text(json.dumps(tok, ensure_ascii=False))
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description=__doc__)
|
||||
ap.add_argument("--reference", required=True, type=Path, help="directory with modeling_spark.py")
|
||||
ap.add_argument("--build", required=True, type=Path, help="Radiance build directory")
|
||||
args = ap.parse_args()
|
||||
import torch
|
||||
import transformers
|
||||
from safetensors.torch import save_file
|
||||
|
||||
torch.set_num_threads(2)
|
||||
cfgmod, modelmod = load_reference(args.reference.resolve())
|
||||
# The checkpoint targets Transformers 4.57; v5 changed this declaration from a list to a map.
|
||||
if int(transformers.__version__.split(".")[0]) >= 5:
|
||||
modelmod.Spark2_5ForCausalLM._tied_weights_keys = {"lm_head.weight": "model.embedding.weight"}
|
||||
config = cfgmod.Spark2_5Config(
|
||||
vocab_size=259, hidden_size=32, intermediate_size=64, num_hidden_layers=2,
|
||||
num_attention_heads=4, num_key_value_heads=2, head_dim=8, hidden_act="gelu",
|
||||
max_position_embeddings=128, rms_norm_eps=1e-6, tie_word_embeddings=True,
|
||||
headwise_attn_output_gate=True, gate_attn_act_mode="sigmoid", sliding_window=4,
|
||||
layer_types=["sliding_attention", "full_attention"],
|
||||
rope_parameters={"sliding_attention": {"rope_theta": 10000.0, "partial_rotary_factor": 1.0},
|
||||
"full_attention": {"rope_theta": 5000000.0, "partial_rotary_factor": 0.5}},
|
||||
bos_token_id=256, eos_token_id=256, pad_token_id=256)
|
||||
torch.manual_seed(19)
|
||||
model = modelmod.Spark2_5ForCausalLM(config).eval().to(torch.bfloat16)
|
||||
with torch.no_grad():
|
||||
for name, w in model.named_parameters():
|
||||
if name.endswith("layernorm.weight") or name == "model.norm.weight":
|
||||
w.copy_((1 + 0.08 * torch.randn(w.shape)).to(w.dtype))
|
||||
elif name == "model.embedding.weight":
|
||||
w.copy_((0.4 * torch.randn(w.shape)).to(w.dtype))
|
||||
elif name.endswith("g_proj.weight"):
|
||||
w.copy_((0.2 * torch.randn(w.shape)).to(w.dtype))
|
||||
else:
|
||||
w.copy_((0.16 * torch.randn(w.shape)).to(w.dtype))
|
||||
|
||||
def reference(ids):
|
||||
n = len(ids)
|
||||
q, k = torch.arange(n)[:, None], torch.arange(n)[None, :]
|
||||
full = torch.where(k <= q, 0.0, float("-inf"))[None, None, :, :]
|
||||
local = torch.where((k <= q) & (k > q - config.sliding_window), 0.0,
|
||||
float("-inf"))[None, None, :, :]
|
||||
# Explicit masks also bridge the Transformers 4.57/5.x mask-helper signature change.
|
||||
with torch.inference_mode():
|
||||
logits = model(torch.tensor([ids]), attention_mask={"full_attention": full,
|
||||
"sliding_attention": local}, use_cache=False).logits[0, -1].float()
|
||||
return torch.log_softmax(logits, dim=-1)
|
||||
|
||||
build = args.build.resolve()
|
||||
env = os.environ.copy()
|
||||
env.update(RADIANCE_HOME=str(build / "radiance_home"), RADIANCE_HOST_VRAM_MIB="512")
|
||||
with tempfile.TemporaryDirectory(prefix="spark25-reference-") as tmp:
|
||||
root = Path(tmp)
|
||||
ckpt = root / "checkpoint"
|
||||
ckpt.mkdir()
|
||||
config_json = config.to_dict()
|
||||
config_json.update(model_type="spark2_5", architectures=["Spark2_5ForCausalLM"])
|
||||
(ckpt / "config.json").write_text(json.dumps(config_json, indent=2))
|
||||
state = {k: v.contiguous().clone() for k, v in model.state_dict().items() if k != "lm_head.weight"}
|
||||
save_file(state, ckpt / "model.safetensors")
|
||||
write_tokenizer(ckpt)
|
||||
rad = root / "model.rad"
|
||||
subprocess.run([build / "bin/rad-convert", ckpt, "-o", rad, "--max-ctx", "64",
|
||||
"--max-tok", "32"], env=env, check=True)
|
||||
with socket.socket() as sock:
|
||||
sock.bind(("127.0.0.1", 0))
|
||||
port = sock.getsockname()[1]
|
||||
with (root / "server.log").open("w+") as log:
|
||||
proc = subprocess.Popen([build / "bin/radiance", "--model", rad, "--host", "127.0.0.1",
|
||||
"--port", str(port), "--kernels", "libref", "--debug-accept-reference-kernels",
|
||||
"--max-model-len", "64", "--max-num-batched-tokens", "32", "--max-num-seqs", "1"],
|
||||
env=env, stdout=log, stderr=subprocess.STDOUT)
|
||||
try:
|
||||
url = f"http://127.0.0.1:{port}"
|
||||
for _ in range(200):
|
||||
if proc.poll() is not None:
|
||||
raise RuntimeError("Radiance exited before listening")
|
||||
try:
|
||||
urllib.request.urlopen(url + "/health", timeout=1).close()
|
||||
break
|
||||
except (OSError, urllib.error.URLError):
|
||||
time.sleep(0.1)
|
||||
else:
|
||||
raise RuntimeError("Radiance did not start")
|
||||
# The longer prompts exceed the local window, and the last also crosses the
|
||||
# 32-token prefill chunk boundary. Compare cached decode to the same HF continuation.
|
||||
for prompt in ("a", "hello", "longer prompt", "the quick brown fox jumps over the lazy dog twice"):
|
||||
request = dict(prompt=prompt, max_tokens=5, temperature=0, logprobs=10,
|
||||
ignore_eos=True)
|
||||
data = json.dumps(request).encode()
|
||||
req = urllib.request.Request(url + "/v1/completions", data,
|
||||
{"Content-Type": "application/json"})
|
||||
result = json.load(urllib.request.urlopen(req, timeout=30))
|
||||
rows = result["choices"][0]["logprobs"]["content"]
|
||||
if len(rows) != 5:
|
||||
raise AssertionError(f"{prompt!r}: expected 5 decode rows, got {len(rows)}")
|
||||
ids = list(prompt.encode())
|
||||
max_error = 0.0
|
||||
for row in rows:
|
||||
expected = reference(ids)
|
||||
if row["id"] != int(expected.argmax()):
|
||||
raise AssertionError(f"{prompt!r}: token {row['id']} != reference {int(expected.argmax())}")
|
||||
for candidate in row["top_logprobs"]:
|
||||
max_error = max(max_error,
|
||||
abs(candidate["logprob"] - float(expected[candidate["id"]])))
|
||||
ids.append(row["id"])
|
||||
# BF16 logits / attention reduction order differ slightly from CPU torch.
|
||||
if max_error >= 0.15:
|
||||
raise AssertionError(f"{prompt!r}: max top-10 log-prob error {max_error:.5f}")
|
||||
print(f"{prompt!r}: 5/5 top-1, max top-10 log-prob error {max_error:.5f}")
|
||||
except Exception:
|
||||
log.flush()
|
||||
log.seek(0)
|
||||
print(log.read(), file=sys.stderr)
|
||||
raise
|
||||
finally:
|
||||
proc.terminate()
|
||||
try:
|
||||
proc.wait(timeout=10)
|
||||
except subprocess.TimeoutExpired:
|
||||
proc.kill()
|
||||
proc.wait()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user