Skip to content

feat(cuda): the Inkling model with every value on the device - #27

Merged
Defilan merged 9 commits into
mainfrom
agent/i2-cuda-ops
Nov 25, 2026
Merged

Defilan merged 9 commits into
mainfrom
agent/i2-cuda-ops

Conversation

@defilan-agent

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

Copy link
Copy Markdown
Contributor

Milestone I2. The real Inkling 2 Mini runs with every value of its forward pass on the GPU and matches the reference. Nothing leaves the device between a token going in and its scores coming out.

What is in it

All in dluth-cuda (needs a GPU; not built by CI), plus two exports in dluth-model.

  • kernels/attention.cu, attention.rs: the attention sublayer other than its matrix products. Per-head scaling, clipping and the late factor; per-head value normalization; the two short convolutions with a ring of recent tokens; attention scores with the sink and the relative-position term over a history kept on the device; softmax; weighted values; the output gate.
  • kernels/small_ops.cu, small.rs: add, weighted normalization, a whole blend across depth in one launch, normalization of several vectors in one launch, the merging gate, the soft cap.
  • ops.rs: CudaOps implements the model crate's Ops trait. Values are device vectors from a pool; weights and parameters are copied to the device on first use. TimedOps wraps it with a stopwatch per kind of operation.
  • floats.rs: a run of floats inside a device buffer, with the extent check every wrapper uses.
  • examples/generate.rs gains the on-device mode (now the default) and a timed mode; examples/matmat_alloc.rs shows that the matrix product's speed does not depend on how weights were laid out or what ran before it.
  • Learning note 21.

A history on the device: the token at position p is in row p modulo the number of rows. A layer with a window has exactly as many rows as its window, so a row is overwritten when its token leaves the window and nothing is ever moved.

One rule keeps it safe, stated at the top of ops.rs: kernels run in the order they are started, so a buffer may go to a later kernel while an earlier one still has to read it, but the host only writes into a buffer right after waiting for the device, or into one no kernel has been given.

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.00005, bar 0.00015
Tiny model against its reference recording 72 of 72 tokens; largest 0.0036, bar 0.0044
Tiny model against the CPU model, two sequences on one model largest 0.0012, bar 0.0044

Speed, greedy, 90 generated tokens: 24.5 tokens per second, 40.9 ms per token, 50 waits for the device per token. (Products on the GPU with the rest on the CPU, the previous step: 19.7.)

The large weight reads are as fast as the memory allows (the vocabulary's scores: 3.8 ms for 0.92 GB). Reading all of a token's weights needs about 22 ms. The rest is small kernels: a token starts about 3,000, they run one after another, and one that does real work takes 5 to 10 microseconds. The note records that this was underestimated earlier, and the first on-device version was slower than the step before it for that reason.

How it was checked

  • GB10, CUDA 13.0: cargo test --release -p dluth-cuda with the real checkpoint, 50 tests. Each kernel is compared with the CPU function it replaces, on inputs that reach its corners: heads over and under the clip, the first tokens of a sequence, a history that wraps around its window three times, distances beyond the reach of the relative-position table. Format check and lint clean across the workspace.
  • A test counts the device's live buffers before a model is made and after it is dropped: every buffer comes back.
  • Planted bugs 75 to 91 (# needs: gpu), each applied in turn on the GB10: its test fails.
  • Mac: 211 tests, format check and lint clean (the default members are untouched apart from two exports).

Review

The qwen reviewer asked for changes, and its main finding was a real defect: the six device buffers each layer pass owns were never freed. A dropped pass now hands them to a list its operations free, and the buffer-count test covers it.

Also made: pool sizes are powers of two (attention's scratch grew with every position and left one buffer per position behind); a run inside a device vector is checked against the vector, not its larger buffer; a position beyond a pass's room is refused before any write; stacked matrices and convolution taps are checked for their lengths; the old run of ones is no longer parked in the parameter table; three comments corrected.

Its other remark, that a single thread per head is slow, was measured (25 microseconds for 24 heads of 128) and fixed: the per-head kernels use a block of threads per head.

Checked and unchanged: host copies are synchronous, so reusing the host staging bytes is safe; the late factor's wiring is covered by the tiny model, whose scaling starts at position 16, with a planted bug.

Declined: dropping the window condition from the capacity check as written in the review. A windowed layer's position passes its row count in normal use; the check is now against the sequence length the pass was made for.

…alization, blend scores, merging gate, soft cap
…evice; golden checks on the tiny and the real model
…e of a message take fewer launches; a stopwatch for the device operations
…ue normalization; a program that checks the matrix product is unaffected by how weights were laid out or what ran before
…izes are powers of two; a position beyond the pass's room is refused before any write; review fixes
@Defilan
Defilan merged commit b88a966 into main Nov 25, 2026
2 checks passed
@Defilan
Defilan deleted the agent/i2-cuda-ops branch November 25, 2026 07:42
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