Model API
RelationalTransformer is the high-level entry point for loading checkpoints and
computing predictions; the Quickstart shows it in context. RTJModel
is the underlying torch.nn.Module for custom training loops and architecture work, and
ModelOutput carries every result field.
RelationalTransformer
- class relational_transformers.RelationalTransformer(model_name_or_path=None, *, task='classification', backend='torch', device=None, revision=None, compile=False, providers=None)
Load and run an RT-J checkpoint.
- Parameters:
model_name_or_path (str | Path | None) – Hugging Face repository, local checkpoint directory, or ONNX file. Defaults to
DEFAULT_MODEL, orDEFAULT_ONNX_MODELfor the ONNX backend.task (str) –
"classification"(default) or"regression".backend (str) –
"torch","triton","onnx", or"meta".device (str | torch.device | None) – PyTorch device. Auto-selects CUDA, MPS, then CPU.
revision (str | None)
compile (bool)
- forward(inputs, *, output='target_scores', target=None)
Run the model and return the requested
ModelOutputview.- Parameters:
inputs – A
RelationalBatch, a mapping of canonical batch fields, a[cells, 2*d_text]all-text cell array, or a sequence of single-context inputs to collate.output –
"target_scores","token_scores","target_features","target_scores_and_text", or"embeddings". The ONNX and Triton backends serve"target_scores"only.target – Target cell position(s), required only for raw cell arrays.
- Returns:
A
ModelOutputwith torch tensors on every backend.- Return type:
- predict(inputs, *, target=None, task_head=None, activation=None, convert_to_numpy=True)
Predict target values for one or more relational contexts.
- Parameters:
inputs – Anything
forward()accepts.target – Target cell position(s) for raw cell arrays.
task_head (str | None) – Name of a head fitted with
fit_head(). When set, the head runs over frozentarget_featuresinstead of the published scalar decoder.activation (str | None) –
Noneselects a default from the problem type: sigmoid for binary, multilabel, and classification tasks, softmax for multiclass heads, and identity for regression. Pass"identity"to obtain raw logits.convert_to_numpy – Return numpy values (a Python float for a single context) instead of a torch tensor.
- Returns:
Activated scores with one value per context, or one row per context for multi-label heads.
- encode(inputs, *, target=None, output_value='embeddings', convert_to_numpy=True)
Return contextual cell states or the pooled target representation.
- Parameters:
inputs – Anything
forward()accepts.target – Target cell position(s) for raw cell arrays.
output_value –
"embeddings"for oned_model-wide state per cell, or"target_features"for the summed target state used by task heads.convert_to_numpy – Return numpy arrays instead of torch tensors.
- export_onnx(path, example, *, target=None, opset_version=18)
Export target-score inference to ONNX with dynamic batch and cell axes.
Requires the torch backend. The example batch fixes
d_textand the architecture; batch size and context length stay dynamic.
- save_pretrained(directory)
Write
model.safetensorsandconfig.jsonto a directory.The directory loads back through the normal constructor and follows the same layout as published Hugging Face checkpoints.
- fit_head(examples, **kwargs)
Fit a named
TaskHeadover frozen features.Each example is encoded once with the frozen backbone, then a linear head is trained on the resulting
[d_model]target features. The fitted head is stored underself.heads[task]and served throughpredict(..., task_head=task). Seerelational_transformers.training.fit_head()for keyword options.
- get_model_kwargs()
Return dimensions without materializing weights (especially useful on meta).
- Return type:
dict
RTJModel
- class relational_transformers.RTJModel(num_blocks=12, d_model=512, d_text=384, num_heads=8, d_ff=2048, legacy_attn=False, *, device=None)
The RT-J backbone and published decoder heads.
Initialize internal Module state, shared by both nn.Module and ScriptModule.
- Parameters:
num_blocks (int)
d_model (int)
d_text (int)
num_heads (int)
d_ff (int)
legacy_attn (bool)
- forward(batch, output='target_scores')
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.- Parameters:
batch (RelationalBatch)
output (str)
- Return type:
ModelOutput
- class relational_transformers.ModelOutput(scores: 'Tensor | None' = None, token_scores: 'Tensor | None' = None, target_text: 'Tensor | None' = None, features: 'Tensor | None' = None, embeddings: 'Tensor | None' = None)