@enigma.kernel for direct compute kernels and @enigma.jit for host-side functions that perform layout algebra and launch kernels.
@enigma.kernel
@enigma.kernel defines a GPU compute kernel. The decorated function is traced at compile time: the Python body runs once with symbolic IRValue placeholders, producing an IR graph that is then lowered to Metal.
Rules for kernel bodies
- No native Python control flow — use
enigma.for_range,enigma.if_,enigma.while_instead. - No Python data types as values — all computation goes through
IRValueobjects. - No early returns — the tracer records the full body unconditionally.
- Types are annotations, not runtime checks —
A: enigma.f32declares the buffer’s element type.
Kernel parameters
Each parameter is one of:enigma.f32,enigma.f16,enigma.bf16,enigma.i32,enigma.u32, etc. — typed buffer (pointer in Metal)enigma.Scalar(dtype)— scalar constant, lowered as a 1-element buffer
Compiling a kernel
compiled is a CompiledKernel. Dispatch it with MetalRuntime.execute(...):
Repeated dispatch
For hot loops, pre-allocate buffers once:@enigma.jit
@enigma.jit is a host-side function that runs at compile time. Use it when you need to:
- Perform layout algebra (tiling, thread-value mapping) before generating kernels
- Launch multiple kernels in sequence
- Accept
Tensorobjects and compute block/thread partitioning
JIT function pattern
Compiling a JIT function
Control flow
Enigma provides tracing-compatible control flow wrappers:enigma.for_range
init=[...]:
