Trim README to intro + quick start
Browse files
README.md
CHANGED
|
@@ -7,9 +7,10 @@ tags:
|
|
| 7 |
---
|
| 8 |
# MSA: Memory Sparse Attention
|
| 9 |
|
| 10 |
-
*A scalable, end-to-end trainable latent-memory framework for 100M-token contexts*
|
| 11 |
|
| 12 |
-
**Anonymous code release for peer review.**
|
|
|
|
| 13 |
|
| 14 |
| | |
|
| 15 |
|---|---|
|
|
@@ -17,274 +18,93 @@ tags:
|
|
| 17 |
| Model weights | [`Anoy123423123/MSA-4B`](https://huggingface.co/Anoy123423123/MSA-4B) |
|
| 18 |
| Benchmark data | [`Anoy123423123/MSA-RAG-BENCHMARKS`](https://huggingface.co/datasets/Anoy123423123/MSA-RAG-BENCHMARKS) |
|
| 19 |
|
| 20 |
-
|
| 21 |
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
-
|
| 27 |
-
- **KV cache compression** with a **Memory Parallel** inference engine to deliver **100M token** throughput on **2×A800** GPUs;
|
| 28 |
-
- **Memory Interleave** for multi-round, multi-hop reasoning across scattered memory segments.
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
> **Scaling from 16K→100M tokens**: MSA fuses top-k selection with sparse attention to remain end-to-end differentiable while allowing document decoupling at inference. On MS MARCO, MSA sustains **<9%** degradation and exhibits strong extrapolation.
|
| 33 |
-
|
| 34 |
-
## ✨ Key Contributions
|
| 35 |
-
|
| 36 |
-
- **Memory-Sparse Attention (MSA)**: an **end-to-end trainable**, **scalable sparse attention** layer with **document-wise RoPE**, realizing **O(L)** complexity and **<9%** degradation from **16K→100M tokens**.
|
| 37 |
-
- **KV cache compression + Memory Parallel**: tiered storage (GPU-resident routing keys, CPU content K/V), distributed scoring, and on-demand transfers to enable **100M-token** inference on **2×A800**.
|
| 38 |
-
- **Memory Interleave**: adaptive alternating "generative retrieval → context expansion → generation," significantly boosting **multi-hop** reasoning across documents.
|
| 39 |
-
- **Comprehensive evaluation**: MSA outperforms same-backbone RAG, best-of-breed RAG pipelines, and top long-context models on long-context QA and NIAH, showing superior **stability** and **accuracy** at scale.
|
| 40 |
-
|
| 41 |
-
---
|
| 42 |
-
|
| 43 |
-
## 🧩 Overall Design
|
| 44 |
-
|
| 45 |
-
### Architecture
|
| 46 |
-
|
| 47 |
-
MSA integrates **retrieval and generation** into a single differentiable loop. Document latent states (**K/V/Kᵣ**) are **chunk-mean pooled** for compression. A **router projector** computes relevance via cosine similarity (mean-pooled over heads, then token-wise max), selects **Top‑k documents**, then concatenates their **compressed K/V** with the query's local **K/V** for autoregressive decoding. Routing applies only to **upper layers**; lower layers keep **independent document processing** for hierarchical alignment.
|
| 48 |
-
|
| 49 |
-
- **Parallel (document-wise) RoPE**: Each document resets positions from 0, preventing position drift between **train-short** and **infer-long**, enabling 64k training to extrapolate to 100M.
|
| 50 |
-
- **Global RoPE (active context)**: The query's starting index is **offset by k** (Top‑k retrieved blocks), preserving causal ordering: *background → query → generation*.
|
| 51 |
-
|
| 52 |
-
Implemented in [`src/msa/memory_sparse_attention.py`](./src/msa/memory_sparse_attention.py) and [`src/msa/model.py`](./src/msa/model.py).
|
| 53 |
-
|
| 54 |
-
---
|
| 55 |
-
|
| 56 |
-
### Inference Pipeline
|
| 57 |
-
|
| 58 |
-
MSA uses a **three-stage** pipeline:
|
| 59 |
-
|
| 60 |
-
1. **Global Memory Encoding (offline)**: forward over the corpus to cache chunk-pooled **(K̄, V̄, K̄ᵣ)**.
|
| 61 |
-
2. **Online Routing & Context Assembly**: project query to **Qᵣ**, match with **K̄ᵣ** to pick **Top‑k**, then load only the selected **K̄/V̄** and concatenate with local context.
|
| 62 |
-
3. **Sparse Generation**: autoregress over the **sparse context**.
|
| 63 |
-
|
| 64 |
-
**Memory Parallel** shards **K̄ᵣ** across GPUs (query broadcast → local scoring → global reduce). Content **K̄/V̄** stays in host DRAM and is **asynchronously fetched** when selected—balancing **VRAM** and **throughput** for **100M-token** deployment.
|
| 65 |
-
|
| 66 |
-
Implemented in [`src/msa_service.py`](./src/msa_service.py) (`MSAEngine`, memory-parallel bucketing) and [`src/prefill.py`](./src/prefill.py) (stage-1 encoding).
|
| 67 |
-
|
| 68 |
-
---
|
| 69 |
-
|
| 70 |
-
## 🚀 Results
|
| 71 |
-
|
| 72 |
-
> **Setup**
|
| 73 |
-
> **QA**: 9 datasets (MS MARCO v1, NQ, DuReader, TriviaQA(10M), NarrativeQA, PopQA, 2WikiMultiHopQA, HotpotQA, MuSiQue), memory banks **277K→10M tokens**, metric: **LLM judge (0–5)**.
|
| 74 |
-
> **NIAH (RULER)**: 8 subtasks, **32K→1M tokens**, report average accuracy.
|
| 75 |
-
> **Backbone**: Qwen3‑4B‑Instruct‑2507. Compare to same-backbone RAG and best-of-breed RAG stacks (KaLMv2 + large generators, optional reranker).
|
| 76 |
-
|
| 77 |
-
### Table 1: MSA vs same-backbone RAG (Qwen3‑4B)
|
| 78 |
-
|
| 79 |
-
**Summary**: Average **3.760**, improving over standard RAG (**+16.0%**), RAG+rerank (**+11.5%**), and HippoRAG2 (**+14.8%**) using their best@k; MSA leads on all but NarrativeQA within the same-backbone group.
|
| 80 |
-
|
| 81 |
-
| Dataset | Tokens | Qwen3-4B R@1 | R@5 | R@10 | Qwen3-4B (RR) R@1 | R@5 | R@10 | HippoRAG2 R@1 | R@5 | R@10 | MSA (adaptive) |
|
| 82 |
-
|---------|--------|--------------|------|-------|----------------------|------|-------|------------------|------|--------|----------------|
|
| 83 |
-
| MS MARCO v1 | 7.34M | 2.893 | 3.011 | 3.005 | 2.934 | <u>3.032</u> | 3.017 | 2.676 | 3.005 | 3.019 | **4.141** |
|
| 84 |
-
| Natural Questions | 1.47M | 3.452 | 3.374 | 3.297 | <u>3.494</u> | 3.408 | 3.385 | 3.338 | 3.389 | 3.374 | **3.545** |
|
| 85 |
-
| DuReader | 277K | 3.726 | 3.579 | 3.594 | <u>3.848</u> | 3.618 | 3.607 | 2.941 | 3.485 | 3.415 | **4.155** |
|
| 86 |
-
| TriviaQA (10M) | 10M | 4.133 | 4.414 | 4.273 | 4.313 | 4.375 | 4.391 | 4.188 | <u>4.430</u> | 4.367 | **4.621** |
|
| 87 |
-
| NarrativeQA | 538K | 1.611 | 2.567 | 2.860 | **3.638** | 3.492 | <u>3.536</u> | 1.959 | 2.628 | 2.655 | 3.395 |
|
| 88 |
-
| PopQA | 1.18M | 2.959 | 3.273 | 3.299 | <u>3.315</u> | 3.264 | 3.266 | 3.111 | 3.249 | 3.249 | **3.433** |
|
| 89 |
-
| 2WikiMultiHopQA | 722K | 1.065 | 3.055 | 3.136 | 1.187 | 3.057 | 3.159 | 1.045 | 3.180 | <u>3.330</u> | **4.280** |
|
| 90 |
-
| HotpotQA | 1.35M | 2.252 | 3.582 | 3.787 | 2.642 | 3.990 | <u>4.022</u> | 3.230 | 3.770 | 3.970 | **4.061** |
|
| 91 |
-
| MuSiQue | 1.41M | 0.936 | 1.752 | 1.928 | 1.144 | 1.960 | 1.965 | 1.020 | 1.907 | <u>2.095</u> | **2.211** |
|
| 92 |
-
| **Average** | — | 2.559 | 3.179 | 3.242 | 2.946 | 3.355 | <u>3.372</u> | 2.612 | 3.227 | 3.275 | **3.760** |
|
| 93 |
-
|
| 94 |
-
*Table 1: Same-backbone RAG vs MSA (@1/@5/@10 vs MSA @adaptive)*
|
| 95 |
-
|
| 96 |
-
---
|
| 97 |
-
|
| 98 |
-
### Table 2: MSA vs best-of-breed RAG (large backbones)
|
| 99 |
-
|
| 100 |
-
**Summary**: Against **KaLMv2+Qwen3‑235B** and **KaLMv2+Llama‑3.3‑70B** (w/ and w/o reranking), MSA achieves the best score on **4/9** datasets and an average **3.760**, with relative gains of **+7.2%**, **+5.0%**, **+10.7%**, and **+5.4%** over the strongest configurations respectively. Gaps on a few datasets (e.g., MuSiQue) are largely attributable to parameter-count and intrinsic reasoning capacity.
|
| 101 |
-
|
| 102 |
-
| Dataset | KaLMv2 + Qwen3‑235B R@1 | R@5 | R@10 | Qwen3‑235B (RR) R@1 | R@5 | R@10 | KaLMv2 + Llama‑3.3 R@1 | R@5 | R@10 | Llama‑3.3 (RR) R@1 | R@5 | R@10 | MSA (adaptive) |
|
| 103 |
-
|---------|---------------------------|------|-------|--------------------------|------|-------|------------------------------|------|--------|-------------------------|------|--------|----------------|
|
| 104 |
-
| MS MARCO v1 | 2.846 | <u>3.028</u> | 3.027 | 2.886 | 3.020 | 2.995 | 2.649 | 2.904 | 2.919 | 2.881 | 2.955 | 2.952 | **4.141** |
|
| 105 |
-
| Natural Questions | <u>3.711</u> | 3.670 | 3.694 | 3.621 | 3.610 | 3.645 | 3.675 | 3.674 | 3.662 | **3.756** | 3.665 | 3.647 | 3.545 |
|
| 106 |
-
| DuReader | 4.044 | 3.991 | 3.978 | 3.973 | 3.932 | 3.891 | <u>4.051</u> | 3.846 | 3.742 | 3.967 | 3.776 | 3.780 | **4.155** |
|
| 107 |
-
| TriviaQA (10M) | 4.367 | 4.656 | 4.578 | 4.492 | 4.320 | 4.555 | 4.273 | **4.740** | <u>4.719</u> | 4.547 | 4.703 | 4.695 | 4.621 |
|
| 108 |
-
| NarrativeQA | 1.413 | 2.130 | 2.427 | 3.212 | **3.427** | 3.375 | 1.290 | 2.123 | 2.382 | 3.150 | 3.263 | 3.317 | <u>3.395</u> |
|
| 109 |
-
| PopQA | 2.810 | 3.347 | <u>3.396</u> | 3.268 | 3.380 | 3.376 | 2.787 | 3.298 | 3.305 | 3.337 | 3.384 | 3.362 | **3.433** |
|
| 110 |
-
| 2WikiMultiHopQA | 2.646 | 3.579 | 3.582 | 1.855 | 3.381 | <u>3.583</u> | 1.339 | 3.263 | 3.445 | 1.651 | 3.332 | 3.541 | **4.280** |
|
| 111 |
-
| HotpotQA | 3.497 | 4.090 | **4.225** | 3.341 | 4.141 | 4.194 | 3.070 | 3.896 | 4.127 | 3.428 | 4.145 | <u>4.203</u> | 4.061 |
|
| 112 |
-
| MuSiQue | 1.988 | 2.462 | **2.647** | 1.801 | 2.522 | 2.605 | 1.704 | 2.317 | 2.258 | 1.895 | 2.462 | <u>2.614</u> | 2.211 |
|
| 113 |
-
| **Average** | 3.036 | 3.439 | 3.506 | 3.161 | 3.526 | <u>3.580</u> | 2.760 | 3.340 | 3.396 | 3.179 | 3.521 | 3.568 | **3.760** |
|
| 114 |
-
|
| 115 |
-
*Table 2: SOTA RAG stacks (strong retriever + large generator + optional reranker) vs MSA*
|
| 116 |
-
|
| 117 |
-
---
|
| 118 |
-
|
| 119 |
-
### RULER NIAH stability (32K→1M)
|
| 120 |
-
|
| 121 |
-
**Summary**: MSA maintains **94.84%** at **1M tokens**. The unmodified backbone collapses beyond **128K** (down to **24.69% @1M**). Hybrid linear-attention long-context models degrade noticeably at **≥128K/256K**. External-memory agents (e.g., RL‑MemoryAgent‑14B) remain stable but are weaker in **absolute accuracy** and show steeper decay than MSA.
|
| 122 |
-
|
| 123 |
-
---
|
| 124 |
-
|
| 125 |
-
## Implementation Notes
|
| 126 |
-
|
| 127 |
-
- **Training**: 158.95B-token continuous pretraining with **auxiliary routing loss**, followed by two-stage SFT (**8k→64k** curriculum).
|
| 128 |
-
- **Ablations** (paper Table 4): curriculum extension, Memory Interleave, continuous pretraining, and injecting original text all contribute substantially; removing them causes **5%–37%** drops depending on task.
|
| 129 |
-
- **Training cost**: MSA's near-linear complexity shows up directly in wall-clock time — full attention grows quadratically with context length while MSA stays close to linear.
|
| 130 |
-
|
| 131 |
-
---
|
| 132 |
-
|
| 133 |
-
## 🚀 Quick Start
|
| 134 |
-
|
| 135 |
-
> This section is self-contained. [**QUICK_START.md**](./QUICK_START.md) additionally documents a
|
| 136 |
-
> prebuilt `flash-attn` wheel, which avoids a slow source build.
|
| 137 |
-
|
| 138 |
-
### Requirements
|
| 139 |
-
|
| 140 |
-
| | |
|
| 141 |
-
|---|---|
|
| 142 |
-
| OS | Linux, x86-64 |
|
| 143 |
-
| Python | 3.12 (tested on 3.12.9) |
|
| 144 |
-
| CUDA | 12.x |
|
| 145 |
-
| GPUs | **8× 80GB** for the default benchmark suite; **2× 80GB (A800)** is enough for the `ms_100M` 100M-token setting via Memory Parallel |
|
| 146 |
-
| Disk | ~9 GB for weights + ~0.6 GB for benchmark data |
|
| 147 |
-
|
| 148 |
-
`transformers` **must** be exactly `4.51.3` — the attention implementation targets that API and will not
|
| 149 |
-
load on 4.52+ or on 5.x. The other pins in `requirements.txt` are the versions everything was tested with.
|
| 150 |
-
|
| 151 |
-
### 1. Get the code
|
| 152 |
|
| 153 |
```bash
|
| 154 |
pip install -U "huggingface_hub==0.31.4"
|
| 155 |
-
#
|
| 156 |
-
# export HF_ENDPOINT=https://hf-mirror.com
|
| 157 |
-
|
| 158 |
huggingface-cli download Anoy123423123/MSA-Code --repo-type=model --local-dir MSA
|
| 159 |
cd MSA
|
| 160 |
```
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
### 2. Install dependencies
|
| 165 |
|
| 166 |
```bash
|
| 167 |
-
conda create -n msa python=3.12 -y
|
| 168 |
-
conda activate msa
|
| 169 |
pip install -r requirements.txt
|
| 170 |
-
```
|
| 171 |
-
|
| 172 |
-
`flash-attn` is installed separately because it needs `--no-build-isolation`:
|
| 173 |
-
|
| 174 |
-
```bash
|
| 175 |
pip install flash-attn==2.7.4.post1 --no-build-isolation
|
| 176 |
```
|
| 177 |
|
| 178 |
-
|
|
|
|
| 179 |
|
| 180 |
```bash
|
| 181 |
python -c "import torch, transformers, flash_attn, pynvml; print(torch.__version__, transformers.__version__, flash_attn.__version__)"
|
| 182 |
# expected: 2.6.0 4.51.3 2.7.4.post1
|
| 183 |
```
|
| 184 |
|
| 185 |
-
|
| 186 |
|
| 187 |
```bash
|
| 188 |
huggingface-cli download --resume-download Anoy123423123/MSA-4B --local-dir ckpt/MSA-4B
|
| 189 |
```
|
| 190 |
|
| 191 |
-
This is the path `scripts/run_benchmarks.sh` expects (`model_path=ckpt/MSA-4B`).
|
| 192 |
-
|
| 193 |
-
### 4. Benchmark data
|
| 194 |
-
|
| 195 |
-
Nothing to do — benchmark files are fetched on first use from
|
| 196 |
-
[`Anoy123423123/MSA-RAG-BENCHMARKS`](https://huggingface.co/datasets/Anoy123423123/MSA-RAG-BENCHMARKS)
|
| 197 |
-
into `./data/<benchmark>/`. Each benchmark is two pickle files: `mdata_*.pkl` (memory corpus) and
|
| 198 |
-
`qdata_*.pkl` (queries). ~0.6 GB for everything; `ms_100M` alone is 440 MB.
|
| 199 |
|
| 200 |
-
|
| 201 |
|
| 202 |
```bash
|
| 203 |
bash scripts/run_benchmarks.sh eval_benchmark
|
| 204 |
```
|
| 205 |
|
| 206 |
-
|
| 207 |
-
|
| 208 |
|
| 209 |
-
|
| 210 |
|
| 211 |
```bash
|
| 212 |
-
export
|
| 213 |
-
model_path=ckpt/MSA-4B
|
| 214 |
-
```
|
| 215 |
-
|
| 216 |
-
The `benchmarks` array holds `name:batch_size` entries. **Batch size must be scaled together with the
|
| 217 |
-
GPU count**: the memory corpus is partitioned into one bucket per visible GPU, so the defaults below
|
| 218 |
-
assume 8 GPUs.
|
| 219 |
-
|
| 220 |
-
| Category | Benchmark | Default batch size |
|
| 221 |
-
|---|---|---|
|
| 222 |
-
| Multi-hop QA | `2wikimultihopqa`, `hotpotqa`, `musique` | 16 |
|
| 223 |
-
| Single-hop QA | `nature_questions`, `triviaqa_06M` | 16 |
|
| 224 |
-
| Single-hop QA (large corpus) | `triviaqa_10M` | 2 |
|
| 225 |
-
| HippoRAG | `hipporag_narrative`, `hipporag_popqa` | 16 |
|
| 226 |
-
| Passage retrieval / multilingual | `msmarco_v1`, `dureader` | 16 |
|
| 227 |
-
| Length scaling (100M tokens) | `ms_100M` | 16 |
|
| 228 |
-
|
| 229 |
-
`ms_100M` and `triviaqa_06M` are commented out by default — uncomment them to reproduce the
|
| 230 |
-
length-scaling results. `ms_100M` is the 100M-token setting and is by far the heaviest run.
|
| 231 |
-
|
| 232 |
-
### 6. Score the answers
|
| 233 |
-
|
| 234 |
-
Answer quality is graded by an LLM judge served through OpenRouter:
|
| 235 |
-
|
| 236 |
-
```bash
|
| 237 |
-
export OPENROUTER_API_KEY=<your key> # or edit scripts/calculate_llm_score.sh
|
| 238 |
bash scripts/calculate_llm_score.sh eval_benchmark
|
| 239 |
```
|
| 240 |
|
| 241 |
-
The argument must match the log directory name from step
|
| 242 |
|
| 243 |
-
|
| 244 |
|
| 245 |
-
|
| 246 |
|
| 247 |
-
```
|
| 248 |
-
|
| 249 |
-
│ ├── run_benchmarks.sh # Run inference on benchmarks
|
| 250 |
-
│ ├── calculate_llm_score.sh # LLM-based answer evaluation
|
| 251 |
-
│ └── resave_model.sh # Convert base model to MSA format
|
| 252 |
-
└── src/
|
| 253 |
-
├── msa/ # Core MSA implementation
|
| 254 |
-
│ ├── configuration_msa.py # MSA model configuration
|
| 255 |
-
│ ├── memory_sparse_attention.py # MemorySparseAttention layer
|
| 256 |
-
│ ├── model.py # MSAForCausalLM / MSAModel
|
| 257 |
-
│ └── generate.py # Generation logic
|
| 258 |
-
├── config/
|
| 259 |
-
│ └── memory_config.py # GenerateConfig, ModelConfig, MemoryConfig
|
| 260 |
-
├── evaluation/
|
| 261 |
-
│ └── llm_judge.py # LLM-based evaluation metrics
|
| 262 |
-
├── app/
|
| 263 |
-
│ └── benchmark.py # Benchmark runner
|
| 264 |
-
├── utils/ # GPU workers, caching, templates, etc.
|
| 265 |
-
├── msa_service.py # Multi-GPU inference engine (MSAEngine)
|
| 266 |
-
├── prefill.py # Stage 1 prefill worker
|
| 267 |
-
├── benchmarks.py # Benchmark registry & specs
|
| 268 |
-
└── types.py # Core type definitions
|
| 269 |
-
```
|
| 270 |
|
| 271 |
-
-
|
| 272 |
|
| 273 |
-
|
|
|
|
| 274 |
|
| 275 |
-
|
|
|
|
|
|
|
| 276 |
|---|---|
|
| 277 |
-
|
|
| 278 |
-
| `import pynvml` fails
|
| 279 |
-
| CUDA OOM | Lower
|
| 280 |
-
|
|
| 281 |
-
| Benchmark data download stalls | Set `export HF_ENDPOINT=https://hf-mirror.com` and retry; partial files resume. |
|
| 282 |
|
| 283 |
-
-
|
| 284 |
|
| 285 |
## Citation
|
| 286 |
|
| 287 |
-
|
| 288 |
|
| 289 |
## License
|
| 290 |
|
|
|
|
| 7 |
---
|
| 8 |
# MSA: Memory Sparse Attention
|
| 9 |
|
| 10 |
+
*A scalable, end-to-end trainable latent-memory framework for 100M-token contexts.*
|
| 11 |
|
| 12 |
+
**Anonymous code release for peer review.** This repository contains the inference and evaluation code
|
| 13 |
+
used to produce the reported benchmark results.
|
| 14 |
|
| 15 |
| | |
|
| 16 |
|---|---|
|
|
|
|
| 18 |
| Model weights | [`Anoy123423123/MSA-4B`](https://huggingface.co/Anoy123423123/MSA-4B) |
|
| 19 |
| Benchmark data | [`Anoy123423123/MSA-RAG-BENCHMARKS`](https://huggingface.co/datasets/Anoy123423123/MSA-RAG-BENCHMARKS) |
|
| 20 |
|
| 21 |
+
## Requirements
|
| 22 |
|
| 23 |
+
Linux x86-64, Python 3.12, CUDA 12.x. **8× 80GB GPUs** for the default benchmark suite; the 100M-token
|
| 24 |
+
`ms_100M` setting runs on **2× A800** via Memory Parallel. ~9 GB disk for weights, ~0.6 GB for data.
|
| 25 |
|
| 26 |
+
`transformers` must be exactly **4.51.3** — the attention implementation targets that API and will not
|
| 27 |
+
load on 4.52+ or 5.x. The other pins in `requirements.txt` are the tested versions.
|
| 28 |
|
| 29 |
+
## Quick Start
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
**1. Get the code**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
```bash
|
| 34 |
pip install -U "huggingface_hub==0.31.4"
|
| 35 |
+
# export HF_ENDPOINT=https://hf-mirror.com # uncomment if you need a mirror
|
|
|
|
|
|
|
| 36 |
huggingface-cli download Anoy123423123/MSA-Code --repo-type=model --local-dir MSA
|
| 37 |
cd MSA
|
| 38 |
```
|
| 39 |
|
| 40 |
+
**2. Install**
|
|
|
|
|
|
|
| 41 |
|
| 42 |
```bash
|
| 43 |
+
conda create -n msa python=3.12 -y && conda activate msa
|
|
|
|
| 44 |
pip install -r requirements.txt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
pip install flash-attn==2.7.4.post1 --no-build-isolation
|
| 46 |
```
|
| 47 |
|
| 48 |
+
`flash-attn` is separate because it needs `--no-build-isolation`. To skip the slow source build, see the
|
| 49 |
+
prebuilt wheel in [QUICK_START.md](./QUICK_START.md). Verify:
|
| 50 |
|
| 51 |
```bash
|
| 52 |
python -c "import torch, transformers, flash_attn, pynvml; print(torch.__version__, transformers.__version__, flash_attn.__version__)"
|
| 53 |
# expected: 2.6.0 4.51.3 2.7.4.post1
|
| 54 |
```
|
| 55 |
|
| 56 |
+
**3. Download the weights**
|
| 57 |
|
| 58 |
```bash
|
| 59 |
huggingface-cli download --resume-download Anoy123423123/MSA-4B --local-dir ckpt/MSA-4B
|
| 60 |
```
|
| 61 |
|
| 62 |
+
This is the path `scripts/run_benchmarks.sh` expects (`model_path=ckpt/MSA-4B`).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
**4. Run inference**
|
| 65 |
|
| 66 |
```bash
|
| 67 |
bash scripts/run_benchmarks.sh eval_benchmark
|
| 68 |
```
|
| 69 |
|
| 70 |
+
Benchmark data is fetched automatically on first use into `./data/`. Results land in
|
| 71 |
+
`src/evaluation/outputs/eval_benchmark/` as one `.log` and one `.json` per benchmark.
|
| 72 |
|
| 73 |
+
**5. Score the answers**
|
| 74 |
|
| 75 |
```bash
|
| 76 |
+
export OPENROUTER_API_KEY=<your key>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
bash scripts/calculate_llm_score.sh eval_benchmark
|
| 78 |
```
|
| 79 |
|
| 80 |
+
The argument must match the log directory name from step 4.
|
| 81 |
|
| 82 |
+
## Benchmarks
|
| 83 |
|
| 84 |
+
Configured in the `benchmarks` array at the top of `scripts/run_benchmarks.sh` as `name:batch_size`:
|
| 85 |
|
| 86 |
+
`2wikimultihopqa` `hotpotqa` `musique` `nature_questions` `triviaqa_06M` `triviaqa_10M`
|
| 87 |
+
`hipporag_narrative` `hipporag_popqa` `msmarco_v1` `dureader` `ms_100M`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
`ms_100M` (the 100M-token setting) and `triviaqa_06M` are commented out by default.
|
| 90 |
|
| 91 |
+
**Batch size must be scaled with your GPU count** — the memory corpus is partitioned into one bucket per
|
| 92 |
+
visible GPU, and the defaults assume the 8 GPUs in `CUDA_VISIBLE_DEVICES`.
|
| 93 |
|
| 94 |
+
## Notes
|
| 95 |
+
|
| 96 |
+
| Symptom | Fix |
|
| 97 |
|---|---|
|
| 98 |
+
| Errors loading the model | `transformers` must be exactly `4.51.3`. |
|
| 99 |
+
| `import pynvml` fails | The module comes from `nvidia-ml-py`; the separate deprecated `pynvml` distribution shadows it. `pip uninstall -y pynvml && pip install nvidia-ml-py==12.575.51` |
|
| 100 |
+
| CUDA OOM | Lower `batch_size` in `scripts/run_benchmarks.sh`. |
|
| 101 |
+
| Data download stalls | `export HF_ENDPOINT=https://hf-mirror.com` and retry; partial files resume. |
|
|
|
|
| 102 |
|
| 103 |
+
See [QUICK_START.md](./QUICK_START.md) for the project layout and the prebuilt `flash-attn` wheel.
|
| 104 |
|
| 105 |
## Citation
|
| 106 |
|
| 107 |
+
Withheld during anonymous peer review.
|
| 108 |
|
| 109 |
## License
|
| 110 |
|