Writing kernels

A kernel implements a model computation in C. It describes that computation in RDL so the compiler can match it, fill parameters and arrange execution.

A basic kernel can support only contiguous tensors, use no scratch, refuse input/output overlap and execute on one thread. Add more layout or threading support when it benefits the workload.

One file, four exports

Keep each kernel in one .c file. Include common.h from Redwood and any needed system/SIMD headers. Keep helpers static in the same file.

Export exactly four functions using REDWOOD_KERNEL:

FunctionJob
<name>Execute the computation
<name>_rdlReturn Python defining its pattern and parameter bind function
<name>_parametersDescribe the C parameter struct and memory requirements
<name>_capabilitiesDeclare the ABI version and required device/software features

The complete add example shows these working together. Metadata covers binding and schemas; memory and threading covers execution ownership.

Execution signature

REDWOOD_KERNEL RedwoodStatus redwood_scalar_example(
    void *const *outputs,
    const void *const *inputs,
    void *scratch,
    const void *parameters,
    const RedwoodKernelContext *context
);

Inputs and outputs are pointer arrays in the declared order. Parameters tell this implementation how to interpret them. Cache storage also comes through these bindings.

The context supplies cancellation and a backend-owned thread pool. Use supplied memory, avoid heap allocations and return only after workers stop accessing invocation data.

Return REDWOOD_OK, REDWOOD_CANCELLED or REDWOOD_ERROR. Poll cancellation at entry and between bounded chunks of work, outside the innermost arithmetic loop where possible.

Match the actual computation

The pattern must describe the kernel’s math, including outputs, casts and cache effects. Prefer an existing higher-level composition when it fits; core operators can express unusual computations.

bind rejects unsupported dtypes, shapes or layouts with ValueError. A contiguous FP32 kernel should check that dtype and layout in its binder.

Kernel metadata can use version-specific compiler binding helpers. These helpers follow the installed Redwood version.

Capabilities

Return REDWOOD_KERNEL_ABI_VERSION and a bitmask using the defined constants. For example, an AVX2/FMA implementation declares both REDWOOD_CAP_X86_AVX2 and REDWOOD_CAP_X86_FMA.

NEON dot-product instructions need their own capability. Software dependencies such as OpenBLAS and Accelerate also require bits.

Compile and use

From a source checkout, uv run task kernels builds the built-in libraries. Use the CLI below to add your own kernel; source files are required.

redwood kernels --backend scalar example_add.c

Objects go under $REDWOOD_HOME/<version>/kernels/<backend>/custom/ and link into custom_kernels.so alongside it. Redwood manages the indexes.

Recompiling a custom symbol replaces it while retaining other custom kernels. Shipped symbols cannot be replaced; use a distinct symbol for another implementation. The compiler then considers compatible candidates for the same computation.

Compile the model again after changing kernels. Existing compiled models retain their selected libraries.