# truekiwwy/llm-surgeon architecture

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

- Interactive diagram: https://gitdiagram.com/truekiwwy/llm-surgeon
- Repository: https://github.com/truekiwwy/llm-surgeon
- Video tour (about a minute): https://gitdiagram.com/truekiwwy/llm-surgeon/video

## Overview

LLM Surgeon is a Rust command-line model-surgery tool with MCP access and desktop interfaces. The main user workflow is to inspect local model files and machine capacity, derive or supply a steering direction or patch, apply the edit with backup/rollback safeguards, then evaluate the result. Separate capabilities include graph analysis, LoRA and checkpoint-diff operations, automated patch pipelines, plugins, and agent tools. The sampled sources support the core orchestration and several subsystem links; unsampled GUI and other modules are included from the tree and README, with uncertain connections omitted.

Main entry points: [`src/app.rs`](https://github.com/truekiwwy/llm-surgeon/blob/main/src/app.rs) (Command orchestration), [`src/patch/mod.rs`](https://github.com/truekiwwy/llm-surgeon/blob/main/src/patch/mod.rs) (Patch apply and restore), [`src/extract/mod.rs`](https://github.com/truekiwwy/llm-surgeon/blob/main/src/extract/mod.rs) (Direction extraction), [`src/eval/mod.rs`](https://github.com/truekiwwy/llm-surgeon/blob/main/src/eval/mod.rs) (Model evaluation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["User interfaces"]
  node_cli["CLI commands<br/>[cli.rs]"]
  node_orchestrator["Command orchestration<br/>[app.rs]"]
  node_desktop["Desktop app<br/>[app.rs]"]
  node_terminal["Terminal interface<br/>[app.rs]"]
end

subgraph group_analysis["Model analysis"]
  node_model["Local model files"]
  node_hardware["Hardware budget<br/>[mod.rs]"]
  node_extract["Direction extraction<br/>[mod.rs]"]
  node_graph["Weight graph<br/>[mod.rs]"]
end

subgraph group_editing["Weight editing"]
  node_patch["Patch apply and restore<br/>[mod.rs]"]
  node_gguf["GGUF editing<br/>[mod.rs]"]
  node_engine["SafeTensors engine<br/>[mod.rs]"]
  node_surgery["Checkpoint surgery<br/>[mod.rs]"]
  node_lora["LoRA operations<br/>[mod.rs]"]
end

subgraph group_runtime["Generation and evaluation"]
  node_backend["Generation backends<br/>[mod.rs]"]
  node_eval["Model evaluation<br/>[mod.rs]"]
end

subgraph group_automation["Automation and extensions"]
  node_pipeline["Patch pipeline<br/>[run.rs]"]
  node_mcp["MCP server and tools<br/>[server.rs]"]
  node_plugins["Extraction plugins<br/>[mod.rs]"]
end

node_user(("User"))

node_user -->|"runs commands"| node_cli
node_user -->|"uses desktop"| node_desktop
node_user -.->|"uses terminal"| node_terminal
node_cli -->|"dispatches to"| node_orchestrator
node_orchestrator -->|"checks capacity"| node_hardware
node_orchestrator -->|"runs extraction"| node_extract
node_orchestrator -->|"applies patches"| node_patch
node_orchestrator -->|"runs evaluation"| node_eval
node_orchestrator -->|"runs pipeline"| node_pipeline
node_model -->|"measures size"| node_hardware
node_model -->|"captures activations"| node_extract
node_model -->|"analyzes weights"| node_graph
node_extract -->|"supplies direction"| node_patch
node_patch -->|"edits tensors"| node_engine
node_patch -->|"applies GGUF edits"| node_gguf
node_patch -->|"writes edited weights"| node_model
node_surgery -->|"reads checkpoints"| node_model
node_lora -->|"merges adapters"| node_model
node_orchestrator -->|"runs surgery"| node_surgery
node_orchestrator -->|"runs LoRA"| node_lora
node_eval -->|"uses generators"| node_backend
node_backend -->|"serves generation"| node_eval
node_eval -->|"measures outcomes"| node_model
node_pipeline -->|"promotes patch"| node_patch
node_mcp -->|"starts worker"| node_pipeline
node_mcp -->|"invokes commands"| node_orchestrator
node_extract -->|"runs methods"| node_plugins

click node_cli "https://github.com/truekiwwy/llm-surgeon/blob/main/src/cli.rs"
click node_orchestrator "https://github.com/truekiwwy/llm-surgeon/blob/main/src/app.rs"
click node_desktop "https://github.com/truekiwwy/llm-surgeon/blob/main/crates/surgeon-gui/src/app.rs"
click node_terminal "https://github.com/truekiwwy/llm-surgeon/blob/main/src/tui/app.rs"
click node_hardware "https://github.com/truekiwwy/llm-surgeon/blob/main/src/hardware/mod.rs"
click node_extract "https://github.com/truekiwwy/llm-surgeon/blob/main/src/extract/mod.rs"
click node_graph "https://github.com/truekiwwy/llm-surgeon/blob/main/src/graph/mod.rs"
click node_patch "https://github.com/truekiwwy/llm-surgeon/blob/main/src/patch/mod.rs"
click node_gguf "https://github.com/truekiwwy/llm-surgeon/blob/main/src/patch/gguf/mod.rs"
click node_engine "https://github.com/truekiwwy/llm-surgeon/blob/main/src/engine/mod.rs"
click node_surgery "https://github.com/truekiwwy/llm-surgeon/blob/main/src/surgery/mod.rs"
click node_lora "https://github.com/truekiwwy/llm-surgeon/blob/main/src/lora/mod.rs"
click node_pipeline "https://github.com/truekiwwy/llm-surgeon/blob/main/src/pipeline/run.rs"
click node_mcp "https://github.com/truekiwwy/llm-surgeon/blob/main/src/mcp/server.rs"
click node_plugins "https://github.com/truekiwwy/llm-surgeon/blob/main/src/plugin/mod.rs"
click node_backend "https://github.com/truekiwwy/llm-surgeon/blob/main/src/backend/mod.rs"
click node_eval "https://github.com/truekiwwy/llm-surgeon/blob/main/src/eval/mod.rs"

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_cli,node_orchestrator,node_desktop,node_terminal,node_user toneBlue
class node_model,node_hardware,node_extract,node_graph toneAmber
class node_patch,node_gguf,node_engine,node_surgery,node_lora toneMint
class node_backend,node_eval toneRose
class node_pipeline,node_mcp,node_plugins toneIndigo
```

## Components

### User interfaces

- [CLI commands](https://github.com/truekiwwy/llm-surgeon/blob/main/src/cli.rs): `src/cli.rs`
- [Command orchestration](https://github.com/truekiwwy/llm-surgeon/blob/main/src/app.rs): `src/app.rs`
- [Desktop app](https://github.com/truekiwwy/llm-surgeon/blob/main/crates/surgeon-gui/src/app.rs): `crates/surgeon-gui/src/app.rs`
- [Terminal interface](https://github.com/truekiwwy/llm-surgeon/blob/main/src/tui/app.rs): `src/tui/app.rs`

### Model analysis

- **Local model files**
- [Hardware budget](https://github.com/truekiwwy/llm-surgeon/blob/main/src/hardware/mod.rs): `src/hardware/mod.rs`
- [Direction extraction](https://github.com/truekiwwy/llm-surgeon/blob/main/src/extract/mod.rs): `src/extract/mod.rs`
- [Weight graph](https://github.com/truekiwwy/llm-surgeon/blob/main/src/graph/mod.rs): `src/graph/mod.rs`

### Weight editing

- [Patch apply and restore](https://github.com/truekiwwy/llm-surgeon/blob/main/src/patch/mod.rs): `src/patch/mod.rs`
- [GGUF editing](https://github.com/truekiwwy/llm-surgeon/blob/main/src/patch/gguf/mod.rs): `src/patch/gguf/mod.rs`
- [SafeTensors engine](https://github.com/truekiwwy/llm-surgeon/blob/main/src/engine/mod.rs): `src/engine/mod.rs`
- [Checkpoint surgery](https://github.com/truekiwwy/llm-surgeon/blob/main/src/surgery/mod.rs): `src/surgery/mod.rs`
- [LoRA operations](https://github.com/truekiwwy/llm-surgeon/blob/main/src/lora/mod.rs): `src/lora/mod.rs`

### Generation and evaluation

- [Generation backends](https://github.com/truekiwwy/llm-surgeon/blob/main/src/backend/mod.rs): `src/backend/mod.rs`
- [Model evaluation](https://github.com/truekiwwy/llm-surgeon/blob/main/src/eval/mod.rs): `src/eval/mod.rs`

### Automation and extensions

- [Patch pipeline](https://github.com/truekiwwy/llm-surgeon/blob/main/src/pipeline/run.rs): `src/pipeline/run.rs`
- [MCP server and tools](https://github.com/truekiwwy/llm-surgeon/blob/main/src/mcp/server.rs): `src/mcp/server.rs`
- [Extraction plugins](https://github.com/truekiwwy/llm-surgeon/blob/main/src/plugin/mod.rs): `src/plugin/mod.rs`

### Other components

- **User**

## Connections

- User → CLI commands: runs commands (evidence: `README.md`)
- User → Desktop app: uses desktop (evidence: `README.md`)
- User → Terminal interface: uses terminal
- CLI commands → Command orchestration: dispatches to (evidence: `src/app.rs`)
- Command orchestration → Hardware budget: checks capacity (evidence: `src/app.rs`)
- Command orchestration → Direction extraction: runs extraction (evidence: `src/app.rs`)
- Command orchestration → Patch apply and restore: applies patches (evidence: `src/app.rs`)
- Command orchestration → Model evaluation: runs evaluation (evidence: `src/app.rs`)
- Command orchestration → Patch pipeline: runs pipeline (evidence: `src/pipeline/front.rs`)
- Local model files → Hardware budget: measures size (evidence: `src/hardware/mod.rs`)
- Local model files → Direction extraction: captures activations (evidence: `src/extract/mod.rs`)
- Local model files → Weight graph: analyzes weights (evidence: `README.md`)
- Direction extraction → Patch apply and restore: supplies direction (evidence: `README.md`)
- Patch apply and restore → SafeTensors engine: edits tensors (evidence: `src/engine/mod.rs`)
- Patch apply and restore → GGUF editing: applies GGUF edits (evidence: `src/patch/mod.rs`)
- Patch apply and restore → Local model files: writes edited weights (evidence: `README.md`)
- Checkpoint surgery → Local model files: reads checkpoints (evidence: `README.md`)
- LoRA operations → Local model files: merges adapters (evidence: `README.md`)
- Command orchestration → Checkpoint surgery: runs surgery (evidence: `src/app.rs`)
- Command orchestration → LoRA operations: runs LoRA (evidence: `src/app.rs`)
- Model evaluation → Generation backends: uses generators (evidence: `src/backend/mod.rs`)
- Generation backends → Model evaluation: serves generation (evidence: `src/backend/mod.rs`)
- Model evaluation → Local model files: measures outcomes (evidence: `README.md`)
- Patch pipeline → Patch apply and restore: promotes patch (evidence: `src/pipeline/run.rs`)
- MCP server and tools → Patch pipeline: starts worker
- MCP server and tools → Command orchestration: invokes commands (evidence: `src/mcp/server.rs`)
- Direction extraction → Extraction plugins: runs methods (evidence: `src/extract/mod.rs`)
