Files
chenlin c32df70326
Checks / Layout, translation and Colab checks (push) Has been cancelled
Checks / Ruff (push) Has been cancelled
Checks / Framework tests (native backend, CPU) (push) Has been cancelled
Checks / Portable Rust controller (push) Has been cancelled
release: v0.1 generic robot training and application platform
2026-11-05 11:05:01 +08:00

33 lines
1.3 KiB
Python

"""Open policy graphs whose data is contained in the graph itself."""
from pathlib import Path
TORCHSCRIPT_SUFFIXES = frozenset({".pt", ".ts", ".jit", ".torchscript"})
SUPPORTED_POLICY_SUFFIXES = TORCHSCRIPT_SUFFIXES | {".onnx"}
def read_embedded_onnx(path: Path):
"""Reject file-backed tensors before a runtime can follow their references."""
import onnx
data = path.read_bytes()
model = onnx.load_model_from_string(data)
pending = [model]
while pending:
message = pending.pop()
if isinstance(message, onnx.TensorProto):
if message.external_data or message.data_location == onnx.TensorProto.EXTERNAL:
raise ValueError("Actor must store all tensor data inside the ONNX file")
# Protobuf traversal also covers sparse tensors, nested graphs, local
# functions, training graphs and tensors on Constant attributes.
for field, value in message.ListFields():
if field.message_type is not None:
repeated = getattr(field, "is_repeated", None)
if repeated is None:
repeated = field.label == field.LABEL_REPEATED
if repeated:
pending.extend(value)
else:
pending.append(value)
return data, model