Skip to main content

Structured sparse layers for building memory-efficient neural networks

Project description

sparse-layers

Structured sparse layers for building memory-efficient neural networks on PyTorch. Drop-in replacements for standard layers using butterfly factorization, SSE attention, and other sparse primitives.

Install

pip install sparse-layers

Usage

import torch
from sparse_layers import ButterflyLinear, ButterflyMLP

# Drop-in replacement for nn.Linear with O(n log n) parameters
layer = ButterflyLinear(in_features=256, out_features=256)
x = torch.randn(32, 256)
y = layer(x)

# MLP with butterfly-factorized linear layers
mlp = ButterflyMLP(in_features=256, hidden_features=512, out_features=256)
y = mlp(x)

Attention

from sparse_layers import ButterflyMultiHeadAttention

# Multi-head attention with butterfly-factorized Q/K/V projections
bf_attn = ButterflyMultiHeadAttention(d_model=256, num_heads=8)

seq = torch.randn(32, 128, 256)  # (batch, seq_len, d_model)
out = bf_attn(seq, seq, seq)

SSE Attention

State Space Exploration modules for efficient sequence modeling with sparse attention patterns.

from sparse_layers.modules import SSEAttention, SSEAttentionConfig

config = SSEAttentionConfig(d_model=256, num_partitions=4)
sse = SSEAttention(config)

x = torch.randn(32, 128, 256)
out = sse(x)

Architecture

Organized following the Flash-Attention pattern:

Ops (sparse_layers.ops)

Primitive operations and utility functions.

Module Description
butterfly Butterfly factor multiply, power-of-2 utilities
SSEMaskingOps Masking utilities for variable-length SSE
SSEVarlenOps Variable-length sequence operations

Modules (sparse_layers.modules)

Composable building blocks — single units of computation.

Module Description
ButterflyLinear Linear layer using butterfly matrix factorization — O(n log n) parameters instead of O(n²)
PaddedButterflyLinear ButterflyLinear with automatic padding for non-power-of-2 dimensions
SSEAttention Sparse attention with state-space-inspired partitioning
SSEAttentionAdaptive SSE with adaptive implementation selection (naive/batched)
SSEMultiPartitionState Manages partition states across sequence chunks
SSEPartitionSelector Selects active partitions per query position
SSESparseSoftmax Sparse softmax over selected partitions
LinearAttention Linear attention baseline (O(n) complexity)

Models (sparse_layers.models)

Composed architectures built from modules.

Module Description
ButterflyMLP Two-layer MLP with butterfly-factorized linear layers
ButterflyMultiHeadAttention Multi-head attention with butterfly-factorized Q/K/V projections
SSEMultiHeadAttention Multi-head variant of SSE attention

The package also includes baseline implementations (SimpleMLP, CustomMLP, CustomLinear, MultiHeadAttention) used internally for validation and testing.

License

MIT

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

sparse_layers-0.2.2.tar.gz (76.2 kB view details)

Uploaded Source

Built Distribution

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

sparse_layers-0.2.2-py3-none-any.whl (28.4 kB view details)

Uploaded Python 3

File details

Details for the file sparse_layers-0.2.2.tar.gz.

File metadata

  • Download URL: sparse_layers-0.2.2.tar.gz
  • Upload date:
  • Size: 76.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sparse_layers-0.2.2.tar.gz
Algorithm Hash digest
SHA256 bfc48a63f5f19a206c1b4e1b655f88599ae31f8968c06ade704e2fe5e26d927d
MD5 df54dcf292e50fd8815b0f2aafcc6ba8
BLAKE2b-256 19fd71d1721f66552a65d5c0fa6c7fe52385f251c3e3169c53d54f8c2259af92

See more details on using hashes here.

Provenance

The following attestation bundles were made for sparse_layers-0.2.2.tar.gz:

Publisher: publish.yml on PhilHem/sparse-layers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sparse_layers-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: sparse_layers-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 28.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sparse_layers-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3c7b9cad093ed10c8512a856d3d1a67aa47f7626a5c06e6e41a0d7a2186f6350
MD5 92461b26047d41d56c3bb606a3e99c24
BLAKE2b-256 6704206bccca27ff8860125638b19df6060e9490c1926d0bb36b81aee589d088

See more details on using hashes here.

Provenance

The following attestation bundles were made for sparse_layers-0.2.2-py3-none-any.whl:

Publisher: publish.yml on PhilHem/sparse-layers

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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