-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodal_app.py
More file actions
2656 lines (2337 loc) · 119 KB
/
Copy pathmodal_app.py
File metadata and controls
2656 lines (2337 loc) · 119 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Modal execution layer for MicroScope (the $30 GPU host, ADR-0003).
Modal is serverless + per-second billed, so there is no idle burn: GPU cost accrues only while a
function runs. This file builds the GPU image and exposes verification entrypoints. Stage wrappers
(reproduce/train/autointerp/eval/controls/circuit) are added here once their library APIs are
verified on this image (RULES.md E4).
Run:
modal run infra/modal_app.py::probe # CPU, import + dump library APIs (E4 verification)
modal run infra/modal_app.py::gpu_smoke # GPU, nvidia-smi, torch.cuda, gated-HF access check
Budget (ADR-0002): hard cap $30. Default GPU = L4 (24 GB, ~$0.80/hr), cheapest 24 GB option that
fits Gemma-2-2B. Keep functions short; nothing here should run more than a few minutes.
"""
from __future__ import annotations
import modal
# Base image: the PyPI-resolvable core. Heavy interp libs (dictionary_learning, delphi, sae-bench,
# sparse-feature-circuits) are layered on AFTER this base builds + probes cleanly (E4 discipline).
base_image = (
modal.Image.debian_slim(python_version="3.11")
.apt_install("git")
.pip_install(
"torch",
"transformers>=4.44",
"datasets>=2.20",
"nnsight>=0.3",
"sae-lens>=4.0",
"einops>=0.7",
"safetensors>=0.4",
"huggingface_hub>=0.24",
"tqdm>=4.66",
"numpy>=1.24",
)
)
# Full image: base + the source-only interp libraries. Each install is `|| true` so a single bad
# URL cannot abort the whole build, probe_interp then reports exactly which imported and their APIs
# (E4 discovery). The verified install commands get pinned into this file once confirmed.
full_image = base_image.run_commands(
"pip install 'git+https://github.com/saprmarks/dictionary_learning.git' || true",
"pip install sae-bench || pip install 'git+https://github.com/adamkarvonen/SAEBench.git' || true",
"pip install 'git+https://github.com/EleutherAI/delphi.git' || true",
"pip install 'git+https://github.com/saprmarks/feature-circuits.git' || true",
# EleutherAI sparsify, trains the custom SAE + skip-transcoder (ADR-0004). Install from the
# EleutherAI repo (NOT PyPI 'sparsify', which is Neural Magic's unrelated package).
"pip install 'git+https://github.com/EleutherAI/sparsify.git' || true",
# torchvision is an unused transitive dep whose video API breaks the HF `datasets` torch
# formatter (ImportError: VideoReader) during delphi's text caching, remove it.
"pip uninstall -y torchvision || true",
# flashinfer JIT-compiles CUDA kernels at runtime, but this image has the CUDA runtime, not the
# toolkit (no nvcc/CUDA_HOME), its sampler build fails in vLLM. Remove it so vLLM falls back to
# prebuilt FlashAttention + the native PyTorch sampler (no compilation needed). (ADR-0003)
"pip uninstall -y flashinfer-python flashinfer || true",
)
# Image variant that also bundles the local `microscope` package, so Modal training/eval functions can
# call the verified package wrappers (microscope.saes.train, etc.) instead of duplicating logic.
pkg_image = full_image.add_local_python_source("microscope")
app = modal.App("microscope-infra")
HF_SECRET = modal.Secret.from_name("hf-token")
# Persistent HF cache so the ~5 GB Gemma-2-2B + SAE download once, not every run (saves GPU $).
hf_cache = modal.Volume.from_name("microscope-hf-cache", create_if_missing=True)
CACHE = {"/root/.cache/huggingface": hf_cache}
CACHE_ENV = {"HF_HOME": "/root/.cache/huggingface", "HF_HUB_ENABLE_HF_TRANSFER": "0"}
# Persistent store for trained dictionaries (Phase 2) so Phase 3 eval can reload them across runs.
artifacts_vol = modal.Volume.from_name("microscope-artifacts", create_if_missing=True)
@app.function(
image=base_image, gpu="L4", secrets=[HF_SECRET], volumes=CACHE, timeout=1800, retries=0
)
def reproduce_recon(layer: int = 12, width: str = "16k", n_docs: int = 128, seq_len: int = 128) -> dict:
"""Phase-1 reproduction (step 1): load the canonical Gemma Scope SAE and measure its
variance-explained + mean L0 on Gemma-2-2B residual activations. Reproduces a KNOWN property of
a pretrained SAE before any custom training (RULES.md R1). Streaming stats => O(1) memory."""
import os
import torch
from datasets import load_dataset
from sae_lens import SAE
from transformer_lens import HookedTransformer
os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
dev = "cuda"
hook = f"blocks.{layer}.hook_resid_post"
# Canonical Gemma Scope recipe: TransformerLens residual stream (what the SAE was trained on).
model = HookedTransformer.from_pretrained("gemma-2-2b", dtype="bfloat16").to(dev)
loaded = SAE.from_pretrained(
"gemma-scope-2b-pt-res-canonical", f"layer_{layer}/width_{width}/canonical",
device=dev, dtype="bfloat16",
)
sae = loaded[0] if isinstance(loaded, tuple) else loaded
print("SAE cfg:", {k: str(v)[:50] for k, v in vars(sae.cfg).items()})
ds = load_dataset("NeelNanda/pile-10k", split=f"train[:{n_docs}]")
texts = [t for t in ds["text"] if t and t.strip()]
sum_x = sum_x2 = sse = 0.0
n_tok = 0
l0_total = xn = rn = 0.0
with torch.no_grad():
for text in texts:
toks = model.to_tokens(text)[:, :seq_len] # prepends BOS
_, cache = model.run_with_cache(
toks, names_filter=hook, stop_at_layer=layer + 1, return_type=None
)
x = cache[hook][0, 1:].float() # drop BOS position; [seq-1, d_model]
if x.shape[0] == 0:
continue
feats = sae.encode(x.to(torch.bfloat16))
recon = sae.decode(feats).float()
sum_x += x.sum(0).double()
sum_x2 += (x * x).sum(0).double()
sse += ((x - recon) ** 2).sum().double()
l0_total += (feats > 0).float().sum().item()
xn += x.norm(dim=-1).sum().item()
rn += recon.norm(dim=-1).sum().item()
n_tok += x.shape[0]
var = (sum_x2 - sum_x.pow(2) / n_tok).sum()
fvu = (sse / var).item()
out = {
"sae_id": f"layer_{layer}/width_{width}/canonical",
"hook": hook,
"n_tokens": n_tok,
"variance_explained": round(1.0 - fvu, 4),
"mean_l0": round(l0_total / n_tok, 1),
"mean_norm_x": round(xn / n_tok, 1),
"mean_norm_recon": round(rn / n_tok, 1),
}
for k, v in out.items():
print(f"{k:20s} {v}")
return out
@app.function(image=full_image, secrets=[HF_SECRET], timeout=1200)
def probe_sparsify2() -> dict[str, str]:
"""E4: sparsify training flow, data tokenization helper + Trainer launch method + dataset type."""
import inspect
import sparsify
out: dict[str, str] = {}
# data submodule: how to tokenize/chunk a HF dataset for the Trainer
try:
from sparsify import data as sdata
out["sparsify.data.public"] = ", ".join(n for n in dir(sdata) if not n.startswith("_"))[:400]
for fn in ("chunk_and_tokenize", "MemmapDataset"):
obj = getattr(sdata, fn, None)
if obj is not None:
try:
out[f"data.{fn}()"] = f"{fn}{inspect.signature(obj)}"[:320]
except (ValueError, TypeError):
out[f"data.{fn}"] = f"(type {type(obj).__name__})"
except Exception as exc: # noqa: BLE001
out["sparsify.data"] = f"FAIL: {type(exc).__name__}: {str(exc)[:120]}"
# Trainer: launch method (.fit / .train / .run?) + __init__ signature
tr = sparsify.Trainer
out["Trainer.methods"] = ", ".join(
m for m in dir(tr) if not m.startswith("_")
)[:300]
for m in ("fit", "train", "run"):
meth = getattr(tr, m, None)
if callable(meth):
try:
out[f"Trainer.{m}()"] = f"{m}{inspect.signature(meth)}"[:200]
except (ValueError, TypeError):
out[f"Trainer.{m}"] = "exists"
try:
out["Trainer.__init__"] = f"{inspect.signature(tr.__init__)}"[:300]
except (ValueError, TypeError):
pass
for k, v in out.items():
print(f"{k}\n {v}")
return out
@app.function(image=pkg_image, timeout=600)
def pkg_smoke() -> dict[str, str]:
"""Verify the local microscope package is importable inside Modal (the unit-2 integration path)."""
import microscope
from microscope.config import reproduction_logged # exists today; confirms package code shipped
return {"version": microscope.__version__, "rl_importable": str(callable(reproduction_logged))}
@app.function(
image=pkg_image,
gpu="L4",
secrets=[HF_SECRET],
volumes={**CACHE, "/root/outputs": artifacts_vol},
timeout=5400,
retries=0,
)
def train_coder_modal(config_dict: dict, kind: str = "sae", randomize: bool = False) -> dict:
"""Phase 2: train an SAE or skip-transcoder on Modal via the verified microscope.saes.train wrapper.
Persists the trained dictionary to the artifacts Volume so Phase-3 eval can reload it. Returns the
metrics dict + a listing of the saved files (the smoke's save/load confirmation)."""
import glob
import os
from microscope.config import RunConfig
from microscope.saes.train import train_coder
# Redirect the dictionary save into the mounted persistent Volume (overrides the YAML save_dir).
os.environ.setdefault("PYTORCH_CUDA_ALLOC_CONF", "expandable_segments:True")
overrides = {"save_dir": "/root/outputs/coders"}
if randomize: # randomized-model control (ADR-0005): random transformer, real embeddings
overrides["randomize_model"] = True
overrides["run_name"] = f"{config_dict.get('name', 'run')}-{kind}-random"
cfg = RunConfig(**{**config_dict, **overrides})
result = train_coder(cfg, kind) # type: ignore[arg-type]
save_path = result.get("save_path", "")
all_paths = sorted(glob.glob(save_path + "/**", recursive=True))
result["saved_files"] = [p.replace("/root/outputs/", "") for p in all_paths if os.path.isfile(p)][:20]
result["n_saved_files"] = sum(1 for p in all_paths if os.path.isfile(p))
artifacts_vol.commit()
print("TRAIN RESULT:", result)
return result
@app.local_entrypoint()
def train_main(config: str, kind: str = "sae", randomize: bool = False) -> None:
"""Run a training job: modal run infra/modal_app.py::train_main --config <yaml> --kind sae|transcoder."""
import yaml
with open(config) as fh:
config_dict = yaml.safe_load(fh)
result = train_coder_modal.remote(config_dict, kind, randomize)
print("FINAL TRAIN RESULT:", result)
@app.function(image=full_image, secrets=[HF_SECRET], timeout=1200)
def probe_sparsify() -> dict[str, str]:
"""E4 (ADR-0004): introspect the installed EleutherAI sparsify, SAE/transcoder config + API."""
import dataclasses
import importlib
import inspect
import subprocess
out: dict[str, str] = {}
try:
import sparsify
except Exception as exc: # noqa: BLE001
out["sparsify"] = f"IMPORT FAILED: {type(exc).__name__}: {str(exc)[:200]}"
for k, v in out.items():
print(f"{k}\n {v}")
return out
out["sparsify.version"] = getattr(sparsify, "__version__", "?")
out["sparsify.file"] = str(getattr(sparsify, "__file__", "?")) # confirm EleutherAI, not Neural Magic
out["sparsify.public"] = ", ".join(n for n in dir(sparsify) if not n.startswith("_"))[:400]
def _resolve(name: str) -> object:
obj = getattr(sparsify, name, None)
if obj is not None:
return obj
for sub in ("config", "trainer", "sae", "sparse_coder", "sparsecoder"):
try:
m = importlib.import_module(f"sparsify.{sub}")
if hasattr(m, name):
return getattr(m, name)
except Exception: # noqa: BLE001
continue
return None
for name in ("SaeConfig", "TrainConfig", "Trainer", "Sae", "SparseCoder"):
obj = _resolve(name)
if obj is None:
out[name] = "NOT FOUND"
continue
if dataclasses.is_dataclass(obj):
fields = [f.name for f in dataclasses.fields(obj)]
out[f"{name}<fields>"] = ", ".join(fields)[:500]
tc = [f for f in fields if any(s in f.lower() for s in ("transcod", "skip", "mlp"))]
out[f"{name}<transcode/skip>"] = ", ".join(tc) or "(none in field names)"
else:
try:
out[f"{name}()"] = f"{name}{inspect.signature(obj)}"[:300]
except (ValueError, TypeError):
out[name] = f"(type {type(obj).__name__})"
if name in ("Sae", "SparseCoder"):
methods = [
m for m in dir(obj)
if not m.startswith("_")
and any(s in m.lower() for s in ("save", "load", "encode", "decode", "pretrained"))
]
out[f"{name}.io_methods"] = ", ".join(methods)[:300]
# CLI flags (does `python -m sparsify --help` expose --transcode / hookpoint / k / width?)
try:
r = subprocess.run(
["python", "-m", "sparsify", "--help"], capture_output=True, text=True, timeout=60
)
help_txt = (r.stdout or "") + (r.stderr or "")
out["cli.transcode_in_help"] = str("transcode" in help_txt.lower())
rel = [
ln.strip() for ln in help_txt.splitlines()
if any(s in ln.lower() for s in ("transcode", "skip", "hookpoint", "--layer", "--k", "expansion", "--data"))
]
out["cli.relevant_flags"] = " || ".join(rel[:14])[:600]
except Exception as exc: # noqa: BLE001
out["cli"] = f"help FAILED: {type(exc).__name__}: {str(exc)[:100]}"
# delphi <- sparsify glue (the Phase-3 integration we must verify)
try:
from delphi.sparse_coders import load_sparsify
out["delphi.load_sparsify()"] = f"load_sparsify{inspect.signature(load_sparsify)}"[:300]
except Exception as exc: # noqa: BLE001
out["delphi.load_sparsify"] = f"n/a: {type(exc).__name__}: {str(exc)[:100]}"
for k, v in out.items():
print(f"{k}\n {v}")
return out
@app.function(image=full_image, secrets=[HF_SECRET], timeout=1200)
def probe_saebench() -> dict[str, str]:
"""E4: introspect the INSTALLED SAEBench sparse_probing module (pip pkg may differ from GitHub)."""
import dataclasses
import importlib
import inspect
out: dict[str, str] = {}
for modname in (
"sae_bench.evals.sparse_probing_sae_probes",
"sae_bench.evals.sparse_probing",
"sae_bench.evals.sparse_probing.main",
"sae_bench.evals.sparse_probing.eval_config",
):
try:
m = importlib.import_module(modname)
names = [n for n in dir(m) if not n.startswith("_")]
out[modname] = ", ".join(names)[:400]
for n in names:
obj = getattr(m, n)
if callable(obj) and any(k in n.lower() for k in ("run", "eval", "main", "probe")):
try:
out[f"{modname}.{n}()"] = f"{n}{inspect.signature(obj)}"[:320]
except (ValueError, TypeError):
pass
if dataclasses.is_dataclass(obj):
out[f"{modname}.{n}<fields>"] = ", ".join(
f.name for f in dataclasses.fields(obj)
)[:320]
except Exception as exc: # noqa: BLE001
out[modname] = f"FAIL: {type(exc).__name__}: {str(exc)[:100]}"
# the SAE loader SAEBench uses
try:
from sae_bench.sae_bench_utils import general_utils
out["general_utils"] = ", ".join(n for n in dir(general_utils) if not n.startswith("_"))[:300]
for fn in ("load_and_format_sae", "get_results_filepath"):
if hasattr(general_utils, fn):
out[f"general_utils.{fn}()"] = f"{fn}{inspect.signature(getattr(general_utils, fn))}"
except Exception as exc: # noqa: BLE001
out["general_utils"] = f"FAIL: {type(exc).__name__}: {str(exc)[:100]}"
for k, v in out.items():
print(f"{k}\n {v}")
return out
@app.function(image=full_image, secrets=[HF_SECRET], timeout=1200)
def probe2() -> dict[str, str]:
"""Confirm Gemma access post-license + locate Gemma Scope SAE IDs + deep-probe delphi/sae_bench."""
import importlib
import os
import pkgutil
token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
out: dict[str, str] = {}
# 1. Gemma-2-2B gated access (small tokenizer download).
try:
from transformers import AutoTokenizer
AutoTokenizer.from_pretrained("google/gemma-2-2b", token=token)
out["gemma2_2b"] = "ACCESS OK"
except Exception as exc: # noqa: BLE001
out["gemma2_2b"] = f"FAIL: {type(exc).__name__}: {str(exc)[:140]}"
# 2. Gemma Scope repo access (metadata only, list files, no big download).
from huggingface_hub import HfApi
api = HfApi()
for repo in ("google/gemma-scope-2b-pt-res", "google/gemma-scope-2b-pt-res-canonical"):
try:
files = api.list_repo_files(repo, token=token)
out[f"gemma_scope::{repo}"] = f"OK ({len(files)} files), e.g. {files[:3]}"
except Exception as exc: # noqa: BLE001
out[f"gemma_scope::{repo}"] = f"FAIL: {type(exc).__name__}: {str(exc)[:100]}"
# 3. sae_lens pretrained directory, find the exact gemma-scope release + sae_id strings.
try:
from sae_lens.loading.pretrained_saes_directory import get_pretrained_saes_directory
d = get_pretrained_saes_directory()
gemma = [k for k in d if "gemma-scope-2b-pt-res" in k]
out["sae_lens.gemma_releases"] = ", ".join(gemma[:8]) or "none found"
if gemma:
rel = d[gemma[0]]
sample = list(rel.saes_map.items())[:3] if hasattr(rel, "saes_map") else "?"
out["sae_lens.sample_sae_ids"] = f"{gemma[0]} -> {sample}"
except Exception as exc: # noqa: BLE001
out["sae_lens.gemma_releases"] = f"probe failed: {type(exc).__name__}: {str(exc)[:140]}"
# 4. Deep-probe delphi + sae_bench submodule trees (names only; selective deep dir()).
for pkg in ("delphi", "sae_bench"):
try:
mod = importlib.import_module(pkg)
subs = [m.name for m in pkgutil.iter_modules(mod.__path__)]
out[f"{pkg}.submodules"] = ", ".join(subs)
except Exception as exc: # noqa: BLE001
out[f"{pkg}.submodules"] = f"FAIL: {type(exc).__name__}: {str(exc)[:120]}"
for k, v in out.items():
print(f"{k}\n {v}")
return out
@app.function(image=full_image, secrets=[HF_SECRET], timeout=1200)
def probe3() -> dict[str, str]:
"""Signatures needed to write Phase-1 wrappers: sae_lens loader, delphi classes, sae_bench evals."""
import importlib
import inspect
out: dict[str, str] = {}
def sig(obj: object, name: str) -> None:
try:
out[name] = f"{name}{inspect.signature(obj)}" # type: ignore[arg-type]
except Exception as exc: # noqa: BLE001
out[name] = f"sig n/a: {type(exc).__name__}"
# sae_lens SAE loader + layer-12 width-16k gemma-scope sae_ids
try:
from sae_lens import SAE
sig(SAE.from_pretrained, "SAE.from_pretrained")
except Exception as exc: # noqa: BLE001
out["SAE.from_pretrained"] = f"FAIL: {exc}"
try:
from sae_lens.loading.pretrained_saes_directory import get_pretrained_saes_directory
rel = get_pretrained_saes_directory()["gemma-scope-2b-pt-res-canonical"]
ids = [k for k in rel.saes_map if "layer_12" in k]
out["gemma_scope_canonical.layer12_ids"] = ", ".join(ids[:12]) or "none"
except Exception as exc: # noqa: BLE001
out["gemma_scope_canonical.layer12_ids"] = f"FAIL: {str(exc)[:120]}"
# delphi: dump public API of the wiring-relevant submodules
for sub in ("config", "latents", "explainers", "scorers", "clients", "pipeline"):
try:
m = importlib.import_module(f"delphi.{sub}")
out[f"delphi.{sub}"] = ", ".join(n for n in dir(m) if not n.startswith("_"))[:320]
except Exception as exc: # noqa: BLE001
out[f"delphi.{sub}"] = f"FAIL: {type(exc).__name__}: {str(exc)[:100]}"
# sae_bench: where do the evals live? try the common paths.
import pkgutil
for path in ("sae_bench.evals", "sae_bench"):
try:
m = importlib.import_module(path)
subs = [s.name for s in pkgutil.iter_modules(m.__path__)]
out[f"{path}.children"] = ", ".join(subs)
except Exception as exc: # noqa: BLE001
out[f"{path}.children"] = f"FAIL: {type(exc).__name__}: {str(exc)[:100]}"
for k, v in out.items():
print(f"{k}\n {v}")
return out
@app.function(image=full_image, timeout=1200)
def probe_interp() -> dict[str, str]:
"""Report which interp libs installed + a sketch of their public API (E4 verification, CPU)."""
import importlib
import inspect
# Candidate import names per logical library (install name != import name sometimes).
candidates = {
"dictionary_learning": ["dictionary_learning"],
"sae_bench": ["sae_bench", "saebench", "sae_bench_utils"],
"delphi": ["delphi"],
"feature_circuits": ["feature_circuits", "circuit", "attribution"],
}
report: dict[str, str] = {}
for logical, names in candidates.items():
imported = None
for name in names:
try:
imported = importlib.import_module(name)
report[logical] = f"OK as '{name}' v{getattr(imported, '__version__', '?')}"
# dump top-level public names to learn the surface
public = [n for n in dir(imported) if not n.startswith("_")]
report[f"{logical}.dir"] = ", ".join(public[:40])
break
except Exception as exc: # noqa: BLE001
report[logical] = f"import '{name}' failed: {type(exc).__name__}: {str(exc)[:120]}"
# try to surface key submodules/signatures for the ones we care about most
if imported is not None and logical == "dictionary_learning":
for sub in ("training", "trainers", "buffer", "dictionary"):
try:
m = importlib.import_module(f"dictionary_learning.{sub}")
report[f"dictionary_learning.{sub}"] = ", ".join(
n for n in dir(m) if not n.startswith("_")
)[:300]
except Exception as exc: # noqa: BLE001
report[f"dictionary_learning.{sub}"] = f"n/a: {type(exc).__name__}"
for k, v in report.items():
print(f"{k}\n {v}")
return report
@app.function(image=base_image, timeout=900)
def probe() -> dict[str, str]:
"""Import the core libs and report versions (CPU, ~free). The E4 verification starting point."""
import importlib
report: dict[str, str] = {}
for mod in ("torch", "transformers", "datasets", "nnsight", "sae_lens", "einops"):
try:
m = importlib.import_module(mod)
report[mod] = getattr(m, "__version__", "imported (no __version__)")
except Exception as exc: # noqa: BLE001 - report, don't crash the probe
report[mod] = f"IMPORT FAILED: {type(exc).__name__}: {exc}"
for k, v in report.items():
print(f"{k:14s} {v}")
return report
@app.function(image=base_image, gpu="L4", secrets=[HF_SECRET], timeout=900)
def gpu_smoke() -> dict[str, str]:
"""Confirm the GPU is allocated, torch sees CUDA, and the hf-token can reach gated Gemma-2-2B."""
import os
import torch
out: dict[str, str] = {
"cuda_available": str(torch.cuda.is_available()),
"device": torch.cuda.get_device_name(0) if torch.cuda.is_available() else "none",
"torch": torch.__version__,
}
if torch.cuda.is_available():
free, total = torch.cuda.mem_get_info()
out["vram_total_gib"] = f"{total / 1024**3:.1f}"
# hf-token secret may expose HF_TOKEN or HUGGING_FACE_HUB_TOKEN, accept either.
token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
out["hf_token_present"] = str(bool(token))
try:
from huggingface_hub import HfApi
who = HfApi().whoami(token=token)
out["hf_user"] = who.get("name", "?")
except Exception as exc: # noqa: BLE001
out["hf_user"] = f"whoami FAILED: {type(exc).__name__}: {exc}"
# Confirm gated Gemma-2-2B license is accepted on this account (tokenizer is a tiny download).
try:
from transformers import AutoTokenizer
AutoTokenizer.from_pretrained("google/gemma-2-2b", token=token)
out["gemma2_2b_access"] = "OK"
except Exception as exc: # noqa: BLE001
out["gemma2_2b_access"] = f"GATED/FAILED: {type(exc).__name__}: {str(exc)[:160]}"
for k, v in out.items():
print(f"{k:18s} {v}")
return out
@app.function(
image=base_image, gpu="L4", secrets=[HF_SECRET], volumes=CACHE, timeout=2400, retries=0
)
def multi_layer_recon(
layers: str = "5,12,19", width: str = "16k", n_docs: int = 96, seq_len: int = 128
) -> dict:
"""Phase-1 reproduction (objective, no LLM): VE + L0 of canonical Gemma Scope SAEs across layers.
Reproduces the documented Gemma Scope trend across depth. One model load; capture all needed
resid_post hooks in a single forward per doc. TransformerLens recipe, BOS excluded (ADR-0003)."""
import torch
from datasets import load_dataset
from sae_lens import SAE
from transformer_lens import HookedTransformer
dev = "cuda"
layer_list = [int(x) for x in layers.split(",")]
hooks = {ly: f"blocks.{ly}.hook_resid_post" for ly in layer_list}
model = HookedTransformer.from_pretrained("gemma-2-2b", dtype="bfloat16").to(dev)
saes = {}
for ly in layer_list:
loaded = SAE.from_pretrained(
"gemma-scope-2b-pt-res-canonical",
f"layer_{ly}/width_{width}/canonical",
device=dev,
dtype="bfloat16",
)
saes[ly] = loaded[0] if isinstance(loaded, tuple) else loaded
ds = load_dataset("NeelNanda/pile-10k", split=f"train[:{n_docs}]")
texts = [t for t in ds["text"] if t and t.strip()]
max_layer = max(layer_list)
stats = {ly: {"sx": 0.0, "sx2": 0.0, "sse": 0.0, "l0": 0.0} for ly in layer_list}
n_tok = {ly: 0 for ly in layer_list}
with torch.no_grad():
for text in texts:
toks = model.to_tokens(text)[:, :seq_len]
_, cache = model.run_with_cache(
toks,
names_filter=list(hooks.values()),
stop_at_layer=max_layer + 1,
return_type=None,
)
for ly in layer_list:
x = cache[hooks[ly]][0, 1:].float() # drop BOS
if x.shape[0] == 0:
continue
feats = saes[ly].encode(x.to(torch.bfloat16))
recon = saes[ly].decode(feats).float()
s = stats[ly]
s["sx"] += x.sum(0).double()
s["sx2"] += (x * x).sum(0).double()
s["sse"] += ((x - recon) ** 2).sum().double()
s["l0"] += (feats > 0).float().sum().item()
n_tok[ly] += x.shape[0]
results = {}
for ly in layer_list:
s = stats[ly]
var = (s["sx2"] - s["sx"].pow(2) / n_tok[ly]).sum()
results[f"layer_{ly}"] = {
"variance_explained": round((1 - s["sse"] / var).item(), 4),
"mean_l0": round(s["l0"] / n_tok[ly], 1),
"n_tokens": n_tok[ly],
}
print(f"layer_{ly}: {results[f'layer_{ly}']}")
return {"width": width, "results": results}
@app.function(
image=full_image, gpu="L4", secrets=[HF_SECRET], volumes=CACHE, timeout=3600, retries=0
)
def auto_interp(
layer: int = 12,
width: str = "16k",
max_latents: int = 20,
scorer_model: str = "Qwen/Qwen2.5-3B-Instruct",
n_tokens: int = 200_000,
) -> dict:
"""Phase-1 auto-interp: run delphi (LOCAL Offline scorer, no paid API) on the Gemma Scope SAE.
Uses delphi's native Gemma Scope path (verified): sparse_model='google/gemma-scope-2b-pt-res',
hookpoint='layer_<L>/width_<W>/average_l0_<L0>'. Reports aggregate detection + fuzzing accuracy.
Scores from a small local scorer are NOT directly comparable to papers using frontier scorers -
label accordingly (R4). Capped at <=500 latents (RULES.md C3)."""
import os
# Belt-and-suspenders: also disable vLLM's flashinfer sampler via env (flashinfer is uninstalled
# from the image because its runtime JIT needs a CUDA toolkit this image lacks, see full_image).
os.environ.setdefault("VLLM_USE_FLASHINFER_SAMPLER", "0")
import asyncio
from pathlib import Path
from delphi.__main__ import run
from delphi.config import CacheConfig, ConstructorConfig, RunConfig, SamplerConfig
from delphi.log.result_analysis import get_agg_metrics, load_data
from huggingface_hub import HfApi
assert max_latents <= 500, "auto-interp cap is 500 latents (RULES.md C3)"
token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
# delphi needs a concrete average_l0 dir (no 'canonical'), discover a valid one near L0~82.
files = HfApi().list_repo_files("google/gemma-scope-2b-pt-res", token=token)
prefix = f"layer_{layer}/width_{width}/average_l0_"
l0s = sorted({int(f.split(prefix)[1].split("/")[0]) for f in files if f.startswith(prefix)})
if not l0s:
raise RuntimeError(f"No average_l0 dirs found for {prefix} in gemma-scope-2b-pt-res")
l0 = min(l0s, key=lambda v: abs(v - 82))
hookpoint = f"layer_{layer}/width_{width}/average_l0_{l0}"
print(f"hookpoint={hookpoint} available_l0s={l0s}")
name = f"g2-l{layer}-w{width}-l0{l0}"
run_cfg = RunConfig(
name=name,
model="google/gemma-2-2b",
sparse_model="google/gemma-scope-2b-pt-res",
hookpoints=[hookpoint],
explainer_provider="offline",
explainer_model=scorer_model,
explainer_model_max_len=4096,
explainer="default",
scorers=["detection", "fuzz"],
max_latents=max_latents,
filter_bos=True,
num_gpus=1,
max_memory=0.6, # base model is freed after caching; 0.6 of 22GB fits the 3B scorer + KV
seed=22,
verbose=False, # skip delphi's plotly/kaleido visualization (kaleido absent -> crash)
hf_token=token,
cache_cfg=CacheConfig(
dataset_repo="NeelNanda/pile-10k",
dataset_split="train",
dataset_column="text",
batch_size=8,
cache_ctx_len=256,
n_tokens=n_tokens,
n_splits=5,
),
constructor_cfg=ConstructorConfig(
example_ctx_len=32,
min_examples=50,
n_non_activating=50,
non_activating_source="random",
),
sampler_cfg=SamplerConfig(
n_examples_train=20,
n_examples_test=40,
n_quantiles=10,
train_type="quantiles",
test_type="quantiles",
),
)
asyncio.run(run(run_cfg))
# delphi stores scores under the RESOLVED module name (e.g. 'layers.12'), not the gemma-scope
# params path, so load_data(hookpoints=[params_path]) finds nothing. Read the raw per-latent
# score files directly and aggregate defensively (schema is also printed for transparency).
import glob
import re
# delphi score files are JSON arrays of per-example records each with one "activating" (ground
# truth) and one "prediction" (scorer's call). Extract those booleans by regex straight from the
# raw text, robust to any JSON-parse quirk, and score balanced accuracy = mean(activating==pred).
act_re = re.compile(r'"activating":\s*(true|false)')
pred_re = re.compile(r'"prediction":\s*(true|false)')
out: dict = {"hookpoint": hookpoint, "scorer": scorer_model, "max_latents": max_latents, "scores": {}}
for scorer in ("detection", "fuzz"):
sdir = Path.cwd() / "results" / name / "scores" / scorer
files = glob.glob(str(sdir / "*"))
per_latent_acc: list[float] = []
total_correct = total_examples = 0
for fp in files:
try:
text = open(fp).read()
except OSError:
continue
acts = act_re.findall(text)
preds = pred_re.findall(text)
n = min(len(acts), len(preds))
if n == 0:
continue
correct = sum(1 for a, p in zip(acts[:n], preds[:n]) if a == p)
per_latent_acc.append(correct / n)
total_correct += correct
total_examples += n
out["scores"][scorer] = {
"n_latents_scored": len(per_latent_acc),
"n_examples": total_examples,
"mean_accuracy_macro": round(sum(per_latent_acc) / len(per_latent_acc), 4) if per_latent_acc else None,
"accuracy_micro": round(total_correct / total_examples, 4) if total_examples else None,
}
print("AUTO-INTERP RESULT:", out)
return out
@app.function(
image=full_image, gpu="L4", secrets=[HF_SECRET], volumes=CACHE, timeout=3600, retries=0
)
def saebench_sparse_probing(
layer: int = 12, width: str = "16k", train_size: int = 1500, test_size: int = 500
) -> dict:
"""Phase-1 SAEBench eval: sparse-probing accuracy of the canonical Gemma Scope SAE.
Objective metric (no LLM scorer). Verified API: sae_bench.evals.sparse_probing.main.run_eval with
selected_saes as native sae_lens (release, sae_id) tuples. Headline = SAE probe top-1 accuracy vs
the residual-stream baseline (~0.65 documented); the SAE should clearly beat the baseline."""
import glob
import json
import os
from sae_bench.evals.sparse_probing import main as sp
sae_id = f"layer_{layer}/width_{width}/canonical"
selected_saes = [("gemma-scope-2b-pt-res-canonical", sae_id)]
config = sp.SparseProbingEvalConfig(
model_name="gemma-2-2b",
random_seed=42,
llm_batch_size=32,
llm_dtype="bfloat16",
dataset_names=["LabHC/bias_in_bios"], # single dataset = fast smoke
probe_train_set_size=train_size,
probe_test_set_size=test_size,
context_length=128,
k_values=[1],
)
out_path = "eval_results/sparse_probing"
sp.run_eval(
config, selected_saes, "cuda", out_path,
force_rerun=True, clean_up_activations=True, save_activations=False,
)
files = glob.glob(os.path.join(out_path, "*_eval_results.json"))
if not files:
raise RuntimeError(f"no SAEBench result json written to {out_path}")
res = json.load(open(files[0]))
metrics = res.get("eval_result_metrics", {})
sae_m = metrics.get("sae", {})
llm_m = metrics.get("llm", {})
out = {
"sae_id": sae_id,
"dataset": "LabHC/bias_in_bios",
"sae_top_1_test_accuracy": sae_m.get("sae_top_1_test_accuracy"),
"sae_test_accuracy": sae_m.get("sae_test_accuracy"),
"llm_top_1_test_accuracy": llm_m.get("llm_top_1_test_accuracy"),
"llm_test_accuracy": llm_m.get("llm_test_accuracy"),
}
print("SAEBENCH SPARSE-PROBING RESULT:", out)
return out
@app.function(
image=pkg_image, secrets=[HF_SECRET], volumes={**CACHE, "/root/outputs": artifacts_vol}, timeout=900
)
def probe_phase3_glue() -> dict:
"""E4 (Phase 3): verify how a sparsify-trained dict feeds delphi (load_sparsify) + assess SAEBench.
Reads the trained Gemma SAE from the artifacts Volume and inspects the delphi loader contract +
the sparsify coder's interface (to decide whether SAEBench needs an adapter)."""
import glob
import importlib
import inspect
import os
out: dict = {}
sae_path = "/root/outputs/coders/train_gemma2_2b_l12-sae"
out["sae_dir_exists"] = str(os.path.isdir(sae_path))
out["sae_files"] = [
p.replace("/root/outputs/", "")
for p in glob.glob(sae_path + "/**", recursive=True)
if os.path.isfile(p)
][:10]
# 1) delphi's sparsify loader (the auto-interp glue)
try:
sc = importlib.import_module("delphi.sparse_coders")
for fn in ("load_sparse_coders", "load_hooks_sparse_coders", "load_sparsify"):
obj = getattr(sc, fn, None)
if obj is not None and callable(obj):
try:
out[f"delphi.sparse_coders.{fn}()"] = f"{fn}{inspect.signature(obj)}"[:260]
except (ValueError, TypeError):
out[f"delphi.sparse_coders.{fn}"] = "callable (no sig)"
mod = importlib.import_module("delphi.sparse_coders.load_sparsify")
out["delphi.load_sparsify.module"] = ", ".join(
n for n in dir(mod) if not n.startswith("_")
)[:300]
except Exception as exc: # noqa: BLE001
out["delphi.sparse_coders"] = f"FAIL: {type(exc).__name__}: {str(exc)[:140]}"
# 2) load the trained sparsify dict + inspect its interface (for SAEBench compatibility)
try:
import sparsify
coder = sparsify.SparseCoder.load_from_disk(sae_path + "/layers.12", device="cpu")
out["loaded_coder_type"] = type(coder).__name__
out["coder_cfg_type"] = type(getattr(coder, "cfg", None)).__name__
out["coder_attrs"] = ", ".join(
a for a in dir(coder)
if not a.startswith("_")
and any(s in a.lower() for s in ("encode", "decode", "cfg", "w_dec", "w_enc", "d_in", "num_latents", "forward"))
)[:300]
except Exception as exc: # noqa: BLE001
out["load_coder"] = f"FAIL: {type(exc).__name__}: {str(exc)[:180]}"
for k, v in out.items():
print(f"{k}\n {v}")
return out
@app.function(
image=pkg_image, gpu="L4", secrets=[HF_SECRET],
volumes={**CACHE, "/root/outputs": artifacts_vol}, timeout=1800, retries=0,
)
def recon_eval(run_name: str, kind: str, layer: int = 12, n_docs: int = 96, seq_len: int = 128) -> dict:
"""Phase 3: each-coder reconstruction variance-explained on its OWN objective, on HF activations
(matching how sparsify trained the coders), with a bootstrap 95% CI over documents.
SAE target = resid_post@L (output of model.layers[L]); transcoder target = MLP-out@L (from MLP-in).
Uses coder.forward(input) for the reconstruction so the transcoder's skip connection is included."""
import os
import numpy as np
import sparsify
import torch
from datasets import load_dataset
from transformers import AutoModel, AutoTokenizer
token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGING_FACE_HUB_TOKEN")
dev = "cuda"
coder = sparsify.SparseCoder.load_from_disk(
f"/root/outputs/coders/{run_name}/layers.{layer}", device=dev
)
model = (
AutoModel.from_pretrained("google/gemma-2-2b", torch_dtype=torch.bfloat16, token=token)
.to(dev).eval()
)
tok = AutoTokenizer.from_pretrained("google/gemma-2-2b", token=token)
ds = load_dataset("NeelNanda/pile-10k", split=f"train[:{n_docs}]")
texts = [t for t in ds["text"] if t and t.strip()]
layer_mod = model.layers[layer]
mlp_mod = layer_mod.mlp
cap: dict = {}
def _t(x): # extract a tensor from a tensor / tuple / structured output
if torch.is_tensor(x):
return x
if isinstance(x, (tuple, list)) and torch.is_tensor(x[0]):
return x[0]
for a in ("sae_out", "output", "recon", "reconstruction", "out", "y"):
v = getattr(x, a, None)
if torch.is_tensor(v):
return v
return None
per_doc = []
printed = False
with torch.no_grad():
for text in texts:
cap.clear()
ids = tok(text, return_tensors="pt", truncation=True, max_length=seq_len).input_ids.to(dev)
if kind == "sae":
h = layer_mod.register_forward_hook(lambda m, i, o: cap.__setitem__("y", _t(o)))
model(ids)
h.remove()
src = tgt = cap["y"][0, 1:].float()
else:
h1 = mlp_mod.register_forward_pre_hook(lambda m, i: cap.__setitem__("in", i[0]))
h2 = mlp_mod.register_forward_hook(lambda m, i, o: cap.__setitem__("out", _t(o)))
model(ids)
h1.remove()
h2.remove()
src = cap["in"][0, 1:].float()
tgt = cap["out"][0, 1:].float()
if tgt.shape[0] == 0:
continue
fwd = coder(src.to(torch.bfloat16))
recon = _t(fwd)
if recon is None:
raise RuntimeError(f"could not extract recon from coder output: {type(fwd)} {dir(fwd)}")
recon = recon.float()
if not printed:
print(f"coder fwd type={type(fwd).__name__} recon_shape={tuple(recon.shape)} tgt={tuple(tgt.shape)}")
printed = True
sse = ((tgt - recon) ** 2).sum().item()
var = ((tgt - tgt.mean(0)) ** 2).sum().item()
per_doc.append((sse, var, tgt.shape[0]))
sse = np.array([d[0] for d in per_doc])
var = np.array([d[1] for d in per_doc])
ve = 1.0 - sse.sum() / var.sum()
rng = np.random.default_rng(0)
n = len(per_doc)
boot = [1.0 - sse[i].sum() / var[i].sum() for i in (rng.integers(0, n, n) for _ in range(1000))]
lo, hi = np.percentile(boot, [2.5, 97.5])
out = {
"run_name": run_name, "kind": kind,
"target": "resid_post" if kind == "sae" else "mlp_out",
"variance_explained": round(float(ve), 4),
"ve_ci95": [round(float(lo), 4), round(float(hi), 4)],
"k_L0": int(getattr(coder.cfg, "k", 0)), "n_docs": n,
}
print("RECON RESULT:", out)
return out
def _auto_interp_impl(run_name: str, layer: int = 12, max_latents: int = 100,
scorer_model: str = "Qwen/Qwen2.5-3B-Instruct",