Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
8e949df
feat(mm): add the inkling 2 image processor
ischencheng Nov 20, 2026
e2171f1
feat(server): add the inkling 2 reasoning and tool-call parsers
ischencheng Nov 20, 2026
30ca54f
feat(inkling2): add the inkling 2 vision tower
ischencheng Nov 20, 2026
5babd4b
feat(inkling2_loop): add the inkling 2 mini text model with its three…
ischencheng Nov 20, 2026
1f65d77
feat(spec): add the inkling 2 dspark draft and its proposer
ischencheng Nov 20, 2026
b7c0ef2
fix(inkling2): build the vision rope frequencies on the cpu
ischencheng Nov 20, 2026
9d4d5cb
feat(moe): stream offload prefill experts in a looped model's layer o…
ischencheng Nov 20, 2026
412fade
test(inkling2_loop): check the checkpoint transforms against the refe…
ischencheng Nov 20, 2026
baf572d
Merge branch 'feat/inkling2-mini-vision' into feat/inkling2-mini
ischencheng Nov 20, 2026
94a34de
test(spec): pin the dspark markov head and the windowed-target gate
ischencheng Nov 20, 2026
8766c38
feat(inkling2_loop): serve image input through the vision tower
ischencheng Nov 20, 2026
2c37b89
feat(cpu-moe): compute the inkling 2 softcapped swiglu on the cpu exe…
ischencheng Nov 20, 2026
96bc7eb
perf(spec): read the dspark full-attention context in chunks
ischencheng Nov 20, 2026
9c8170a
docs(spec): name the dspark tensor groups
ischencheng Nov 20, 2026
55b5668
perf(spec): size the dspark context from host lengths
ischencheng Nov 20, 2026
6faeca7
test(spec): check sampled dspark drafts follow their reported q
ischencheng Nov 20, 2026
9e9dd3a
feat(moe): serve experts of several formats under --moe-strategy cpu
ischencheng Nov 20, 2026
6f7ebe1
test(spec): let the dspark bookkeeping check part only at a near tie
ischencheng Nov 20, 2026
a34eb8d
Merge branch 'feat/inkling2-mini-dspark' into feat/inkling2-mini
ischencheng Nov 20, 2026
1fc1ab3
feat(inkling2_loop): serve the dspark draft for speculative decoding
ischencheng Nov 20, 2026
eacbbab
feat(moe): keep experts of several formats resident
ischencheng Nov 20, 2026
2a5d31a
fix(inkling2_loop): add the vision-only reader and document the model
ischencheng Nov 20, 2026
822b724
Merge branch 'feat/inkling2-mini-hybrid' into feat/inkling2-mini
ischencheng Nov 20, 2026
24bc166
fix(moe): clear the prefill staging after the last call of the declar…
ischencheng Nov 20, 2026
c296586
docs(models): note the memory ratio inkling 2 mini needs on a 16 gb card
ischencheng Nov 20, 2026
660bdda
docs(models): note the window kv inkling 2 mini reserves per request
ischencheng Nov 20, 2026
61ced0b
fix(moe): pick the overlap prefill buffer by the call's place in the …
ischencheng Nov 21, 2026
c88be90
fix(inkling2_loop): refuse quantized kv formats the attention kernel …
ischencheng Nov 21, 2026
6b04117
fix(mm): refuse inkling 2 images past the image token limit
ischencheng Nov 21, 2026
80b6ef2
refactor(models): drop the unused kv_layer_count property
ischencheng Nov 21, 2026
75a0f74
Merge remote-tracking branch 'upstream/main' into feat/inkling2-mini
ischencheng Nov 21, 2026
94d84e7
Merge remote-tracking branch 'upstream/main' into feat/inkling2-mini
ischencheng Nov 26, 2026
908cf43
feat(inkling2_loop): serve the mxfp8 experts on the mxfp8 expert kernels
ischencheng Nov 26, 2026
3314730
test(moe): give the mxfp8 layer its own config in the resident and cp…
ischencheng Nov 26, 2026
3daae39
chore(inkling2_loop): list the mxfp8 expert banks for the prebuilt co…
ischencheng Nov 26, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(inkling2_loop): serve the mxfp8 experts on the mxfp8 expert kernels
  • Loading branch information
ischencheng committed Nov 26, 2026
commit 908cf430813dfee3e2bbbedef59d8e421a956749
2 changes: 1 addition & 1 deletion docs/models.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ unset on PCIe GPUs: wider grids add no bandwidth there.
one window plus one prefill chunk on all 40 sliding layers (about 1.4 GiB at the default
`--max-extend-length 8192`); lower `--max-extend-length` to run many requests at once. The NVFP4
experts (layers 0-19) run W4A16, since the checkpoint declares only per-token activation scales; the
MXFP8 experts (layers 20-23) are dequantized to bf16 at load. Speculative decoding uses the
MXFP8 experts (layers 20-23) run W8A16 from their stored blocks. Speculative decoding uses the
checkpoint's DSpark draft (`mtp.safetensors`). On a 16 GB card start with `--memory-ratio 0.85`: the
prefill activations are not in the memory budget, and at the default 0.9 a 4K-token prompt runs out of
memory in the expert GEMM.
Expand Down
11 changes: 5 additions & 6 deletions python/freetoken/models/inkling2_loop/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,16 @@ def parse_config(hf_config: Any) -> ModelConfig:


_EXPERT_TENSOR = re.compile(r"^(?P<module>.+\.mlp\.experts)\.w(13|2)_weight$")
# hf_quant_config.json compute dtype -> the ModelOpt algo FreeToken serves it as; experts in other formats
# (MXFP8) load through the bf16 path, dequantized by iter_expert_pieces
_SERVED_ALGOS = {"nvfp4": "W4A16_NVFP4"}
# hf_quant_config.json compute dtype -> the ModelOpt algo FreeToken serves it as
_SERVED_ALGOS = {"nvfp4": "W4A16_NVFP4", "mxfp8": "MXFP8"}


def quant_config(model_path: str, hf_config: Any, *, name_map, unquantized):
"""The checkpoint's expert formats as a ModelOpt MIXED_PRECISION config.

The config.json says ``modelopt`` / ``NVFP4`` for the whole model, but only the routed experts of some
layers are NVFP4; ``hf_quant_config.json`` lists the tensors of each format. The activation
quantization it declares for the NVFP4 experts is not applied (W4A16), as in the reference."""
The config.json says ``modelopt`` / ``NVFP4`` for the whole model, but only the routed experts are quantized,
NVFP4 on most layers and MXFP8 on the last ones; ``hf_quant_config.json`` lists the tensors of each format. The
activation quantization it declares for the experts is not applied (W4A16 / W8A16), as in the reference."""
from freetoken.layers.quantization import ModelOptConfig, QuantConfig
from freetoken.utils.hf import optional_hf_file

Expand Down
30 changes: 19 additions & 11 deletions python/freetoken/models/inkling2_loop/weight.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

Routed experts are stacked per layer (``mlp.experts.w13_weight`` [E, 2I, ...] with gate and up rows
interleaved, ``w2_weight`` [E, H, ...]). NVFP4 layers carry fp8 block scales and a per-expert global scale
``w*_weight_amax / w*_weight_outer_scale_denominator``; MXFP8 layers carry e8m0 block scales and are dequantized
to bf16 here, since FreeToken has no MXFP8 expert kernel.
``w*_weight_amax / w*_weight_outer_scale_denominator``; MXFP8 layers carry one e8m0 code per 32 inputs of a row.
Both are served as stored, with gate and up de-interleaved.
"""

from __future__ import annotations
Expand Down Expand Up @@ -183,13 +183,13 @@ def _layer_tensors(f, layer: int) -> dict[str, torch.Tensor]:


def iter_expert_pieces(model_path, config, kind: QuantKind, *, layers: Collection[int] | None = None, parallel=False, workers=8, chunk=8 << 20):
"""``(layer, 0, E, {role: tensor})`` per MoE layer whose experts are served as ``kind``: NVFP4 rows as stored
(gate / up de-interleaved, the per-expert global from amax / denominator), MXFP8 dequantized to bf16 for ``NONE``."""
"""``(layer, 0, E, {role: tensor})`` per MoE layer whose experts are served as ``kind``: NVFP4 and MXFP8 rows as
stored (gate / up de-interleaved, the NVFP4 per-expert global from amax / denominator), dequantized to bf16 for ``NONE``."""
import safetensors

from freetoken.models.loader import iter_weight_files

if kind not in (QuantKind.NVFP4, QuantKind.NONE):
if kind not in (QuantKind.NVFP4, QuantKind.MXFP8, QuantKind.NONE):
return None
want = set(range(config.num_moe_layers)) if layers is None else set(layers)

Expand All @@ -203,8 +203,8 @@ def pieces():
continue
t = _layer_tensors(f, layer)
nvfp4 = "w13_weight_amax" in t
if kind is QuantKind.NVFP4 and not nvfp4:
raise ValueError(f"layer {layer} experts are not NVFP4; the quant config serves them as NVFP4")
if kind is not QuantKind.NONE and (kind is QuantKind.NVFP4) != nvfp4:
raise ValueError(f"layer {layer} experts are {'NVFP4' if nvfp4 else 'not NVFP4'}; the quant config serves them as {kind.name}")
if nvfp4:
g13 = t["w13_weight_amax"].float() / t["w13_weight_outer_scale_denominator"].float()
g2 = t["w2_weight_amax"].float() / t["w2_weight_outer_scale_denominator"].float()
Expand All @@ -225,10 +225,18 @@ def pieces():
else:
if t["w13_weight"].dtype != torch.float8_e4m3fn:
raise ValueError(f"layer {layer} experts are neither NVFP4 nor MXFP8 ({t['w13_weight'].dtype})")
piece = {
"gate_up": deinterleave_rows(dequant_mxfp8(t["w13_weight"], t["w13_weight_scale"])).to(torch.bfloat16),
"down": dequant_mxfp8(t["w2_weight"], t["w2_weight_scale"]).to(torch.bfloat16),
}
if kind is QuantKind.MXFP8:
piece = {
"gate_up": deinterleave_rows(t["w13_weight"]),
"gate_up_scale": deinterleave_rows(t["w13_weight_scale"]),
"down": t["w2_weight"],
"down_scale": t["w2_weight_scale"],
}
else:
piece = {
"gate_up": deinterleave_rows(dequant_mxfp8(t["w13_weight"], t["w13_weight_scale"])).to(torch.bfloat16),
"down": dequant_mxfp8(t["w2_weight"], t["w2_weight_scale"]).to(torch.bfloat16),
}
seen.add(layer)
yield layer, 0, config.num_experts, piece
if missing := sorted(want - seen):
Expand Down
51 changes: 51 additions & 0 deletions tests/models/inkling2_loop/test_weight.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,57 @@ def test_mxfp8_experts_match_reference():
assert torch.equal(dequant_mxfp8(deinterleave_rows(weight), deinterleave_rows(scale)), _hf_interleave(ref))


def test_each_layer_is_served_in_its_stored_format(tmp_path):
"""hf_quant_config.json's groups pick each layer's expert kind, and the reader hands MXFP8 rows over as stored."""
import json

import pytest
import safetensors.torch

from freetoken.layers.quantization import QuantKind
from freetoken.models.inkling2_loop.config import quant_config
from freetoken.models.inkling2_loop.weight import deinterleave_rows, iter_expert_pieces

g = torch.Generator().manual_seed(2)
E, I, H = 2, 32, 64
base = "model.llm.layers.{}.mlp.experts."
nvfp4 = {
"w13_weight": torch.randint(0, 256, (E, 2 * I, H // 2), generator=g, dtype=torch.uint8),
"w13_weight_scale": torch.ones(E, 2 * I, H // 16).to(torch.float8_e4m3fn),
"w13_weight_amax": torch.ones(E), "w13_weight_outer_scale_denominator": torch.ones(1),
"w2_weight": torch.randint(0, 256, (E, H, I // 2), generator=g, dtype=torch.uint8),
"w2_weight_scale": torch.ones(E, H, I // 16).to(torch.float8_e4m3fn),
"w2_weight_amax": torch.ones(E), "w2_weight_outer_scale_denominator": torch.ones(1),
}
mxfp8 = {
"w13_weight": torch.randn(E, 2 * I, H, generator=g).to(torch.float8_e4m3fn),
"w13_weight_scale": torch.randint(110, 130, (E, 2 * I, H // 32), generator=g, dtype=torch.uint8),
"w2_weight": torch.randn(E, H, I, generator=g).to(torch.float8_e4m3fn),
"w2_weight_scale": torch.randint(110, 130, (E, H, I // 32), generator=g, dtype=torch.uint8),
}
tensors = {base.format(0) + k: v for k, v in nvfp4.items()} | {base.format(1) + k: v for k, v in mxfp8.items()}
safetensors.torch.save_file(tensors, str(tmp_path / "model.safetensors"))
groups = [
{"compute_dtype": dtype, "tensors": [base.format(layer) + "w13_weight", base.format(layer) + "w2_weight"]}
for layer, dtype in ((0, "nvfp4"), (1, "mxfp8"))
]
(tmp_path / "hf_quant_config.json").write_text(json.dumps({"model": {"parameter_groups": groups}}))

config = quant_config(str(tmp_path), None, name_map=None, unquantized=())
assert [config.scheme_for_name(f"model.llm.layers.{layer}.mlp.experts").kind for layer in (0, 1)] == [QuantKind.NVFP4, QuantKind.MXFP8]

model = SimpleNamespace(num_moe_layers=2, num_experts=E)
((layer, _, _, piece),) = iter_expert_pieces(str(tmp_path), model, QuantKind.MXFP8, layers=[1])
assert layer == 1 and set(piece) == {"gate_up", "gate_up_scale", "down", "down_scale"}
for role, name in (("gate_up", "w13_weight"), ("gate_up_scale", "w13_weight_scale")):
assert piece[role].dtype == mxfp8[name].dtype
assert torch.equal(piece[role].view(torch.uint8), deinterleave_rows(mxfp8[name]).view(torch.uint8))
assert torch.equal(piece["down"].view(torch.uint8), mxfp8["w2_weight"].view(torch.uint8))
assert torch.equal(piece["down_scale"], mxfp8["w2_weight_scale"])
with pytest.raises(ValueError, match="layer 0 experts are NVFP4; the quant config serves them as MXFP8"):
list(iter_expert_pieces(str(tmp_path), model, QuantKind.MXFP8, layers=[0]))


def test_message_queries_merge():
from freetoken.models.inkling2_loop.weight import merge_message_queries

Expand Down