Write GPU kernels
in plain Kotlin.
Add @Kernel to a Kotlin function. At compile time Kernelin translates it into a WGSL compute shader that runs on any GPU through WebGPU, and the same function still runs on the CPU for your unit tests.
id("io.github.rajumark.kernelin") version "0.3.0"
on an Apple M5 (waves)
by hand
CPU for unit tests
by the naga validator in tests
Kotlin in. WGSL out.
Nothing to hand-write.
A K2 compiler plugin reads your function's IR right after type checking and emits a compute shader. Arrays become storage buffers, Int and Float parameters move into a uniform, and globalId() becomes the GPU thread index. Hover a Kotlin line to see what it became.
Same results (100% match). 118×
Test it on the CPU.
Ship it to the GPU.
A kernel is still a normal Kotlin function. Run it with KernelinCpu in a plain unit test, step through it in the debugger, then send the very same code to the GPU. No second language, no copy that drifts out of sync.
- KernelinCpu.dispatch runs every thread on one core, great for tests
- dispatchParallel spreads it across all JVM cores
- WebGpu.create() runs it on the real GPU from Kotlin/Wasm
1fun fact(n: Int): Int = if (n <= 1) 1 else n * fact(n - 1)23@Kernel4fun factorials(out: IntArray) {5 val i = globalId()6 out[i] = fact(i)7}
fact as a for loop → builds, runs on CPU and GPUErrors at compile time,
not on the GPU.
If your kernel uses something a GPU can't run (strings, Double, Long, lambdas, recursion, exceptions, nullable types) you get a normal Kotlin compiler error with file, line and a hint. In the IDE and in CI, before anything ships.
- Clear messages that say what to write instead
- Every shader validated by naga, and kernels proven equal to Kotlin on a real GPU in CI
- The manifest tells hosts exactly how to bind buffers
From .kt to silicon
in one build step.
Kernelin plugs into the Kotlin compiler as an IR generation extension. It runs before any platform lowering, so loops, &&, ++ and += still have their original shape and map cleanly onto WGSL.
@Kernel fun
Plain Kotlin in commonMain or jvm
Resolve + check
Types and calls are resolved by the K2 compiler
IR → WGSL
Walks the IR tree and writes a compute shader
.wgsl + manifest
Shader plus a JSON map of bindings
kernelSource(::k)
The call is replaced by the shader at compile time
WebGPU
Dispatched in workgroups of 64 threads
Real numbers,
honestly measured.
GPU kernel time versus the same Kotlin code on all 10 CPU cores of an Apple M5. Heavy math wins big. Light kernels win only when data stays on the GPU; copying it there and back costs more than the work.
Not a demo.
Workloads people pay for.
Three open-source apps built with Kernelin 0.1.0 from Maven Central. Each runs the same Kotlin on the GPU and the CPU, checks its answers against exact formulas or ground truth, and shows measured speed.
Risk Engine
Monte Carlo pricing and greeks of an options book (Asian, barrier, lookback): 1M paths × 252 days × 4 scenarios in 46 ms, 53× faster than 10 JVM cores. Checked against Black–Scholes on every run.
Open app → IMAGEPhoto Studio
Live photo editing with edge-preserving denoise: 3.8 MP × 169 taps in 48 ms, +11.3 dB PSNR on a test chart with real sensor noise, 119× faster than in-browser CPU.
Open app → VIDEOMotion Guard
Traffic camera analytics: block-matching motion vectors, 576 M comparisons per 720p frame in 19 ms (live 30 fps, 53× in-browser CPU), vehicle speed within 6%, precision 0.995 vs ground truth.
Open app →Everything you need,
for humans and for agents.
From your first kernel to the binding layout of the generated shader. The whole guide is also published as one Markdown file, so an AI coding agent can read it and write correct kernels on the first try.
Quick start
Add the Gradle plugin, write a kernel, run it on the CPU and print its WGSL. Five minutes.
Read → 02Writing kernels
Types, loops, helpers and kotlin.math: exactly which Kotlin the GPU accepts, and why.
Read → 03Running on the GPU
Use the Kotlin/Wasm WebGPU host, pass scalars and buffers, and read results back.
Read → 04API reference
@Kernel, globalId, kernelSource, KernelinCpu, WebGpu, GpuKernel, GpuData, KernelManifest.
Read → 05Compiler internals
How IR becomes WGSL, the binding layout, the manifest format and the error pipeline.
Read → 06 · AI READYFor AI agents
A strict rule sheet, copy-paste templates and machine-readable docs for coding agents.
llms.txtdocs.mdAGENTS.mdRead →Put your Kotlin on the GPU today
One line in your Gradle build. Kernelin compiles your @Kernel functions into WebGPU shaders and ships them inside your app, on every platform.