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.4.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.4-py3-none-any.whl (28.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sparse_layers-0.2.4.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.4.tar.gz
Algorithm Hash digest
SHA256 e9bd0f25826246bca02a7a17ce88e1211054662b73325f8e2783bb2497980dd4
MD5 eec06a88e285ea674182a48d0eea6250
BLAKE2b-256 4d310a158816a1acec0f2476205726dc5d8570a29153c4d3f5f26dd98581df17

See more details on using hashes here.

Provenance

The following attestation bundles were made for sparse_layers-0.2.4.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.4-py3-none-any.whl.

File metadata

  • Download URL: sparse_layers-0.2.4-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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fde96c8190cb6125f4cceca603452f24ea941e8cb766abb42a36e5b2f8035b65
MD5 512e90effd4e3f3d436fdeab97ff51e2
BLAKE2b-256 6f4179478a72c8748583e7e8a991c4e0387356e0f65e1df1db65137bb4fdd8f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for sparse_layers-0.2.4-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