Core 基础能力
Typed registry、规范化 helper、确定性工具、图像组合与可复用安全 guardrail protocol。
基础 Core API 看起来很小,但它们定义了许多大型系统共同使用的约定:标识符规范化、确定性 registry、环境 flag、精确整除、list 归一化、图像物化与组合、随机种子和安全接口。
Typed registry 示例
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",)查找 Key 和 alias 时,会先对它们进行 strip 与 case-fold 处理,但 RegistryItem 会保留原始公开 key。注册冲突会显式报错,不会静默覆盖;枚举顺序是确定的,因此生成 manifest 和测试结果也稳定。
Guardrail 组合示例
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 和 PostprocessingGuardrail 是结构化 protocol:实现只需要提供文档声明的方法。GuardrailRunner 在第一个不安全 classifier 处停止,并在需要时按顺序应用 postprocessor。classifier list 为空时,它会告警并返回安全;生产策略必须明确决定这种 fail-open 行为是否可接受。
Safety package 会从模型 runtime 环境导入 NumPy 和基于 Loguru 的 rank-aware logging。Registry 示例本身只使用 import-light 的 Core 顶层 facade。
工具边界
env_is_true 只识别有限的 truthy 词表,并不是通用 parser。divide 会断言精确整除,适合形状或 group invariant。as_list 在 API 边界把标量或 sequence 规范成 list。set_random_seed 会协调常见随机后端,但仅靠它不能保证 CUDA kernel 完全确定。
图像 helper 会在各自文档声明的范围内接受路径、PIL image、array 或 tensor。compose_horizontal_views 和 split_horizontal_views 会遵守指定 view 顺序;应让这个顺序与 artifact 一起保存,不要让后续 reader 猜测。
完整参考
以下为该类别的生成签名。可用本页符号索引跳转;源码链接指向各惰性导出背后的具体实现。
23 个公开符号
class ContentSafetyGuardrail(Protocol)ContentSafetyGuardrail — Interface implemented by prompt and media safety classifiers. 属于 Core 基础能力(registry、通用工具、安全契约)。
方法
is_safe — Return whether `input` is safe and an optional explanation.
参数
inputAny返回值
def cuda_visible_devices_from_device(device: str | torch.device | None,*,inherited: str | None = None,map_inherited: bool = True,default_cuda: str = '0') -> str | None简介
cuda_visible_devices_from_device — Convert a device string into a `CUDA_VISIBLE_DEVICES value. 属于 Core 基础能力(registry、通用工具、安全契约)。 标注返回类型:str | None`。
源码 docstring
Convert a device string into a `CUDA_VISIBLE_DEVICES` value.
`cuda:N is interpreted as a local index into an inherited CUDA_VISIBLE_DEVICES` list by default, which is the behavior expected by subprocess launchers nested under a scheduler or torchrun process.
参数
devicestr | torch.device | Noneinheritedstr | None默认值 Nonemap_inheritedbool默认值 Truedefault_cudastr默认值 '0'返回值
class DuplicateRegistryKeyError()DuplicateRegistryKeyError — Raised when a key or alias maps to multiple different entries. 属于 Core 基础能力(registry、通用工具、安全契约)。
def env_is_true(env_name: str) -> boolenv_is_true — Return whether an environment variable uses a recognized truthy spelling. 属于 Core 基础能力(registry、通用工具、安全契约)。 标注返回类型:bool。
参数
env_namestr返回值
def get_current_torch_device() -> torch.device简介
get_current_torch_device — Return the accelerator selected for this process, or CPU. 属于 Core 基础能力(registry、通用工具、安全契约)。 标注返回类型:torch.device。
源码 docstring
Return the accelerator selected for this process, or CPU.
Distributed launchers select a process-local device with `set_device`. Using that current index avoids collapsing every local rank onto CUDA 0.
返回值
class GuardrailRunner(safety_models: Sequence[ContentSafetyGuardrail] | None = None,generic_block_msg: str = '',generic_safe_msg: str = '',postprocessors: Sequence[PostprocessingGuardrail] | None = None)GuardrailRunner — Run reusable safety classifiers and postprocessors in sequence. 属于 Core 基础能力(registry、通用工具、安全契约)。
参数
generic_block_msgstr默认值 ''generic_safe_msgstr默认值 ''postprocess`.方法
简介
run_safety_check — Run classifiers in order and return the first block or final safe result.
源码 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.
参数
inputAny返回值
postprocess — Apply every configured safety postprocessor to a frame array.
参数
framesnp.ndarray返回值
def normalize_action_values(values: Any,statistics: Mapping[str, Any],*,mode: str = 'min_max',clip: float | None = None) -> np.ndarraynormalize_action_values — Normalize action values using checkpoint statistics along the last axis. 属于 Core 基础能力(registry、通用工具、安全契约)。 标注返回类型:np.ndarray。
参数
valuesAnystatisticsMapping[str, Any]select_modality_statistics.modestr默认值 'min_max'min_max (default), q99 / quantile / bounds, mean_std / standard / zscore, or identity`.clipfloat | None默认值 None[-clip, clip]` after normalization.返回值
float` array with the same shape as *values*.def normalize_registry_key(value: str,*,field_name: str = 'registry key') -> strnormalize_registry_key — Strip and `casefold a user-facing Registry key for case-insensitive lookup. 属于 Core 基础能力(registry、通用工具、安全契约)。 标注返回类型:str`。
参数
valuestrfield_namestr默认值 'registry key'返回值
class PostprocessingGuardrail(Protocol)PostprocessingGuardrail — Interface implemented by safety postprocessors such as face blurring. 属于 Core 基础能力(registry、通用工具、安全契约)。
方法
postprocess — Return safety-processed frames.
参数
framesnp.ndarray返回值
class PromptProcessor()简介
PromptProcessor — Apply optional prompt refiners and extenders without owning model loading. 属于 Core 基础能力(registry、通用工具、安全契约)。
源码 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()`.
方法
load_prompt_refiners(model_source: Any,refiner_classes: Sequence[type[_PromptHookFactory]] = ()) -> None源码load_prompt_refiners — Instantiate each refiner with `from_model_manager(model_source)` and append it.
参数
model_sourceAnyrefiner_classesSequence[type[_PromptHookFactory]]默认值 ()返回值
load_prompt_extenders(model_source: Any,extender_classes: Sequence[type[_PromptHookFactory]] = ()) -> None源码load_prompt_extenders — Instantiate each extender with `from_model_manager(model_source)` and append it.
参数
model_sourceAnyextender_classesSequence[type[_PromptHookFactory]]默认值 ()返回值
process_prompt — Run refiners in order on one prompt or a list; lists are processed recursively.
参数
promptpositivebool默认值 True简介
extend_prompt — Wrap `prompt as {"prompt": ...}` and run extenders in order.
源码 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.
参数
promptstrpositivebool默认值 Trueclass RegistryError()RegistryError — Base class for registry definition errors. 属于 Core 基础能力(registry、通用工具、安全契约)。
class RegistryItem(key: str,value: ItemT,aliases: tuple[str, ...] = (),metadata: Mapping[str, object] = <dict factory>)RegistryItem — One registered item plus its public aliases. 属于 Core 基础能力(registry、通用工具、安全契约)。
属性
keystrvalueItemTaliasestuple[str, ...]默认值 ()metadataMapping[str, object]默认值 <dict factory>def resolve_inference_device(device: str | torch.device | None = 'cuda',*,allow_cpu_fallback: bool = False) -> str简介
resolve_inference_device — Resolve a concrete inference device without silently selecting the wrong GPU. 属于 Core 基础能力(registry、通用工具、安全契约)。 标注返回类型:str。
源码 docstring
Resolve a concrete inference device without silently selecting the wrong GPU.
Bare `cuda resolves to the CUDA device currently selected for this process. Explicit indices are preserved, which is important when a caller deliberately selects (for example) cuda:4` under an eight-GPU workspace.
参数
devicestr | torch.device | None默认值 'cuda'allow_cpu_fallbackbool默认值 False返回值
def resolve_inference_dtype(device: str | torch.device,dtype: str | torch.dtype | None = 'auto',*,strict: bool = True) -> torch.dtype简介
resolve_inference_dtype — Resolve an inference dtype using the selected accelerator's capability. 属于 Core 基础能力(registry、通用工具、安全契约)。 标注返回类型:torch.dtype。
源码 docstring
Resolve an inference dtype using the selected accelerator's capability.
`auto` selects bf16 on Ampere/Hopper/Blackwell-or-newer CUDA devices, fp16 on older CUDA devices, and fp32 on CPU. Explicit unsupported bf16 is rejected in strict mode instead of producing a later kernel failure.
参数
devicestr | torch.devicedtypestr | torch.dtype | None默认值 'auto'strictbool默认值 True返回值
def select_modality_statistics(dataset_statistics: Mapping[str, Any],*,modality: str = 'action',key: str | None = None) -> tuple[str | None, Mapping[str, Any]]简介
select_modality_statistics — Select one modality's statistics from a checkpoint statistics mapping. 属于 Core 基础能力(registry、通用工具、安全契约)。 标注返回类型:tuple[str | None, Mapping[str, Any]]。
源码 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.
参数
dataset_statisticsMapping[str, Any]modalitystr默认值 'action'"action"`).keystr | None默认值 None返回值
(resolved_key, stats_mapping). *resolved_key* is None` when the input was already a direct statistics mapping.异常
KeyError- *key* is missing or has no *modality* mapping.
ValueError- Empty input, or multiple dataset keys with no *key*.
def set_random_seed(seed: int,by_rank: bool = False) -> int简介
set_random_seed — Set Python, NumPy, and PyTorch seeds with optional distributed-rank offset. 属于 Core 基础能力(registry、通用工具、安全契约)。 标注返回类型:int。
源码 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.
参数
seedintby_rankbool默认值 False返回值
def stack_or_pad_tensors(values: Sequence[Tensor],*,padding_value: float | int | bool | None = None) -> Tensor简介
stack_or_pad_tensors — Stack equal tensors or right-pad variable-length 1-D tensors. 属于 Core 基础能力(registry、通用工具、安全契约)。 标注返回类型:Tensor。
源码 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.
参数
valuesSequence[Tensor]padding_valuefloat | int | bool | None默认值 None返回值
class TypedRegistry(items: Iterable[RegistryItem[ItemT]] = ())简介
TypedRegistry — Deterministic keyed registry with alias support. 属于 Core 基础能力(registry、通用工具、安全契约)。
源码 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.
参数
异常
DuplicateRegistryKeyError- On conflicting keys or aliases.
UnknownRegistryKeyError- When a lookup cannot be resolved.
方法
register(key: str,value: ItemT,*,aliases: Iterable[str] = (),metadata: Mapping[str, object] | None = None) -> RegistryItem[ItemT]源码register — Register an item and return the normalized registry record.
参数
keystrvalueItemTaliasesIterable[str]默认值 ()metadataMapping[str, object] | None默认值 None返回值
keys — Return canonical keys in deterministic order.
返回值
aliases — Return normalized alias to normalized canonical key mapping.
返回值
items — Return registry items sorted by their public key.
返回值
values — Return registered values sorted by public key.
返回值
class UnknownRegistryKeyError()UnknownRegistryKeyError — Raised when a registry lookup cannot be resolved. 属于 Core 基础能力(registry、通用工具、安全契约)。
def unnormalize_action_values(normalized_values: Any,statistics: Mapping[str, Any],*,mode: str = 'min_max') -> np.ndarray简介
unnormalize_action_values — Convert normalized policy outputs back to environment-space actions. 属于 Core 基础能力(registry、通用工具、安全契约)。 标注返回类型:np.ndarray。
源码 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.
参数
normalized_valuesAnystatisticsMapping[str, Any]modestr默认值 'min_max'