K on the GPU

KGPU compiles a small dialect of K, the array language, into WGSL compute shaders that match a CPU interpreter bit for bit. raea composites its video frames with it.

Measured 14 October 2026

K is an array language: every verb works on whole arrays, expressions read right to left, and programs are a few dense lines. That shape fits a GPU, where one small program runs once for every element of the output. KGPU takes a kernel written in a K dialect and fuses it into a single WGSL compute shader, with no temporary arrays in memory between verbs.

The same kernel text also runs on the CPU through an interpreter, and the two must agree bit for bit. Everything is 32-bit integers with wrapping arithmetic, truncating division and fixed loop orders, so there is nothing a GPU is free to reorder or round differently.

The dialect

WriteMeans
name:expra binding; an output is a binding the caller names
+ - * %add, subtract, multiply, divide (truncating)
& |min and max, as in K
ithis element's index
word[b;k] byte[b;k]the k-th word or byte of input buffer b
pop xor and or shl shrbit operations
sum[k;n;expr]K's over: a sum across k = 0..n−1, in a fixed order
Evaluation is right to left with no precedence: a*b+c is a*(b+c). Lines starting with / are comments.

What raea runs on it

There are 21 kernels in raea today. composite.k draws every rendered frame: the 3D scene as the background, then up to four video clips straight from their NV12 planes by depth, with bilinear sampling and BT.709 colour in 8.8 fixed point, then the interface overlay. nv12.k converts finished frames back for the encoder. The look_* and cv_* kernels build the perception columns and hypervectors that raea searches.

kgpu/kernels/composite.k, the start of the frame compositor
/ The frame on the GPU: the camera's target T is the background (clear colour, 3D), then up to four clips from
/ their NV12 planes (P0..P3) by z, then the Blitz overlay O (premultiplied RGBA).
x:i!w
y:i%w
bg:tex[T;x;y]
r:and[bg;255]
g:and[shr[bg;8];255]
b:and[shr[bg;16];255]

Because each kernel matches its interpreter exactly, a frame composited on a Mac's GPU, an iPhone's GPU or a CPU in a test is the same frame. The perception projection and gram kernels check the same way, with relative errors of 4.1e-4 and 1.6e-5 against their float references.

sources in the raea repository

  • kgpu/src/lib.rs: the dialect
  • kgpu/kernels/
  • goal.feature: CV's columns

What would you make first?

Tell void, and leave your email. Early access opens in small batches, and we'll write when yours is ready.

Early access opens in small batches.