Skip to content
naver-aiPublic

About

Official code for "Measuring Layer-wise Intrinsic Dimensionality of FFNs in LLMs via PCA" (NeurIPS 2026)

Resources

Stars

5 stars

Watchers

1 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

Measuring Layer-wise Intrinsic Dimensionality of FFNs in LLMs via PCA

arXiv NeurIPS 2026 License

Daehee Kim · NAVER Cloud

Official implementation of the NeurIPS 2026 paper (arXiv:2611.32782).

Per-layer d_theta/D profiles of 51 scanned models, grouped by family

Per-layer dθ,l/Dl profiles at θ = 0.99 for 51 of the 58 scanned models, grouped by family (paper Figure 5). The middle-heavy profile recurs across architecture, training corpus, and scale.

TL;DR

  • Measure. For each layer, PCA on the FFN down-projection input gives dθ: the fewest principal components that reconstruct the activation up to mean cosine similarity θ = 0.99.
  • Observe. Across 58 open-weight LLMs (14 families, 70M–72B), the per-layer profile is middle-heavy, robust to the calibration corpus (even English vs. Chinese), and largely preserved through post-training. σ99 summarizes how unevenly it is spread across layers.
  • Use. As the per-layer FFN width of a distilled student at a matched budget, the PCA-guided allocation beats the uniform width by +2.3 points on Qwen3-8B and +2.5 on Qwen2.5-3B (+0.9 on Gemma-3-4B, +0.5 on LLaMA-3-8B). A budget-matched inverted allocation does worst on all four teachers, the gap tracks σ99, and the advantage persists through downstream SFT.

Updates

  • Dec 2026 — Poster at NeurIPS 2026, Sydney (Thu Dec 10, 17:00–20:00 AEDT).
  • Nov 2026 — Paper on arXiv.
  • Nov 2026 — Code, the 58-model scan results, and the pretraining-trajectory measurements released.
  • Sep 2026 — Accepted to NeurIPS 2026.

Contents

Overview · Repository layout · Installation · Quick start · Released results · Training · Evaluation · Citation · License

Overview

Let hl be the input of the FFN down-projection in layer l (model.layers[l].mlp.down_proj), with FFN width Dl. From about 100K SlimPajama tokens (50 sequences of 2,048 tokens) we eigendecompose the centered covariance of hl; Pd projects onto the top-d principal directions.

$$d_{\theta,l} = \min \Big\{ d \;:\; \mathbb{E}_h \big[ \cos\big(h,\ P_d(h - \bar{h}_l) + \bar{h}_l\big) \big] \ge \theta \Big\}, \qquad \sigma_{99} = \mathrm{std}_l \big( d_{0.99,l} / D_l \big)$$

dθ,l is linearly interpolated between widths d/Dl ∈ {0.1, …, 0.9}; θ = 0.99 throughout.

Pipeline

  1. Measure dθ per layer — lidf.pca, lidf.dtheta_widths
  2. Allocate an FFN budget B = Σl dθ,l — configs/
    • PCA: wl = dθ,l, rounded to multiples of 128
    • Uniform: wl ≈ B / L
    • Inverted: 2 · Uniform − PCA, clipped to [128, Dl] and rescaled to B
  3. Distill — Minitron-style pruning to these widths (lidf.minitron_init), KL distillation from the same teacher (lidf.train), and evaluation (lidf.eval)

Three width allocations under matched FFN budgets

Three width allocations under matched FFN budgets (paper Figure 4): per-layer FFN width as a percentage of Dl, with the total budget in each panel title. PCA (blue) follows dθ,l, Uniform (orange) is B/L, and Inverted (red) mirrors PCA around Uniform.

Repository layout

.
├── lidf/                      # Python package (run as modules from the repository root)
│   ├── pca.py                 # per-layer PCA scan of the FFN down-projection input
│   ├── dtheta_widths.py       # scan -> d_theta widths and per-model summary
│   ├── minitron_init.py       # student init: importance-based FFN pruning to per-layer widths
│   ├── train.py               # KD training (FSDP)
│   ├── eval.py                # lm-evaluation-harness wrapper
│   └── models/                # adaptive-width LLaMA / Qwen2 (Qwen2.5) / Qwen3 / Gemma-3 students
├── configs/                   # per-teacher width vectors for every allocation, and check_budgets.py
├── scripts/                   # pca_scan.sh, per-teacher recipes (init + KD + eval), extract_gemma3_text.py
├── results/
│   ├── scan_58_models.csv          # 58-model scan: per-model d_theta summary
│   └── pretraining_trajectories.csv # pretraining trajectories: sigma_99 per checkpoint
├── assets/                    # README title and figures (paper Figures 4 and 5)
└── requirements.txt

All commands run from the repository root; the scripts in scripts/ add it to PYTHONPATH.

Installation

# Python >= 3.10. Verified: Python 3.12, torch 2.6.0 + CUDA 12.4
pip install torch==2.6.0 --index-url https://download.pytorch.org/whl/cu124
pip install -r requirements.txt

Other verified package versions are listed at the top of requirements.txt.

Quick start

Set the three paths used by the scripts:

export DATA_ROOT=/path/to/data              # contains SlimPajama/data/*.parquet
export PCA_OUTPUT_ROOT=/path/to/pca_outputs # PCA scan outputs
export OUTPUT_ROOT=/path/to/outputs         # student checkpoints, evaluation outputs, extracted Gemma teacher

1. Measure dθ (one GPU)

bash scripts/pca_scan.sh Qwen/Qwen2.5-3B pca_qwen25_3b 11008

Writes pca_results_100000.json (mean cosine per layer and rank) to $PCA_OUTPUT_ROOT/pca_qwen25_3b/ and prints the 128-rounded dθ widths and the per-model summary (σ99 and the layer-third means). Extra lidf.pca flags go after the FFN width, e.g. --dtype bfloat16 (Gemma-3), --trust_remote_code (MAP-Neo), --device_map auto (70B+).

2. Check the allocations

python -m configs.check_budgets   # one OK line per allocation, then "ALL BUDGETS OK"

3. Init, KD, and evaluation (one 8-GPU node)

bash scripts/qwen25_3b.sh pca

Released results

Both files use θ = 0.99 and about 100K SlimPajama tokens. dθ is interpolated on the width grid (about 10%, …, 90% of D, where D = intermediate) and left unrounded, as in lidf/dtheta_widths.py. Layer thirds: the first ⌊L/3⌋ layers, the last ⌊L/3⌋ layers, and the middle rest.

File Contents
results/scan_58_models.csv One row per model of the 58-model scan (§3, Figures 2 and 5, Appendix B)
results/pretraining_trajectories.csv One row per checkpoint of the 21 pretraining trajectories (§3.3, Figure 3)
Columns of scan_58_models.csv
Column Meaning
model, family Model name and family (14 families)
params_B Parameter count, billions
n_layers Number of transformer layers L
intermediate FFN intermediate width D
sigma_99 σ99: standard deviation (ddof=0) of dθ/D over layers
edge_early_mean, middle_mean, edge_late_mean Mean dθ/D of the first / middle / last third of layers
middle_heavy_mean_criterion 1 if middle_mean exceeds both edge means (44 of the 58 models)
Columns of pretraining_trajectories.csv
Column Meaning
trajectory, family Model and family
params_B Parameter count, billions
checkpoint Public revision or tag
tokens_B Training tokens seen, billions
chinchilla_multiplier tokens_B / (20 × params_B)
sigma_99 As in scan_58_models.csv

Training

bash scripts/qwen25_3b.sh     <pca|uniform|inverted>
bash scripts/qwen3_8b_base.sh <pca|uniform|inverted>
python scripts/extract_gemma3_text.py $OUTPUT_ROOT/gemma3_4b_pt_text   # Gemma teacher, once
bash scripts/gemma3_4b.sh     <pca|uniform|inverted>
bash scripts/llama3_8b.sh     <pca|uniform|inverted>

Each script runs Minitron init, KD training, and evaluation for one allocation. The training settings (learning-rate schedule, batch size, number of steps) are listed at the top of each script. Checkpoints are saved to $OUTPUT_ROOT/<teacher>/<allocation>/train/step<N> and evaluation results to $OUTPUT_ROOT/<teacher>/<allocation>/eval/<step>/eval_results.json.

Evaluation

lidf.eval runs lm-evaluation-harness (lm_eval 0.4.11) on MMLU, ARC-Challenge, HellaSwag, WinoGrande, and PIQA at harness-default few-shot settings, with log-likelihood scoring and no chat template.

accelerate launch --num_processes=8 -m lidf.eval \
    --model_path $OUTPUT_ROOT/qwen25_3b/pca/train/step3000 --tokenizer Qwen/Qwen2.5-3B \
    --tasks mmlu,arc_challenge,hellaswag,winogrande,piqa \
    --output_path $OUTPUT_ROOT/qwen25_3b/pca/eval/step3000 --batch_size auto --seed 0

Citation

@inproceedings{kim2026lidf,
  title         = {Measuring Layer-wise Intrinsic Dimensionality of {FFNs} in {LLMs} via {PCA}},
  author        = {Kim, Daehee},
  booktitle     = {Advances in Neural Information Processing Systems},
  year          = {2026},
  eprint        = {2611.32782},
  archivePrefix = {arXiv},
  primaryClass  = {cs.LG}
}

Acknowledgements

This code builds on Hugging Face Transformers (the adaptive-width models in lidf/models/ adapt its modeling code; see NOTICE), lm-evaluation-harness, and Liger Kernel, and uses the SlimPajama dataset. All measured and distilled models are open-weight models used under their published licenses.

License

LiDF
Copyright (c) 2026-present NAVER Cloud Corp.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

lidf/models/lidf_*.py adapt Hugging Face Transformers modeling code (Apache-2.0); see NOTICE.

About

Official code for "Measuring Layer-wise Intrinsic Dimensionality of FFNs in LLMs via PCA" (NeurIPS 2026)

Resources

Stars

5 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages