Skip to main content

torch-kirigami

CI PyPI License: MIT

Structured pruning and dependency analysis for PyTorch models.

torch-kirigami helps you build smaller neural networks by physically removing channels, features, and attention heads together with their dependent parameters. It uses PyTorch FX to analyze model structure, then validates and applies pruning plans to the original nn.Module.

Use it for automatic model pruning, custom pruning algorithms, or inspecting how a structural change propagates through a model.

Quick start · Documentation · ImageNet examples · Development

Features

  • Dependency-aware pruning: propagate removals through connected operators and coordinate the required tensor and module-attribute changes.
  • Automatic and manual selection: use magnitude or Taylor scores, supply explicit removals, or implement custom metrics and strategies. Set channel limits or a final whole-model parameter budget.
  • Sparse training and iterative pruning: compose regularizers, channel gates, parameter operations, and cumulative budgets with your own training loop.
  • Compact-model checkpoints: save the pruned structure and weights, then restore them using the original model definition.
  • Custom operators: extend analysis and pruning through explicit operator rules, including fused attention blocks.
  • Model measurement: inspect parameter counts, supported MACs, and measured inference latency, including compiled execution.

Installation

Requires Python 3.10+ and PyTorch 2.6+. PyTorch is the only runtime dependency.

Install from PyPI with uv:

uv venv .venv
source .venv/bin/activate
uv pip install --torch-backend=auto torch-kirigami

For an editable installation and development tools, see the development guide. ImageNet examples have additional dependencies listed in the workflow guide.

Quick start

Prune a small network to a final parameter budget. This example uses the current accelerator; set device = torch.device("cpu") to run it on a CPU.

import torch
from torch import nn

from torch_kirigami import DependencyGraph
from torch_kirigami.pruning import Greedy, GroupMagnitude, ParameterBudget, Pruner

device = torch.accelerator.current_accelerator()
model = nn.Sequential(nn.Linear(4, 8), nn.ReLU(), nn.Linear(8, 3)).to(device).eval()
x = torch.randn(2, 4, device=device)

graph = DependencyGraph.build(model, args=(x,))
pruner = Pruner(model, graph=graph)
candidates = pruner.discover_candidates()

model, result = pruner.prune(
    candidates,
    budget=ParameterBudget(max_params=51),
    strategy=Greedy(GroupMagnitude(p=2)),
)

assert model[0].out_features == model[2].in_features == 6
assert model(x).shape == (2, 3)
print(result.plan.explain())

The hidden width shrinks from 8 to 6, reducing the model from 67 to 51 parameters. The first layer's weight rows and bias entries are removed together with the second layer's matching input columns. Model input and output dimensions remain unchanged.

prune() selects and applies a plan in place. You can also inspect a plan before applying it, or specify exactly which positions to remove. The getting-started tutorial covers manual pruning, training after pruning, and checkpoint restoration.

For another pruning round, rebuild the dependency graph. Create a new optimizer before continuing training, because physical pruning replaces parameters.

Examples

Start with the small API examples:

Example What it demonstrates
Dependency analysis Inspect the effects of a removal without modifying the model
Two pruning rounds Automatic selection, training, and graph rebuilding
Custom operator Declare structural relations for a custom module
Fused attention Prune attention groups and restore a compact checkpoint

The pretrained ImageNet workflows provide complete pruning and fine-tuning examples for ResNet, ViT, and ConvNeXt models. They cover magnitude and Taylor pruning, FPGM, sparse training, iterative pruning, VBP, Isomorphic Pruning, OSSCAR, and ViT attention-head and FFN pruning. Each workflow reports validation accuracy, parameter counts, MACs, and latency.

Model choices and pruning scopes vary by method. See method selection and adaptations for the implemented algorithms and their research references. These examples do not claim to reproduce published benchmark results.

Documentation

Topic Guide
First pruning operation Getting started
Supported models and operators Model support, operator coverage
Budgets, scoring, and custom pruning policies Pruning
Sparse training components Sparse training
Saving and restoring compact models Persistence
Parameters, MACs, and latency Measurement
Development, testing, and PyPI releases Development guide

The documentation index also links to the architecture and dependency-graph references.

Model support

Support depends on the model's operator forms and the dimensions being pruned. The graph is captured with PyTorch FX; tensor-dependent Python control flow and unsupported structural transformations are reported explicitly. Consult the model support contract before adapting a new architecture.

Pruning changes the model's computation. Evaluate the compact model on your task and fine-tune as needed; the library leaves the loss, optimizer, and training loop under your control.

Contributing

Bug reports, operator extensions, and pruning workflows are welcome. Include a minimal model and representative inputs when reporting an issue. Follow the development guide for environment setup and the testing guide for regression tests and public API checks.

License

MIT.

Metadata

Release files for torch-kirigami 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for torch-kirigami 0.1.1
File Size Uploaded
torch_kirigami-0.1.1.tar.gz 538.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for torch-kirigami 0.1.1
File Interpreter ABI Platform
torch_kirigami-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 716.8 kB

Release files / torch_kirigami-0.1.1.tar.gz

Download URL torch_kirigami-0.1.1.tar.gz
Size 538.4 kB
Tags Source
SHA-256 checksum
How to use checksums
9e778f6a48b891508d1f0b980f1fddf8c537b2a9d73a154689344b63a575395e
BLAKE2b-256 checksum
How to use checksums
dc3d8bc95f0f898b05ac1b471f4ac7462ecceb2c6ba2aa99557dda92f11e4942
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 3, 2026.

Transparency log

Release files / torch_kirigami-0.1.1-py3-none-any.whl

Download URL torch_kirigami-0.1.1-py3-none-any.whl
Size 178.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f8928bd5a0369ab01ce84f34f2986a0003dd6157e4d53da252024db0aefa4cec
BLAKE2b-256 checksum
How to use checksums
9de69959a9ad4ab0184722e91be4f6c5181365aabb84b28e73736500d3b2f20c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 3, 2026.

Transparency log

Release history Release notifications | RSS feed

0.1.3

2 release files

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page