math-under-llm / app.py
Alex W.
Two independent changes in one commit:
6f1ac4c
# app.py
"""
Wang's Five Laws โ€” LLM Spectral Analyzer
ไธปๅ…ฅๅฃ๏ผŒ็ป„่ฃ…ๆ‰€ๆœ‰ Tab
"""
import gradio as gr
from db.schema import init_db
from ui.tab_inspect import build_tab_inspect
from ui.tab_analyze import build_tab_analyze
from ui.tab_leaderboard import build_tab_leaderboard
from ui.tab_database import build_tab_database
from ui.tab_plot import build_tab_plot
from ui.tab_tables import build_tab_tables
# โ”€โ”€ ๅฏๅŠจๆ—ถๅˆๅง‹ๅŒ–ๆ•ฐๆฎๅบ“ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
init_db()
with gr.Blocks(
title="Wang's Five Laws โ€” LLM Spectral Analyzer",
) as demo:
# โ”€โ”€ ๅŒ่ฏญๆ ‡้ข˜ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
gr.Markdown("""
# ๐Ÿ”ฌ Wang's Five Laws โ€” LLM Spectral Analyzer
### ็Ž‹ๆฐไบ”ๅฎšๅพ‹ โ€” ๅคงๆจกๅž‹่ฐฑๅˆ†ๆžๅทฅๅ…ท
**Mathematical Foundations of Large Language Models (MF-LLM)**
Reads HF weights via **HTTP Range Request** โ€” no full model download required.
Auto-detects model structure (GQA / MHA / K=V shared / heterogeneous head_dim),
computes all Five Laws metrics per attention head, persists results to SQLite.
้€š่ฟ‡ **HTTP Range Request** ็›ดๆŽฅ่ฏปๅ– HF ๆƒ้‡๏ผŒๆ— ้œ€ไธ‹่ฝฝๆ•ดไธชๆจกๅž‹ใ€‚
่‡ชๅŠจ่ฏ†ๅˆซๆจกๅž‹็ป“ๆž„๏ผŒ้€ๅคด่ฎก็ฎ—็Ž‹ๆฐไบ”ๅฎšๅพ‹ๅ…จ้ƒจๆŒ‡ๆ ‡๏ผŒ็ป“ๆžœๆŒไน…ๅŒ–ๅˆฐ SQLiteใ€‚
[![DOI](https://img.shields.io/badge/DOI-10.5281%2Fzenodo.19707844-blue)](https://doi.org/10.5281/zenodo.19707844)
[![HAL](https://img.shields.io/badge/HAL-hal--05609398-red)](https://hal.science/hal-05609398)
[![Wang's Law](https://img.shields.io/badge/Wang%27s%20Law-r%3D1-blue)](https://github.com/emis-framework/math-under-llm)
""")
# โ”€โ”€ ๅŒ่ฏญ่กจๆ ผๅนถๆŽ’ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
with gr.Row():
gr.Markdown("""
| Law | Metric | Ideal |
|-----|--------|-------|
| Law 1 | Pearson r (Qโ€“K spectral alignment) | โ†’ 1 |
| Law 2 | SSR (spectral shape residual) | โ†’ 0 |
| Law 3 | Condition number ฮบ | smaller = more stable |
| Law 4 | cosU(Uq, Uv) super-orthogonal | < 1/โˆšd_head |
| Law 5 | cosV input subspace random orthogonal | โ‰ˆ 1/โˆšd_model |
""")
gr.Markdown("""
| ๅฎšๅพ‹ | ๆŒ‡ๆ ‡ | ็†่ฎบๆžๅ€ผ |
|------|------|---------|
| ็ฌฌไธ€ๅฎšๅพ‹ | Pearson r๏ผˆQ-K ่ฐฑ็บฟๆ€งๅฏน้ฝ๏ผ‰ | โ†’ 1 |
| ็ฌฌไบŒๅฎšๅพ‹ | SSR๏ผˆ่ฐฑๅฝข็Šถๆฎ‹ๅทฎ๏ผ‰ | โ†’ 0 |
| ็ฌฌไธ‰ๅฎšๅพ‹ | ๆกไปถๆ•ฐ ฮบ | ่ถŠๅฐ่ถŠ็จณๅฎš |
| ็ฌฌๅ››ๅฎšๅพ‹ | cosU(Uq, Uv)๏ผˆ่ถ…ๆญฃไบค๏ผ‰ | < 1/โˆšd_head |
| ็ฌฌไบ”ๅฎšๅพ‹ | cosV๏ผˆ่พ“ๅ…ฅๅญ็ฉบ้—ด้šๆœบๆญฃไบค๏ผ‰ | โ‰ˆ 1/โˆšd_model |
""")
# โ”€โ”€ Tabs โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
with gr.Tabs():
inspect_model_id, inspect_token = build_tab_inspect()
analyze_model_id, analyze_token = build_tab_analyze()
build_tab_leaderboard()
build_tab_database()
build_tab_plot()
build_tab_tables()
# โ”€โ”€ Tab1 โ†’ Tab2 ่”ๅŠจ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
inspect_model_id.change(
fn=lambda x: x,
inputs=inspect_model_id,
outputs=analyze_model_id,
)
inspect_token.change(
fn=lambda x: x,
inputs=inspect_token,
outputs=analyze_token,
)
if __name__ == "__main__":
demo.launch()