Metadata-Version: 2.5
Name: loraspec
Version: 1.0.0
Summary: LoRA Subspace Auditor (KnOTS) & Per-Token Top-k MoLoRA Triton Studio — pre-flight Grassmann/SVD collision SARIF auditor, factorized KnOTS alignment merger, and @triton.jit Arrow-SGMV multi-LoRA serving engine.
Project-URL: Homepage, https://github.com/sashwat2212/loraspec
Project-URL: Repository, https://github.com/sashwat2212/loraspec
Project-URL: Documentation, https://github.com/sashwat2212/loraspec/tree/main/docs
Project-URL: Issues, https://github.com/sashwat2212/loraspec/issues
Author: loraspec contributors
License: Apache-2.0
License-File: LICENSE
Keywords: grassmann,knots,lora,molora,peft,sarif,svd,triton,vllm
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Quality Assurance
Requires-Python: >=3.11
Requires-Dist: httpx>=0.27
Requires-Dist: jinja2>=3.1
Requires-Dist: pydantic<3,>=2.7
Requires-Dist: pyyaml>=6.0
Requires-Dist: rich>=13.7
Requires-Dist: typer>=0.12
Provides-Extra: kernels
Requires-Dist: torch<2.11,>=2.2; (sys_platform == 'linux') and extra == 'kernels'
Requires-Dist: torch>=2.2; (sys_platform != 'linux') and extra == 'kernels'
Requires-Dist: triton>=3.0; (sys_platform == 'linux') and extra == 'kernels'
Provides-Extra: serving
Requires-Dist: peft>=0.12.0; extra == 'serving'
Requires-Dist: vllm>=0.6.4; (sys_platform == 'linux') and extra == 'serving'
Provides-Extra: spectral
Requires-Dist: numpy>=1.26; extra == 'spectral'
Requires-Dist: safetensors>=0.4.3; extra == 'spectral'
Provides-Extra: studio
Requires-Dist: fastapi>=0.115; extra == 'studio'
Requires-Dist: uvicorn>=0.30; extra == 'studio'
Requires-Dist: websockets>=12.0; extra == 'studio'
Provides-Extra: study
Requires-Dist: accelerate>=0.33; extra == 'study'
Requires-Dist: datasets>=2.20; extra == 'study'
Requires-Dist: peft>=0.12; extra == 'study'
Requires-Dist: safetensors>=0.4.3; extra == 'study'
Requires-Dist: transformers>=4.44; extra == 'study'
Description-Content-Type: text/markdown

<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="./docs/assets/loraspec-hero-dark.svg" />
    <source media="(prefers-color-scheme: light)" srcset="./docs/assets/loraspec-hero-light.svg" />
    <img alt="loraspec — LoRA Subspace Auditor, KnOTS Merger & Top-k MoLoRA Triton Studio" src="./docs/assets/loraspec-hero-dark.svg" width="100%" />
  </picture>
</p>

# `loraspec` — LoRA Subspace Auditor (`KnOTS`) & Per-Token Top-$k$ `MoLoRA` Triton Studio

[![CI](https://github.com/sashwat2212/loraspec/actions/workflows/ci.yml/badge.svg)](https://github.com/sashwat2212/loraspec/actions/workflows/ci.yml)
[![Action Smoke](https://github.com/sashwat2212/loraspec/actions/workflows/action-smoke.yml/badge.svg)](https://github.com/sashwat2212/loraspec/actions/workflows/action-smoke.yml)
[![PyPI](https://img.shields.io/pypi/v/loraspec?color=0ea5e9&label=PyPI)](https://pypi.org/project/loraspec/)
[![Python 3.11+](https://img.shields.io/badge/python-3.11%2B-3776ab.svg)](https://www.python.org/downloads/)
[![GitHub Action](https://img.shields.io/badge/GitHub_Action-loraspec%40v1-2088ff.svg?logo=github-actions&logoColor=white)](./action.yml)
[![License: Apache-2.0](https://img.shields.io/badge/License-Apache_2.0-10b981.svg)](./LICENSE)

**Pre-flight Grassmann/SVD subspace collision & rank-collapse auditor, scalable factorized `KnOTS` joint-SVD alignment merger, and custom `@triton.jit` per-token Top-$k$ `Arrow-SGMV` multi-LoRA serving engine.**

`loraspec` bridges the 2026 production gap between **`huggingface/peft`** (static merges and unbatched eager `Arrow` routing) and **`vLLM V1`** (Punica SGMV kernels locked to one static `LoRARequest` per sequence).

---

## Release Status: `v1.0.0` Production Stable (`GATE-7`, Verified on `NVIDIA A30` + CPU/MPS)

`loraspec v1.0.0` ships a zero-`pickle` `.safetensors` inspector, factorized Grassmann/SVD subspace auditor, scalable `KnOTS` joint-SVD merger, custom `@triton.jit` per-token Top-$k$ `Arrow-SGMV` kernel, `vLLM V1` plugin, and wheel-bundled dark-mode React 19 Telemetry Studio:

- **Bounds-Safe `.safetensors` & PEFT Inspector (`src/loraspec/inspect/`)**: Zero-`pickle` binary header + IEEE-754 `F32`/`F16`/`BF16` tensor reader with `adapter_config.json` validation and `lora_A`/`lora_B` pairing.
- **Factorized Rank-$r$ QR+SVD & Grassmann Subspace Math (`src/loraspec/core/`)**: Computes singular spectra, effective stable rank $r_{\text{eff}} = \|\Delta W\|_F^2 / \|\Delta W\|_2^2$, spectral norm spikes, and pairwise **Grassmann principal angles**, **chordal similarity**, **geodesic distance**, and **directional sign conflicts** without materializing $O(d_{\text{out}} d_{\text{in}})$ matrices (`atol <= 1e-9`, `< 10s` on 252-layer adapters with `[spectral]`).
- **Scalable Factorized `KnOTS` Joint-SVD Alignment Merger (`src/loraspec/knots/`, `loraspec merge`)**: Conditioned thin-QR + compressed $R_{\text{sum}} \times R_{\text{sum}}$ core-SVD alignment across heterogeneous ranks ($r_m \in \{4..128\}$), supporting `knots_ties`, `knots_dare_ties`, `knots_linear`, factorized **`GenKnowSub`** subtraction (`--gks-adapter`), and PEFT export (`78.25%` sign-conflict reduction, `82.79%` energy retention at $r_{\text{out}}=32$, and **`3.5x` compositional `pass@1` vs `peft_ties`** on 5 `Qwen2.5-3B-Instruct` experts).
- **Per-Token Top-$k$ `Arrow` Router & `@triton.jit` `Arrow-SGMV` (`src/loraspec/kernels/`, `loraspec bench`)**: Zero-shot unit singular routing prototypes + fused two-stage ragged-rank Triton kernels verified on live `NVIDIA A30` (`2.00x–2.76x` synthetic & `1.59x–2.19x` real-adapter speedup, `71.5%–76.8%` scratchpad VRAM savings vs `peft` eager Arrow, and **`100.0%` single-domain `pass@1`** on the 6-expert `Qwen2.5-3B-Instruct` study).
- **`vLLM V1` Plugin & Wheel-Bundled React 19 Telemetry Studio (`src/loraspec/serving/`, `ui/`, `loraspec serve` / `replay`)**: Zero-patch `vllm.general_plugins` hook (`loraspec_molora`), FastAPI SSE/WebSocket gateway, wheel-bundled `DEMO_REPLAY=true` zero-GPU mode with `LORASPEC_PUBLIC_DEMO_LOCK=true` filesystem lockdown, and 252-layer dark-mode React 19 Studio (`Dockerfile` + `Dockerfile.gpu` + `docker-compose.yml`).
- **Zero-CDN HTML Certification Card (`--html`), OASIS SARIF 2.1.0 & GitHub Action (`action.yml`)**: Self-contained `< 250 KB` HTML Certification Cards, planted fault corpus (`100%` recall, `0.0%` FAR), and `< 10s` `[spectral]` BLAS CPU GitHub Action.

---

## Empirical Highlights & Telemetry Studio Previews (`NVIDIA A30` Verified)

| Controlled 4-Arm `Qwen2.5-3B-Instruct` Study | Pre-Flight Grassmann Subspace Collision Audit |
|---|---|
| ![4-Arm Multi-LoRA Study](./assets/social/four_arm_ablation.png) | ![Grassmann Chordal Similarity Heatmap](./assets/social/grassmann_heatmap.png) |
| **`triton_arrow_sgmv` reaches `100.0%` single-domain `pass@1`**; **`knots_ties` retains `3.5x` compositional `pass@1` vs `peft_ties` (`19.4%` vs `5.6%`)**. | **`$0` CPU audit flags `Grade D` (`MergeRiskScore 32.77`)**, rank collapse (`RULE-SPEC-002`), and pairwise subspace collisions before merging. |

| Multi-GPU Kernel Roofline (`L4` / `A30` / `A100`) | `peft` Eager Stall vs `@triton.jit` `Arrow-SGMV` |
|---|---|
| ![Roofline Latency vs Batch Size](./assets/social/roofline_l4_a100.png) | ![PEFT Stall vs Triton Arrow-SGMV](./assets/social/peft_stall_vs_triton.png) |
| **`2.00x` suite-wide mean speedup** (`2.76x` peak on `A30` `64×64`) with numerical error `<= 3.27e-4` FP16 / `<= 3.70e-3` BF16. | **`76.8%` scratchpad VRAM reduction** (`674.7 MB -> 162.2 MB` on `A30` `64×64`) with zero Python $K$-loop intermediate materialization. |

- **60fps Animated Token-Routing Studio Clip (`H.264` MP4):** [`assets/social/molora_arena_60fps.mp4`](./assets/social/molora_arena_60fps.mp4)
- **Full Empirical Technical Report & INR Ledger:** [`docs/TECHNICAL-REPORT.md`](./docs/TECHNICAL-REPORT.md)

---

## Quickstart

```bash
# Out-of-tree zero-install via uvx (or pip install "loraspec[spectral,studio]")
uvx "loraspec[spectral]" doctor

# Or install workspace extras (runs 100% on CPU/MPS; add CUDA torch+triton on Linux GPUs)
uv sync --extra spectral --extra kernels --extra studio

# 1. Safely inspect a PEFT LoRA adapter directory (zero-pickle)
uv run loraspec inspect ./path/to/adapter

# 2. Audit >= 2 adapters for rank collapse & Grassmann collision -> JSON + SARIF 2.1.0 + HTML
uv run loraspec audit ./adapters/sql_lora ./adapters/rust_lora \
  --out-dir ./artifacts/audit-01 \
  --sarif ./artifacts/audit-01/report.sarif \
  --html ./artifacts/audit-01/report.html \
  --fail-on high

# 3. Align & merge heterogeneous LoRAs via factorized KnOTS joint-SVD
uv run loraspec merge ./adapters/sql_lora ./adapters/rust_lora \
  --method knots_ties \
  --target-rank 32 \
  --out-dir ./artifacts/merged_knots_lora

# 4. Benchmark Arrow-SGMV parity & roofline or launch the bundled zero-GPU Telemetry Studio
uv run loraspec bench --synthetic --smoke
DEMO_REPLAY=true LORASPEC_PUBLIC_DEMO_LOCK=true uv run loraspec serve --port 8000
```

> [!TIP]
> **Public Cloud Hosting (`₹0/month`):** Set `DEMO_REPLAY=true` and `LORASPEC_PUBLIC_DEMO_LOCK=true` (enabled by default in `docker-compose.yml` for `loraspec-studio`) so public visitors can explore all bundled `L4`/`A30`/`A100` replay traces while non-`@replay` server filesystem paths are strictly blocked with HTTP `403`.

### GitHub Actions Gate (`action.yml`)

```yaml
permissions:
  contents: read
  actions: read
  security-events: write

steps:
  - uses: actions/checkout@v4
  - uses: sashwat2212/loraspec@v1
    with:
      paths: "./adapters/sql_lora ./adapters/rust_lora" # or adapter-paths
      fail-on: high
```

See [action/README.md](./action/README.md) for full inputs and outputs.

### Development Checks & Benchmarks

```bash
uv run ruff format --check .
uv run ruff check .
uv run pyright
uv run pytest -v
(cd ui && npm ci && npx tsc --noEmit && npm run build && npx playwright test)
uv run python benchmarks/planted_collisions/generate_corpus.py --out-dir /tmp/loraspec-corpus --verify
```

---

## Documentation Index (`SPEC-GATE` SDLC Stages 0–7)

| Stage | Document | Description |
|---|---|---|
| Public Audit | [results/public_lora_audit/AUDIT_REPORT.md](./results/public_lora_audit/AUDIT_REPORT.md) | `$0` CPU spectral & Grassmann audit of 31 real public Hugging Face Hub PEFT LoRAs (`20.9%` mean rank utilization, `48.4%` rank collapse) |
| Walkthrough | [docs/06-STUDIO-AND-APPLICATION-WALKTHROUGH.md](./docs/06-STUDIO-AND-APPLICATION-WALKTHROUGH.md) | End-to-end application flow, tab-by-tab React 19 Studio walkthrough & non-AI geometric brand system |
| Runbook | [docs/07-HARDENING-AND-A100-RUNBOOK.md](./docs/07-HARDENING-AND-A100-RUNBOOK.md) | Production hardening matrix & step-by-step optional `NVIDIA A100` (`40GB`/`80GB`) execution runbook |
| Report | [docs/TECHNICAL-REPORT.md](./docs/TECHNICAL-REPORT.md) | Empirical `v1.0.0` report: live `NVIDIA A30` `Arrow-SGMV` roofline & 6-expert 4-arm `Qwen2.5-3B` study |
| Changelog | [CHANGELOG.md](./CHANGELOG.md) | Complete release history from `v0.1.0-alpha` through `v1.0.0` (`GATE-5` → `GATE-7`) |
| Research | [docs/RESEARCH-DOSSIER.md](./docs/RESEARCH-DOSSIER.md) | 2025–2026 LoRA interference, Grassmann geometry, `KnOTS`, `Arrow`, `GenKnowSub` & `vLLM V1` dossier |
| Stage 0 | [docs/00-CHARTER.md](./docs/00-CHARTER.md) | Charter, 4-Gate rename collision process (`loraspec`), **`SPEC-GATE`** 8-Stage SDLC & Unified Grading Rubric, `₹2,000–₹3,000` budget |
| Stage 1 | [docs/01-BRD.md](./docs/01-BRD.md) | Graded Business Requirements (`BRD-REQ-01`–`22`), KPIs (`KPI-ENG/RES/SHW`), risk register & Stakeholder Sign-Off Matrix |
| Stage 2 | [docs/02-FRD-PRD.md](./docs/02-FRD-PRD.md) | Complete PRD (`EPIC-01`–`07`, Graded `FEAT-01`–`22`) & FRD (Graded `FRD-REQ-01`–`36`, CLI/API/Pydantic v2 contracts, Graded `NFR-01`–`08`) |
| Stage 3 | [docs/03-ARCHITECTURE.md](./docs/03-ARCHITECTURE.md) | 5-Plane Architecture (`ARCH-COMP-01`–`12`), sequence diagrams, threat model & `ADR-001`–`009` |
| Stage 4 | [docs/04-DESIGN-SPECTRAL-AUDITOR.md](./docs/04-DESIGN-SPECTRAL-AUDITOR.md) | `SDD-01`: Bounds-safe `.safetensors`, factorized QR+SVD, Grassmann angles & `MergeRiskScore` |
| Stage 4 | [docs/04-DESIGN-KNOTS-MERGER.md](./docs/04-DESIGN-KNOTS-MERGER.md) | `SDD-02`: Memory-safe Gram-QR `KnOTS` alignment, `TIES`/`DARE`, `GenKnowSub` & PEFT export |
| Stage 4 | [docs/04-DESIGN-TRITON-MOLORA.md](./docs/04-DESIGN-TRITON-MOLORA.md) | `SDD-03`: `Arrow` prototype router, `@triton.jit` Top-$k$ `Arrow-SGMV` & PyTorch parity spec |
| Stage 4 | [docs/04-DESIGN-VLLM-STUDIO.md](./docs/04-DESIGN-VLLM-STUDIO.md) | `SDD-04`: `vLLM V1` plugin, FastAPI SSE/WS gateway, `DEMO_REPLAY=true` & React 19 Studio |
| Stage 4 | [docs/04-DESIGN-CLI-CI-BENCHMARKS.md](./docs/04-DESIGN-CLI-CI-BENCHMARKS.md) | `SDD-05`: CLI, SARIF 2.1.0/HTML reports, `action.yml` & L4/A30/A100 4-arm empirical study |
| Stage 5–7 | [docs/05-IMPLEMENTATION-PLAN.md](./docs/05-IMPLEMENTATION-PLAN.md) | Exhaustive 1-to-1 Bidirectional RTM (`36/36` FRDs, `22/22` Features, exact symbols/tests), file-by-file plan, `GATE-0..7` & INR ledger |

---

## License

Apache License 2.0 — see [LICENSE](./LICENSE).
