Skip to content

feat: Inkling-2 followups for 32217 - #32268

Draft
danielhanchen wants to merge 1 commit into
ggml-org:pwilkin/inkling2from
danielhanchen:inkling2-followups
Draft

danielhanchen wants to merge 1 commit into
ggml-org:pwilkin/inkling2from
danielhanchen:inkling2-followups

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Follow-ups for Inkling 2 on top of #32217:

  • Per-expert scales for fused gate_up / down experts. NVFP4 expert GGUFs need these: they keep ffn_gate_up_exps.scale / ffn_down_exps.scale because the scale is too small to fold into the FP8 block scales. Without this they are refused at load, and ffn_down_exps.scale was loaded but never applied.
  • Embedding norm applied to token embeddings only, not to image embeddings (image captions were wrong)
  • Inkling 2 opts out of mixed token + embedding batches and -sm tensor, as Inkling 1 does (mixed-batch NMSE 0.35; -sm tensor asserted at split setup)
  • mtmd: each image in its own <|content_image|> block, as the template renders it
  • mtmd: reproduce the reference bf16 patch preprocessing (image embeddings were 2.9% rel L2 off, now 6.8e-4); test_image_preprocessor_inkling2 updated to match
  • Chat template + parser: thinking / text blocks, tool calls (<|message_model|>NAME<|content_invoke_tool_json|>{...}), parallel calls, turns without the opening <|message_model|> read as text (was HTTP 500), no <|end_message|> leak. Inkling 1 templates (with <|content_audio_input|>) are not routed to it
  • jinja: tojson(sort_keys=True), used by the official template (rendering failed with HTTP 400)

Additional information

Tested: CPU and CUDA builds; test-backend-ops, test-chat, test-mtmd-impl and test-llama-archs -a inkling2 pass. test-chat / test-jinja pass with Inkling 2 cases (not included here to keep the diff small). GGUFs from IQ1_S to BF16 converted to this layout give the same KLD as before, and the vision caption is correct.

Requirements

  • I have read and agree with the contributing guidelines Yes
  • AI usage disclosure: Yes - local models + Claude, Codex

@github-actions github-actions Bot added model Model specific mtmd Related to multimodal functionality (video/image/audio) jinja parser Issues related to the jinja parser labels Nov 14, 2026
@CISC

CISC commented Nov 14, 2026

Copy link
Copy Markdown
Member
  • jinja: tojson(sort_keys=True), used by the official template (rendering failed with HTTP 400)

Make this a separate PR please (well, to be honest, all of them should probably be).

- inkling2: apply per-expert gate_up / down scales when present (NVFP4 expert files)
- inkling2: apply the embedding norm to token embeddings only, not image embeddings
- inkling2: opt out of mixed token + embedding batches and -sm tensor, as Inkling 1 does
- mtmd: put each image in its own <|content_image|> block
- mtmd: reproduce the reference bf16 patch preprocessing (test-mtmd-impl updated)
- chat: Inkling 2 template and parser (thinking, text, tool calls, parallel calls, turns without the opening header); Inkling 1 templates are not routed to it
- jinja: support tojson(sort_keys=True)
Comment thread tools/mtmd/mtmd-image.cpp
Comment on lines +2194 to +2207
// the reference CLIP-normalises to bf16, then the encoder maps back to [-1, 1] in bf16; a single affine step cannot match it
constexpr float proc_mean[3] = { 0.48145466f, 0.4578275f, 0.40821073f };
constexpr float proc_std[3] = { 0.26862954f, 0.2613026f, 0.2757771f };
constexpr float enc_mean[3] = { 0.48145466f, 0.4578275f, 0.40821073f };
constexpr float enc_std[3] = { 0.26862954f, 0.26130258f, 0.27577711f };
const auto bf16_round = [](float v) { return ggml_bf16_to_fp32(ggml_fp32_to_bf16(v)); };
for (int c = 0; c < 3; ++c) {
mean[c] = hparams.image_mean[c] * 255.0f;
std[c] = hparams.image_std[c] * 255.0f;
const float m = bf16_round(enc_mean[c]), sd = bf16_round(enc_std[c]);
for (size_t i = c; i < pixels.size(); i += 3) {
const float x = bf16_round((pixels[i] / 255.0f - proc_mean[c]) / proc_std[c]);
pixels[i] = bf16_round(2.0f * (x * sd + m) - 1.0f);
}
}
padded.normalize(mean, std);
padded.cpy_buf(pixels);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have my agent reviewed this and confirmed that this is wrong, please revert changes here

Reasons:

  • It is the identity in exact arithmetic. The two steps undo each other, so the output equals (v - 127.5) / 127.5 plus rounding error and nothing else.
  • Size of the error: up to 1.07 gray levels per pixel (0.67 and 0.51 on the other two channels). In the blue channel two of the 256 pixel values map to the same output. A single bf16 cast of the plain formula would be at most 0.25 levels. I computed this in numpy with round-to-nearest-even, like ggml_fp32_to_bf16.
  • Constants: the CLIP mean/std are hardcoded in the preprocessor and the image_mean/image_std from the GGUF are ignored. Those CLIP values are in Inkling 1's processor_config.json; Inkling 2's has 0.5/0.5.
  • Transformers does not do this. Inkling2ImageProcessor._preprocess does one float32 rescale_and_normalize with the config mean/std, and Inkling2VisionEmbeddings.forward applies the patch embedding straight to pixel_values with no un-normalize step.
  • The PR does not name its reference. The description only says "the reference bf16 patch preprocessing".
  • Its own test admits the drift. The updated test_image_preprocessor_inkling2 adds a tolerance of 1/64 against the exact value, which is about 2 gray levels.

Comment thread tests/test-mtmd-impl.cpp
Comment on lines +57 to +66
// mapped back to [-1, 1] in bf16 as the reference encoder does, so (v - 127.5) / 127.5 up to bf16 rounding
static float inkling2_pixel(float v, int c) {
static const float proc_mean[3] = { 0.48145466f, 0.4578275f, 0.40821073f };
static const float proc_std[3] = { 0.26862954f, 0.2613026f, 0.2757771f };
static const float enc_mean[3] = { 0.48145466f, 0.4578275f, 0.40821073f };
static const float enc_std[3] = { 0.26862954f, 0.26130258f, 0.27577711f };
const auto bf16 = [](float f) { return ggml_bf16_to_fp32(ggml_fp32_to_bf16(f)); };
const float x = bf16((v / 255.0f - proc_mean[c]) / proc_std[c]);
return bf16(2.0f * (x * bf16(enc_std[c]) + bf16(enc_mean[c])) - 1.0f);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, remove it

jesdga95 added a commit to jesdga95/basalt that referenced this pull request Nov 22, 2026
…ised and rounded to bf16, then mapped to [-1, 1] and rounded again; one affine step from the byte left the rows 1.2 to 2.4% (rel L2) from that form

- The class-token tower's patch rows (CUDA patch_rows_k and the CPU patchify_padded, which only the Inkling 2 encoder
  uses) take the processor's CLIP mean and std and the encoder's own, with ggml-org/llama.cpp#32268's two roundings;
  the device's multiply-adds are not contracted, so its rows equal the host's bit for bit (cls_encoder_parity).
- The rows of five pictures against the one-step rows: 57x23 1.2e-2, 640x480 1.9e-2, 512x512 with alpha 1.5e-2,
  1280x720 2.4e-2, 4096x4096 2.2e-2 (lowest row cosine 0.990). CUDA against the CPU encoder stays as before
  (out 5.3e-3 to 1.1e-2, inkling2_vision_parity --cpu).
- transformers' Inkling2ImageProcessor (5.24) normalises once in fp32; the release's own pipeline, which llama.cpp
  matched to 6.8e-4, rounds twice.
FahadAlAlawi pushed a commit to FahadAlAlawi/basalt-RTX40 that referenced this pull request Nov 25, 2026
…ised and rounded to bf16, then mapped to [-1, 1] and rounded again; one affine step from the byte left the rows 1.2 to 2.4% (rel L2) from that form

- The class-token tower's patch rows (CUDA patch_rows_k and the CPU patchify_padded, which only the Inkling 2 encoder
  uses) take the processor's CLIP mean and std and the encoder's own, with ggml-org/llama.cpp#32268's two roundings;
  the device's multiply-adds are not contracted, so its rows equal the host's bit for bit (cls_encoder_parity).
- The rows of five pictures against the one-step rows: 57x23 1.2e-2, 640x480 1.9e-2, 512x512 with alpha 1.5e-2,
  1280x720 2.4e-2, 4096x4096 2.2e-2 (lowest row cosine 0.990). CUDA against the CPU encoder stays as before
  (out 5.3e-3 to 1.1e-2, inkling2_vision_parity --cpu).
- transformers' Inkling2ImageProcessor (5.24) normalises once in fp32; the release's own pipeline, which llama.cpp
  matched to 6.8e-4, rounds twice.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jinja parser Issues related to the jinja parser model Model specific mtmd Related to multimodal functionality (video/image/audio) testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants