Skip to main content

SparseKit

SparseKit is the reference implementation of S3 (Structured Sparsity Specification), a unified framework for expressing and pruning structured sparse neural networks.

Library Overview

sparsekit/
├── view.py          # View         — zero-copy strided parameter wrapper (torch.as_strided)
├── block.py         # BlockSpec / BlockCoupling   — atomic pruning unit (block)
├── scope.py         # ScopeSpec / ScopeCoupling   — decision scope
├── builder.py       # SparsityBuilder fluent API
├── linalg.py        # Utility solvers (proximal, thresholds)
├── tensor_ops.py    # kth_largest, layout helpers
├── kernels.py       # Triton kernels (auto-dispatched for large K/k)
├── viz.py           # draw_layout() — visualize sparsity patterns
├── pruners/
│   ├── obs.py       # StructuredOBS — S-OBS with per-row Schur updates
│   ├── sparsegpt.py # SparseGPT column-sequential pruning
│   └── obd.py       # OBD and magnitude pruning
└── training/
    ├── data.py      # Calibration data loaders (C4)
    └── hooks.py     # ModuleInputCatcher, transfer_to_device

Terminology:

  • Block — atomic pruning unit: the smallest set of weights pruned or kept together.
  • Scope — decision scope: a set of blocks that compete; the pruning budget is enforced per scope.

Quick Example

import torch
from torch.nn import Parameter
from sparsekit import BlockSpec, ScopeSpec, StructuredOBS

M, K = 2560, 9728
W = Parameter(torch.randn(M, K, device="cuda"))
X = torch.randn(1024, K, device="cuda")          # calibration inputs

# Express 2:4 sparsity: scalar blocks, scopes of 4
block = BlockSpec(W, shape=(1, 1))
scope = ScopeSpec(block, shape=(1, 4))

# Prune with Structured OBS
hessian = (X.T @ X) / X.shape[0]
obs = StructuredOBS(scope, hessian)
obs.prune_true_obs(nnz=2)                     # keep 2 of 4, in-place

Any of the four experimental patterns replaces the two BlockSpec/ScopeSpec lines above; the StructuredOBS call is identical.

Sparsity Patterns

Pattern Block shape Scope shape Description
2:4 (1, 1) (1, 4) Keep 2 of 4 contiguous columns
4:8 (1, 2) (1, 4) Keep 2 of 4 column-pairs
Coupled 2:4 (1, 1, 1, 2) (1, 1, 4, 1) Pair columns 8 apart via View
16-col block (1, 1, 16) (1, 2, 1) 16-col blocks, 8-row coupling

Reproducing Paper Results

Table 1 (single-layer, 4 patterns):

python scripts/structured_obs.py --pattern 24         --ng 64   # 2:4
python scripts/structured_obs.py --pattern 48         --ng 64   # 4:8
python scripts/structured_obs.py --pattern coupled24  --ng 64   # Coupled 2:4
python scripts/structured_obs.py --pattern block16    --ng 64   # 16-col block, 8-row coupled

Table 2 + Figures (end-to-end LLM pruning):

# SparseGPT baseline
python scripts/prune_gpt.py --method sparsegpt_24 --model Qwen/Qwen3-1.7B

# S-OBS (True OBS)
python scripts/prune_gpt.py --method true_obs_24 --model Qwen/Qwen3-1.7B --ng 64

Plots (from saved CSVs):

python scripts/plot_results.py experiments/results --model Qwen3-1.7B

Requirements

  • Python >= 3.10
  • PyTorch >= 2.4
  • Triton >= 3.0
  • CUDA GPU

Additional for LLM experiments (prune_gpt.py):

  • transformers, datasets, lm_eval, pandas

Metadata

Release files for sparsekit 0.1.5

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

Source distribution (sdist)

Source distribution for sparsekit 0.1.5
File Size Uploaded Provenance Actions
sparsekit-0.1.5.tar.gz 50.3 kB Without provenance Details for sparsekit-0.1.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sparsekit 0.1.5
File Interpreter ABI Platform Actions
sparsekit-0.1.5-py3-none-any.whl Python 3 none any Details for sparsekit-0.1.5-py3-none-any.whl

Total release size: 107.3 kB

Release files / sparsekit-0.1.5.tar.gz

Download URL sparsekit-0.1.5.tar.gz
Size 50.3 kB
Tags Source
SHA-256 checksum
How to use checksums
356cec43660b327a2def2efdb441b7f72dca7dc639e13ec74d188e6d69b29f18
BLAKE2b-256 checksum
How to use checksums
2dd30f3381168e2472251422fb3c4ca9db982d3539c31f7268b7ac6e15392bff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.2 CPython/3.13.11 Linux/6.12.74-gentoo-x86_64

Release files / sparsekit-0.1.5-py3-none-any.whl

Download URL sparsekit-0.1.5-py3-none-any.whl
Size 57.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7386e9f91d521c0cca5edfbdd79edd53b3def16e88b7226ed2151eca5dab5973
BLAKE2b-256 checksum
How to use checksums
a204f1c73b5cad03f40fadef8e1a00791e8246c513ddfecbdea267a6e86b7ab6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.2 CPython/3.13.11 Linux/6.12.74-gentoo-x86_64

Security for sparsekit 0.1.5

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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