File size: 1,997 Bytes
7f50cd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Task entry point: SIGN feature computation and saved-model inference."""
from __future__ import annotations

import time
STARTED = time.monotonic()

import json
import os
from pathlib import Path
import resource
import sys

import numpy as np

sys.path.insert(0, os.environ["CORE_DIR"])
from interface.data import read, write
from graph import sign_features
from training import HOPS, predict
from checkpoint import load


def main():
    meta, arrays = read(sys.argv[1])
    query = np.asarray(arrays["query"], dtype=np.int64)
    if not len(query):
        write(sys.argv[2], query, np.empty(0, dtype=np.int64))
        return
    model, metadata = load(Path(__file__).resolve().parent / "model.npz")
    if metadata["input_width"] != arrays["features"].shape[1]:
        raise ValueError("Input feature width differs from the fitted SIGN model")
    if int(meta["classes"]) > metadata["classes"]:
        raise ValueError("Input label domain exceeds the fitted SIGN model")
    features = sign_features(meta, arrays, hops=HOPS)
    preprocessing_seconds = time.monotonic() - STARTED
    del arrays
    predictions = predict(model, features, query, classes=int(meta["classes"]))
    write(sys.argv[2], query, predictions)
    report = {
        "method": "DGL SIGN; saved-model inference",
        "checkpoint_selected_epoch": metadata["training"]["selected_epoch"],
        "checkpoint_reduced_data_smoke": metadata["training"]["reduced_data_smoke"],
        "preprocessing_seconds": preprocessing_seconds,
        "elapsed_seconds": time.monotonic() - STARTED,
        "threads": int(os.environ.get("OPENBLAS_NUM_THREADS", "4")),
        "peak_rss_kib": resource.getrusage(resource.RUSAGE_SELF).ru_maxrss,
        "query_count": len(query), "optimizer_steps_at_prediction": 0,
    }
    (Path(sys.argv[2]) / "inference_report.json").write_text(
        json.dumps(report, indent=2) + "\n", encoding="utf-8")
    print(json.dumps(report), flush=True)


if __name__ == "__main__":
    main()