aboutsummaryrefslogtreecommitdiff
path: root/tests/check_workspace_api.py
blob: 40e51b635efe7c21d94e843afdc74a561db216d8 (plain) (blame)
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
#!/usr/bin/env python3
"""Run the workspace/model API smoke test with real weights.

The C test is tests/test_workspace.c, the same source `make test` builds with
the synthetic fixture. With MODEL_DIR, it loads real weights from that directory
instead and exercises the workspace and batch APIs.

Requires only python3 and a model directory with vocab.json; no reference stack.

For mean-pooled models, also checks that:
- a whole-sequence span pools to the embedding
- half-spans recombine by token-count weighting

For last-token/CLS models, these mean-span checks are skipped because sentence
embedding and contextual span APIs intentionally pool different rows.

Usage:
  tests/check_workspace_api.py --model-dir DIR
"""

import argparse
import os
import subprocess
import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
DRIVER = ROOT / "tests/test_workspace"


def ensure_driver(cc: str) -> None:
    """Build tests/test_workspace via the Makefile so a C change is picked up."""
    proc = subprocess.run(["make", "-C", "tests", f"CC={cc}", "test_workspace"],
                          cwd=ROOT, text=True, stdout=subprocess.PIPE,
                          stderr=subprocess.STDOUT, check=False)
    if proc.returncode != 0:
        sys.stderr.write(proc.stdout)
        raise SystemExit(proc.returncode)


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--model-dir", required=True)
    ap.add_argument("--cc", default=os.environ.get("CC", "cc"))
    args = ap.parse_args()
    ensure_driver(args.cc)
    return subprocess.run([str(DRIVER), args.model_dir], cwd=ROOT).returncode


if __name__ == "__main__":
    raise SystemExit(main())