File size: 8,895 Bytes
55b60a8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | <p align="center">
<img src="https://raw.githubusercontent.com/wbopan/flashtrace/master/docs/assets/flashtrace-logo.png" alt="FlashTrace logo" width="160">
</p>
<h1 align="center">FlashTrace</h1>
<p align="center">
<em>Fast token attribution for reasoning language models.</em>
</p>
<p align="center">
<a href="https://pypi.org/project/flashtrace/"><img alt="PyPI" src="https://img.shields.io/pypi/v/flashtrace.svg?style=flat-square&logo=pypi&logoColor=white&label=PyPI"></a>
<a href="https://pypi.org/project/flashtrace/"><img alt="Python" src="https://img.shields.io/pypi/pyversions/flashtrace.svg?style=flat-square&logo=python&logoColor=white"></a>
<a href="https://github.com/wbopan/flashtrace/blob/master/LICENSE"><img alt="License" src="https://img.shields.io/badge/License-MIT-blue.svg?style=flat-square"></a>
<a href="https://pytorch.org/"><img alt="PyTorch" src="https://img.shields.io/badge/PyTorch-2.5%2B-EE4C2C.svg?style=flat-square&logo=pytorch&logoColor=white"></a>
<a href="https://arxiv.org/abs/2602.01914"><img alt="arXiv" src="https://img.shields.io/badge/arXiv-2602.01914-B31B1B.svg?style=flat-square&logo=arxiv&logoColor=white"></a>
</p>
FlashTrace traces generated answers back to the prompt tokens that shaped them. Use it from Python or the command line, export JSON traces, and render standalone HTML heatmaps for inspection and sharing.
<p align="center">
<a href="https://arxiv.org/abs/2602.01914">馃搫 Paper</a>
路
<a href="#quickstart">馃殌 Quickstart</a>
路
<a href="#command-line">馃捇 CLI</a>
路
<a href="#citation">馃摑 Citation</a>
</p>
## Why FlashTrace
Reasoning models produce long generated chains, final answers, and intermediate spans that deserve targeted inspection. FlashTrace gives researchers a package-first workflow for tracing a selected generated span back to its supporting prompt tokens.
You get:
- top-k prompt tokens ranked by attribution score
- JSON traces for downstream analysis
- standalone HTML token heatmaps
- optional per-hop attribution panels
- inclusive generation-token span controls for answer and reasoning segments
## Install
From PyPI:
```bash
pip install flashtrace
```
From a local checkout:
```bash
pip install -e .
```
For development:
```bash
pip install -e ".[dev]"
```
FlashTrace uses PyTorch, Transformers, Accelerate, NumPy, and tqdm. A CUDA-capable GPU is recommended for public-scale Hugging Face models.
## Quickstart
```python
from flashtrace import FlashTrace, load_model_and_tokenizer
prompt = """Context: Paris is the capital of France.
Question: What is the capital of France?"""
target = "Paris"
model, tokenizer = load_model_and_tokenizer("Qwen/Qwen3-8B", device_map="auto")
tracer = FlashTrace(model, tokenizer, chunk_tokens=128, sink_chunk_tokens=32)
trace = tracer.trace(
prompt=prompt,
target=target,
output_span=(0, 0),
hops=1,
)
print(trace.topk_inputs(10))
trace.to_json("trace.json")
trace.to_html("trace.html")
```
`trace.topk_inputs(10)` returns `TokenScore` objects aligned to prompt-token indices:
```text
rank index token score
1 2 Paris 0.184
2 7 capital 0.131
3 10 France 0.119
```
`trace.html` is a standalone heatmap that highlights prompt tokens by final attribution score and includes trace metadata for the selected generated span.
`FlashTrace(..., use_chat_template=True)` formats prompts with the tokenizer chat template for chat-tuned models.
## Command Line
Create prompt and target files:
```bash
printf "Context: Paris is the capital of France.\nQuestion: What is the capital of France?\n" > prompt.txt
printf "Paris" > target.txt
```
Run a trace:
```bash
flashtrace trace \
--model Qwen/Qwen3-8B \
--prompt prompt.txt \
--target target.txt \
--output-span 0:0 \
--hops 1 \
--html trace.html \
--json trace.json
```
The command prints a compact top-k table and writes the requested artifacts.
Useful flags:
- `--model`: Hugging Face model id or local model path
- `--prompt`: UTF-8 prompt text file
- `--target`: UTF-8 target text file
- `--output-span`: inclusive `START:END` indices over generated tokens
- `--reasoning-span`: inclusive `START:END` indices for a reasoning segment
- `--method`: `flashtrace`, `ifr-span`, or `ifr-matrix`
- `--recompute-attention`: lower-memory attention recomputation path
- `--use-chat-template`: format prompts with the tokenizer chat template
- `--device-map`: Transformers device map, default `auto`
- `--dtype`: `auto`, `float16`, `bfloat16`, or `float32`
## Token Spans
`output_span` and `reasoning_span` use inclusive generation-token indices. The first generated token has index `0`.
Use an initial trace to inspect tokenization:
```python
for index, token in enumerate(trace.generation_tokens):
print(index, repr(token))
```
Then choose spans:
```python
trace = tracer.trace(
prompt=prompt,
target=target,
reasoning_span=(0, 79),
output_span=(80, 85),
hops=1,
)
```
Scores are aligned to `trace.prompt_tokens`. `trace.per_hop_scores` stores the same prompt-token alignment for each hop.
## Interpreting Results
High-scoring prompt tokens are the tokens FlashTrace attributes most strongly to the selected generated span. For answer inspection, use `output_span` around the final answer tokens. For chain-of-thought or reasoning inspection, use `reasoning_span` around the generated reasoning segment.
Recommended workflow:
1. Run a trace with your prompt and target.
2. Inspect `trace.generation_tokens`.
3. Select the answer or reasoning span.
4. Export `trace.html`.
5. Compare top-k tokens with the source prompt and any expected evidence.
## Supported Models
FlashTrace targets Llama/Qwen-style decoder-only Hugging Face causal LMs with:
- `model.layers`
- Q/K/V/O attention projections
- RMSNorm or LayerNorm
- RoPE metadata
Validated model families for the first public release:
- Qwen2
- Qwen3
- Llama
## Python API
The public package exports:
```python
from flashtrace import FlashTrace, TraceResult, load_model_and_tokenizer
```
`FlashTrace.trace(...)` accepts:
- `prompt: str`
- `target: str | None`
- `output_span: tuple[int, int] | None`
- `reasoning_span: tuple[int, int] | None`
- `hops: int`
- `method: "flashtrace" | "ifr-span" | "ifr-matrix"`
- `renorm_threshold: float | None`
`TraceResult` includes:
- `prompt_tokens`
- `generation_tokens`
- `scores`
- `per_hop_scores`
- `thinking_ratios`
- `output_span`
- `reasoning_span`
- `method`
- `metadata`
Export helpers:
```python
trace.topk_inputs(20)
trace.to_dict()
trace.to_json("trace.json")
trace.to_html("trace.html")
```
## Examples
```bash
python examples/quickstart.py --help
python examples/quickstart.py \
--model Qwen/Qwen3-8B \
--prompt "Context: Paris is the capital of France. Question: What is the capital of France?" \
--target "Paris" \
--output-span 0:0 \
--html trace.html
```
Heavy model examples are intended for GPU environments. CPU smoke tests use tiny randomly initialized models.
## Repository Map
- `flashtrace/`: reusable Python package
- `examples/`: public quickstarts
- `tests/`: CPU smoke tests
- `exp/`: paper experiments and research artifacts
- `docs/superpowers/`: design and implementation planning documents
## Research Experiments
The `exp/` directory contains the paper-era experiment runners, case studies, and saved artifacts. The public package API lives in `flashtrace/`; experiment scripts keep compatibility imports during the package migration.
## Troubleshooting
**CUDA memory**
Use smaller models, lower precision, `device_map="auto"`, shorter prompts, or `--recompute-attention`.
**Span selection**
Print `trace.generation_tokens` and select inclusive generated-token indices. Tokenization can split visible words into multiple model tokens.
**Deterministic generation**
Pass a `target` file for attribution against a known output. Leave `--target` out when you want the CLI to generate with deterministic defaults.
**Tokenizer alignment**
Inspect `trace.prompt_tokens` and `trace.generation_tokens` when scores appear shifted from visible text. Attribution scores follow tokenizer-level alignment.
**HTML export**
`trace.to_html("trace.html")` writes a standalone file that can be opened locally or shared as an artifact.
## Paper
FlashTrace implements the method described in [Towards Long-Horizon Interpretability: Efficient and Faithful Multi-Token Attribution for Reasoning LLMs](https://arxiv.org/abs/2602.01914).
## Citation
```bibtex
@misc{pan2026flashtrace,
title={Towards Long-Horizon Interpretability: Efficient and Faithful Multi-Token Attribution for Reasoning LLMs},
author={Pan, Wenbo and Liu, Zhichao and Wang, Xianlong and Yu, Haining and Jia, Xiaohua},
year={2026},
eprint={2602.01914},
archivePrefix={arXiv},
primaryClass={cs.LG}
}
```
|