Graph and checkpoint declarations
Declare checkpoints upfront, then use an outer Graph context for the computation. Input/output declarations become the public tensor contract of the compiled model.
Graph
from redwood.rdl import *
with Graph() as graph:
x = Input(name="x", shape=(4,), dtype="fp32")
Output(Mul(x, 2.0), name="y")
Operators called inside the context record the computation that the runtime will execute.
Model and Weight
W = Model("checkpoint")
weight = W["projection.weight"]
low_precision = Weight(f"{W.root}.projection.weight", dtype="bf16")
Model(name) declares a checkpoint namespace. Names contain only letters. W["tensor.name"] references a tensor in that checkpoint; W.root gives the namespace string. The first segment of a full weight reference identifies the checkpoint, and the remaining text identifies its tensor.
An omitted weight dtype uses source-resolution information. An explicit dtype requests that logical representation during compilation. Shape comes from the resolved tensor.
A model with several parts or adapters can declare several bindings:
BASE = Model("base")
ADAPTER = Model("adapter")
Supply base=... adapter=... to the compiler. The graph must describe how their computations combine. Two references to the same source tensor can share stored weight data; a separate representation may be needed when selected kernels require a different dtype or packing.
Input
Input(
*, name, shape, dtype, maximum_shape=None,
minimum=None, maximum=None, multiple_of=None,
)
| Parameter | Meaning |
|---|---|
name | Unique input name exposed to the runtime |
shape | Fixed dimensions or None for variable dimensions |
dtype | Logical dtype |
maximum_shape | Explicit bounds for variable dimensions; fixed dimensions must match |
minimum, maximum, multiple_of | Value constraints for scalar integer inputs |
A scalar has shape=(). A token sequence usually has shape=(None,).
with Graph() as graph:
tokens = Input(
name="tokens", shape=(None,), dtype="int32",
maximum_shape=(4096,),
)
height = Input(
name="height", shape=(), dtype="int32",
minimum=16, maximum=1920, multiple_of=16,
)
Output(tokens, name="tokens")
The example illustrates dimension and scalar value bounds. A dimension bound limits tensor size. A scalar value bound limits a control such as image height. The compiler needs both when that control determines memory requirements.
When maximum_shape is omitted, --max-input-size supplies default bounds for variable input dimensions. Inputs are declared in the outer graph.
Output
Output(x, *, name)
Declare a named result in the outer graph. Output returns nothing; it exposes the supplied tensor. Names must be unique among outputs.
A model can return several outputs, such as generated tokens and a truncated flag. Processors decide how those results become application values.
Constant, TensorShape and Tensor
Constant(value, dtype=None) creates a graph value from a scalar, NumPy array or weight reference. For a checkpoint tensor in a core operation, wrap its Weight in Constant; higher-level operations commonly accept either form.
TensorShape((None, 128)) declares shape metadata. Most authoring functions also accept a tuple directly. Shape(x) is different: it creates an integer tensor containing dimensions during execution.
Tensor is the graph value type used in function annotations. Obtain values through Input and operators. Use RDL control flow for decisions based on tensor values.