AQIT 0.1.0
Loading...
Searching...
No Matches
activations_cli.py
Go to the documentation of this file.
1# Copyright (c) 2025-present Aquin Labs Private Limited. All Rights Reserved.
2"""aquin activations capture | replay"""
3
4from __future__ import annotations
5
6import json
7import sys
8
9
10def _parse_flag(args: list[str], name: str) -> str | None:
11 for i, a in enumerate(args):
12 if a == name and i + 1 < len(args):
13 return args[i + 1]
14 return None
15
16
17def _print_help() -> None:
18 print("Recorded activation workflows.")
19 print("")
20 print("Usage: aquin activations <capture|replay> ...")
21 print("")
22 print(" capture Record activations + metadata into a capture directory")
23 print(" aquin activations capture --dir ./captures/run1 [--prompts probes.jsonl]")
24 print("")
25 print(" replay Slice recorded activation metadata by filter/group")
26 print(" aquin activations replay --from ./captures/run1 [--filter lang=hi] [--group-by layer]")
27 print("")
28 print("Notes:")
29 print(" - capture always writes root metadata.json + manifest.json + summary.jsonl")
30 print(" - replay reads saved metadata; it does not re-run the model")
31 print("")
32 print("Docs: https://aquin.app/docs/sae-training")
33
34
35def _cmd_replay_activations(args: list[str]) -> None:
36 if not args or args[0] in ("-h", "--help", "help"):
37 print("Usage: aquin activations replay --from <capture_dir> [--filter key=value,...] [--group-by key] [--limit N]")
38 sys.exit(0 if args and args[0] in ("-h", "--help", "help") else 1)
40 from aquin.compute.activation_replay import replay_capture
41
42 source = _parse_flag(args, "--from") or _parse_flag(args, "--dir")
43 if not source and args and not args[0].startswith("-"):
44 source = args[0]
45 if not source:
46 print("Error: --from <capture_dir> is required")
47 sys.exit(1)
48
49 filter_expr = _parse_flag(args, "--filter")
50 group_by = _parse_flag(args, "--group-by")
51 limit_raw = _parse_flag(args, "--limit")
52 try:
53 limit = int(limit_raw) if limit_raw else 20
54 except ValueError:
55 print("Error: --limit must be an integer")
56 sys.exit(1)
57
58 try:
59 result = replay_capture(source, filter_expr=filter_expr, group_by=group_by, limit=limit)
60 except Exception as e:
61 print(f"Error: {e}")
62 sys.exit(1)
63
64 print(f"[activations replay] root={result['root']}")
65 print(
66 f"[activations replay] model={result.get('model_id', '—')} "
67 f"mode={result.get('model_mode', '—')} matched={result.get('n_filtered', 0)}/{result.get('n_rows', 0)}"
68 )
69 if result.get("filter"):
70 print(f"[activations replay] filter={result['filter']}")
71 if result.get("group_by"):
72 print(f"[activations replay] group_by={result['group_by']}")
73
74 groups = result.get("groups") or []
75 if groups:
76 print("")
77 print("Groups")
78 for row in groups:
79 layers = ", ".join(f"L{k}:{v}" for k, v in (row.get("layers") or {}).items())
80 print(f" {row.get('group', 'unknown')}: {row.get('count', 0)} {layers}".rstrip())
81 return
82
83 rows = result.get("rows") or []
84 if not rows:
85 print("No matching activation rows.")
86 return
87 print("")
88 print("Rows")
89 for row in rows:
90 text = " ".join(str(row.get("text") or "").split())
91 if len(text) > 72:
92 text = text[:71] + "…"
93 meta = []
94 for key in ("label", "lang", "stressor", "group", "quant_run_id"):
95 if row.get(key) is not None:
96 meta.append(f"{key}={row[key]}")
97 meta_s = " ".join(meta)
98 print(f" {row.get('probe_id', '?')} L{row.get('layer', '?')} {meta_s}".rstrip())
99 if text:
100 print(f" {text}")
101
102
103def cmd_activations(args: list[str]) -> None:
104 if not args or args[0] in ("-h", "--help", "help"):
106 return
108 sub = args[0]
109 rest = args[1:]
110
111 if sub == "capture":
112 from aquin.capture_cli import cmd_capture_activations
113
114 cmd_capture_activations(rest)
115 return
116
117 if sub == "replay":
119 return
120
121 print(f"Unknown activations subcommand: {sub}")
122 print("Run: aquin activations --help")
123 sys.exit(1)
str|None _parse_flag(list[str] args, str name)
None cmd_activations(list[str] args)
None _cmd_replay_activations(list[str] args)