A complete add kernel

This kernel adds equal-shaped, contiguous FP32 tensors. It runs on one thread, uses separate output storage and requires no scratch. Those choices keep the first implementation small.

Save the following as example_add.c in your working directory:

#include "common.h"

typedef struct {
    size_t count;
} Parameters;

static RedwoodStatus requirements(
    const void *opaque, size_t *inputs, size_t *outputs, size_t *scratch
) {
    const Parameters *p = opaque;
    inputs[0] = inputs[1] = outputs[0] = *scratch = 0;
    if (p->count > SIZE_MAX / sizeof(float)) {
        return REDWOOD_ERROR;
    }
    inputs[0] = inputs[1] = outputs[0] = p->count * sizeof(float);
    return REDWOOD_OK;
}

REDWOOD_KERNEL RedwoodStatus redwood_scalar_example_add(
    void *const *outputs, const void *const *inputs,
    void *scratch, const void *opaque, const RedwoodKernelContext *context
) {
    (void)scratch;
    const Parameters *p = opaque;
    const float *a = inputs[0], *b = inputs[1];
    float *output = outputs[0];
    if (context->is_cancelled(context->cancellation_context)) {
        return REDWOOD_CANCELLED;
    }
    size_t start = 0;
    while (start < p->count) {
        if (context->is_cancelled(context->cancellation_context)) {
            return REDWOOD_CANCELLED;
        }
        size_t chunk = p->count - start;
        if (chunk > 4096) {
            chunk = 4096;
        }
        size_t end = start + chunk;
        for (size_t i = start; i < end; ++i) {
            output[i] = a[i] + b[i];
        }
        start = end;
    }
    return REDWOOD_OK;
}

REDWOOD_KERNEL const char *redwood_scalar_example_add_rdl(void) {
    return "from redwood.rdl import *\n"
        "from redwood.compiler.kernels.bindings import _contiguous_binary, _contiguous_strides\n"
        "def pattern(a: Tensor, b: Tensor) -> tuple[Tensor, ...]:\n"
        "    return (Add(a, b),)\n"
        "def bind(context):\n"
        "    output = context.outputs[0]\n"
        "    strides = _contiguous_strides(output.shape)\n"
        "    if output.dtype != 'fp32' or (output.strides or strides) != strides:\n"
        "        raise ValueError('requires contiguous FP32 output')\n"
        "    return {'count': _contiguous_binary(context)['count']}\n";
}

REDWOOD_KERNEL const RedwoodParameterSchema *redwood_scalar_example_add_parameters(void) {
    static const RedwoodParameter fields[] = {
        {"count", "size_t", offsetof(Parameters, count), NULL}
    };
    static const size_t inputs[] = {REDWOOD_ALIGNOF(float), REDWOOD_ALIGNOF(float)};
    static const size_t outputs[] = {REDWOOD_ALIGNOF(float)};
    static const RedwoodParameterSchema schema = {
        sizeof(Parameters), REDWOOD_ALIGNOF(Parameters), 1, fields, 2, 1,
        requirements, NULL, inputs, outputs, 1
    };
    return &schema;
}

REDWOOD_KERNEL const RedwoodCapabilities *redwood_scalar_example_add_capabilities(void) {
    static const RedwoodCapabilities capabilities = {REDWOOD_KERNEL_ABI_VERSION, 0};
    return &capabilities;
}

What each part does

  • pattern tells the compiler this kernel covers Add(a,b)
  • bind checks its supported inputs and fills count
  • The schema describes that size_t field and the two-input/one-output pointer contract
  • requirements reports bytes, while a NULL alias callback refuses overwrite reuse
  • Execution polls cancellation between chunks and performs the add
  • Capabilities require only the baseline backend and current ABI

The Python binder uses helpers supplied with the installed Redwood version. Its parameter dictionary contains only count, matching the C struct.

Compile and select

redwood kernels --backend scalar example_add.c

Then compile a model containing compatible adds with --backend scalar. The compiler considers the new implementation alongside other compatible add kernels.

This example establishes the contract. Optimize arithmetic, threading or supported layouts separately when measurements justify those changes.