--- name: lora-subspace description: Analyze weight-delta (LoRA) subspace convergence across independent adapters of the same base model. Discovers cohorts via the local hf-model-search index, downloads only lightweight adapter safetensors, and computes exact low-rank SVD, layer/projection energy localization, effective rank, Frobenius cosine similarity, and Grassmann subspace overlap enriched over random baselines (with within-domain vs. between-domain contrast). --- # LoRA Subspace Convergence `scripts/lora_subspace.py` tests whether independent fine-tunes or LoRA adapters of the same base model converge to shared low-rank weight subspaces and layer profiles in a domain (e.g. math, code, medical, or a target language). It reads `.safetensors` directly with NumPy (no PyTorch or GPU required) and uses exact low-rank QR + small-SVD algebra on $(B, A)$ factors without ever materializing full $d_{\text{out}} \times d_{\text{in}}$ weight matrices. Run it from this skill's folder: ```bash # 1. Discover independent adapters of a base model from local hfmodels.db python3 scripts/lora_subspace.py cohort --base-model Qwen/Qwen3-8B --query math --dedup-authors --limit 15 --out math_cohort.json # 2. Fetch only adapter_config.json + adapter_model.safetensors (skips adapters > 150 MB) python3 scripts/lora_subspace.py fetch --cohort math_cohort.json --cache-dir ./adapters/math # 3. Analyze layer localization, effective rank, and subspace convergence (numpy via uv) uv run -q scripts/lora_subspace.py analyze ./adapters/math --top-k 8 --out receipt.json # 4. Compare within-domain vs. between-domain convergence (e.g. math vs. code) uv run -q scripts/lora_subspace.py analyze ./adapters/math ./adapters/code \ --group math=./adapters/math --group code=./adapters/code --top-k 8 --out contrast.json # Render Markdown from any receipt JSON python3 scripts/lora_subspace.py report contrast.json ``` ## What it measures | Metric | Definition | What it tells you | |---|---|---| | **Effective rank (`erank`)** | $\exp(-\sum p_i \log p_i)$ over singular values of $\Delta W = s B A$ | How many effective dimensions an adapter actually uses (often far smaller than configured `r`) | | **Layer & projection localization** | Fraction of total $\|\Delta W\|_F^2$ in each layer and projection (`q/k/v/o`, `gate/up/down`) | Where in the network a domain update concentrates its energy, and how consistent that profile is across independent labs | | **Exact Frobenius cosine** | $\langle \Delta W_1, \Delta W_2 \rangle_F / (\|\Delta W_1\|_F \|\Delta W_2\|_F)$ | Signed directional alignment between two weight updates (exact even when $r_1 \neq r_2$) | | **Grassmann subspace overlap** | $\frac{1}{k}\|(U_1^{(k)})^T U_2^{(k)}\|_F^2$ (and same for $V^{(k)}$) | Sign-invariant alignment of the top-$k$ output and input subspaces | | **Enrichment vs. random** | $\text{overlap} / (k / d)$ | How many times stronger the shared subspace is than two random $k$-subspaces in $\mathbb{R}^d$ (`1.0x` = random chance) | | **Group contrast** | Within-group vs. between-group enrichment (`--group`) | Separates *domain-specific* subspace convergence from *generic fine-tuning* directions shared by all adapters | ## Cohort curation tips - Always pass `--dedup-authors` in `cohort` so multiple checkpoints from the same user/org do not inflate convergence. - Always include a **control group** (e.g. `--group math=... --group control=...`): all SFT/LoRA runs on a base model share some generic chat/formatting directions, so **within-domain enrichment > between-domain enrichment** is the real test for domain-specific structure. - `cohort` reads the local SQLite database from `hf-model-search` (`$HFMODELS_DB`, `../hf-model-search/hfmodels.db`, or `~/.cache/hfmodels/hfmodels.db`). ## Reading results honestly - **Weight-space convergence requires a shared base model:** comparing $\Delta W$ directly only makes sense for adapters trained from the *same* base checkpoint (permutation symmetry prevents direct weight comparison across distinct base models). - **Generic SFT drift vs. domain structure:** even unrelated fine-tunes of the same base model often show >`1x` enrichment over random because they move along the same instruction-following or formatting directions. Always interpret raw enrichment alongside a between-group control. - **Popularity != benchmark performance:** `cohort` sorts by Hub downloads and likes. To link subspace structure to *capability*, pair the receipt with actual domain benchmark scores for each adapter. - **No base weights needed, no inference run:** this tool measures static weight-delta geometry; it does not evaluate functional accuracy or activation-space similarity (CKA).