Skip to content

feat(cuda): several positions per sweep of the weights on the device - #30

Merged
Defilan merged 3 commits into
mainfrom
agent/i2-cuda-batch
Nov 25, 2026
Merged

Defilan merged 3 commits into
mainfrom
agent/i2-cuda-batch

Conversation

@defilan-agent

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

Copy link
Copy Markdown
Contributor

Milestone I2, second step of the batch dimension. Every kernel of the device model takes a batch of up to 16 consecutive positions in one launch, so a sweep through the model over 16 positions starts about as many kernels, and waits for the device as often, as a sweep over one.

What is in it

  • kernels/attention.cu, small_ops.cu, expert_ops.cu: every kernel gains the position within the batch. New: store_rows (a batch's keys or values into their rows of a history, wrapping), ring_store (a batch's last tokens into a convolution's memory), vec_copy.
  • Attention: all positions' keys and values are stored first; each position is then scored against its own view, which ends at itself. A windowed history has window + 15 rows, so that the last position of a batch does not take a row the first can still see.
  • Short convolutions: for a batch the kernel takes an earlier token from the batch itself or from the memory and writes nothing (one position's write would race another's read); ring_store runs afterwards. A batch of one behaves as before.
  • The late factor of global layers is computed in the kernel from each position.
  • Experts: the router's scores of every position come back in one copy; an expert chosen by several positions is one job that reads its matrix once.
  • ops.rs: DeviceVec is a batch; the stacked products are one product for one position and one per matrix for a batch.
  • examples/generate.rs reads the text again in batches of 1 to 16 after generating, and prints what a batch costs.
  • Learning note 22.

Measured (Inkling 2 Mini on the GB10)

Reading 160 tokens without scoring them:

Positions per sweep ms per sweep tokens per second
1 36.4 27.5
2 44.2 45.2
4 58.0 69.0
8 82.2 97.4
16 133.3 120.0

Generation one token at a time: 24.5 tokens per second, against 24.6 to 24.7 for the code before on the same prompt (three runs each).

How it was checked

  • Each batched kernel against the CPU function position by position: batches of one, several and 16; batches that end on a window, straddle it, are longer than it and wrap around the end of a history; convolution memories and histories pre-filled with other values, which must never be read.
  • Tiny model on the device in batches against the CPU model one token at a time, at every position of two sequences, at the bar of the recordings (largest difference 0.0012, bar 0.0044). The waits for the device per batch are asserted: two plus one per layer pass.
  • Real model: each recorded prompt fed in batches of 16 gives the same log-probabilities for the five most likely tokens as one token at a time; the recordings still match 96 of 96 (largest difference 0.00005).
  • GB10: format check, clippy on the workspace, 56 dluth-cuda tests with the real checkpoint. Mac: format check, clippy, 216 tests.
  • Planted bugs: 13 new for batches (98 to 110), 17 regenerated after the rewrite. All 55 that need the GPU were applied one at a time on the GB10; each is caught by its test.

Review

The qwen reviewer asked for changes. It traced the views, the scratch stride, the sink's place, row wrapping, the convolution memories, the expert grouping, the launch order and the argument lists of all 13 changed kernels, and found them correct. Its must-fix was a doc line: attend gave the sizes of the relative vectors and sinks per head where they are per query and head.

Made: that line; taps that overlap a convolution's memory are refused (with a test); each position's number of chosen experts is compared with the configured number.

Declined: "the number of vectors may not be a multiple of the heads" in head_scale_clip. The check that the floats are a multiple of heads * dim already makes it one.

…the ring are refused; each position's expert count is checked; review fixes
@Defilan
Defilan merged commit a2d560d into main Nov 25, 2026
2 checks passed
@Defilan
Defilan deleted the agent/i2-cuda-batch branch November 25, 2026 09:45
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