Model
Model combines preprocessing, inference and postprocessing. Use it when the caller works with application values such as prompts, audio paths or image-generation requests.
from redwood.runtime import Model
with Model[str, str]("/path/to/compiled-model", threads=4) as model:
answer = model.run("What is grouped-query attention?")
print(answer)
Reference
Model(directory, *, preprocess=None, postprocess=None, threads=None)
| Parameter | Meaning |
|---|---|
directory | Path to a compiled model directory |
preprocess | Optional function converting a request into named input tensors |
postprocess | Optional function converting named output tensors into a result |
threads | Execution thread count; omitted uses the compiled default |
Model[Input, Output] describes the Python types accepted by run and returned from it. Those types come from your processing functions.
An explicit callback overrides that stage’s manifest default. Both stages must be available. If a stage has neither a callback nor a default, construction fails before allocating model resources.
run(value, *, on_text=None)
Preprocess value, execute the graph, then postprocess its output. Return the postprocessor’s result.
The preprocessor receives the value you pass. Calls on the same instance are serialized, including both processing stages.
supports_text_streaming
A boolean property indicating whether the built-in text postprocessor is active.
cancel() and close()
cancel() requests cancellation and can be called from another thread. close() cancels any active request and releases resources. The context manager calls close() when it exits.
Create a fresh instance after cancellation or native execution failure. See sessions and cancellation.
Stream text
from redwood.runtime import Model
with Model[str, str]("/path/to/compiled-model") as model:
answer = model.run(
"Explain KV caching briefly.",
on_text=lambda chunk: print(chunk, end="", flush=True),
)
print()
The callback receives decoded text chunks. run still returns the complete answer. Keep the callback short; it executes on the requesting thread.
Passing on_text without built-in text postprocessing raises an error. For token-level callbacks, use Runtime.
Supply custom processors
import numpy as np
from redwood.processing import TensorInputs, TensorOutputs
from redwood.runtime import Model
def prepare(values: list[float]) -> TensorInputs:
return {"x": np.asarray(values, dtype=np.float32)}
def finish(outputs: TensorOutputs) -> list[float]:
return [float(value) for value in outputs["y"]]
with Model[list[float], list[float]](
"/path/to/compiled-vector-model",
preprocess=prepare,
postprocess=finish,
) as model:
result = model.run([1.0, 2.0, 3.0])
This assumes the model declares a rank-one x input and y output. Pass custom callback functions directly to Model.
See custom processing for tokenizer reuse, assets and validation.
State and ownership
Keep an instance open to retain its caches. Independent instances have independent state. Only one session can execute native inference at a time per Python process. An overlapping run from another session raises ExecutionError.
Use Model inside your application, with your own message storage and request handling.