Reusable layer-wise representation alignment analysis for multimodal models.
Project description
repralign
repralign is a small, reusable Python toolkit for layer-wise representation alignment analysis in PyTorch and multimodal research workflows. It is designed for experiments where you want to compare intermediate features from one model against one or more reference models, compute similarity curves across layers, and save clean artifacts for later inspection.
The project follows a paper-inspired analysis workflow: compare a candidate model against different reference models, inspect how alignment evolves across layers, and summarize the result with publication-style figures. It is designed as a generic, configurable research toolkit for reusable alignment studies.
Motivation
Many multimodal papers reason about whether a learned representation behaves more like a semantic encoder, a generation-oriented encoder, or something in between. repralign provides a generic foundation for that style of analysis:
- extract intermediate features from configurable layers
- pool or normalize features into comparable representations
- compute layer-wise similarity with reusable metrics
- cache extracted features to avoid repeated forward passes
- save CSV, PNG, and JSON outputs for review and paper drafting
Installation
Install from PyPI:
pip install repralign
Install with optional Hugging Face support:
pip install "repralign[huggingface]"
Install from source for local development:
python -m venv .venv
source .venv/bin/activate
pip install -e .[dev]
For local development with optional Hugging Face support:
pip install -e .[dev,huggingface]
Package Structure
repralign/
src/repralign/
adapters/
metrics/
cache.py
cli.py
config.py
extract.py
hooks.py
plotting.py
pooling.py
registry.py
tests/
examples/
configs/
Practical View Of The Metrics
CKA
Linear CKA measures whether two representation spaces preserve similar pairwise structure across examples. In practice, it is useful when comparing layers with different feature dimensionalities because it focuses on relative geometry rather than exact coordinates.
Expected input shape:
features_a:(n_samples, n_features_a)features_b:(n_samples, n_features_b)
CKNNA
repralign implements CKNNA as a neighborhood-overlap style metric: for each sample, compare its top-k nearest neighbors in one representation space with its top-k nearest neighbors in the other space, then average the overlap ratio. This makes it easy to ask whether two layers induce similar local neighborhoods even if their global geometry differs.
Expected input shape:
features_a:(n_samples, n_features_a)features_b:(n_samples, n_features_b)
Limitations:
- both metrics expect aligned samples in the same row order
- CKNNA becomes unstable for very small batch sizes
- pooled features discard token-level structure in this v0.1
Quickstart
Run the toy comparison example:
python examples/minimal_compare.py
Run the paper-inspired workflow example:
python examples/paper_figure_analysis.py
Both examples save outputs under outputs/.
CLI Usage
List layers from a model factory:
repralign list-layers \
--factory examples.model_factories:build_toy_candidate \
--adapter generic_torch
Extract features with a YAML config:
repralign extract-features --config configs/example_analysis.yaml
Run similarity analysis from cached feature files:
repralign analyze \
--candidate-cache outputs/paper_like/candidate_features.npz \
--reference-cache outputs/paper_like/semantic_reference_features.npz \
--metric cka \
--output-csv outputs/paper_like/cka_semantic.csv \
--output-json outputs/paper_like/cka_semantic.json
Plot one or more similarity curves:
repralign plot \
--input-csv outputs/paper_like/cka_semantic.csv \
--input-csv outputs/paper_like/cka_generation.csv \
--output-png outputs/paper_like/cka_comparison.png \
--title "Layer-wise Alignment"
Python API
from repralign.adapters.generic_torch import GenericTorchAdapter
from repralign.extract import extract_feature_dict
from repralign.metrics import linear_cka
adapter = GenericTorchAdapter(model, layer_names=["encoder.layers.0", "encoder.layers.1"])
features = extract_feature_dict(adapter=adapter, batch=batch, pooling="mean_tokens")
score = linear_cka(features["encoder.layers.0"], features["encoder.layers.1"])
YAML Configs
The v0.1 CLI uses explicit YAML configuration so model factories, layer names, input tensors, pooling, and output directories are visible in one place. See configs/example_analysis.yaml for a runnable example.
Paper-Inspired Workflow
This repository uses published paper-style analysis structure as inspiration for:
- comparing a candidate model against more than one reference model
- organizing figures as layer-wise similarity curves
- structuring experiments around semantic-style and generation-style references
It should not be treated as a source of hidden implementation detail. This repository intentionally keeps model internals configurable and only relies on public, generic analysis primitives.
TODO
- add token-level similarity analysis without pooling
- add dataset loaders beyond tensor files
- add richer multimodal adapters for custom architectures such as Show-o2
- add diffusion and DiT-style reference adapters
- add batched large-scale cache writing for very large datasets
- add confidence intervals and multi-run aggregation utilities
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file repralign-0.1.0.tar.gz.
File metadata
- Download URL: repralign-0.1.0.tar.gz
- Upload date:
- Size: 16.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bfee7d804df33a62f1596da214a4cf645fb52c9032392b7837ca40525c5c943c
|
|
| MD5 |
03d16339048e87229be2414132c4bab9
|
|
| BLAKE2b-256 |
c4fb1c14ff9f201176b7ca3919b0e72c58bfa2cd187158af7c98487954ee44b0
|
Provenance
The following attestation bundles were made for repralign-0.1.0.tar.gz:
Publisher:
publish.yml on Jnmz/repralign
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
repralign-0.1.0.tar.gz -
Subject digest:
bfee7d804df33a62f1596da214a4cf645fb52c9032392b7837ca40525c5c943c - Sigstore transparency entry: 1339573446
- Sigstore integration time:
-
Permalink:
Jnmz/repralign@d41ddd5d25e9ba0e0ce693fab289614a9e518e27 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Jnmz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d41ddd5d25e9ba0e0ce693fab289614a9e518e27 -
Trigger Event:
release
-
Statement type:
File details
Details for the file repralign-0.1.0-py3-none-any.whl.
File metadata
- Download URL: repralign-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06e155515e371f154e81c862f730ca12110e5e0f9eceaa9527bd212deeb310f8
|
|
| MD5 |
b934b5d198ee107c6aa02eb7c69e2166
|
|
| BLAKE2b-256 |
bc319de7cf6b0c6ff06045abd213fa6600977866e9a5efa342f09d81d7fd1be8
|
Provenance
The following attestation bundles were made for repralign-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on Jnmz/repralign
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
repralign-0.1.0-py3-none-any.whl -
Subject digest:
06e155515e371f154e81c862f730ca12110e5e0f9eceaa9527bd212deeb310f8 - Sigstore transparency entry: 1339573447
- Sigstore integration time:
-
Permalink:
Jnmz/repralign@d41ddd5d25e9ba0e0ce693fab289614a9e518e27 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/Jnmz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@d41ddd5d25e9ba0e0ce693fab289614a9e518e27 -
Trigger Event:
release
-
Statement type: