Code & Repository: https://github.com/kernel-loophole/autoencoder →
Linear probing and direct linear interventions (\(h_t' = h_t + \alpha \cdot \text{probe}\)) in vision-language models can produce collateral semantic changes because learned linear directions are frequently entangled with non-target properties such as color, texture, object identity, shape, background, and language attributes.
This research codebase investigates whether Multimodal Sparse Autoencoders (SAEs) provide a more interpretable, disentangled feature basis for spatial concepts (such as containment, inside/outside, left/right, above/below, front/behind, depth ordering).
Linear probing and direct linear interventions (\(h_t' = h_t + \alpha \cdot \text{probe}\)) in vision-language models (VLMs) can produce collateral semantic changes because learned linear directions are frequently entangled with non-target visual and linguistic properties, such as object color, shape, texture, identity, and background context.
This project investigates whether Sparse Autoencoders (SAEs) can extract a more interpretable, disentangled feature basis that enables precise causal steering of spatial reasoning while preserving unrelated semantic attributes.
add, clamp, suppress, and scale in the SAE latent space (\(h \to z \to z' \to h'\)).
The framework implements an end-to-end modular pipeline consisting of seven decoupled stages:
┌─────────────────────────┐
│ Synthetic Scene Data │
│ & Counterfactual Pairs │
└────────────┬────────────┘
│
▼
┌─────────────────────────┐
│ Vision-Language Model │
│ (VLM Adapter) │
└────────────┬────────────┘
│
▼
┌─────────────────────────┐
│ Activation Extraction │
│ & Layer Storage │
└────────────┬────────────┘
│
▼
┌─────────────────────────┐
│ Sparse Autoencoder │
│ (SAE Training) │
└────────────┬────────────┘
│
▼
┌─────────────────────────┐
│ Feature Discovery & │
│ Correlation Analysis │
└────────────┬────────────┘
│
▼
┌─────────────────────────┐
│ Causal Steering │
│ Intervention Engine │
└────────────┬────────────┘
│
▼
┌─────────────────────────┐
│ Disentanglement & │
│ Causal Evaluation │
└─────────────────────────┘
Given extracted model residual activations \(x \in \mathbb{R}^d\), the Sparse Autoencoder projects \(x\) into an expanded sparse latent representation \(z \in \mathbb{R}^M\):
With an \(L_1\)-penalized reconstruction loss:
Or alternatively using Top-K SAEs:
During forward pass inference, we intercept activation \(h\) at the target layer, compute latent activations \(z\), manipulate the target spatial feature index \(j\), and reconstruct into the residual stream:
.
├── README.md
├── LICENSE
├── pyproject.toml
├── .gitignore
├── .env.example
├── configs/
│ ├── model/ # VLM specifications (Qwen2-VL, LLaVA, Mock)
│ ├── data/ # Synthetic scene & counterfactual configs
│ ├── sae/ # SAE architectures (L1, Top-K, loss)
│ ├── extraction/ # Activation caching & layer storage configs
│ ├── intervention/ # Causal steering strategies & strengths
│ └── experiments/ # End-to-end evaluation runs
├── src/
│ └── spatial_sae/
│ ├── data/ # Synthetic scene rendering & counterfactual pairs
│ ├── models/ # VLM adapters & forward hook interceptors
│ ├── extraction/ # Layer activation collectors & chunked disk storage
│ ├── sae/ # Sparse Autoencoder PyTorch models & trainers
│ ├── probing/ # Linear probe baselines & probe direction steering
│ ├── intervention/ # SAE causal steering engine & runtime hooks
│ ├── evaluation/ # Spatial flip rate, attribute specificity & KL drift
│ ├── experiments/ # Compositional binding & relation transfer benchmarks
│ └── utils/ # Utilities, config loaders & logging
├── scripts/
│ ├── prepare_data.py
│ ├── extract_activations.py
│ ├── train_sae.py
│ ├── analyze_features.py
│ ├── run_probe.py
│ └── run_intervention.py
├── tests/
├── notebooks/
├── checkpoints/
└── outputs/
| Module | Description |
|---|---|
spatial_sae.data |
Manages synthetic scene rendering, counterfactual pair generation, schemas, and PyTorch datasets. |
spatial_sae.models |
VLM adapters (VLMAdapter, HuggingFaceVLMAdapter, MockVLMAdapter), representation definitions, and forward hook managers. |
spatial_sae.extraction |
Layer activation collection, disk storage shards (ActivationStorage), and extraction pipelines (ActivationExtractor). |
spatial_sae.sae |
PyTorch Sparse Autoencoder (SparseAutoencoder), \(L_1\) / Top-K loss functions (SAELoss), trainer (SAETrainer), metrics, and feature discovery tools. |
spatial_sae.probing |
Linear probe baselines (LinearProbe) and direction steering comparison (\(h' = h + \alpha \cdot w_{\text{probe}}\)). |
spatial_sae.intervention |
Steering intervention engine (SAEInterventionEngine), PyTorch forward steering hooks, and strategies (add, clamp, suppress, scale). |
spatial_sae.evaluation |
Quantitative metrics for spatial accuracy, feature selectivity, counterfactual invariance, specificity score, and KL divergence. |
spatial_sae.experiments |
Standardized experiment execution scripts and compositional binding evaluation. |
You can run a complete end-to-end synthetic pipeline (dataset generation, dry-run extraction, SAE training, feature discovery, and causal intervention) without downloading a VLM model:
python scripts/prepare_data.py --config configs/data/default.yaml --num_pairs 100
python scripts/extract_activations.py --config configs/extraction/default.yaml --dry_run
python scripts/train_sae.py --config configs/sae/default.yaml
python scripts/analyze_features.py --config configs/sae/default.yaml
# Linear Probe Baseline
python scripts/run_probe.py --config configs/experiments/default.yaml
# SAE Causal Steering & Compositional Binding Experiment
python scripts/run_intervention.py --config configs/intervention/default.yaml --compositional
To configure and run experiments on a real open-source Vision-Language Model (such as Qwen/Qwen2-VL-7B-Instruct or llava-hf/llava-1.5-7b-hf):
configs/model/default.yaml)model_name: "Qwen/Qwen2-VL-7B-Instruct"
adapter_type: "huggingface"
device: "cuda"
dtype: "bfloat16"
target_layer: "model.layers.16"
python scripts/extract_activations.py --config configs/extraction/default.yaml --model_config configs/model/default.yaml
python scripts/train_sae.py --config configs/sae/default.yaml
python scripts/run_intervention.py --config configs/intervention/default.yaml
| Metric | Definition & Objective |
|---|---|
| Spatial Accuracy | Correctness of target spatial relation classification. |
| Spatial Selectivity & Purity | Correlation with spatial label vs. maximum correlation with non-spatial attribute confounds. |
| Counterfactual Invariance | Cosine similarity of feature activations between counterfactual scene pairs (e.g., A: red ball in blue box vs B: blue ball in red box). |
| Intended Effect Rate | Percentage of steered forward passes where model prediction matches the targeted spatial relation. |
| Specificity & Collateral Shift | Frequency of unintended mutations to object identity/color bindings; measured alongside KL divergence of output logit distributions. |
set_seed()) synchronized across Python, NumPy, and PyTorch.configs/**/*.yaml) serialized and saved alongside all checkpoint weights and evaluation runs.
The full research codebase, experimental configs, and reproduction scripts are hosted on GitHub:
https://github.com/kernel-loophole/autoencoder →