Skip to content

Inkling 2: the layer visit in about half the kernels, bit for bit mlx-lm's - #20

Open
srcterm wants to merge 4 commits into
mainfrom
feat/fusion
Open

srcterm wants to merge 4 commits into
mainfrom
feat/fusion

Conversation

@srcterm

@srcterm srcterm commented Nov 25, 2026

Copy link
Copy Markdown
Owner

A decode step of mlx-lm's inkling2 queues about 150 GPU kernels a layer visit, most of them a cast, an add or a mask over one vector: Inkling 2 Mini queues 7,097 a token. The fused visit computes the same values in 3,955. It applies to visits of up to 16 rows, a decode step or a step's drafts checked together; prompt chunks still run mlx-lm's code.

What changes

  • inkling2_step (new): fuse(model) gives mlx-lm's inkling2 layers (Inkling 2 Mini's looped stack and Inkling-2-Small's) a visit that keeps every operation and its order, element by element:
    • a value cast twice is cast once, and constants are cast once, at load;
    • runs of elementwise ops are one compiled kernel each (mx.compile, as mlx-lm's own clips are);
    • what a token's visits share is computed once a token: the distances from the query to a window's keys, with what they hide, and a global layer's query scale;
    • a sliding layer's three message gates share the norm of its input, and a convolution state is a view of the window that made it.
  • Two checks at load: each compiled group is compared with its ops run one by one, and a visit of each kind of layer with mlx-lm's own, every output and cache entry, bit for bit. If a compiled group differs, the model stays as it was and a line on stderr says why. The CPU's compiler contracts a multiply and an add, so the fused visit stays off on the CPU.
  • A kind of layer is its class, its place in its block, whether it takes the loop's messages, and the dtypes of its parameters. mlx-lm casts a convolution's output to its input's dtype, the projection's, while the fused visit casts to the stream's; the two agree only while the projections give the stream's dtype. A kind that differs keeps mlx-lm's visit and stderr names its layers; the others take the fused one.
  • At run time a visit takes the fused path only with the stream in the dtype the check used, the embedding's. Input embeddings of another dtype run mlx-lm's code.
  • Values made from parameters (a norm weight's casts, the relative logits table's, the static convolution weights, the loop's message queries) are made again when a parameter is replaced, as model.load_weights does. fuse() on a fused model checks it anew and makes them again, which loading weights of other dtypes, or updating a parameter in place (an indexed assignment keeps its identity), needs.
  • A draft head's prefill records each layer visit once also where some layers keep mlx-lm's visit: the recorders wrap a fused layer's class and its parent, and a fused layer falls back to the parent for chunks of more than 16 rows.
  • mlx-lm: it needs the mlx-lm with the layer it mirrors, #2086, which the Mini needs anyway. With #2061's inkling2 it stays off.
  • integrate: load_streaming_model and install_streaming apply it; STREAMLX_FUSED=0 turns it off.
  • validate: it says how many layers take the fused visit.

Checks

  • Inkling 2 Mini oQ4e, every expert held, on an M4 MacBook Air:
    • In 48 decode steps, a 4-row step and 4 more, the logits and every cache entry are identical.
    • Plain decoding goes from 17.68 to 18.16 tok/s (+2.8%) synchronous, and from 19.59 to 20.28 tok/s (+3.5%) with run-ahead.
    • On this Mac the step is about 47 of its 49 ms weight reads. The host's work a token falls by about 30%, so the gain should be larger where memory is faster; that was not measured.
  • streamlx validate passes through it on the Mini, held (128 slots) and streamed (16 slots, 40% misses): all 24 layers fused, 64 of 64 tokens, prefill logits bitwise.
  • Inkling-2-Small, streamed from the external SSD:
    • All 48 layers take the fused visit. No resident reference fits in memory, so validate cannot run; instead mlx-lm's visit and the fused one ran on two copies of one cache, step by step.
    • In 16 decode steps, a 4-row step and 4 more, the logits and every cache entry are identical.
    • Its decoding is bound by the drive (2.0 to 2.4 GB/s over a request), so its speed does not change.
  • Real pi on the Mini through streamlx serve with its defaults (write fib.py, run it):
    • The same conversation as main, every request the same.
    • Decode is 22.3, 23.2, 33.7 and 20.7 tok/s, against 22.5, 23.6, 32.4 and 20.6 on main: the head's drafted steps are bound by their expert rows.
  • Real pi on Inkling-2-Small through streamlx serve with its defaults, with the fused visit (#2086) and without it (#2061): the same conversation, request for request, at the same speed (the drive bounds it).
  • Tests: test_inkling2_step covers toy checkpoints of both layer kinds, looped and not:
    • a layer whose value projection keeps its scales in float32 keeps mlx-lm's visit while the others fuse, bit for bit;
    • input embeddings in float16, and the steps after them, bit for bit;
    • weights loaded after fuse(), and a parameter updated in place then fuse() again, bit for bit;
    • a draft head's rows over a 17-token prompt with one layer on mlx-lm's visit, looped and not, equal to stock's;
    • the module imports with an mlx-lm that has no inkling2.
    • The suite: 62 pass with mlx-lm #2086; 51 pass and 11 are skipped with #2061; 48 pass and 14 are skipped on the CPU; 47 pass and 15 are skipped on the CPU with an mlx-lm that has no inkling2.

…half the kernels, bit for bit mlx-lm's (a value cast twice is cast once, constants are cast at load, runs of elementwise ops are one compiled kernel each, what a token's visits share is computed once a token; applied only after each compiled group matches its ops run one by one and a visit of each layer kind matches mlx-lm's, every output and cache entry; needs mlx-lm #2086's layer, stays off on the CPU; STREAMLX_FUSED=0 turns it off). Inkling 2 Mini oQ4e: 7,097 -> 3,955 kernels a token, plain decoding +2.8% synchronous and +3.5% with run-ahead; tests
… checked by kind and by their parameters' dtypes, since a projection kept in float32 gave another token, and a kind that differs keeps mlx-lm's visit; a stream in another dtype than the check's, as float16 input embeddings, runs mlx-lm's code); what it makes of parameters is made again when they are replaced; fuse() checks a fused model anew; the test module imports without an mlx-lm that has inkling2; tests
…cords each layer visit once where some layers keep mlx-lm's visit (a fused layer falling back reached its class's parent, which the recorders also wrapped: a 17-token prompt raised, and the looped head's rows were wrong); what the fused visit makes of parameters takes them from its caller, so no reference cycle holds a model's modules; an update in place needs fuse() again (documented); tests
…x-lm's own _conv_input (mlx-lm #2086's head keeps the convolution state in the stream's dtype since 11-22 where the fused visit cast it to float32, so fuse() found the values differ and kept mlx-lm's visit in every layer; now either is followed); round32, unused, removed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant