How xdcloudy/modelsurgeon works
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: builder.py Component graph builder · physical_plan.py Physical mutation plan · tiered.py Tiered evaluation · loader.py HF model loader
Components 19
CLI & Configuration
- inspect CLI · Active local inspection entry point.
src/modelsurgeon/cli/app.py - Configuration
src/modelsurgeon/config.py
Model Intake
- Adapter contract
src/modelsurgeon/adapters/base.py - HF discovery
src/modelsurgeon/adapters/huggingface/discovery.py - HF model loader
src/modelsurgeon/adapters/huggingface/loader.py - Calibration data
src/modelsurgeon/datasets/huggingface.py
Canonical Analysis
- Component graph builder · Builds validated stable component and coupling records.
src/modelsurgeon/graph/builder.py - Graph schema & IDs · Stable identities prevent invalid mutation targets.
src/modelsurgeon/graph/schema.py - Runtime instrumentation
src/modelsurgeon/instrumentation/hooks.py - Feature records & cache
src/modelsurgeon/features/cache.py - Feature extraction · Combines topology, weights, and bounded runtime signals.
src/modelsurgeon/features/activation.py
Surgery Paths
- Surgery target resolution
src/modelsurgeon/surgery/target_resolution.py - Physical mutation plan
src/modelsurgeon/surgery/physical_plan.py - Transactional output · Protects destinations and supports rollback.
src/modelsurgeon/surgery/transaction.py - GGUF container I/O · Direct tensor reads and writes avoid unnecessary conversion.
src/modelsurgeon/adapters/gguf/container.py - GGUF family surgery · Performs family-specific selective requantized surgery.
src/modelsurgeon/adapters/gguf/llama_surgery.py
Evaluation & Records
- Tiered evaluation · Runs numerical, perplexity, latency, and optional external checks.
src/modelsurgeon/evaluation/tiered.py - llama.cpp validation
src/modelsurgeon/evaluation/llama_cpp.py - Experiment records · Tracks identity, migrations, hardware, and memory plans.
src/modelsurgeon/experiments/schema.py
Connections 22
- 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