Custom processing
Custom callbacks connect your application’s input/output types to the tensors a model declares. They are ordinary Python functions, supplied when constructing runtime Model.
Before writing them, identify the model’s input/output names, shapes, dtypes and bounds. Follow its actual token conventions, sample rate, image layout and conditioning requirements.
A complete example
For an LLM declaring tokens and reset inputs and a generated tokens output:
from pathlib import Path
import numpy as np
from redwood.processing import TensorInputs, TensorOutputs, utils
from redwood.runtime import Model
assets = Path("/path/to/tokenizer-assets")
def prepare(prompt: str) -> TensorInputs:
return {
"tokens": utils.tokenize(prompt, assets=assets, chat=True),
"reset": np.asarray(False, dtype=np.bool_),
}
def finish(outputs: TensorOutputs) -> str:
return utils.decode_tokens(outputs["tokens"], assets=assets)
with Model[str, str](
"/path/to/compiled-llm", preprocess=prepare, postprocess=finish,
) as model:
print(model.run("Explain attention in one sentence."))
The preprocessor receives exactly the value passed to run. The postprocessor receives the named NumPy outputs. Their argument/result types are your application contract.
The example sends one complete prompt at a time. For multi-turn chat, accept the complete message list and preserve the model’s reset/reuse semantics.
What to return
Use input names declared by the graph. Return explicitly typed NumPy arrays when possible, including rank-zero arrays for scalar controls. Respect maximum shapes and value bounds before execution.
Validate external requests in preprocessing. In postprocessing, interpret the declared outputs and handle completion flags such as truncated before returning a final result.
Reuse supplied functions
You can override only the stage that needs customization. The other stage can come from manifest defaults, or be a supplied callback with its assets/options bound.
For PocketTTS voice conditioning, for example, start with the supplied tokenizer and add the inputs required by the RDL:
from pathlib import Path
from redwood.processing import TensorInputs, preprocessing
assets = {"tokenizer": Path("/path/to/tokenizer-assets")}
def prepare_text(text: str) -> TensorInputs:
return preprocessing.pocket_tts(text, assets=assets, options={})
This fragment tokenizes text. For synthesis, add the voice inputs declared by the RDL, using their specified names and shapes.
Keep setup outside each request
A closure or callable object can hold asset paths, loaded resources or request metadata shared between stages. Calls on a Model are serialized, so the normal pair of callbacks completes one request before the next starts.
If you use the same stateful callbacks across several instances, arrange their synchronization yourself. A postprocessor receives only output tensors; any additional context must come from your application.
Give long-running custom processors a cooperative cancellation mechanism so they can stop when requested.
Compile and load
Leave processing unspecified or empty in the RDL if the application supplies both stages. If defaults exist, explicit callbacks override their corresponding stages.
Your application ships its custom processing code and passes the functions directly to Model. Both processing stages must be available when Model opens.
For full control, call preprocessing, Runtime.run and postprocessing directly. The tensor contract is the same whichever wrapper you choose.