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 workflow methods 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 support
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, regression tests, and public API checks.

License

MIT.

Metadata

Release files for torch-kirigami 0.2.2

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.2.2
File Size Uploaded Provenance Actions
torch_kirigami-0.2.2.tar.gz 613.2 kB With provenance Details for torch_kirigami-0.2.2.tar.gz

Built distribution (wheel)

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

Total release size: 834.7 kB

Release files / torch_kirigami-0.2.2.tar.gz

Download URL torch_kirigami-0.2.2.tar.gz
Size 613.2 kB
Tags Source
SHA-256 checksum
How to use checksums
6376a064516104ae3b8f6dda73f1e4858d6b51da062f72180cadac2d4e2c536b
BLAKE2b-256 checksum
How to use checksums
cc97f4eaa69b7c3eccaaed2d6fad9617a33f92f16ef4ff89bc615b410d7aabec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 30, 2026.

Transparency log

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

Download URL torch_kirigami-0.2.2-py3-none-any.whl
Size 221.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
62a8e1bd036081a0aa25fae96e33e21250c54d1d735a2d1d0092901b1cd47342
BLAKE2b-256 checksum
How to use checksums
16450181cf79f5d9d41da7981dbf509c19282c04d9631288129e7649e86a27a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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 30, 2026.

Transparency log

Security for torch-kirigami 0.2.2

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for torch_kirigami-0.2.2.tar.gz

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 30, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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