Runtime

Runtime runs the model’s tensor computation directly. Use it when the application already handles preprocessing and postprocessing.

import numpy as np

from redwood.runtime import Runtime

with Runtime("/path/to/compiled-llm", threads=4) as runtime:
    outputs = runtime.run({
        "tokens": np.asarray([1, 2, 3], dtype=np.int32),
        "reset": np.asarray(True, dtype=np.bool_),
    })
    generated = outputs["tokens"]

The names and shapes above assume an LLM declaring tokens and reset. Another RDL program can have entirely different inputs.

Reference

Runtime(directory, *, threads=None)

directory is a compiled model directory. threads overrides its execution thread count. Opening the runtime verifies the bundle and prepares native resources. Use a context manager to release them.

inputs and outputs

Both return mappings from graph names to TensorSpec:

FieldMeaning
dtypeLogical Redwood dtype
shapeFixed dimensions and None for runtime dimensions
maximum_shapeMaximum dimensions used to plan storage
from redwood.runtime import Runtime

with Runtime("/path/to/compiled-model") as runtime:
    for name, spec in runtime.inputs.items():
        print(name, spec.dtype, spec.shape, spec.maximum_shape)

run(inputs, *, on_token=None)

Supply a mapping of input names to numerical arrays or supported JSON values. Return a dictionary of output names to NumPy arrays.

Use explicitly typed NumPy arrays when controlling dtype matters. Inputs must satisfy the compiled shape and value constraints. A missing input or incompatible value is an error.

Output arrays are independently owned. They remain valid across later calls, even as the backend reuses intermediate buffers.

cancel() and close()

cancel() requests cancellation without waiting for an active native kernel to complete. close() also releases the session’s resources. After cancellation or execution failure, close the instance and create a new one.

Stream tokens

import numpy as np

from redwood.runtime import Runtime, TokenEvent


def receive(event: TokenEvent) -> None:
    print(event.output, event.index, event.token)


with Runtime("/path/to/compiled-llm") as runtime:
    outputs = runtime.run(
        {
            "tokens": np.asarray([1, 2, 3], dtype=np.int32),
            "reset": np.asarray(True, dtype=np.bool_),
        },
        on_token=receive,
    )

Streaming applies to the supported rank-one integer output named tokens.

TokenEvent contains output, index and token. The callback runs on the requesting thread and should return promptly. An exception from it aborts execution and invalidates the active session. The final output arrays are still returned when execution completes normally.

Errors

CancelledError reports cancellation. ExecutionError reports native execution failure. Invalid arguments and bundle problems can also raise ordinary Python validation or filesystem errors.

Create a fresh session after failed native execution. See sessions and cancellation for recovery and custom processing behavior.

Backend boundary

Runtime controls private CPU backend code that allocates memory, owns the thread pool and executes the manifest. Users select the backend during compilation and the thread count during execution.

A bundle requires compatible instructions, software capabilities and native ABI. See files and versions.