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.

Documentation · ImageNet examples

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: use Torch Prisma for parameter counts, MACs, FLOPs, and inference latency, including compiled execution.

Installation

Requires Python 3.10+ and PyTorch 2.6+. Runtime dependencies are PyTorch and Torch Prisma.

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

Read the documentation for guides, API references, and development instructions.

Model support

Support depends on the model's operator forms and the dimensions being pruned. PyTorch FX traces forward. The library then executes the captured graph on example inputs to collect tensor metadata. A Python condition on a symbolic tensor value can stop tracing, while a Python identity or type check can silently select a different branch from eager execution. Within a captured graph, operations without a structural rule block requests that reach them. 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.6

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.6
File Size Uploaded Provenance Actions
torch_kirigami-0.2.6.tar.gz 678.5 kB With provenance Details for torch_kirigami-0.2.6.tar.gz

Built distribution (wheel)

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

Total release size: 935.7 kB

Release files / torch_kirigami-0.2.6.tar.gz

Download URL torch_kirigami-0.2.6.tar.gz
Size 678.5 kB
Tags Source
SHA-256 checksum
How to use checksums
06a84c929165a5344d3c5c816b21f4d7ba9bda5a83e93ea542309b0e58fbb900
BLAKE2b-256 checksum
How to use checksums
39fbf21f03c875d0147053416c787630e8221a3c5698bb5a76c98d3a75f49cb8
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 Nov 19, 2026.

Transparency log

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

Download URL torch_kirigami-0.2.6-py3-none-any.whl
Size 257.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7ccfa09967f9b2f9ad43fc39f6b49d595bcf8d779c5bd39e8a0033b7fd24d3de
BLAKE2b-256 checksum
How to use checksums
904a5d67fb21755758ac3dd1d41fc661e5e5ba61b74aacbfab10b3a381822ece
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 Nov 19, 2026.

Transparency log

Security for torch-kirigami 0.2.6

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.6.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 Nov 19, 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