Metric Choice Determines Semantic Geometry in LLM Hidden States
Accepted at the Mechanistic Interpretability Workshop at ICML 2026 (Seoul, South Korea).
This repository is the code, prompt-suite, and results package for the accepted workshop paper:
Metric Choice Determines Semantic Geometry in LLM Hidden States: A Measurement Study of Hidden-State Geometry
It does not include manuscript source or camera-ready assets; see
REPRODUCIBILITY_PACKAGE.md for what is and is not packaged.
The paper tests three falsifiable hypotheses about layerwise hidden-state geometry in GPT-2, Phi-4-mini, Mistral-7B, and Gemma-3-12B:
- Expansion. Raw norms and raw pairwise L2 distances grow with depth across prompt families.
- Not semantic. The same growth appears for incoherent and unrelated
controls, so raw expansion is not evidence of semantic separation. In the
32-vs-32 MIND diagnostic, the median raw-norm growth from the embedding layer
to the final layer across the four checkpoints is
31.5xfor meaningful news prompts and33.3xfor length-matched unigram-random controls from the same word pool (out/pass_2026-05-05/mind_meaningful_vs_unigram_random_all_models/). - Recoverable. Sentence-, topic-, and category-level structure appears
under shared-centroid (contrast-centered) cosine distance, which is the
paper's primary semantic metric. The full four-checkpoint pass is in
out/pass_2026-05-08/contrast_centered_cosine_full_4model/.
The takeaway is that the choice of metric, not the raw distance growth, determines whether hidden-state geometry reads as semantic structure. Raw norm and raw L2 are treated as expansion diagnostics only.
This repo studies what layerwise distances and separations in transformer hidden states mean for controlled prompt suites. The current research question is not whether distance directly measures "meaning"; it is when a particular representation and distance metric support a falsifiable semantic or topic-structure interpretation.
The latest completed result surface is the April 29, 2026 AG News random-cloud run. It replaces the earlier MIND 43-word random word-bag baseline as the current external-suite result. The active claim is:
Mean-pooled centered-cosine geometry tracks dataset-level semantic and topic structure across controlled prompt families, while last-token geometry is a useful but weaker decoder-final-position baseline.
The current clean external prompt artifact is:
prompt_suites/external_prompt_suite_stsb_agnews_words_512.jsonl
It is generated by:
./.venv/bin/python scripts/build_stsb_agnews_prompt_corpus.py --seed 0It contains 512 unique prompt texts across nine families:
stsb_high_similarity:64prompts from32high-score STS-B designed sentence pairsstsb_low_similarity:64prompts from32low-score STS-B designed sentence pairsag_news_same_topic:64prompts from32same-label AG News designed article pairsag_news_cross_topic:64prompts from32different-label AG News designed article pairsag_news_random_64_stratified:64real AG News articles, stratified as16prompts per AG News label; analysis uses all2016unordered pairs and splits them into480within-label pairs and1536between-label pairsmind_news_same_category:64prompts from32same-subcategory MIND designed title/abstract pairsmind_news_random_64_category:64title/abstract prompts from64distinct MIND news subcategories; analysis uses all2016exact category-disjoint unordered article pairsword_random:32single-word prompts drawn from low-score SimLex-999 pairs; analysis uses all496exact category-disjoint unordered word pairsword_correlated:32prompts from16high-score SimLex-999 designed word pairs
The AG News random cloud is now the dataset-native AG News background distribution. It should be used for the internal AG News within/all/between diagnostic instead of the superseded synthetic MIND word-bag baseline.
The primary interpretive surface is mean_pool: the average of all
non-padding token hidden states at each layer. This gives one vector per prompt
while using the whole prompt.
The last_token surface remains required as a baseline because it matches the
decoder-LM final-position representation used by the earlier reproduction
surface. It should be interpreted as decoder-final-position geometry, not
automatically as whole-prompt semantic geometry.
Interpretation rules:
- effects stable across
mean_poolandlast_tokenare stronger evidence for prompt-level geometry - effects found only under
last_tokenare decoder-final-position effects - effects found only under
mean_poolare distributed prompt-level effects - low-dimensional PCA shape alone is not semantic evidence without supporting distance statistics and extraction-mode comparison
The completed April 29 full 512-prompt AG News random-cloud distance passes are:
out/pass_2026-04-29/external_agnews_random64_full_model_distance_mean_pool/
out/pass_2026-04-29/external_agnews_random64_full_model_distance_last_token/
The batch runner wrote these status files:
out/pass_2026-04-29/external_agnews_random64_full_model_distance_mean_pool/distance_extraction_status_2026-04-29-external-agnews-random64-mean-pool.json
out/pass_2026-04-29/external_agnews_random64_full_model_distance_last_token/distance_extraction_status_2026-04-29-external-agnews-random64-last-token.json
Both status files report ok for the four retained model slugs:
gpt2phi4minimistral7bgemma3_12b
Required per-model files:
README_results.txtlayerwise_family_distance_summary.csvlayerwise_pair_distances_long.csvlayerwise_family_extrema.csvpair_manifest.csvplots/<pair_group>_<metric>_four_stats.png
layerwise_pair_distances_long.csv is written by every run but is omitted from
this repository because of its size (it is reproducible from the prompt suite
and scripts/run_distance_extraction_pass.py). Compact CSVs derived from it are
under results/derived/.
The active metrics are:
raw_l2raw_cosinecentered_l2centered_cosine
Every summary row records pair_scope and extraction_mode. Designed
families use their metadata-defined pairs. Random-control pools marked with
pair_summary_policy: exact_constrained_all_pairs use exact all-pair summaries
for mean, median, min, and max.
The newest centered-cosine policy is contrast_center: for each controlled
comparison, both compared prompt groups share a centroid before cosine distance
is computed. The full four-checkpoint pass is stored at:
out/pass_2026-05-08/contrast_centered_cosine_full_4model/
Average final-layer contrast-centered gaps (mean_pool) across GPT-2,
Phi-4-mini, Mistral-7B, and Gemma-3-12B
(results/derived/contrast_centered_cosine_final_layer_gaps_full_4model.csv):
| contrast | average control-minus-related gap |
|---|---|
| STS-B low minus high similarity | 0.2730 |
| AG News cross minus same topic | 0.1818 |
| MIND random minus same category | 0.2254 |
| SimLex random minus correlated words | 0.1127 |
The regenerated paper figures are under paper_scripts/figures/, including
paper_scripts/figures/main_fig2_centered_cosine_results.png and
paper_scripts/figures/appendix/appendix_centered_cosine_comparisons.png.
The strongest current AG News result is internal to
ag_news_random_64_stratified. At the final layer, averaged across the four
retained model representatives:
| extraction mode | all 2016 pairs | within-label 480 pairs | between-label 1536 pairs | between minus within |
|---|---|---|---|---|
mean_pool |
1.0155 | 0.8430 | 1.0694 | 0.2264 |
last_token |
1.0017 | 0.8719 | 1.0422 | 0.1703 |
This is the cleanest current random-cloud comparison because all three values come from the same AG News pair surface and the same family-centering surface. Mean-pool gives the larger within-vs-between gap.
Other current findings:
- STS-B high-similarity pairs are closer than STS-B low-similarity pairs in
4/4retained models. - AG News same-topic designed pairs are closer than AG News cross-topic
designed pairs in
4/4retained models. - MIND same-subcategory articles are closer than random cross-subcategory MIND
articles in
4/4retained models.
Paper figures and the inspection graph pack:
paper_scripts/figures/main_fig1_broad_expansion.pngpaper_scripts/figures/main_fig2_centered_cosine_results.pngpaper_scripts/figures/appendix/appendix_centered_cosine_comparisons.pnggraphs/inspect_graphs/README.mdgraphs/inspect_graphs/manifest.json
Manuscript source, progress notes, and the earlier inspect*.md interpretation
notes are intentionally not part of this package; see
REPRODUCIBILITY_PACKAGE.md.
Graph pack layout:
graphs/inspect_graphs/mean-pool/<model>/...graphs/inspect_graphs/last-token/<model>/...graphs/inspect_graphs/pca_layers/
Key graph files (one set per model folder):
ag_main_groups_random64.pngag_random64_within_between.pngstsb_high_vs_low.pngag_same_vs_cross.pngmind_same_vs_random_article.pngword_correlated_vs_random.png
distance_extraction/: reusable extraction, pair construction, metrics, and output-writing packagescripts/: active command-line entry points for prompt building, model runs, inspect graph generation, and PCA visualizationprompt_suites/: generated JSONL prompt suites kept as reviewable inputsdocs/: workflow contracts, rules, and prompt-family design notesout/: retained compact generated outputs (summaries, manifests, plots)results/derived/: compact CSVs derived from the omitted raw pairwise CSVs, used to audit the paper tables and figuresgraphs/inspect_graphs/: generated inspection graph packpaper_scripts/: figure-generation scripts and the generated paper figures underpaper_scripts/figures/tests/: regression tests for extraction modes and pair policiesPACKAGE_MANIFEST.json,REPRODUCIBILITY_PACKAGE.md: package file list and notes on what is included and excluded
The active batch runner now targets the useful general-purpose model set for expansion-rate experiments:
openai-community/gpt2microsoft/Phi-4-mini-instructmistralai/Mistral-7B-v0.1google/gemma-3-12b-pt
Bootstrap:
./init.shAuthenticated Hugging Face run surface:
export HF_HOME="$HOME/.cache/huggingface"
export XDG_CACHE_HOME="$HOME/.cache"
export MPLCONFIGDIR=/tmp/iw-cache/mplRegenerate the current external prompt suite:
./.venv/bin/python scripts/build_stsb_agnews_prompt_corpus.py --seed 0Run a current full distance pass:
./.venv/bin/python scripts/run_distance_extraction_pass.py \
--prompts prompt_suites/external_prompt_suite_stsb_agnews_words_512.jsonl \
--out_root out/pass_2026-04-29/external_agnews_random64_full_model_distance_mean_pool \
--date_tag 2026-04-29-external-agnews-random64-mean-pool \
--run_label distance_512 \
--device cuda \
--max_length 256 \
--extraction_mode mean_pool \
--distance_block 256 \
--metrics raw_l2 raw_cosine centered_l2 centered_cosineRun the same command with --out_root out/pass_2026-04-29/external_agnews_random64_full_model_distance_last_token,
--date_tag 2026-04-29-external-agnews-random64-last-token, and
--extraction_mode last_token for the decoder-final-position baseline.
Derive expansion rates from existing distance outputs (this reads
layerwise_pair_distances_long.csv, which is not shipped in this repository, so
run the two distance passes above first):
./.venv/bin/python scripts/extract_expansion_rates.py \
--distance_roots out/pass_2026-04-29/external_agnews_random64_full_model_distance_mean_pool \
out/pass_2026-04-29/external_agnews_random64_full_model_distance_last_token \
--metrics centered_cosine \
--min_previous_distance 1e-4 \
--out_dir out/pass_2026-05-04/expansion_rates_agnews_random64For attention-vs-MLP component diagnostics, rerun extraction with component hooks:
./.venv/bin/python scripts/run_distance_extraction_pass.py \
--prompts prompt_suites/external_prompt_suite_stsb_agnews_words_512.jsonl \
--out_root out/pass_2026-05-04/external_agnews_random64_component_distance_mean_pool \
--date_tag 2026-05-04-external-agnews-random64-components-mean-pool \
--run_label distance_512_components \
--device cuda \
--max_length 256 \
--extraction_mode mean_pool \
--capture_block_components \
--metrics centered_cosineCheap smoke run:
./.venv/bin/python scripts/run_distance_extraction_pass.py \
--models gpt2 \
--prompts prompt_suites/external_prompt_suite_stsb_agnews_words_512.jsonl \
--out_root out/smoke_distance_extraction \
--date_tag smoke \
--device cpu \
--max_prompts 8 \
--extraction_mode mean_pool \
--metrics raw_l2 raw_cosine centered_l2 centered_cosine