22 sae_layer: int |
None =
None,
25 Run a forward pass through model and collect top SAE features + logit lens.
26 Falls back to empty tensors if SAE weights are not available.
32 model = get_loaded_model()
34 raise RuntimeError(
"No model loaded. Run: aquin load --model <model-id>")
36 model_id = getattr(model,
"cfg",
None)
40 hf_name = model.cfg.model_name
if hasattr(model,
"cfg")
else str(type(model).__name__)
41 short = resolve_model_id(hf_name)
42 cfg = get_config(short)
44 sae_layer = cfg.get(
"sae_layer", 0)
45 hf_name = cfg[
"hf_name"]
52 tokens = model.to_tokens(prompt, prepend_bos=
True)
54 _, cache = model.run_with_cache(tokens)
57 logit_lens: list[dict] = []
59 n_layers = model.cfg.n_layers
60 for layer
in range(n_layers):
61 resid = cache[f
"blocks.{layer}.hook_resid_post"][0, -1]
62 logits = model.unembed(model.ln_final(resid.unsqueeze(0).unsqueeze(0)))[0, 0]
63 top_tok = int(logits.argmax().item())
64 top_str = model.to_string([top_tok])
65 logit_lens.append({
"layer": layer,
"token": top_str,
"token_id": top_tok})
70 top_features: list[dict] = []
73 sae = load_sae(model, sae_layer, short)
74 resid = cache[f
"blocks.{sae_layer}.hook_resid_post"][0, -1]
75 feat_acts, top_idxs = sae.get_top_features(resid, top_k=20)
77 {
"feature_idx": int(idx),
"activation": float(act)}
78 for idx, act
in zip(top_idxs.tolist(), feat_acts.tolist())
85 logits = model(tokens)
86 next_tok = int(logits[0, -1].argmax().item())
87 response = model.to_string([next_tok])
92 session_id=session_id,
97 created_at=datetime.now(timezone.utc).isoformat(),
98 top_features=top_features,
100 logit_lens=logit_lens,
106 bundle: CaptureBundle,
109 base_url: str =
"https://api.aquin.app",
112 Upload a CaptureBundle to Aquin Cloud. Returns capture_id.
115 base_url = os.environ.get(
"AQUIN_BASE_URL", base_url).rstrip(
"/")
117 payload = bundle.model_copy(update={
"session_id": session_id}).model_dump()
118 body = json.dumps(payload).encode()
121 f
"{base_url}/api/sync/captures/upload",
123 "Authorization": f
"Bearer {api_key}",
124 "Content-Type":
"application/json",
130 resp.raise_for_status()
131 capture_id = resp.json()[
"capture_id"]