Skip to content

feat(cuda): the Inkling model's matrix products on the GPU - #25

Merged
Defilan merged 8 commits into
mainfrom
agent/i2-t9c-expert-stage
Nov 25, 2026
Merged

Defilan merged 8 commits into
mainfrom
agent/i2-t9c-expert-stage

Conversation

@defilan-agent

@defilan-agent defilan-agent Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Milestone I2. The real Inkling 2 Mini now runs with its matrix products on the GPU and matches the reference.

What is in it

dluth-model (built and tested by CI)

  • Products::experts becomes Products::expert_layer: one request for a layer's chosen experts, covering both matrix products of each, the capped activation between them and the weighted sum. The CPU implementation keeps the same arithmetic in the same order, so CPU results are unchanged.

dluth-cuda (needs a GPU; not built by CI)

  • inkling::CudaProducts implements Products on a CUDA device. A BF16 product is a round trip (copy the vector in, launch, wait, copy the result out). A layer's experts stay on the device from input to weighted sum: one copy in, four launches, one copy out. Weights are copied to the device the first time a matrix is used.
  • kernels/expert_ops.cu: capped_gate_pairs (the activation) and weighted_sum, with a wrapper that refuses anything outside its buffers.
  • examples/generate.rs: generates text and reports where a token's time goes.
  • Learning note 20.

Result on a GB10

Check Result
Real Inkling 2 Mini against the 32-bit reference recording 96 of 96 tokens; largest log-probability difference 0.00006, bar 0.00015
Tiny model against its reference recording 72 of 72 tokens; largest 0.0033, bar 0.0044
Tiny model, GPU against CPU on 40 tokens the recordings do not cover largest 0.0032, bar 0.0044

Speed, greedy, 90 generated tokens: 19.7 tokens per second (the CPU model: 2.6 on an Apple M5 Max).

Per generated token Time
BF16 products, 689 round trips 27.0 ms
Experts, 48 layer passes 10.5 ms
Everything else, on the CPU 13.2 ms

The products alone would take about 22 ms at the rate the kernels read weights. The rest is the round trips and the CPU; removing them is the next work.

How it was checked

  • Mac: 211 tests with both real models, CPU golden figures unchanged to the last digit; format check and lint clean; tools/mutants.sh cpu 52 killed, 0 not killed (two forward-pass patches regenerated after the expert code moved).
  • GB10, CUDA 13.0: cargo test --release -p dluth-cuda with the real checkpoint, 33 tests; format check and lint clean across the workspace.
  • Planted bugs 68 to 70 and 72 to 74 (# needs: gpu), each applied in turn on the GB10: its test fails.

Review

The qwen reviewer asked for changes. Made:

  • The note gave the tiny model's bar in a sentence that read as the real model's. It now states each model's figure against its own bar.
  • The CPU expert layer checks that the first matrix has twice the inner width in rows, as the GPU side already did.
  • The GPU side no longer allocates a staging buffer per product.
  • weighted_sum refuses one buffer used for two arguments; a test's sanity check now looks at the device's results.

Checked and unchanged: the new kernel file is built (the build script compiles every file in kernels/); a job's vector position is in floats, which is what the code passes; no other implementation of the trait exists.

Declined: counting a refused expert stage in the timing counters. A refusal ends the run with an error; the counters are for runs that finish.

…e Products trait; golden checks on the tiny and the real model
…he device from input to weighted sum; the GPU-against-CPU check uses the measured bar
… the GPU products and the expert-step kernels
…on per product on the GPU side; a refusal for aliased buffers; note states each model's bar; review fixes
@Defilan
Defilan merged commit eb6ed09 into main Nov 25, 2026
2 checks passed
@Defilan
Defilan deleted the agent/i2-t9c-expert-stage branch November 25, 2026 05:48
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