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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e9bd0f25826246bca02a7a17ce88e1211054662b73325f8e2783bb2497980dd4
|
|
| MD5 |
eec06a88e285ea674182a48d0eea6250
|
|
| BLAKE2b-256 |
4d310a158816a1acec0f2476205726dc5d8570a29153c4d3f5f26dd98581df17
|
Provenance
The following attestation bundles were made for sparse_layers-0.2.4.tar.gz:
Publisher:
publish.yml on PhilHem/sparse-layers
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sparse_layers-0.2.4.tar.gz -
Subject digest:
e9bd0f25826246bca02a7a17ce88e1211054662b73325f8e2783bb2497980dd4 - Sigstore transparency entry: 1167513958
- Sigstore integration time:
-
Permalink:
PhilHem/sparse-layers@256b07fbac74bc6579563aa457bb3703c1d1117f -
Branch / Tag:
refs/tags/v0.2.4 - Owner: https://github.com/PhilHem
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@256b07fbac74bc6579563aa457bb3703c1d1117f -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fde96c8190cb6125f4cceca603452f24ea941e8cb766abb42a36e5b2f8035b65
|
|
| MD5 |
512e90effd4e3f3d436fdeab97ff51e2
|
|
| BLAKE2b-256 |
6f4179478a72c8748583e7e8a991c4e0387356e0f65e1df1db65137bb4fdd8f8
|
Provenance
The following attestation bundles were made for sparse_layers-0.2.4-py3-none-any.whl:
Publisher:
publish.yml on PhilHem/sparse-layers
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
sparse_layers-0.2.4-py3-none-any.whl -
Subject digest:
fde96c8190cb6125f4cceca603452f24ea941e8cb766abb42a36e5b2f8035b65 - Sigstore transparency entry: 1167514258
- Sigstore integration time:
-
Permalink:
PhilHem/sparse-layers@256b07fbac74bc6579563aa457bb3703c1d1117f -
Branch / Tag:
refs/tags/v0.2.4 - Owner: https://github.com/PhilHem
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@256b07fbac74bc6579563aa457bb3703c1d1117f -
Trigger Event:
push
-
Statement type: