33 lines
1.3 KiB
Python
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
|