1- # Alignment Framework
1+ # LossLens
22
3- Neural network analysis and structured pruning using alignment metrics and information theory .
3+ Loss-sensitive neural network analysis and structured pruning tools .
44
5+ [ ![ Tests] ( https://github.com/KempnerInstitute/alignment/actions/workflows/test.yml/badge.svg )] ( https://github.com/KempnerInstitute/alignment/actions/workflows/test.yml )
56[ ![ Lint] ( https://github.com/KempnerInstitute/alignment/actions/workflows/lint.yml/badge.svg )] ( https://github.com/KempnerInstitute/alignment/actions/workflows/lint.yml )
6- [ ![ Pre-commit] ( https://github.com/KempnerInstitute/alignment/actions/workflows/pre-commit.yml/badge.svg )] ( https://github.com/KempnerInstitute/alignment/actions/workflows/pre-commit.yml )
7- [ ![ Code Lines] ( https://img.shields.io/tokei/lines/github/KempnerInstitute/alignment?logo=files&logoColor=white )] ( https://github.com/KempnerInstitute/alignment )
8- [ ![ CLI] ( https://img.shields.io/badge/CLI-scripts%2Frun_experiment.py-121011?logo=gnubash&logoColor=white )] ( scripts/run_experiment.py )
7+ [ ![ Documentation] ( https://github.com/KempnerInstitute/alignment/actions/workflows/docs.yml/badge.svg )] ( https://github.com/KempnerInstitute/alignment/actions/workflows/docs.yml )
8+ [ ![ Release] ( https://github.com/KempnerInstitute/alignment/actions/workflows/release.yml/badge.svg )] ( https://github.com/KempnerInstitute/alignment/actions/workflows/release.yml )
99[ ![ Python] ( https://img.shields.io/badge/python-%3E%3D3.8-3776AB?logo=python&logoColor=white )] ( pyproject.toml )
10+ [ ![ Artifacts] ( https://img.shields.io/badge/Hugging%20Face-artifacts-ffcc33 )] ( https://huggingface.co/datasets/hsafaai/supernodes-scar-artifacts )
11+ [ ![ License] ( https://img.shields.io/badge/license-MIT-blue.svg )] ( LICENSE )
12+
13+ LossLens is a research codebase for studying which channels, neurons, and
14+ features matter most for model behavior. The current Python package is imported
15+ as ` alignment ` for backward compatibility.
16+
17+ The repository supports two related workflows:
18+
19+ - General metric analysis for vision models, transformers, and LLMs.
20+ - Paper-specific releases under ` projects/ ` , including the Supernodes and SCAR
21+ artifact workflow.
22+
23+ ## What The Code Does
24+
25+ ``` mermaid
26+ flowchart LR
27+ A[Model + calibration data] --> B[Capture activations and gradients]
28+ B --> C[Compute channel metrics]
29+ C --> D[Identify loss-critical cores]
30+ C --> E[Estimate redundancy and halo structure]
31+ D --> F[Structured pruning and ablation probes]
32+ E --> F
33+ F --> G[Figures, tables, manifests, HF artifacts]
34+ ```
1035
11- ## Overview
12-
13- This framework provides tools for analyzing and pruning neural networks through:
36+ Core capabilities:
1437
15- - ** Alignment metrics** : Rayleigh quotient, activation-based importance
16- - ** Information-theoretic analysis** : Mutual information, redundancy, synergy
17- - ** Cluster-based analysis** : Functional type identification, cross-layer halo tracking
18- - ** Structured pruning** : Channel/neuron removal with multiple scoring strategies
38+ - Loss-sensitive channel scoring, including SCAR loss-proxy metrics.
39+ - Activation, curvature, Taylor, Rayleigh quotient, and information-theoretic metrics.
40+ - Structured pruning strategies for channel-level model analysis.
41+ - Cluster and halo-style analyses for local redundancy structure.
42+ - Reproducible project folders for paper artifacts and public releases.
1943
20- ** Supported architectures** : MLPs, CNNs (ResNet, VGG, MobileNet), Transformers, LLMs (LLaMA, Mistral, Qwen)
44+ Supported model families include MLPs, CNNs, transformer language models, and
45+ LLM backends through Hugging Face causal language models.
2146
2247## Installation
2348
@@ -29,144 +54,110 @@ conda activate alignment
2954pip install -e .
3055```
3156
57+ For documentation and optional analysis tools:
58+
59+ ``` bash
60+ pip install -e .[all]
61+ ```
62+
3263## Quick Start
3364
3465``` bash
3566# Vision model analysis
3667python scripts/run_experiment.py --config configs/examples/mnist_basic.yaml
3768
3869# CNN pruning
39- python scripts/run_experiment.py --config configs/examples/resnet_pruning .yaml
70+ python scripts/run_experiment.py --config configs/vision_prune/resnet18_cifar10_full .yaml
4071
41- # LLM analysis
42- python scripts/run_experiment.py --config configs/paper/llama3_8b_full.yaml
43-
44- # Cluster-based analysis
45- python scripts/run_experiment.py --config configs/cluster_analysis/resnet18_cifar10_full.yaml
72+ # LLM supernode and SCAR analysis
73+ python scripts/run_experiment.py --config configs/prune_llm/llama3_8b_unified.yaml
4674```
4775
48- ## Experiment Types
76+ Package the public Supernodes and SCAR artifacts:
77+
78+ ``` bash
79+ python projects/supernodes_scar/scripts/prepare_hf_artifacts.py \
80+ --output-dir outputs/supernodes_scar_hf \
81+ --clean
4982
50- | Type | Description | Config Example |
51- | ------| -------------| ----------------|
52- | ` alignment_analysis ` | General alignment metrics | ` mnist_basic.yaml ` |
53- | ` llm_alignment ` | LLM supernode/SCAR analysis | ` llama3_8b_full.yaml ` |
54- | ` cluster_analysis ` | Metric-space clustering with halos | ` resnet18_cifar10_full.yaml ` |
83+ python projects/supernodes_scar/scripts/verify_hf_artifacts.py \
84+ outputs/supernodes_scar_hf
85+ ```
5586
56- ## Metrics
87+ ## Paper Releases
5788
58- | Category | Metrics |
59- | ----------| ---------|
60- | Activation | ` activation_l2_norm ` , ` activation_variance ` , ` activation_outlier_index ` |
61- | Alignment | ` rayleigh_quotient ` , ` delta_alignment ` |
62- | Information | ` mutual_information_gaussian ` , ` pairwise_redundancy_gaussian ` , ` gaussian_pid_synergy_mmi ` |
63- | SCAR (LLM) | ` scar_activation_power ` , ` scar_taylor ` , ` scar_curvature ` , ` scar_loss_proxy ` |
64- | Synergy | ` synergy_continuous_target ` (with logit margin) |
89+ Paper-specific release material lives under ` projects/ ` . Reusable library code
90+ stays in ` src/alignment ` , while each project folder records the exact configs,
91+ artifact layout, reproducibility notes, and release checklist for a paper.
6592
66- ## Cluster-Based Analysis
93+ Current project:
6794
68- The cluster analysis framework groups channels/neurons into functional types:
95+ - ` projects/supernodes_scar/ ` : release material for "Supernodes and Halos:
96+ Loss-Critical Hubs in LLM Feed-Forward Layers".
6997
70- | Type | Characteristics | Pruning Implication |
71- | ------| -----------------| ---------------------|
72- | Critical | High RQ, Low Redundancy, High Synergy | Protect |
73- | Redundant | Moderate RQ, High Redundancy | Target for pruning |
74- | Synergistic | Moderate RQ, High Synergy | Preserve pairs |
75- | Background | Low on all metrics | Safe to remove |
98+ Derived artifacts for this project are staged on Hugging Face:
7699
77- Cross-layer halo analysis tracks downstream dependencies to predict cascade effects.
100+ - ` https://huggingface.co/datasets/hsafaai/supernodes-scar-artifacts `
78101
79- ## Pruning Strategies
102+ ## Main Concepts
80103
81- | Strategy | Description |
82- | ---------- | --- ----------|
83- | ` magnitude ` | Prune by weight magnitude |
84- | ` alignment ` | Prune by alignment score |
85- | ` composite ` | Combine multiple metrics |
86- | ` cluster_aware ` | Use cluster membership and halo analysis |
87- | ` random ` | Random baseline |
104+ | Area | Examples |
105+ | ------| ----------|
106+ | Activation metrics | ` activation_l2_norm ` , ` activation_variance ` , ` activation_outlier_index ` |
107+ | Alignment metrics | ` rayleigh_quotient ` , ` delta_alignment ` |
108+ | Information metrics | ` mutual_information_gaussian ` , ` pairwise_redundancy_gaussian ` , ` gaussian_pid_synergy_mmi ` |
109+ | SCAR metrics | ` scar_activation_power ` , ` scar_taylor ` , ` scar_curvature ` , ` scar_loss_proxy ` |
110+ | Pruning strategies | ` magnitude ` , ` alignment ` , ` composite ` , ` cluster_aware ` , ` random ` |
88111
89- ## Project Structure
112+ ## Repository Layout
90113
91- ```
114+ ``` text
92115alignment/
93- ├── configs/
94- | ├── cluster_analysis/ # Cluster-based analysis configs
95- | ├── paper/ # Paper experiment configs
96- | └── examples/ # Example configs
97- ├── scripts/
98- | ├── run_experiment.py # Main entry point
99- | └── run_analysis.py # Post-hoc analysis
100- ├── src/alignment/
101- | ├── analysis/ # Visualization, clustering, cascade analysis
102- | ├── experiments/ # Experiment classes
103- | ├── metrics/ # Importance metrics
104- | ├── models/ # Model wrappers
105- | └── pruning/ # Pruning strategies
106- ├── tests/ # Unit tests
107- └── docs/ # Documentation
116+ |-- configs/
117+ | |-- prune_llm/ # LLM and SCAR configs
118+ | |-- vision_prune/ # Vision pruning configs
119+ | `-- examples/ # Small example configs
120+ |-- projects/ # Paper-specific release material
121+ |-- scripts/
122+ | |-- run_experiment.py # Main experiment entry point
123+ | `-- run_analysis.py # Post-hoc analysis
124+ |-- src/alignment/
125+ | |-- analysis/ # Visualization, clustering, cascade analysis
126+ | |-- experiments/ # Experiment classes
127+ | |-- metrics/ # Importance metrics
128+ | |-- models/ # Model wrappers
129+ | `-- pruning/ # Pruning strategies
130+ |-- tests/ # Unit tests
131+ `-- docs/ # Documentation
108132```
109133
110- ## Key Modules
111-
112- ### Analysis
113- - ` MetricSpaceClustering ` : K-means clustering in (RQ, Redundancy, Synergy) space
114- - ` CrossLayerHaloAnalysis ` : Track downstream channel dependencies
115- - ` CascadeAnalysis ` : Validate importance via ablation
116- - ` UnifiedVisualizer ` : Generate analysis plots
117-
118- ### Experiments
119- - ` GeneralAlignmentExperiment ` : Vision model analysis
120- - ` LLMAlignmentExperiment ` : LLM supernode and SCAR analysis
121- - ` ClusterAnalysisExperiment ` : Cluster-based analysis for any architecture
122-
123- ### Metrics
124- - ` RayleighQuotient ` : Input-weight alignment
125- - ` PairwiseRedundancyGaussian ` : Gaussian MI-based redundancy
126- - ` SynergyContinuousTarget ` : PID synergy with continuous target
127- - SCAR metrics for LLMs
128-
129134## Documentation
130135
131- - [ Usage Guide] ( docs/usage.md ) - Running experiments and configuration
132- - [ API Reference] ( docs/api_reference.md ) - Core classes and functions
133- - [ LLM Guide] ( docs/llm_guide.md ) - LLM-specific analysis
134- - [ Metric Consistency] ( docs/METRIC_CONSISTENCY.md ) - Theory-code verification
135-
136- ## Configuration
137-
138- ``` yaml
139- experiment_type : cluster_analysis # or llm_alignment, alignment_analysis
140-
141- model :
142- name : resnet18
143- pretrained : true
136+ - [ Usage Guide] ( docs/usage.md )
137+ - [ API Reference] ( docs/api_reference.md )
138+ - [ LLM Guide] ( docs/llm_guide.md )
139+ - [ Metric Consistency] ( docs/METRIC_CONSISTENCY.md )
140+ - [ Supernodes and SCAR Release Notes] ( projects/supernodes_scar/README.md )
144141
145- dataset :
146- name : cifar10
147- batch_size : 128
142+ Build the Sphinx docs locally:
148143
149- clustering :
150- n_clusters : 4
151- compute_stability : true
152-
153- halo_analysis :
154- percentile : 90.0
155-
156- pruning :
157- ratios : [0.3, 0.5, 0.7]
158- methods : [magnitude, taylor, cluster_aware]
144+ ``` bash
145+ cd docs
146+ make html
159147```
160148
161- See ` configs/template.yaml` for complete parameter reference.
162-
163149## Testing
164150
165151``` bash
166152pytest tests/
167153pytest tests/unit/ -v
168154```
169155
156+ ## Citation
157+
158+ If you use the Supernodes and SCAR release, please cite the paper and the
159+ archived code/artifact versions listed in ` CITATION.cff ` .
160+
170161## License
171162
172- See LICENSE file .
163+ This repository is released under the MIT license. See [ LICENSE] ( LICENSE ) .
0 commit comments