Skip to main content

Sparsity Kit for Structured Sparsity Specification

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sparsekit-0.1.5.tar.gz (50.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sparsekit-0.1.5-py3-none-any.whl (57.1 kB view details)

Uploaded Python 3

File details

Details for the file sparsekit-0.1.5.tar.gz.

File metadata

  • Download URL: sparsekit-0.1.5.tar.gz
  • Upload date:
  • Size: 50.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.11 Linux/6.12.74-gentoo-x86_64

File hashes

Hashes for sparsekit-0.1.5.tar.gz
Algorithm Hash digest
SHA256 356cec43660b327a2def2efdb441b7f72dca7dc639e13ec74d188e6d69b29f18
MD5 eb06999aaeaacdaed65570d3dc8ea29b
BLAKE2b-256 2dd30f3381168e2472251422fb3c4ca9db982d3539c31f7268b7ac6e15392bff

See more details on using hashes here.

File details

Details for the file sparsekit-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: sparsekit-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 57.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.13.11 Linux/6.12.74-gentoo-x86_64

File hashes

Hashes for sparsekit-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 7386e9f91d521c0cca5edfbdd79edd53b3def16e88b7226ed2151eca5dab5973
MD5 fdbee5b1036720daac88a9a3b70bf045
BLAKE2b-256 a204f1c73b5cad03f40fadef8e1a00791e8246c513ddfecbdea267a6e86b7ab6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page