# csaugust/nanospec architecture

> Architecture diagram of the GitHub repository csaugust/nanospec, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/csaugust/nanospec
- Repository: https://github.com/csaugust/nanospec

## Overview

NanoSpec is a GPU-focused speculative-decoding system that dynamically narrows the draft model’s active vocabulary. Its main workflow loads a base and draft model, generates and verifies draft-token trees while maintaining context-aware vocabulary state, and returns tokens and timing/acceptance data to benchmark evaluators. The repository also includes EAGLE-3 and Medusa variants, evaluation and profiling capabilities, and custom FlashAttention kernel code. Relationships among unsampled modules are omitted unless documented or supported by the source index.

Main entry points: [`src/flash_attn/src/flash_fwd_kernel.h`](https://github.com/csaugust/nanospec/blob/main/src/flash_attn/src/flash_fwd_kernel.h) (Forward kernel), [`evaluation/inference_eagle.py`](https://github.com/csaugust/nanospec/blob/main/evaluation/inference_eagle.py) (EAGLE runner), [`evaluation/inference_eagle3.py`](https://github.com/csaugust/nanospec/blob/main/evaluation/inference_eagle3.py) (EAGLE-3 runner), [`evaluation/inference_baseline.py`](https://github.com/csaugust/nanospec/blob/main/evaluation/inference_baseline.py) (Baseline runner)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Inference runtime"]
  node_runner_eagle["EAGLE runner<br/>[inference_eagle.py]"]
  node_runner_eagle3["EAGLE-3 runner"]
  node_baseline_runner["Baseline runner"]
  node_base_llm["Base LLM<br/>[llama.py]"]
end

subgraph group_drafting["Drafting and pruning"]
  node_tree_drafter["Tree generation<br/>[tree_drafter.py]"]
  node_eagle["EAGLE-2 model<br/>[eagle.py]"]
  node_eagle3["EAGLE-3 model<br/>[eagle3.py]"]
  node_medusa["Medusa model<br/>[medusa.py]"]
  node_medusa_choices["Medusa choices<br/>[medusa_choices.py]"]
  node_ring_drafter["Ring drafter"]
end

subgraph group_evaluation["Evaluation and profiling"]
  node_mt_eval["MT-Bench evaluation<br/>[eval.py]"]
  node_gsm_eval["GSM8K evaluation<br/>[eval.py]"]
  node_gsm_utils["GSM8K utilities<br/>[utils.py]"]
  node_he_eval["HumanEval evaluation<br/>[eval.py]"]
  node_ablation["Ablation profiling"]
end

subgraph group_attention["FlashAttention kernels"]
  node_flash_api["FlashAttention API<br/>[flash_api.hpp]"]
  node_flash_kernel["Forward kernel<br/>[flash_fwd_kernel.h]"]
  node_kernel_traits["Kernel traits<br/>[kernel_traits.h]"]
  node_masking["Attention masking<br/>[mask.h]"]
  node_softmax["Attention softmax<br/>[softmax.h]"]
  node_dropout["Attention dropout<br/>[dropout.h]"]
  node_rotary["Rotary embeddings<br/>[rotary.h]"]
  node_flash_core["Attention interface<br/>[flash.h]"]
end

node_user(("Inference user"))

node_user -->|"runs inference"| node_runner_eagle
node_user -->|"runs inference"| node_runner_eagle3
node_user -->|"runs inference"| node_baseline_runner
node_runner_eagle -->|"creates model"| node_eagle
node_runner_eagle3 -->|"creates model"| node_eagle3
node_baseline_runner -->|"creates model"| node_base_llm
node_eagle -->|"extends"| node_tree_drafter
node_eagle3 -->|"uses"| node_tree_drafter
node_medusa -->|"uses"| node_tree_drafter
node_tree_drafter -->|"extends"| node_base_llm
node_runner_eagle -->|"routes evaluation"| node_mt_eval
node_runner_eagle -->|"routes evaluation"| node_he_eval
node_runner_eagle -->|"routes evaluation"| node_gsm_eval
node_runner_eagle3 -->|"routes evaluation"| node_mt_eval
node_runner_eagle3 -->|"routes evaluation"| node_he_eval
node_runner_eagle3 -->|"routes evaluation"| node_gsm_eval
node_baseline_runner -->|"routes evaluation"| node_mt_eval
node_baseline_runner -->|"routes evaluation"| node_he_eval
node_baseline_runner -->|"routes evaluation"| node_gsm_eval
node_gsm_eval -->|"uses utilities"| node_gsm_utils
node_medusa -->|"uses choices"| node_medusa_choices
node_flash_api -->|"calls"| node_flash_core
node_flash_core -.->|"dispatches"| node_flash_kernel
node_flash_kernel -->|"uses traits"| node_kernel_traits
node_flash_kernel -->|"uses masks"| node_masking
node_flash_kernel -->|"uses softmax"| node_softmax
node_flash_kernel -->|"uses dropout"| node_dropout
node_flash_kernel -->|"uses rotary"| node_rotary
node_user -->|"runs profiling"| node_ablation

click node_runner_eagle "https://github.com/csaugust/nanospec/blob/main/evaluation/inference_eagle.py"
click node_runner_eagle3 "https://github.com/csaugust/nanospec/blob/main/evaluation/inference_eagle3.py"
click node_baseline_runner "https://github.com/csaugust/nanospec/blob/main/evaluation/inference_baseline.py"
click node_base_llm "https://github.com/csaugust/nanospec/blob/main/llamacu/llama.py"
click node_tree_drafter "https://github.com/csaugust/nanospec/blob/main/llamacu/speculative/tree_drafter.py"
click node_eagle "https://github.com/csaugust/nanospec/blob/main/llamacu/speculative/eagle.py"
click node_eagle3 "https://github.com/csaugust/nanospec/blob/main/llamacu/speculative/eagle3.py"
click node_medusa "https://github.com/csaugust/nanospec/blob/main/llamacu/speculative/medusa.py"
click node_medusa_choices "https://github.com/csaugust/nanospec/blob/main/llamacu/speculative/medusa_choices.py"
click node_ring_drafter "https://github.com/csaugust/nanospec/blob/main/llamacu/speculative/tree_drafter_ring.py"
click node_mt_eval "https://github.com/csaugust/nanospec/blob/main/evaluation/mt_bench/eval.py"
click node_gsm_eval "https://github.com/csaugust/nanospec/blob/main/evaluation/gsm8k/eval.py"
click node_gsm_utils "https://github.com/csaugust/nanospec/blob/main/evaluation/gsm8k/utils.py"
click node_he_eval "https://github.com/csaugust/nanospec/blob/main/evaluation/he_local/eval.py"
click node_ablation "https://github.com/csaugust/nanospec/blob/main/experiments/ablation/profiling_generate.py"
click node_flash_api "https://github.com/csaugust/nanospec/blob/main/src/flash_attn/flash_api.hpp"
click node_flash_kernel "https://github.com/csaugust/nanospec/blob/main/src/flash_attn/src/flash_fwd_kernel.h"
click node_kernel_traits "https://github.com/csaugust/nanospec/blob/main/src/flash_attn/src/kernel_traits.h"
click node_masking "https://github.com/csaugust/nanospec/blob/main/src/flash_attn/src/mask.h"
click node_softmax "https://github.com/csaugust/nanospec/blob/main/src/flash_attn/src/softmax.h"
click node_dropout "https://github.com/csaugust/nanospec/blob/main/src/flash_attn/src/dropout.h"
click node_rotary "https://github.com/csaugust/nanospec/blob/main/src/flash_attn/src/rotary.h"
click node_flash_core "https://github.com/csaugust/nanospec/blob/main/src/flash_attn/src/flash.h"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_runner_eagle,node_runner_eagle3,node_baseline_runner,node_base_llm,node_user toneBlue
class node_tree_drafter,node_eagle,node_eagle3,node_medusa,node_medusa_choices,node_ring_drafter toneAmber
class node_mt_eval,node_gsm_eval,node_gsm_utils,node_he_eval,node_ablation toneMint
class node_flash_api,node_flash_kernel,node_kernel_traits,node_masking,node_softmax,node_dropout,node_rotary,node_flash_core toneRose
```

## Components

### Inference runtime

- [EAGLE runner](https://github.com/csaugust/nanospec/blob/main/evaluation/inference_eagle.py): `evaluation/inference_eagle.py`
- [EAGLE-3 runner](https://github.com/csaugust/nanospec/blob/main/evaluation/inference_eagle3.py): `evaluation/inference_eagle3.py`
- [Baseline runner](https://github.com/csaugust/nanospec/blob/main/evaluation/inference_baseline.py): `evaluation/inference_baseline.py`
- [Base LLM](https://github.com/csaugust/nanospec/blob/main/llamacu/llama.py): `llamacu/llama.py`

### Drafting and pruning

- [Tree generation](https://github.com/csaugust/nanospec/blob/main/llamacu/speculative/tree_drafter.py): `llamacu/speculative/tree_drafter.py`
- [EAGLE-2 model](https://github.com/csaugust/nanospec/blob/main/llamacu/speculative/eagle.py): `llamacu/speculative/eagle.py`
- [EAGLE-3 model](https://github.com/csaugust/nanospec/blob/main/llamacu/speculative/eagle3.py): `llamacu/speculative/eagle3.py`
- [Medusa model](https://github.com/csaugust/nanospec/blob/main/llamacu/speculative/medusa.py): `llamacu/speculative/medusa.py`
- [Medusa choices](https://github.com/csaugust/nanospec/blob/main/llamacu/speculative/medusa_choices.py): `llamacu/speculative/medusa_choices.py`
- [Ring drafter](https://github.com/csaugust/nanospec/blob/main/llamacu/speculative/tree_drafter_ring.py): `llamacu/speculative/tree_drafter_ring.py`

### Evaluation and profiling

- [MT-Bench evaluation](https://github.com/csaugust/nanospec/blob/main/evaluation/mt_bench/eval.py): `evaluation/mt_bench/eval.py`
- [GSM8K evaluation](https://github.com/csaugust/nanospec/blob/main/evaluation/gsm8k/eval.py): `evaluation/gsm8k/eval.py`
- [GSM8K utilities](https://github.com/csaugust/nanospec/blob/main/evaluation/gsm8k/utils.py): `evaluation/gsm8k/utils.py`
- [HumanEval evaluation](https://github.com/csaugust/nanospec/blob/main/evaluation/he_local/eval.py): `evaluation/he_local/eval.py`
- [Ablation profiling](https://github.com/csaugust/nanospec/blob/main/experiments/ablation/profiling_generate.py): `experiments/ablation/profiling_generate.py`

### FlashAttention kernels

- [FlashAttention API](https://github.com/csaugust/nanospec/blob/main/src/flash_attn/flash_api.hpp): `src/flash_attn/flash_api.hpp`
- [Forward kernel](https://github.com/csaugust/nanospec/blob/main/src/flash_attn/src/flash_fwd_kernel.h): `src/flash_attn/src/flash_fwd_kernel.h`
- [Kernel traits](https://github.com/csaugust/nanospec/blob/main/src/flash_attn/src/kernel_traits.h): `src/flash_attn/src/kernel_traits.h`
- [Attention masking](https://github.com/csaugust/nanospec/blob/main/src/flash_attn/src/mask.h): `src/flash_attn/src/mask.h`
- [Attention softmax](https://github.com/csaugust/nanospec/blob/main/src/flash_attn/src/softmax.h): `src/flash_attn/src/softmax.h`
- [Attention dropout](https://github.com/csaugust/nanospec/blob/main/src/flash_attn/src/dropout.h): `src/flash_attn/src/dropout.h`
- [Rotary embeddings](https://github.com/csaugust/nanospec/blob/main/src/flash_attn/src/rotary.h): `src/flash_attn/src/rotary.h`
- [Attention interface](https://github.com/csaugust/nanospec/blob/main/src/flash_attn/src/flash.h): `src/flash_attn/src/flash.h`

### Other components

- **Inference user**

## Connections

- Inference user → EAGLE runner: runs inference
- Inference user → EAGLE-3 runner: runs inference
- Inference user → Baseline runner: runs inference
- EAGLE runner → EAGLE-2 model: creates model (evidence: `evaluation/inference_eagle.py`)
- EAGLE-3 runner → EAGLE-3 model: creates model (evidence: `evaluation/inference_eagle3.py`)
- Baseline runner → Base LLM: creates model (evidence: `evaluation/inference_baseline.py`)
- EAGLE-2 model → Tree generation: extends (evidence: `llamacu/speculative/eagle.py`)
- EAGLE-3 model → Tree generation: uses (evidence: `llamacu/speculative/eagle3.py`)
- Medusa model → Tree generation: uses (evidence: `llamacu/speculative/medusa.py`)
- Tree generation → Base LLM: extends (evidence: `llamacu/speculative/tree_drafter.py`)
- EAGLE runner → MT-Bench evaluation: routes evaluation (evidence: `evaluation/inference_eagle.py`)
- EAGLE runner → HumanEval evaluation: routes evaluation (evidence: `evaluation/inference_eagle.py`)
- EAGLE runner → GSM8K evaluation: routes evaluation (evidence: `evaluation/inference_eagle.py`)
- EAGLE-3 runner → MT-Bench evaluation: routes evaluation (evidence: `evaluation/inference_eagle3.py`)
- EAGLE-3 runner → HumanEval evaluation: routes evaluation (evidence: `evaluation/inference_eagle3.py`)
- EAGLE-3 runner → GSM8K evaluation: routes evaluation (evidence: `evaluation/inference_eagle3.py`)
- Baseline runner → MT-Bench evaluation: routes evaluation (evidence: `evaluation/inference_baseline.py`)
- Baseline runner → HumanEval evaluation: routes evaluation (evidence: `evaluation/inference_baseline.py`)
- Baseline runner → GSM8K evaluation: routes evaluation (evidence: `evaluation/inference_baseline.py`)
- GSM8K evaluation → GSM8K utilities: uses utilities (evidence: `evaluation/gsm8k/eval.py`)
- Medusa model → Medusa choices: uses choices (evidence: `llamacu/speculative/medusa.py`)
- FlashAttention API → Attention interface: calls (evidence: `src/flash_attn/flash_api.hpp`)
- Attention interface → Forward kernel: dispatches
- Forward kernel → Kernel traits: uses traits (evidence: `src/flash_attn/src/flash_fwd_kernel.h`)
- Forward kernel → Attention masking: uses masks (evidence: `src/flash_attn/src/flash_fwd_kernel.h`)
- Forward kernel → Attention softmax: uses softmax (evidence: `src/flash_attn/src/flash_fwd_kernel.h`)
- Forward kernel → Attention dropout: uses dropout (evidence: `src/flash_attn/src/flash_fwd_kernel.h`)
- Forward kernel → Rotary embeddings: uses rotary (evidence: `src/flash_attn/src/flash_fwd_kernel.h`)
- Inference user → Ablation profiling: runs profiling
