AQIT 0.1.0
Loading...
Searching...
No Matches
data.py
Go to the documentation of this file.
1# Copyright (c) 2025-present Aquin Labs Private Limited. All Rights Reserved.
2# This file is part of the Aquin Engine. Unauthorized copying, modification,
3# distribution, or use of this file, via any medium, is strictly prohibited.
4# Proprietary and confidential. See LICENSE for terms.
5
6"""Data — activation capture / replay, feature compare."""
7
8from __future__ import annotations
9
10import json
11from pathlib import Path
12from typing import Any
13
14from aquin.sdk._runtime import invoke_direct, prepare_compute
15
16
17def _load_probes(probes: Any) -> list[dict[str, Any]]:
18 if probes is None:
19 return []
20 if isinstance(probes, list):
21 return list(probes)
22 path = Path(str(probes)).expanduser()
23 text = path.read_text(encoding="utf-8")
24 if path.suffix == ".jsonl":
25 rows = []
26 for line in text.splitlines():
27 line = line.strip()
28 if not line:
29 continue
30 row = json.loads(line)
31 rows.append(row if isinstance(row, dict) else {"text": str(row)})
32 return rows
33 data = json.loads(text)
34 if isinstance(data, list):
35 return [r if isinstance(r, dict) else {"text": str(r)} for r in data]
36 if isinstance(data, dict) and "probes" in data:
37 return list(data["probes"])
38 raise ValueError(f"Unsupported probes file: {path}")
39
40
42 *,
43 output_dir: str | Path,
44 probes: Any = None,
45 model_id: str | None = None,
46 **kwargs: Any,
47) -> dict[str, Any]:
48 """Record activations (+ metadata) into ``output_dir``."""
49 from aquin.compute.activation_capture import run_capture_activations
50 from aquin.compute.model_loader import get_active_model_id
51
52 mid = model_id or get_active_model_id()
53 if not mid:
54 raise ValueError("No model loaded. Call aquin.sdk.session.load_model(...) first.")
55 prepare_compute(model_id=mid)
56 probe_rows = _load_probes(probes)
57 if not probe_rows:
58 raise ValueError("probes= is required (list of dicts or path to json/jsonl).")
59 return invoke_direct(
60 run_capture_activations,
61 mid,
62 probe_rows,
63 output_dir,
64 command="activations capture",
65 tool_name="run_capture_activations",
66 **kwargs,
67 )
68
69
71 path: str | Path,
72 *,
73 filter_expr: str | None = None,
74 group_by: str | None = None,
75 limit: int | None = None,
76) -> dict[str, Any]:
77 """Slice a saved capture by metadata without re-running the model."""
78 from aquin.compute.activation_replay import replay_capture
79
80 return invoke_direct(
81 replay_capture,
82 path,
83 command="activations replay",
84 tool_name="activations_replay",
85 filter_expr=filter_expr,
86 group_by=group_by,
87 limit=limit,
88 )
89
90
91def features_compare(**kwargs: Any) -> dict[str, Any]:
92 from aquin.features_cli import cmd_features
93
94 argv: list[str] = ["compare"]
95 for key, flag in (
96 ("from_dir", "--from"),
97 ("source", "--from"),
98 ("group", "--group"),
99 ("encode_sae", "--encode-sae"),
100 ):
101 if kwargs.get(key) is not None:
102 argv.extend([flag, str(kwargs[key])])
103 cmd_features(argv)
104 return {"ok": True}
dict[str, Any] replay_activations(str|Path path, *, str|None filter_expr=None, str|None group_by=None, int|None limit=None)
Definition data.py:80
dict[str, Any] features_compare(**Any kwargs)
Definition data.py:95
list[dict[str, Any]] _load_probes(Any probes)
Definition data.py:21
dict[str, Any] capture_activations(*, str|Path output_dir, Any probes=None, str|None model_id=None, **Any kwargs)
Definition data.py:51