# xdcloudy/modelsurgeon architecture

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

- Interactive diagram: https://gitdiagram.com/xdcloudy/modelsurgeon
- Repository: https://github.com/xdcloudy/modelsurgeon

## Overview

#### Purpose and current entry points
ModelSurgeon is experimental, local-first research software for learning safe structural pruning/approximation decisions for neural-network checkpoints. Its active CLI entry point is `modelsurgeon inspect`, implemented in `src/modelsurgeon/cli/app.py` and `src/modelsurgeon/cli/inspection.py`; it loads supported Hugging Face causal LMs, identifies their architecture, and emits stable component records. Configuration and persisted configuration parsing are centered in `src/modelsurgeon/config.py` and `src/modelsurgeon/config_io.py`.

#### Adapter boundary and model intake
Adapters isolate model formats and families from the core pipeline. The common contract and family selection live in `src/modelsurgeon/adapters/base.py` and `src/modelsurgeon/adapters/family.py`. Hugging Face discovery/loading is implemented by `src/modelsurgeon/adapters/huggingface/discovery.py` and `src/modelsurgeon/adapters/huggingface/loader.py`; calibration data can come through `src/modelsurgeon/datasets/huggingface.py`. Safetensors indexing is a separate format boundary at `src/modelsurgeon/adapters/safetensors/index.py`.

#### Canonical component graph
Loaded models are translated into a validated, stable-ID component/coupling graph—the central representation for inspection, feature extraction, and valid mutation targeting. Construction, schema, IDs, remapping, and persistence are in `src/modelsurgeon/graph/builder.py`, `src/modelsurgeon/graph/schema.py`, `src/modelsurgeon/graph/component_id.py`, and `src/modelsurgeon/graph/serialization.py`. Validation prevents surgery plans from relying on malformed or incompatible topology.

#### Measurement and instrumentation
Features combine static weights/topology with runtime activations, gradients, memory, and latency signals. Hook lifecycle and runtime collectors are bounded by `src/modelsurgeon/instrumentation/hooks.py`, `src/modelsurgeon/instrumentation/gradients.py`, and `src/modelsurgeon/instrumentation/memory_telemetry.py`. Feature records/caching are organized through `src/modelsurgeon/features/schema.py`, `src/modelsurgeon/features/cache.py`, and feature implementations such as `src/modelsurgeon/features/activation.py`. This layer supports streaming and constrained-memory execution rather than requiring datacenter hardware.

#### Mutation planning and execution
The surgery subsystem resolves graph components into safe masks or physical checkpoint mutations. Core contracts and target resolution are in `src/modelsurgeon/surgery/contracts.py`, `src/modelsurgeon/surgery/target_resolution.py`, and `src/modelsurgeon/surgery/physical_plan.py`. It supports attention-head and MLP/channel operations, with transactional output handling in `src/modelsurgeon/surgery/transaction.py` and checkpoint destination protection in `src/modelsurgeon/surgery/checkpoint_destination.py`. The intended control flow is inspect/measure → predict or select mutation → plan → mutate → evaluate → retain or roll back.

#### GGUF-native checkpoint surgery
GGUF is a dedicated low-level path, separated from in-memory Hugging Face mutation. Container parsing, tensor reads, codecs, and writing are in `src/modelsurgeon/adapters/gguf/container.py`, `src/modelsurgeon/adapters/gguf/tensor_reader.py`, and `src/modelsurgeon/adapters/gguf/writer.py`. Family-specific physical surgery resides in `src/modelsurgeon/adapters/gguf/llama_surgery.py` and peers. Selective dequantization/requantization and direct copy minimize unnecessary tensor conversion; resumable and disk-aware operations address large checkpoints.

#### Evaluation and experiment record
Candidates undergo tiered evaluation: numerical/logit checks, perplexity, latency, and optional `llama.cpp` validation, implemented in `src/modelsurgeon/evaluation/tier0.py`, `src/modelsurgeon/evaluation/tiered.py`, `src/modelsurgeon/evaluation/perplexity.py`, and `src/modelsurgeon/evaluation/llama_cpp.py`. Local experiment identity, schema, migrations, hardware inventory, and memory planning live in `src/modelsurgeon/experiments/identity.py`, `src/modelsurgeon/experiments/schema.py`, `src/modelsurgeon/experiments/migrations.py`, and `src/modelsurgeon/experiments/memory.py`. External dependencies are optional PyTorch/Transformers and Hugging Face models/datasets; `llama.cpp` is an evaluation integration.

#### Operational constraints
The architecture targets Windows 11/WSL2 and bounded GPU/VRAM environments, favoring CPU offload, mixed precision, disk-backed intermediates, resumability, and OOM-aware planning. Design contracts under `docs/design/` define these durable boundaries, especially component identity, calibration, GGUF mutation, transactional writing, and memory modes.

Main entry points: [`src/modelsurgeon/graph/builder.py`](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/graph/builder.py) (Component graph builder), [`src/modelsurgeon/surgery/physical_plan.py`](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/surgery/physical_plan.py) (Physical mutation plan), [`src/modelsurgeon/evaluation/tiered.py`](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/evaluation/tiered.py) (Tiered evaluation), [`src/modelsurgeon/adapters/huggingface/loader.py`](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/adapters/huggingface/loader.py) (HF model loader)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["CLI &amp; Configuration"]
  node_cli["inspect CLI<br/>CLI command<br/>[app.py]"]
  node_config["Configuration<br/>config parsing<br/>[config.py]"]
end

subgraph group_intake["Model Intake"]
  node_adapter_contract["Adapter contract<br/>family adapter API<br/>[base.py]"]
  node_hf_discovery["HF discovery<br/>model discovery<br/>[discovery.py]"]
  node_hf_loader["HF model loader<br/>checkpoint loader<br/>[loader.py]"]
  node_calibration[("Calibration data<br/>HF dataset input<br/>[huggingface.py]")]
end

subgraph group_core["Canonical Analysis"]
  node_component_graph{{"Component graph builder<br/>canonical graph construction<br/>[builder.py]"}}
  node_graph_schema["Graph schema &amp; IDs<br/>topology validation<br/>[schema.py]"]
  node_instrumentation["Runtime instrumentation<br/>hooks and gradients<br/>[hooks.py]"]
  node_feature_store[("Feature records &amp; cache<br/>streaming feature cache<br/>[cache.py]")]
  node_feature_extraction["Feature extraction<br/>activation features<br/>[activation.py]"]
end

subgraph group_surgery["Surgery Paths"]
  node_surgery_contracts["Surgery target resolution<br/>mutation contracts"]
  node_physical_plan["Physical mutation plan<br/>safe checkpoint plan<br/>[physical_plan.py]"]
  node_transaction["Transactional output<br/>checkpoint transaction<br/>[transaction.py]"]
  node_gguf_container["GGUF container I/O<br/>native checkpoint I/O<br/>[container.py]"]
  node_gguf_surgery["GGUF family surgery<br/>low-level mutation<br/>[llama_surgery.py]"]
end

subgraph group_evaluation["Evaluation &amp; Records"]
  node_tiered_evaluation["Tiered evaluation<br/>candidate validation<br/>[tiered.py]"]
  node_llama_cpp["llama.cpp validation<br/>optional integration<br/>[llama_cpp.py]"]
  node_experiments[("Experiment records<br/>local experiment schema<br/>[schema.py]")]
end

node_cli -->|"loads"| node_config
node_cli -->|"inspects"| node_hf_discovery
node_adapter_contract -->|"selects family"| node_hf_discovery
node_hf_discovery -->|"resolves model"| node_hf_loader
node_hf_loader -->|"loaded model"| node_component_graph
node_component_graph -->|"validates IDs and topology"| node_graph_schema
node_component_graph -->|"component targets"| node_feature_extraction
node_calibration -->|"drives runtime collection"| node_instrumentation
node_hf_loader -->|"instrumented model"| node_instrumentation
node_instrumentation -->|"activations and gradients"| node_feature_extraction
node_feature_extraction -->|"streams and caches"| node_feature_store
node_component_graph -->|"valid targets"| node_surgery_contracts
node_feature_store -->|"selection signals"| node_surgery_contracts
node_surgery_contracts -->|"resolves mutation"| node_physical_plan
node_physical_plan -->|"executes safely"| node_transaction
node_physical_plan -->|"GGUF plan"| node_gguf_surgery
node_gguf_surgery -->|"reads and writes tensors"| node_gguf_container
node_transaction -->|"candidate checkpoint"| node_tiered_evaluation
node_gguf_container -->|"mutated GGUF"| node_tiered_evaluation
node_tiered_evaluation -.->|"optional validation"| node_llama_cpp
node_tiered_evaluation -->|"records results"| node_experiments
node_experiments -.->|"memory-aware planning"| node_physical_plan

click node_cli "https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/cli/app.py"
click node_config "https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/config.py"
click node_adapter_contract "https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/adapters/base.py"
click node_hf_discovery "https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/adapters/huggingface/discovery.py"
click node_hf_loader "https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/adapters/huggingface/loader.py"
click node_calibration "https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/datasets/huggingface.py"
click node_component_graph "https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/graph/builder.py"
click node_graph_schema "https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/graph/schema.py"
click node_instrumentation "https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/instrumentation/hooks.py"
click node_feature_store "https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/features/cache.py"
click node_feature_extraction "https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/features/activation.py"
click node_surgery_contracts "https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/surgery/target_resolution.py"
click node_physical_plan "https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/surgery/physical_plan.py"
click node_transaction "https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/surgery/transaction.py"
click node_gguf_container "https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/adapters/gguf/container.py"
click node_gguf_surgery "https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/adapters/gguf/llama_surgery.py"
click node_tiered_evaluation "https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/evaluation/tiered.py"
click node_llama_cpp "https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/evaluation/llama_cpp.py"
click node_experiments "https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/experiments/schema.py"

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_config toneBlue
class node_adapter_contract,node_hf_discovery,node_hf_loader,node_calibration toneAmber
class node_component_graph,node_graph_schema,node_instrumentation,node_feature_store,node_feature_extraction toneMint
class node_surgery_contracts,node_physical_plan,node_transaction,node_gguf_container,node_gguf_surgery toneRose
class node_tiered_evaluation,node_llama_cpp,node_experiments toneIndigo
```

## Components

### CLI & Configuration

- [inspect CLI](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/cli/app.py): CLI command · Active local inspection entry point. · `src/modelsurgeon/cli/app.py`
- [Configuration](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/config.py): config parsing · `src/modelsurgeon/config.py`

### Model Intake

- [Adapter contract](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/adapters/base.py): family adapter API · `src/modelsurgeon/adapters/base.py`
- [HF discovery](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/adapters/huggingface/discovery.py): model discovery · `src/modelsurgeon/adapters/huggingface/discovery.py`
- [HF model loader](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/adapters/huggingface/loader.py): checkpoint loader · `src/modelsurgeon/adapters/huggingface/loader.py`
- [Calibration data](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/datasets/huggingface.py): HF dataset input · `src/modelsurgeon/datasets/huggingface.py`

### Canonical Analysis

- [Component graph builder](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/graph/builder.py): canonical graph construction · Builds validated stable component and coupling records. · `src/modelsurgeon/graph/builder.py`
- [Graph schema & IDs](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/graph/schema.py): topology validation · Stable identities prevent invalid mutation targets. · `src/modelsurgeon/graph/schema.py`
- [Runtime instrumentation](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/instrumentation/hooks.py): hooks and gradients · `src/modelsurgeon/instrumentation/hooks.py`
- [Feature records & cache](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/features/cache.py): streaming feature cache · `src/modelsurgeon/features/cache.py`
- [Feature extraction](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/features/activation.py): activation features · Combines topology, weights, and bounded runtime signals. · `src/modelsurgeon/features/activation.py`

### Surgery Paths

- [Surgery target resolution](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/surgery/target_resolution.py): mutation contracts · `src/modelsurgeon/surgery/target_resolution.py`
- [Physical mutation plan](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/surgery/physical_plan.py): safe checkpoint plan · `src/modelsurgeon/surgery/physical_plan.py`
- [Transactional output](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/surgery/transaction.py): checkpoint transaction · Protects destinations and supports rollback. · `src/modelsurgeon/surgery/transaction.py`
- [GGUF container I/O](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/adapters/gguf/container.py): native checkpoint I/O · Direct tensor reads and writes avoid unnecessary conversion. · `src/modelsurgeon/adapters/gguf/container.py`
- [GGUF family surgery](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/adapters/gguf/llama_surgery.py): low-level mutation · Performs family-specific selective requantized surgery. · `src/modelsurgeon/adapters/gguf/llama_surgery.py`

### Evaluation & Records

- [Tiered evaluation](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/evaluation/tiered.py): candidate validation · Runs numerical, perplexity, latency, and optional external checks. · `src/modelsurgeon/evaluation/tiered.py`
- [llama.cpp validation](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/evaluation/llama_cpp.py): optional integration · `src/modelsurgeon/evaluation/llama_cpp.py`
- [Experiment records](https://github.com/xdcloudy/modelsurgeon/blob/main/src/modelsurgeon/experiments/schema.py): local experiment schema · Tracks identity, migrations, hardware, and memory plans. · `src/modelsurgeon/experiments/schema.py`

## Connections

- inspect CLI → Configuration: loads
- inspect CLI → HF discovery: inspects
- Adapter contract → HF discovery: selects family
- HF discovery → HF model loader: resolves model
- HF model loader → Component graph builder: loaded model
- Component graph builder → Graph schema & IDs: validates IDs and topology
- Component graph builder → Feature extraction: component targets
- Calibration data → Runtime instrumentation: drives runtime collection
- HF model loader → Runtime instrumentation: instrumented model
- Runtime instrumentation → Feature extraction: activations and gradients
- Feature extraction → Feature records & cache: streams and caches
- Component graph builder → Surgery target resolution: valid targets
- Feature records & cache → Surgery target resolution: selection signals
- Surgery target resolution → Physical mutation plan: resolves mutation
- Physical mutation plan → Transactional output: executes safely
- Physical mutation plan → GGUF family surgery: GGUF plan
- GGUF family surgery → GGUF container I/O: reads and writes tensors
- Transactional output → Tiered evaluation: candidate checkpoint
- GGUF container I/O → Tiered evaluation: mutated GGUF
- Tiered evaluation → llama.cpp validation: optional validation
- Tiered evaluation → Experiment records: records results
- Experiment records → Physical mutation plan: memory-aware planning
