File size: 14,084 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
"""Export portable reference fixtures and compare the native Swift SDK without MLX or Torch."""
from __future__ import annotations

import argparse
from datetime import datetime, timezone
import json
from pathlib import Path
import platform
import subprocess
import sys
import time

import numpy as np

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))
from scripts.validate import (
    ACCEPTANCE, FIXTURE_NAMES, VARIANTS, atomic_json, compare, fixture_signature,
    fixtures, load_reference_cache, sha256,
)

DTYPES = {"float32": np.dtype("<f4"), "float64": np.dtype("<f8"), "bool": np.dtype("u1")}


def default_executable(name):
    candidates = [ROOT / ".build" / folder / name for folder in (
        "release", "debug", "out/Products/Release", "out/Products/Debug",
        "arm64-apple-macosx/release", "arm64-apple-macosx/debug",
    )]
    return next((path for path in candidates if path.is_file()), candidates[0])


def run_cli(command, timeout):
    """Keep child workers in the outer guard's process group and clean them on timeout."""
    import psutil

    process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
    try:
        stdout, stderr = process.communicate(timeout=timeout)
        return subprocess.CompletedProcess(command, process.returncode, stdout, stderr)
    except BaseException:
        try:
            parent = psutil.Process(process.pid)
            children = parent.children(recursive=True)
            for child in children + [parent]:
                try:
                    child.terminate()
                except psutil.NoSuchProcess:
                    pass
            _, alive = psutil.wait_procs(children + [parent], timeout=3)
            for child in alive:
                try:
                    child.kill()
                except psutil.NoSuchProcess:
                    pass
        except psutil.NoSuchProcess:
            pass
        process.communicate(timeout=3)
        raise


def write_tensor(base, relative, array):
    dtype = "float64" if array.dtype == np.float64 else "bool" if array.dtype == bool else "float32"
    converted = np.ascontiguousarray(array, dtype=DTYPES[dtype])
    path = base / relative
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_bytes(converted.tobytes())
    return dict(file=relative, dtype=dtype, shape=list(converted.shape), bytes=path.stat().st_size, sha256=sha256(path))


def read_tensor(base, descriptor):
    path = (base / descriptor["file"]).resolve()
    if not path.is_relative_to(base.resolve()) or not path.is_file():
        raise ValueError("Tensor path must resolve to a file inside its fixture/result directory")
    dtype = DTYPES.get(descriptor.get("dtype"))
    shape = descriptor.get("shape")
    if dtype is None or not isinstance(shape, list) or not shape or any(not isinstance(v, int) or v < 1 for v in shape):
        raise ValueError("Invalid portable tensor dtype or shape")
    count = 1
    for dimension in shape:
        count *= dimension
    if count * dtype.itemsize != descriptor.get("bytes") or path.stat().st_size != descriptor["bytes"]:
        raise ValueError("Portable tensor byte count does not match shape")
    if sha256(path) != descriptor.get("sha256"):
        raise ValueError("Portable tensor checksum mismatch")
    result = np.fromfile(path, dtype=dtype).reshape(shape)
    return result.astype(bool) if descriptor["dtype"] == "bool" else result


def export_fixtures(output, source, cache, upstream):
    output.mkdir(parents=True, exist_ok=True)
    entries = []
    for case in fixtures(extended=True):
        name, context, horizon, kwargs = case
        directory = output / name
        directory.mkdir(exist_ok=True)
        inputs = {"context": write_tensor(directory, "context.f32le", context)}
        options = {"symmetric_averaging": True, "clip_nonnegative": True}
        for key, value in kwargs.items():
            if isinstance(value, np.ndarray):
                extension = "f64le" if value.dtype == np.float64 else "f32le"
                inputs[key] = write_tensor(directory, f"{key}.{extension}", value)
            else:
                options[key] = value
        references, provenance = {}, {}
        for variant in VARIANTS:
            arrays, metadata = load_reference_cache(cache, variant, case, source, upstream)
            if set(arrays) != {"mlx", "upstream"}:
                raise ValueError("Swift fixtures require both MLX and original Mistral reference arrays")
            references[variant] = {
                key: write_tensor(directory, f"references/{variant}/{key}.f32le", value)
                for key, value in arrays.items()
            }
            provenance[variant] = metadata
        quantile_levels = json.loads((source / "5M" / "config.json").read_text())["quantile_knots"]
        document = dict(
            format="timestral-swift-fixture-v1", name=name, prediction_length=horizon,
            inputs=inputs, options=options, quantile_levels=quantile_levels,
            references=references, provenance=provenance, original_fixture_signature=fixture_signature(case),
        )
        path = directory / "fixture.json"
        atomic_json(path, document)
        entries.append(dict(name=name, file=path.relative_to(output).as_posix(), sha256=sha256(path)))
    manifest = dict(format="timestral-swift-fixtures-v1", byte_order="little-endian",
                    variants=list(VARIANTS), fixtures=entries, exporter_sha256=sha256(Path(__file__)),
                    notes="Binary FP32/FP64 arrays preserve NaNs and large categorical codes; no reference models loaded during export.")
    atomic_json(output / "manifest.json", manifest)
    return manifest


def swift_provenance(root, binary, worker, bundle):
    files = sorted((root / "swift" / "Sources").rglob("*.swift"))
    files += [root / "Package.swift"]
    return dict(
        swift_sources_sha256={path.relative_to(root).as_posix(): sha256(path) for path in files},
        binary_sha256=sha256(binary), worker_binary_sha256=sha256(worker),
        bundle_manifest_sha256=sha256(bundle / "manifest.json"),
        validator_sha256=sha256(Path(__file__)),
    )


def validate_variant(variant, *, root, bundles, fixture_root, binary, worker, reports,
                     selected=None, timeout=120, cache_mode="adaptive"):
    manifest = json.loads((fixture_root / "manifest.json").read_text())
    if manifest.get("format") != "timestral-swift-fixtures-v1":
        raise ValueError("Unsupported Swift fixture collection")
    entries = {entry["name"]: entry for entry in manifest["fixtures"]}
    names = list(dict.fromkeys(selected)) if selected else list(FIXTURE_NAMES)
    before = swift_provenance(root, binary, worker, bundles / variant)
    report = dict(
        format="timestral-swift-validation-v1", variant=variant, status="running", passed=False,
        requested_fixtures=names, expected_fixture_count=len(names), acceptance=ACCEPTANCE,
        started_at=datetime.now(timezone.utc).isoformat(), provenance=before,
        fixture_manifest_sha256=sha256(fixture_root / "manifest.json"),
        environment={"platform": platform.platform(), "python": platform.python_version(),
                     "swift": subprocess.check_output(["xcrun", "swift", "--version"], text=True).strip()},
        fixtures=[], notes="Native Swift preprocessing, queue/worker runtime, and postprocessing; compared against both pinned references. Timings are not controlled benchmarks.",
    )

    def checkpoint(status=None, **updates):
        if status:
            report["status"] = status
        report.update(updates, updated_at=datetime.now(timezone.utc).isoformat())
        report["provenance_after"] = swift_provenance(root, binary, worker, bundles / variant)
        unchanged = report["provenance_after"] == before
        done = {record["fixture"] for record in report["fixtures"] if record.get("passed")}
        report["provenance_unchanged"] = unchanged
        report["passed"] = bool(report["status"] == "completed" and unchanged and done == set(names))
        report["extended_suite_passed"] = bool(report["passed"] and done == set(FIXTURE_NAMES))
        report["completed_fixture_count"] = len(report["fixtures"])
        atomic_json(reports / f"{variant}.json", report)

    status = "failed"
    checkpoint(active_fixture=None)
    try:
        for name in names:
            entry = entries[name]
            fixture_path = fixture_root / entry["file"]
            if sha256(fixture_path) != entry["sha256"]:
                raise ValueError(f"Fixture manifest checksum mismatch: {name}")
            fixture = json.loads(fixture_path.read_text())
            # Validate raw input bytes before handing them to the native executable.
            for descriptor in fixture["inputs"].values():
                read_tensor(fixture_path.parent, descriptor)
            result_path = reports / "outputs" / variant / name / "forecast.json"
            result_path.parent.mkdir(parents=True, exist_ok=True)
            checkpoint(active_fixture=name)
            command = [str(binary), "validate-fixture", "--bundle", str(bundles / variant),
                       "--fixture", str(fixture_path), "--output", str(result_path),
                       "--worker", str(worker), "--cache-mode", cache_mode]
            started = time.perf_counter()
            completed = run_cli(command, timeout)
            if completed.returncode:
                raise RuntimeError(f"Swift CLI exited {completed.returncode}: {completed.stderr[-4000:]}")
            result = json.loads(result_path.read_text())
            if result.get("format") != "timestral-swift-forecast-v1":
                raise ValueError("Swift CLI returned an unsupported result format")
            actual = read_tensor(result_path.parent, result["quantiles"])
            if result["quantile_levels"] != fixture["quantile_levels"]:
                raise ValueError("Swift quantile levels differ from the reference")
            comparisons = {
                backend: compare(actual, read_tensor(fixture_path.parent, descriptor))
                for backend, descriptor in fixture["references"][variant].items()
            }
            passed = set(comparisons) == {"mlx", "upstream"} and all(v["passed"] for v in comparisons.values())
            record = dict(fixture=name, passed=passed, seconds=time.perf_counter() - started,
                          comparisons=comparisons, swift_result=result,
                          fixture_sha256=entry["sha256"], result_sha256=sha256(result_path))
            report["fixtures"].append(record)
            checkpoint(active_fixture=None)
            print(f"{variant} / {name}: passed={passed}, nRMSE={comparisons['upstream'].get('normalized_rmse')}", flush=True)
            if not passed:
                report["error"] = "Stopped at the first numerical failure"
                break
        else:
            status = "completed"
    except BaseException as exc:
        report["error"] = f"{type(exc).__name__}: {exc}"
        status = "interrupted" if isinstance(exc, KeyboardInterrupt) else "failed"
        checkpoint(status)
        if isinstance(exc, (KeyboardInterrupt, SystemExit)):
            raise
    checkpoint(status, active_fixture=None)
    return report["passed"]


def parse_args(argv=None):
    parser = argparse.ArgumentParser(description=__doc__)
    subparsers = parser.add_subparsers(dest="command", required=True)
    export = subparsers.add_parser("export-fixtures", help="Export the nine fixtures and both references as portable binary tensors")
    export.add_argument("--output", type=Path, default=ROOT / "swift" / "Fixtures")
    export.add_argument("--source", type=Path, default=ROOT.parent / "timestral-mlx")
    export.add_argument("--reference-cache", type=Path, default=ROOT / "reports" / "reference-cache")
    export.add_argument("--upstream", type=Path, default=ROOT / "references" / "timestral-a534bdbd244d64f3d9d743f46c7bf6fc330ed4ff" / "src")
    validate = subparsers.add_parser("validate", help="Run the compiled Swift CLI and compare both portable references")
    validate.add_argument("variants", nargs="*", metavar="VARIANT")
    validate.add_argument("--binary", type=Path, default=default_executable("timestral"))
    validate.add_argument("--worker", type=Path, default=default_executable("timestral-worker"))
    validate.add_argument("--bundles", type=Path, default=ROOT)
    validate.add_argument("--fixture-root", type=Path, default=ROOT / "swift" / "Fixtures")
    validate.add_argument("--reports", type=Path, default=ROOT / "reports" / "swift")
    validate.add_argument("--fixture", action="append", choices=FIXTURE_NAMES)
    validate.add_argument("--timeout", type=float, default=120)
    validate.add_argument("--cache-mode", choices=("adaptive", "warm", "sequential"), default="adaptive")
    args = parser.parse_args(argv)
    if args.command == "validate":
        args.variants = args.variants or list(VARIANTS)
        if any(variant not in VARIANTS for variant in args.variants):
            parser.error(f"variants must be selected from {', '.join(VARIANTS)}")
    return args


def main():
    args = parse_args()
    if args.command == "export-fixtures":
        result = export_fixtures(args.output, args.source, args.reference_cache, args.upstream)
        print(f"Exported {len(result['fixtures'])} portable fixtures with all four variants' references to {args.output}")
        return
    for variant in args.variants:
        passed = validate_variant(
            variant, root=ROOT, bundles=args.bundles.resolve(), fixture_root=args.fixture_root.resolve(),
            binary=args.binary.resolve(), worker=args.worker.resolve(), reports=args.reports.resolve(),
            selected=args.fixture, timeout=args.timeout, cache_mode=args.cache_mode,
        )
        if not passed:
            raise SystemExit("Swift validation failed; inspect the checkpointed report")


if __name__ == "__main__":
    main()