Kernel metadata
Metadata connects a mathematical operation to one native implementation. There are three questions: what does it compute, which inputs can it handle, and what must be passed to C?
RDL pattern and binding
<name>_rdl() returns a string of Python source. It defines pattern and bind. For a contiguous FP32 add implementation, the metadata can look like this:
from redwood.rdl import *
from redwood.compiler.kernels.bindings import _contiguous_binary, _contiguous_strides
def pattern(a: Tensor, b: Tensor) -> tuple[Tensor, ...]:
return (Add(a, b),)
def bind(context):
output = context.outputs[0]
strides = _contiguous_strides(output.shape)
if output.dtype != "fp32" or (output.strides or strides) != strides:
raise ValueError("requires contiguous FP32 output")
return {"count": _contiguous_binary(context)["count"]}
The add example supplies the matching C schema and execution function.
pattern takes named tensor arguments and returns a tuple of results. Matching compares the computation described by the pattern. Expose cache effects when the implementation reads or updates persistent state.
bind returns a dictionary whose keys match C parameter field names. In this example, count supplies the element count. It also rejects unsupported inputs through the helper. The helper returns additional fields used by other kernels; return only those in this kernel’s schema.
Binding context
| Field | Contents |
|---|---|
inputs | Pattern argument names mapped to tensor information |
outputs | Tensor information in output order |
attrs | Matched operation attributes, such as an axis or epsilon |
Tensor information includes logical dtype, shape, maximum shape and strides. Dimensions and strides may contain runtime expressions. Preserve those expressions when filling parameters.
Use ValueError to reject unsupported combinations. Matching can then consider another implementation.
Binding objects and helpers currently live in the private compiler module and can change with the installed version. Kernel authors target that version; application code should use the public RDL/runtime APIs.
Parameter schema
<name>_parameters() returns a pointer to a RedwoodParameterSchema that remains valid while the library is loaded.
| Schema information | Purpose |
|---|---|
| Struct size and alignment | Allocate a correctly laid-out parameter block |
| Field names, types and offsets | Put each bound value in the correct C field |
| Input/output counts | Match pointer arrays to the implementation |
| Input/output and scratch alignments | Arrange compatible storage |
requirements | Calculate input/output byte spans and scratch bytes |
can_alias | Grant safe output/input overwrite permissions |
Describe field offsets with offsetof. Supported scalar field types are size_t, int32_t, uint32_t, int64_t, uint64_t and float. An array field identifies the parameter field containing its length; the C field itself is a pointer to those elements.
For a simple struct { size_t count; }, the binding returns {"count": ...} and the schema describes one size_t field. requirements can then report count * sizeof(float) bytes per FP32 tensor and zero scratch.
See memory requirements before adding views, aliasing, cache destinations or shared scratch.
Physical packing
Logical dtype says what numbers the tensor represents. Packing says how a particular implementation stores/accesses them.
The optional packings metadata maps pattern input names to required physical format names. physical_inputs can explicitly identify physical input bindings when the default pattern argument list is insufficient.
The compiler selects physical packing to satisfy kernel requirements. Model authors choose logical dtypes in RDL.
Capability schema
<name>_capabilities() returns a persistent RedwoodCapabilities with abi_version and required fields. Use REDWOOD_KERNEL_ABI_VERSION and OR the required constants together.
Hardware bits belong to the backend. Software bits include REDWOOD_CAP_LIBM, REDWOOD_CAP_OPENBLAS and REDWOOD_CAP_ACCELERATE. REDWOOD_CAP_PLATFORM_BLAS selects Accelerate on Apple platforms and OpenBLAS elsewhere.
Declare instructions actually executed and libraries actually required. The directory identifies the backend; the capability mask declares its required features.