AQIT 0.1.0
Loading...
Searching...
No Matches
info_sae_display.py
Go to the documentation of this file.
1# Copyright (c) 2025-present Aquin Labs Private Limited. All Rights Reserved.
2"""Tables for `aquin info sae` — quiet bordered field/value."""
3
4from __future__ import annotations
5
6from typing import Any
7
8from rich import box
9from rich.console import Console
10from rich.table import Table
11
12# Internal / redundant keys kept off the default surfaces.
13_SKIP = frozenset({
14 "id", "r2_key", "meta_key", "umap_key",
15 "train_steps", "final_loss", "l0", "dead_pct", "created_at",
16})
18# Preferred field order + quiet labels (API key → display).
19_FIELDS: list[tuple[str, str]] = [
20 ("model_slug", "model"),
21 ("model_name", "name"),
22 ("layer", "layer"),
23 ("d_model", "d_model"),
24 ("d_sae", "d_sae"),
25 ("k", "k"),
26 ("arch", "arch"),
27 ("interp_score", "interp"),
28]
29
30
31def _sae_slug(r: dict[str, Any]) -> str:
32 slug = r.get("model_slug")
33 layer = r.get("layer")
34 if slug is not None and layer is not None:
35 return f"{slug}-l{layer}"
36 return str(r.get("id") or "")
37
38
39def _fmt_value(key: str, value: Any) -> str:
40 if value is None:
41 return "-"
42 if key == "interp_score":
43 try:
44 return f"{float(value):.4f}"
45 except (TypeError, ValueError):
46 return str(value)
47 return str(value)
48
49
50def _rows(r: dict[str, Any]) -> list[tuple[str, str, str]]:
51 """(api_key, label, formatted_value) in display order, then leftover keys."""
52 seen: set[str] = set()
53 out: list[tuple[str, str, str]] = []
54 for key, label in _FIELDS:
55 if key not in r or key in _SKIP:
56 continue
57 seen.add(key)
58 out.append((key, label, _fmt_value(key, r.get(key))))
59 for key, value in r.items():
60 if key in seen or key in _SKIP:
61 continue
62 out.append((key, key, _fmt_value(key, value)))
63 return out
64
65
66def info_sae_payload(r: dict[str, Any]) -> dict[str, Any]:
67 slug = _sae_slug(r)
68 fields: dict[str, Any] = {}
69 for key, label, _fmt in _rows(r):
70 raw = r.get(key)
71 if key == "interp_score" and raw is not None:
72 try:
73 fields[label] = float(raw)
74 except (TypeError, ValueError):
75 fields[label] = raw
76 else:
77 fields[label] = raw
78 return {
79 "kind": "sae",
80 "slug": slug,
81 "fields": fields,
82 "load": f"aquin load sae {slug}" if slug else "aquin load sae <slug>",
83 }
84
85
86def render_info_sae_quiet(r: dict[str, Any]) -> None:
87 from aquin.table_plain import print_table
88
89 payload = info_sae_payload(r)
90 title = f"SAE {payload['slug']}" if payload.get("slug") else "SAE"
91 print_table(
92 ["field", "value"],
93 [[label, fmt] for _key, label, fmt in _rows(r)],
94 aligns=["left", "left"],
95 max_col=48,
96 title=title,
97 )
98 print()
99 print(f" {payload['load']}")
100 print()
101
102
103def render_info_sae(r: dict[str, Any]) -> None:
104 """Legacy Rich SIMPLE layout (`--plain`)."""
105 console = Console(highlight=False, soft_wrap=True)
106 slug = _sae_slug(r)
107 table = Table(
108 box=box.SIMPLE,
109 show_header=True,
110 header_style="bold",
111 pad_edge=False,
112 padding=(0, 2),
113 )
114 table.add_column("FIELD", no_wrap=True)
115 table.add_column("VALUE", overflow="ellipsis", max_width=56)
116
117 for _key, label, fmt in _rows(r):
118 table.add_row(label, fmt)
119
120 console.print()
121 console.print(f"SAE {slug}" if slug else "SAE")
122 console.print(table)
123 console.print()
124 if slug:
125 console.print(f"Load: aquin load sae {slug}")
126 console.print()
str _sae_slug(dict[str, Any] r)
str _fmt_value(str key, Any value)
dict[str, Any] info_sae_payload(dict[str, Any] r)
None render_info_sae(dict[str, Any] r)
None render_info_sae_quiet(dict[str, Any] r)
list[tuple[str, str, str]] _rows(dict[str, Any] r)