Download tests/test_loading.py from jbilcke/timestral-coreai: direct link, hf CLI and curl.
- Browser
- Download file 5.02 kB
-
https://huggingface.co/jbilcke/timestral-coreai/resolve/main/tests/test_loading.py
- Command line
-
hf download hf://jbilcke/timestral-coreai/tests/test_loading.py
-
curl -L -o test_loading.py https://huggingface.co/jbilcke/timestral-coreai/resolve/main/tests/test_loading.py
5.02 kB
| """Bundle integrity and dependency boundaries, without compiling a model.""" | |
| import json | |
| import subprocess | |
| import sys | |
| from pathlib import Path | |
| import pytest | |
| from timestral_coreai import TimestralConfig | |
| from timestral_coreai.loading import BUNDLE_FORMAT, GRAPH_INPUTS, STAGE_INPUTS, STAGE_OUTPUTS, load_model, sha256, verify_bundle | |
| def bundle(tmp_path): | |
| config = TimestralConfig(patch_size=4, d_model=16, num_heads=2, mlp_hidden_dim=32, | |
| block_schedule=("time",), quantile_knots=(.1, .5, .9), | |
| block_size=2, context_length=16) | |
| stages = {"embedding": "embedding.aimodel", "blocks": ["block_000.aimodel"], "head": "head.aimodel"} | |
| for name in ("embedding.aimodel", "block_000.aimodel", "head.aimodel"): | |
| (tmp_path / name).mkdir() | |
| contents = {"config.json": json.dumps(config.to_dict()), "LICENSE": "test license", | |
| **{name + "/program": "test graph" for name in ("embedding.aimodel", "block_000.aimodel", "head.aimodel")}} | |
| for name, content in contents.items(): | |
| (tmp_path / name).write_text(content) | |
| manifest = dict(format=BUNDLE_FORMAT, dtype="float32", variant="5M", function="main", | |
| inputs=GRAPH_INPUTS, outputs=["quantiles"], stages=stages, | |
| stage_inputs=STAGE_INPUTS, stage_outputs=STAGE_OUTPUTS, | |
| shape_limits=dict(max_rows=256, max_context_patches=1056, max_forecast_patches=1056), files={ | |
| name: dict(bytes=(tmp_path / name).stat().st_size, | |
| sha256=sha256(tmp_path / name)) | |
| for name in contents}) | |
| (tmp_path / "manifest.json").write_text(json.dumps(manifest)) | |
| return tmp_path | |
| def test_manifest_checks_actual_bytes(bundle): | |
| path, manifest = verify_bundle(bundle, variant="5M") | |
| assert path == bundle.resolve() | |
| assert manifest["variant"] == "5M" | |
| (bundle / "block_000.aimodel/program").write_text("fake graph") # Same byte length. | |
| with pytest.raises(ValueError, match="integrity"): | |
| verify_bundle(bundle) | |
| def test_rejects_unverified_model_files(bundle): | |
| (bundle / "head.aimodel/extra").write_text("unlisted") | |
| with pytest.raises(ValueError, match="unverified"): | |
| verify_bundle(bundle) | |
| def test_rejects_escaping_manifest_path(bundle): | |
| manifest = json.loads((bundle / "manifest.json").read_text()) | |
| manifest["files"]["../outside"] = dict(bytes=0, sha256="") | |
| (bundle / "manifest.json").write_text(json.dumps(manifest)) | |
| with pytest.raises(ValueError, match="Unsafe"): | |
| verify_bundle(bundle) | |
| def test_rejects_variant_mismatch(bundle): | |
| with pytest.raises(ValueError, match="variant"): | |
| verify_bundle(bundle, variant="28M") | |
| def test_staged_manifest_contract(bundle, mutation, match): | |
| manifest = json.loads((bundle / "manifest.json").read_text()) | |
| mutation(manifest) | |
| (bundle / "manifest.json").write_text(json.dumps(manifest)) | |
| with pytest.raises(ValueError, match=match): | |
| verify_bundle(bundle) | |
| def test_rejects_symlinked_asset_directory_even_inside_bundle(bundle): | |
| (bundle / "head.aimodel/alias").symlink_to(bundle / "embedding.aimodel", target_is_directory=True) | |
| with pytest.raises(ValueError, match="symlinks"): | |
| verify_bundle(bundle) | |
| def test_stage_count_must_match_verified_config(bundle): | |
| config = json.loads((bundle / "config.json").read_text()) | |
| config["block_schedule"].append("time") | |
| (bundle / "config.json").write_text(json.dumps(config)) | |
| manifest = json.loads((bundle / "manifest.json").read_text()) | |
| manifest["files"]["config.json"] = dict(bytes=(bundle / "config.json").stat().st_size, sha256=sha256(bundle / "config.json")) | |
| (bundle / "manifest.json").write_text(json.dumps(manifest)) | |
| with pytest.raises(ValueError, match="stage count"): | |
| verify_bundle(bundle) | |
| def test_missing_local_bundle_is_not_downloaded(tmp_path): | |
| with pytest.raises(FileNotFoundError, match="Local"): | |
| load_model(tmp_path / "not-a-bundle") | |
| def test_package_import_does_not_import_training_or_mlx_runtimes(): | |
| root = Path(__file__).resolve().parents[1] | |
| result = subprocess.run( | |
| [sys.executable, "-c", "import sys; import timestral_coreai; " | |
| "assert 'torch' not in sys.modules; assert 'mlx.core' not in sys.modules; " | |
| "assert 'coreai_torch' not in sys.modules; assert 'coreai' not in sys.modules"], | |
| cwd=root, capture_output=True, text=True, | |
| ) | |
| assert result.returncode == 0, result.stdout + result.stderr | |