File size: 2,906 Bytes
506f40d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""Smoke-test an installed wheel and an explicitly selected local model bundle."""
import argparse
import hashlib
import importlib.metadata
import importlib.util
import json
from pathlib import Path
import sys
import zipfile

import numpy as np
import timestral_coreai
from timestral_coreai import TimestralForecaster


def digest(path):
    return hashlib.sha256(path.read_bytes()).hexdigest()


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--bundle", type=Path, required=True)
    parser.add_argument("--wheel", type=Path, required=True)
    parser.add_argument("--report", type=Path, required=True)
    args = parser.parse_args()
    package = Path(timestral_coreai.__file__).resolve().parent
    assert "site-packages" in package.parts, package
    assert importlib.util.find_spec("torch") is None
    assert importlib.util.find_spec("mlx") is None
    source_hashes = {p.name: digest(p) for p in sorted(package.glob("*.py"))}
    with zipfile.ZipFile(args.wheel) as wheel:
        wheel_hashes = {Path(name).name: hashlib.sha256(wheel.read(name)).hexdigest()
                        for name in wheel.namelist() if name.startswith("timestral_coreai/") and name.endswith(".py")}
    assert wheel_hashes == source_hashes, "Installed package does not match the selected wheel"
    context = np.sin(np.arange(512, dtype=np.float32) * (2 * np.pi / 24))
    context[31:35] = np.nan
    with TimestralForecaster.from_pretrained(args.bundle.resolve(), compute_unit="cpu") as model:
        result = model.forecast(context, 96).quantiles
        assert result.shape == (1, 1, 96, 21) and result.dtype == np.float32
        assert np.isfinite(result).all() and (np.diff(result, axis=-1) >= 0).all()
        backend, policy = model.model.runtime.backend, model.model.runtime.compute_unit
    assert "torch" not in sys.modules and "mlx.core" not in sys.modules
    assert "coreai" not in sys.modules, "Native runtime must remain in the child process"
    report = dict(
        passed=True, installed_package=str(package), bundle=str(args.bundle.resolve()),
        backend=backend, compute_unit=policy, shape=list(result.shape), finite=True, monotone=True,
        torch_installed=False, mlx_installed=False, coreai_imported_in_parent=False,
        versions={name: importlib.metadata.version(name) for name in ("timestral-coreai", "numpy", "coreai-core")},
        package_sha256=source_hashes, bundle_manifest_sha256=digest(args.bundle / "manifest.json"),
        wheel=str(args.wheel), wheel_sha256=digest(args.wheel), installed_sources_match_wheel=True,
    )
    args.report.parent.mkdir(parents=True, exist_ok=True)
    args.report.write_text(json.dumps(report, indent=2) + "\n")
    print(json.dumps({key: report[key] for key in ("passed", "shape", "backend", "compute_unit", "torch_installed", "mlx_installed")}))


if __name__ == "__main__":
    main()