Core foundations
Typed registries, normalization helpers, deterministic utilities, image composition, and reusable safety guardrail protocols.
Foundational Core APIs are small, but they define conventions shared by many larger systems: normalized identifiers, deterministic registries, environment flags, exact division, common list conversion, image materialization/composition, random seeding, and safety interfaces.
Typed registry example
from worldfoundry.core import TypedRegistry
metrics = TypedRegistry()
metrics.register(
"temporal-consistency",
object(),
aliases=("temporal", "tc"),
metadata={"direction": "higher-is-better"},
)
assert metrics.get("TC") is metrics.get("temporal-consistency")
assert metrics.keys() == ("temporal-consistency",)Keys and aliases use stripped, case-folded lookup, while the original public key remains in RegistryItem. Registration rejects collisions instead of silently overwriting them. Enumeration is deterministic, which makes generated manifests and tests stable.
Guardrail composition example
from worldfoundry.core.safety import GuardrailRunner
class RejectEmptyPrompt:
def is_safe(self, value):
prompt = str(value).strip()
return (bool(prompt), "prompt is empty" if not prompt else "")
runner = GuardrailRunner(
safety_models=[RejectEmptyPrompt()],
generic_block_msg="Request rejected",
)
assert runner.run_safety_check("walk through a snowy forest")[0] is True
assert runner.run_safety_check("") == (False, "Request rejected")ContentSafetyGuardrail and PostprocessingGuardrail are structural protocols: implementations only need the documented method. GuardrailRunner stops at the first unsafe classifier, then applies postprocessors sequentially when requested. An empty classifier list returns safe with a warning; production policy should decide explicitly whether that fail-open behavior is acceptable.
The safety package imports NumPy and rank-aware Loguru logging from the model runtime environment. The registry example itself uses only the import-light top-level Core facade.
Utility boundaries
env_is_true recognizes a bounded truthy vocabulary; it is not a general parser. divide asserts exact divisibility and is intended for shape/group invariants. as_list normalizes a scalar or sequence at API boundaries. set_random_seed coordinates common random backends but does not by itself guarantee deterministic CUDA kernels.
Image helpers accept paths, PIL images, arrays, or tensors where documented. compose_horizontal_views and split_horizontal_views preserve a named view ordering; keep that ordering with the resulting artifact instead of relying on a later reader to guess it.
Complete reference
The blocks below are the generated signatures for this category. Use the on-page symbol index to jump; source links open the defining implementation behind each lazy export.
26 public symbols
batched_image_features
def batched_image_features(images: torch.Tensor,encoder: Callable[[torch.Tensor], torch.Tensor],device: torch.device | str | int,batch_size: int | None = None,default_batch_size: int = 32,scope: str = 'image_features',normalize: bool = False,output_device: torch.device | str | int | None = 'cpu',batch_size_buckets: Sequence[int] | None = None) -> torch.TensorEncode images with OOM backoff and shape-stable tail padding. Belongs to Core foundations (registries, utilities, safety contracts). Annotated return type: torch.Tensor.
Parameters
imagestorch.TensorencoderCallable[[torch.Tensor], torch.Tensor]devicetorch.device | str | intbatch_sizeint | Nonedefault Nonedefault_batch_sizeintdefault 32scopestrdefault 'image_features'normalizebooldefault Falseoutput_devicetorch.device | str | int | Nonedefault 'cpu'batch_size_bucketsSequence[int] | Nonedefault NoneReturns
compose_horizontal_views
def compose_horizontal_views(images,target_size: tuple[int, int] | None = None) -> Image.ImageResize optional per-view images and concatenate them horizontally. Belongs to Core foundations (registries, utilities, safety contracts). Annotated return type: Image.Image.
Parameters
imagestarget_sizetuple[int, int] | Nonedefault NoneReturns
ContentSafetyGuardrail
class ContentSafetyGuardrail(Protocol)Interface implemented by prompt and media safety classifiers. Belongs to Core foundations (registries, utilities, safety contracts).
Methods
Return whether `input` is safe and an optional explanation.
Parameters
inputAnyReturns
def divide(numerator: int,denominator: int) -> intReturn exact integer division and reject a non-divisible numerator. Belongs to Core foundations (registries, utilities, safety contracts). Annotated return type: int.
Parameters
numeratorintdenominatorintReturns
Raises
AssertionError- `
numeratoris not divisible bydenominator`.
class DuplicateRegistryKeyError()Raised when a key or alias maps to multiple different entries. Belongs to Core foundations (registries, utilities, safety contracts).
env_is_true
def env_is_true(env_name: str) -> boolReturn whether an environment variable uses a recognized truthy spelling. Belongs to Core foundations (registries, utilities, safety contracts). Annotated return type: bool.
Parameters
env_namestrReturns
class GuardrailRunner(safety_models: Sequence[ContentSafetyGuardrail] | None = None,generic_block_msg: str = '',generic_safe_msg: str = '',postprocessors: Sequence[PostprocessingGuardrail] | None = None)Run reusable safety classifiers and postprocessors in sequence. Belongs to Core foundations (registries, utilities, safety contracts).
Parameters
generic_block_msgstrdefault ''generic_safe_msgstrdefault ''postprocess`.Methods
Overview
Run classifiers in order and return the first block or final safe result.
Source docstring
Run classifiers in order and return the first block or final safe result.
Note the deliberate fail-open default: with no configured safety models the input is reported safe (with a warning). Deployments that require enforcement must configure at least one classifier.
Parameters
inputAnyReturns
Apply every configured safety postprocessor to a frame array.
Parameters
framesnp.ndarrayReturns
load_pil_image
def load_pil_image(image_input,first_sequence_item: bool = True) -> Image.ImageOverview
Normalize a path, PIL image, numpy array, torch tensor, or sequence to RGB PIL. Belongs to Core foundations (registries, utilities, safety contracts). Annotated return type: Image.Image.
Parameters
image_inputfirst_sequence_itembooldefault TrueReturns
materialize_image_input
def materialize_image_input(image_input,output_dir: str | os.PathLike[str],filename: str = 'input.png') -> strNormalize an image-like input and save it at a stable local path. Belongs to Core foundations (registries, utilities, safety contracts). Annotated return type: str.
Parameters
image_inputoutput_dirstr | os.PathLike[str]filenamestrdefault 'input.png'Returns
def mean_pairwise_cosine_distance(features: Sequence[torch.Tensor]) -> floatReturn the mean cosine distance across all unordered feature pairs. Belongs to Core foundations (registries, utilities, safety contracts). Annotated return type: float.
Parameters
featuresSequence[torch.Tensor]Returns
normalize_action_values
def normalize_action_values(values: Any,statistics: Mapping[str, Any],mode: str = 'min_max',clip: float | None = None) -> np.ndarrayNormalize action values using checkpoint statistics along the last axis. Belongs to Core foundations (registries, utilities, safety contracts). Annotated return type: np.ndarray.
Parameters
valuesAnystatisticsMapping[str, Any]select_modality_statistics.modestrdefault 'min_max'min_max (default), q99 / quantile / bounds, mean_std / standard / zscore, or identity`.clipfloat | Nonedefault None[-clip, clip]` after normalization.Returns
float` array with the same shape as *values*.normalize_registry_key
def normalize_registry_key(value: str,field_name: str = 'registry key') -> strStrip and `casefold a user-facing Registry key for case-insensitive lookup. Belongs to Core foundations (registries, utilities, safety contracts). Annotated return type: str`.
Parameters
valuestrfield_namestrdefault 'registry key'Returns
PostprocessingGuardrail
class PostprocessingGuardrail(Protocol)Interface implemented by safety postprocessors such as face blurring. Belongs to Core foundations (registries, utilities, safety contracts).
Methods
Return safety-processed frames.
Parameters
framesnp.ndarrayReturns
class PromptProcessor()Overview
Apply optional prompt refiners and extenders without owning model loading. Belongs to Core foundations (registries, utilities, safety contracts).
Source docstring
Apply optional prompt refiners and extenders without owning model loading.
`refiners rewrite a string or list in place; extenders take a {"prompt": ...} mapping and return an expanded dict. Both run under torch.no_grad()`.
Methods
Instantiate each refiner with `from_model_manager(model_source)` and append it.
Parameters
model_sourcerefiner_classesSequence[type]default ()Returns
Instantiate each extender with `from_model_manager(model_source)` and append it.
Parameters
model_sourceextender_classesSequence[type]default ()Returns
Run refiners in order on one prompt or a list; lists are processed recursively.
Parameters
promptpositivebooldefault TrueOverview
Wrap `prompt as {"prompt": ...}` and run extenders in order.
Source docstring
Wrap `prompt as {"prompt": ...}` and run extenders in order.
`positive` is kept only to match the refiner API; this implementation does not use it.
Parameters
promptstrpositivebooldefault TrueRegistryError
class RegistryError()Base class for registry definition errors. Belongs to Core foundations (registries, utilities, safety contracts).
RegistryItem
class RegistryItem(key: str,value: ItemT,aliases: tuple[str, ...] = (),metadata: Mapping[str, object] = <dict factory>)One registered item plus its public aliases. Belongs to Core foundations (registries, utilities, safety contracts).
Attributes
keystrvalueItemTaliasestuple[str, ...]default ()metadataMapping[str, object]default <dict factory>resize_and_letterbox
def resize_and_letterbox(image,target_width: int,target_height: int,fill: tuple[int, int, int] = (255, 255, 255)) -> Image.ImageFit an RGB image inside a fixed canvas while preserving aspect ratio. Belongs to Core foundations (registries, utilities, safety contracts). Annotated return type: Image.Image.
Parameters
imagetarget_widthinttarget_heightintfilltuple[int, int, int]default (255, 255, 255)Returns
select_modality_statistics
def select_modality_statistics(dataset_statistics: Mapping[str, Any],modality: str = 'action',key: str | None = None) -> tuple[str | None, Mapping[str, Any]]Overview
Select one modality's statistics from a checkpoint statistics mapping. Belongs to Core foundations (registries, utilities, safety contracts). Annotated return type: tuple[str | None, Mapping[str, Any]].
Source docstring
Select one modality's statistics from a checkpoint statistics mapping.
Both common layouts are accepted: a direct `{"min": ..., "max": ...} mapping and a dataset-keyed {"robot": {"action": {...}}}` mapping. Dataset-key selection is strict when more than one key is available.
Parameters
dataset_statisticsMapping[str, Any]modalitystrdefault 'action'"action"`).keystr | Nonedefault NoneReturns
(resolved_key, stats_mapping). *resolved_key* is None` when the input was already a direct statistics mapping.Raises
KeyError- *key* is missing or has no *modality* mapping.
ValueError- Empty input, or multiple dataset keys with no *key*.
set_random_seed
def set_random_seed(seed: int,by_rank: bool = False) -> intOverview
Set Python, NumPy, and PyTorch seeds with optional distributed-rank offset. Belongs to Core foundations (registries, utilities, safety contracts). Annotated return type: int.
Source docstring
Set Python, NumPy, and PyTorch seeds with optional distributed-rank offset.
Thin wrapper over :func:set_seed_everywhere (the canonical entry point). When the process group is unavailable, `by_rank` silently falls back to the unoffset seed.
Parameters
seedintby_rankbooldefault FalseReturns
split_horizontal_views
def split_horizontal_views(image,num_views: int,target_size: tuple[int, int] | None = None) -> list[Image.Image]Overview
Split a horizontally concatenated image into equally sized RGB views. Belongs to Core foundations (registries, utilities, safety contracts). Annotated return type: list[Image.Image].
Parameters
imagenum_viewsinttarget_sizetuple[int, int] | Nonedefault NoneReturns
stack_or_pad_tensors
def stack_or_pad_tensors(values: Sequence[Tensor],padding_value: float | int | bool | None = None) -> TensorOverview
Stack equal tensors or right-pad variable-length 1-D tensors. Belongs to Core foundations (registries, utilities, safety contracts). Annotated return type: Tensor.
Source docstring
Stack equal tensors or right-pad variable-length 1-D tensors.
This is the common collation shape used by multimodal policies whose dense image/state inputs are fixed while token and mask lengths may vary. More complex shape mismatches are rejected instead of being padded silently.
Parameters
valuesSequence[Tensor]padding_valuefloat | int | bool | Nonedefault NoneReturns
TypedRegistry
class TypedRegistry(items: Iterable[RegistryItem[ItemT]] = ())Overview
Deterministic keyed registry with alias support. Belongs to Core foundations (registries, utilities, safety contracts).
Source docstring
Deterministic keyed registry with alias support.
Registration methods (mutate state): - `register(key, value, ...)` — add one item and its aliases.
Lookup methods (read state): - `get(key) / get_item(key) — resolve a key or alias. - keys / values / items / aliases` — enumerate entries.
Parameters
Raises
DuplicateRegistryKeyError- On conflicting keys or aliases.
UnknownRegistryKeyError- When a lookup cannot be resolved.
Methods
register(key: str,value: ItemT,aliases: Iterable[str] = (),metadata: Mapping[str, object] | None = None) -> RegistryItem[ItemT]SourceRegister an item and return the normalized registry record.
Parameters
keystrvalueItemTaliasesIterable[str]default ()metadataMapping[str, object] | Nonedefault NoneReturns
Resolve a key or alias to the registered value.
Parameters
keystrReturns
Return normalized alias to normalized canonical key mapping.
Returns
Return registry items sorted by their public key.
Returns
Return registered values sorted by public key.
Returns
class UnknownRegistryKeyError()Raised when a registry lookup cannot be resolved. Belongs to Core foundations (registries, utilities, safety contracts).
unnormalize_action_values
def unnormalize_action_values(normalized_values: Any,statistics: Mapping[str, Any],mode: str = 'min_max') -> np.ndarrayOverview
Convert normalized policy outputs back to environment-space actions. Belongs to Core foundations (registries, utilities, safety contracts). Annotated return type: np.ndarray.
Source docstring
Convert normalized policy outputs back to environment-space actions.
Inverse of :func:normalize_action_values for the same *mode* and *statistics*. Masked-off channels are copied through unchanged. Unlike the forward path, this function does not clip.
Parameters
normalized_valuesAnystatisticsMapping[str, Any]modestrdefault 'min_max'