Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(ts): require safetensors for ONNX export

HDDc49557c07ea9
diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml
index 46e86b10..1e5662dc 100644
--- a/.github/workflows/security.yml
+++ b/.github/workflows/security.yml
@@ -85,11 +85,11 @@ jobs:
# torch.load / pickle execute arbitrary code during deserialization. Laya loads
# checkpoints from the Hub, so a pickle path would be remote code execution on
# `laya.load("someone/their-model")`.
- if grep -rnE "\bpickle\b|torch\.load\(|joblib\.load\(|yaml\.load\(" laya/; then
+ if grep -rnE --include='*.py' "\bpickle\b|torch\.load\(|joblib\.load\(|yaml\.load\(" laya/ laya-ts/scripts/; then
echo "::error::unsafe deserialization in the shipped package - use safetensors"
exit 1
fi
- echo "OK: no pickle/torch.load/yaml.load in laya/"
+ echo "OK: no unsafe deserialization in laya/ or laya-ts/scripts/"
- name: No shell-out or dynamic exec in the shipped package
run: |
if grep -rnE "os\.system\(|subprocess\.|shell=True|\beval\(|\bexec\(" laya/ \
@@ -98,3 +98,5 @@ jobs:
exit 1
fi
echo "OK: no os.system/subprocess/eval/exec in laya/"
+ - name: ONNX exporter requires safetensors weights
+ run: python tests/test_export_onnx_safety.py
diff --git a/laya-ts/scripts/export_onnx.py b/laya-ts/scripts/export_onnx.py
index b1b2b9d2..d82f7f28 100644
--- a/laya-ts/scripts/export_onnx.py
+++ b/laya-ts/scripts/export_onnx.py
@@ -61,6 +61,14 @@ def _ensure_tokenizer_json(model_dir):
shutil.copy(src, dest)
+def _checkpoint_weights_path(model_dir):
+ """Require the checkpoint's safetensors weights before building a model."""
+ path = os.path.join(model_dir, "model.safetensors")
+ if not os.path.isfile(path):
+ raise SystemExit("checkpoint is missing model.safetensors: %s" % model_dir)
+ return path
+
+
def parse_args(argv=None):
p = argparse.ArgumentParser(description=__doc__)
src = p.add_mutually_exclusive_group(required=True)
@@ -81,6 +89,7 @@ def main(argv=None):
if build_model is None:
raise SystemExit("export needs torch + laya installed: pip install torch transformers onnx onnxruntime")
model_dir = args.model_dir or _download(args.repo or DEFAULT_REPO, args.subfolder, args.token)
+ weights_path = _checkpoint_weights_path(model_dir)
_ensure_tokenizer_json(model_dir)
import torch
@@ -132,17 +141,9 @@ def main(argv=None):
enc_dir = os.path.join(model_dir, "encoder")
model = build_model(cfg, encoder_dir=enc_dir if os.path.exists(enc_dir) else None,
pretrained=False)
- try:
- from safetensors.torch import load_file as _load_safetensors
-
- for name in ("model.safetensors", "pytorch_model.bin"):
- path = os.path.join(model_dir, name)
- if os.path.exists(path):
- state = _load_safetensors(path) if name.endswith(".safetensors") else torch.load(path, map_location="cpu")
- model.load_state_dict(state, strict=False)
- break
- except ImportError:
- pass
+ from safetensors.torch import load_file as _load_safetensors
+
+ model.load_state_dict(_load_safetensors(weights_path), strict=False)
model.eval().float()
# Batch must stay symbolic: tracing with batch=1 lets dynamo bake batch=1
diff --git a/tests/test_export_onnx_safety.py b/tests/test_export_onnx_safety.py
new file mode 100644
index 00000000..feaa98cb
--- /dev/null
+++ b/tests/test_export_onnx_safety.py
@@ -0,0 +1,28 @@
+"""The shipped ONNX exporter must refuse checkpoints without safe weights."""
+
+import importlib.util
+import tempfile
+from pathlib import Path
+
+
+root = Path(__file__).resolve().parents[1]
+spec = importlib.util.spec_from_file_location(
+ "laya_ts_export_onnx", root / "laya-ts" / "scripts" / "export_onnx.py"
+)
+exporter = importlib.util.module_from_spec(spec)
+spec.loader.exec_module(exporter)
+
+
+with tempfile.TemporaryDirectory() as directory:
+ model_dir = Path(directory)
+ (model_dir / "pytorch_model.bin").write_bytes(b"legacy weights must never be loaded")
+ try:
+ exporter._checkpoint_weights_path(str(model_dir))
+ except SystemExit as error:
+ assert "model.safetensors" in str(error)
+ else:
+ raise AssertionError("a legacy .bin checkpoint must be rejected")
+
+ weights = model_dir / "model.safetensors"
+ weights.touch()
+ assert exporter._checkpoint_weights_path(str(model_dir)) == str(weights)