Repository navigation
feat(cuda): the Inkling model with every value on the device - #27
Merged
Merged
Conversation
…ested against its CPU function
…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
…he 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 indluth-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:CudaOpsimplements the model crate'sOpstrait. Values are device vectors from a pool; weights and parameters are copied to the device on first use.TimedOpswraps 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.rsgains the on-device mode (now the default) and a timed mode;examples/matmat_alloc.rsshows that the matrix product's speed does not depend on how weights were laid out or what ran before it.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
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
cargo test --release -p dluth-cudawith 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.# needs: gpu), each applied in turn on the GB10: its test fails.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.