Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -12,34 +12,61 @@ tags:
|
|
| 12 |
|
| 13 |
# MedGemma Circuit Tools
|
| 14 |
|
| 15 |
-
Circuit-tracing artifacts for MedGemma, from SAIL Lab.
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
trained on layer-17 MLP activations collected from radiology-report text
|
| 21 |
-
([ReXGradient-160K](https://huggingface.co/datasets/rajpurkarlab/ReXGradient-160K)).
|
| 22 |
-
Built for circuit tracing and mechanistic analysis of paraphrase sensitivity in
|
| 23 |
-
medical vision-language models (PSF-Med, Sadanandan et al. 2026).
|
| 24 |
-
|
| 25 |
-
## What it is
|
| 26 |
|
| 27 |
- **Architecture:** top-k transcoder, 2,560 → 20,480 features (8x expansion), `top_k = 64`
|
| 28 |
-
- **Hookpoint:**
|
| 29 |
-
|
| 30 |
- **Training:** 50,000 steps, batch 32 x 512 tokens, ~1.6M documents (~11 epochs
|
| 31 |
over ReXGradient-160K Findings + Impression), AdamW lr 1e-4 with cosine decay
|
| 32 |
-
- **Motivation:** layer 17 carries Feature 3818, the "clinical query register gate"
|
| 33 |
-
identified in PSF-Med circuit analysis using Gemma Scope 2. This transcoder is
|
| 34 |
-
the domain-adapted counterpart for MedGemma-specific mechanistic work.
|
| 35 |
|
| 36 |
-
##
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
| Slice | Explained variance | L0 | Notes |
|
| 39 |
|---|---|---|---|
|
| 40 |
| ReXGradient reports (in-distribution) | 0.9964 | 28.8 | matches training |
|
| 41 |
| PSF-Med clinical questions (unseen) | 0.9648 | 55.0 | target distribution for circuit tracing |
|
| 42 |
-
| WikiText-103 (out-of-domain) | 0.8840 | 36.1 | domain-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
## Usage
|
| 45 |
|
|
@@ -51,9 +78,10 @@ from train_layer17_transcoder import (
|
|
| 51 |
)
|
| 52 |
|
| 53 |
model, processor = load_model(device="cuda")
|
| 54 |
-
collector = Layer17ActivationCollector(model, layer=
|
| 55 |
|
| 56 |
-
ckpt = torch.load("
|
|
|
|
| 57 |
transcoder = TranscoderLayer17(d_model=2560, expansion_factor=8, top_k=64,
|
| 58 |
dtype=torch.float32, device="cuda")
|
| 59 |
transcoder.load_state_dict(ckpt["state_dict"])
|
|
@@ -64,31 +92,35 @@ mlp_in, mlp_out = collector.get_activations(inputs)
|
|
| 64 |
reconstructed, features = transcoder(mlp_in.view(-1, 2560).float())
|
| 65 |
```
|
| 66 |
|
| 67 |
-
Training and validation code
|
|
|
|
| 68 |
`circuit_tracing/validate_layer17_transcoder.py` in the
|
| 69 |
[medical-vlm-robustness](https://github.com/UNHSAILLab/medical-vlm-robustness) repository.
|
| 70 |
|
| 71 |
## Limitations
|
| 72 |
|
| 73 |
-
- **
|
| 74 |
-
suite. For other layers or general-domain text, use
|
| 75 |
[Gemma Scope 2](https://huggingface.co/google/gemma-scope-2-4b-it).
|
| 76 |
-
- **Domain
|
| 77 |
-
(EV 0.884 on WikiText)
|
|
|
|
|
|
|
| 78 |
- **Text-only training stream.** No image tokens appeared in training data;
|
| 79 |
reconstruction quality on image-token positions is untested.
|
| 80 |
- **Research artifact.** Not validated for, and not to be used in, any clinical
|
| 81 |
workflow. MedGemma itself is governed by Google's Health AI Developer
|
| 82 |
-
Foundations terms;
|
| 83 |
|
| 84 |
## Citation
|
| 85 |
|
| 86 |
```bibtex
|
| 87 |
-
@misc{
|
| 88 |
-
title = {MedGemma-
|
|
|
|
| 89 |
author = {Sadanandan, Binesh and Behzadan, Vahid},
|
| 90 |
year = {2026},
|
| 91 |
-
note = {Domain-adapted
|
| 92 |
sensitivity in medical VLMs. SAIL Lab, University of New Haven.}
|
| 93 |
}
|
| 94 |
```
|
|
|
|
| 12 |
|
| 13 |
# MedGemma Circuit Tools
|
| 14 |
|
| 15 |
+
Circuit-tracing artifacts for MedGemma, from SAIL Lab. Two transcoders so far,
|
| 16 |
+
at the two ends of the candidate two-stage paraphrase-flip circuit identified in
|
| 17 |
+
PSF-Med (Sadanandan et al. 2026): the layer-17 register gate (Feature 3818) and
|
| 18 |
+
the layer-29 decision feature (Feature 12139).
|
| 19 |
|
| 20 |
+
Both are top-k transcoders trained on MLP activations of
|
| 21 |
+
[`google/medgemma-4b-it`](https://huggingface.co/google/medgemma-4b-it) collected
|
| 22 |
+
from radiology-report text
|
| 23 |
+
([ReXGradient-160K](https://huggingface.co/datasets/rajpurkarlab/ReXGradient-160K)),
|
| 24 |
+
for circuit tracing and mechanistic analysis of paraphrase sensitivity in medical
|
| 25 |
+
vision-language models.
|
| 26 |
|
| 27 |
+
## Shared configuration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
- **Architecture:** top-k transcoder, 2,560 → 20,480 features (8x expansion), `top_k = 64`
|
| 30 |
+
- **Hookpoint:** MLP output reconstructed from MLP input at the target layer of
|
| 31 |
+
MedGemma-4B's language backbone (Gemma 3 4B)
|
| 32 |
- **Training:** 50,000 steps, batch 32 x 512 tokens, ~1.6M documents (~11 epochs
|
| 33 |
over ReXGradient-160K Findings + Impression), AdamW lr 1e-4 with cosine decay
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
## Entry 1: Layer-17 transcoder (`layer17_transcoder_final.pt`)
|
| 36 |
+
|
| 37 |
+
- **Motivation:** layer 17 carries Feature 3818, the "clinical query register
|
| 38 |
+
gate" identified in PSF-Med circuit analysis using Gemma Scope 2. This is the
|
| 39 |
+
domain-adapted counterpart for MedGemma-specific mechanistic work.
|
| 40 |
+
- **Final training metrics:** loss 5.8e-05, EV 0.996, L0 29.5
|
| 41 |
+
|
| 42 |
+
Validation (post-training, 64 documents per slice):
|
| 43 |
|
| 44 |
| Slice | Explained variance | L0 | Notes |
|
| 45 |
|---|---|---|---|
|
| 46 |
| ReXGradient reports (in-distribution) | 0.9964 | 28.8 | matches training |
|
| 47 |
| PSF-Med clinical questions (unseen) | 0.9648 | 55.0 | target distribution for circuit tracing |
|
| 48 |
+
| WikiText-103 (out-of-domain) | 0.8840 | 36.1 | strongly domain-specialized |
|
| 49 |
+
|
| 50 |
+
## Entry 2: Layer-29 transcoder (`layer29_transcoder_final.pt`)
|
| 51 |
+
|
| 52 |
+
- **Motivation:** layer 29 carries Feature 12139, the downstream yes/no decision
|
| 53 |
+
feature in the candidate two-stage 3818 → 12139 circuit. Training a matched
|
| 54 |
+
transcoder here allows the circuit account to be re-tested with domain-adapted
|
| 55 |
+
features at both stages instead of borrowed Gemma Scope 2 features.
|
| 56 |
+
- **Final training metrics:** loss 1.0e-04, EV 0.998, L0 27.3
|
| 57 |
+
|
| 58 |
+
Validation (post-training, 64 documents per slice):
|
| 59 |
+
|
| 60 |
+
| Slice | Explained variance | L0 | Notes |
|
| 61 |
+
|---|---|---|---|
|
| 62 |
+
| ReXGradient reports (in-distribution) | 0.9985 | 27.1 | matches training |
|
| 63 |
+
| PSF-Med clinical questions (unseen) | 0.9627 | 60.1 | target distribution for circuit tracing |
|
| 64 |
+
| WikiText-103 (out-of-domain) | 0.9611 | 34.7 | largely domain-general |
|
| 65 |
+
|
| 66 |
+
Note the contrast between the two entries: the layer-17 transcoder loses ~11 EV
|
| 67 |
+
points off-domain while the layer-29 transcoder loses ~4, evidence that the
|
| 68 |
+
register computation at layer 17 is medically specialized while the late decision
|
| 69 |
+
computation at layer 29 is more generic.
|
| 70 |
|
| 71 |
## Usage
|
| 72 |
|
|
|
|
| 78 |
)
|
| 79 |
|
| 80 |
model, processor = load_model(device="cuda")
|
| 81 |
+
collector = Layer17ActivationCollector(model, layer=29) # or 17
|
| 82 |
|
| 83 |
+
ckpt = torch.load("layer29_transcoder_final.pt", map_location="cpu",
|
| 84 |
+
weights_only=False)
|
| 85 |
transcoder = TranscoderLayer17(d_model=2560, expansion_factor=8, top_k=64,
|
| 86 |
dtype=torch.float32, device="cuda")
|
| 87 |
transcoder.load_state_dict(ckpt["state_dict"])
|
|
|
|
| 92 |
reconstructed, features = transcoder(mlp_in.view(-1, 2560).float())
|
| 93 |
```
|
| 94 |
|
| 95 |
+
Training and validation code (layer-selectable via `--layer`):
|
| 96 |
+
`circuit_tracing/train_layer17_transcoder.py`,
|
| 97 |
`circuit_tracing/validate_layer17_transcoder.py` in the
|
| 98 |
[medical-vlm-robustness](https://github.com/UNHSAILLab/medical-vlm-robustness) repository.
|
| 99 |
|
| 100 |
## Limitations
|
| 101 |
|
| 102 |
+
- **Two layers only.** These are layer-17 and layer-29 transcoders, not a full
|
| 103 |
+
transcoder suite. For other layers or general-domain text, use
|
| 104 |
[Gemma Scope 2](https://huggingface.co/google/gemma-scope-2-4b-it).
|
| 105 |
+
- **Domain profile differs by layer.** The layer-17 transcoder degrades on
|
| 106 |
+
non-medical text (EV 0.884 on WikiText) and should be treated as
|
| 107 |
+
medical-only; the layer-29 transcoder is more forgiving (EV 0.961) but was
|
| 108 |
+
still trained on medical text only.
|
| 109 |
- **Text-only training stream.** No image tokens appeared in training data;
|
| 110 |
reconstruction quality on image-token positions is untested.
|
| 111 |
- **Research artifact.** Not validated for, and not to be used in, any clinical
|
| 112 |
workflow. MedGemma itself is governed by Google's Health AI Developer
|
| 113 |
+
Foundations terms; these artifacts are derived from MedGemma activations.
|
| 114 |
|
| 115 |
## Citation
|
| 116 |
|
| 117 |
```bibtex
|
| 118 |
+
@misc{sadanandan2026medgemma_circuit_tools,
|
| 119 |
+
title = {MedGemma Circuit Tools: Domain-Adapted Layer-17 and Layer-29
|
| 120 |
+
Transcoders (ReXGradient-trained)},
|
| 121 |
author = {Sadanandan, Binesh and Behzadan, Vahid},
|
| 122 |
year = {2026},
|
| 123 |
+
note = {Domain-adapted transcoders for circuit tracing of paraphrase
|
| 124 |
sensitivity in medical VLMs. SAIL Lab, University of New Haven.}
|
| 125 |
}
|
| 126 |
```
|