Structured Linear CDE (SLiCE) layers for sequence modelling in PyTorch
Project description
SLiCEs
slices is a small PyTorch package providing Structured Linear CDE (SLiCE) layers.
Mathematical form
Given an input sequence $x_i \in \mathbb{R}^D$ for $i=1,\dots,T$, a SLiCE computes hidden states $y_i \in \mathbb{R}^H$ via
$$ y_i = y_{i-1} + A(X_i)y_{i-1} + B(X_i), $$
where $A(\cdot): \mathbb{R}^D \rightarrow \mathbb{R}^{H \times H}$ and $B(\cdot): \mathbb{R}^D \rightarrow \mathbb{R}^H$ are learned linear maps, the initial state $y_0$ is either a function of $X_0$ or a learnt vector, and the input is augmented with an extra channel:
inc= a constant “increment” channel (all ones)
such that
$$ X_i = [inc_i, x_i] \in \mathbb{R}^{D+1}. $$
Installation
pip install torch-slices
Or install from source:
pip install git+https://github.com/datasig-ac-uk/slices.git
What's included
SLiCE: the core structured linear recurrence layerSLiCEBlock: a residual block wrappingSLiCEwith a post-activation stage (GLUortanh)StackedSLiCE: stacks multipleSLiCEBlocks with an embedding + output projection (supports tokens or continuous inputs)
SLiCE supports both:
- Recurrent execution (step-by-step update)
- Parallel chunked scan execution using
torch.associative_scan
Structured transition matrices
SLiCE supports different $A(X_i)$ structures:
1) Diagonal (elementwise update)
Set:
diagonal_dense=Falseblock_size=1
Then $A(X_i)$ is diagonal, which aligns with the approach used by Mamba (see here for more details).
2) Block-diagonal
Set:
diagonal_dense=Falseblock_size > 1
Then $A(X_i)$ is block-diagonal with blocks of shape (block_size × block_size).
3) Diagonal + dense tail block
Set:
diagonal_dense=Trueblock_size > 1
Then the first (hidden_dim - block_size) dimensions are diagonal, and the final block_size dimensions are updated via a dense (block_size × block_size) matrix.
Quickstart
Use SLiCE directly
import torch
from slices import SLiCE
x = torch.randn(8, 128, 32) # (batch, seq, input_dim)
layer = SLiCE(
input_dim=32,
hidden_dim=64,
block_size=4,
diagonal_dense=False,
bias=True,
use_parallel=True,
chunk_size=256,
)
y = layer(x) # (8, 128, 64)
print(y.shape)
Execution mode is configured via constructor arguments (use_parallel, chunk_size).
Use SLiCEBlock as a residual sequence block
import torch
from slices import SLiCEBlock
x = torch.randn(4, 256, 64)
block = SLiCEBlock(
input_dim=64,
block_size=4,
diagonal_dense=True,
dropout_rate=0.01,
use_glu=True,
)
y = block(x) # (4, 256, 64)
Stack blocks for a full model
Token sequence mode (tokens=True)
Uses an nn.Embedding(data_dim, hidden_dim) front-end.
import torch
from slices import StackedSLiCE
batch, seq_len = 2, 128
vocab_size = 5000
x = torch.randint(0, vocab_size, (batch, seq_len))
model = StackedSLiCE(
num_blocks=4,
data_dim=vocab_size,
hidden_dim=256,
label_dim=vocab_size,
tokens=True,
block_size=4,
diagonal_dense=False,
use_glu=True,
)
logits = model(x) # (batch, seq_len, vocab_size)
Continuous time-series mode (tokens=False)
Uses an nn.Linear(data_dim, hidden_dim) front-end.
import torch
from slices import StackedSLiCE
x = torch.randn(16, 100, 12) # (batch, seq, data_dim)
model = StackedSLiCE(
num_blocks=3,
data_dim=12,
hidden_dim=64,
label_dim=10,
tokens=False,
block_size=4,
diagonal_dense=True,
)
y = model(x) # (16, 100, 10)
Benchmarking
To compare recurrent vs parallel throughput across sequence lengths and hidden dimensions:
python examples/benchmark_parallel_vs_recurrent.py
This script:
- benchmarks all four SLiCE matrix modes (
diagonal,block_diagonal,diagonal_dense,dense) - prints timing/speedup tables
- saves a combined 3D plot to
examples/images/parallel_vs_recurrent_speedup_3d_all_modes.png
For plotting in development, install development dependencies (includes matplotlib):
uv sync --dev
Requirements
- Python ≥ 3.11
- PyTorch ≥ 2.8.0
- NumPy ≥ 2.4.1
License
MIT License. See LICENSE.
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 torch_slices-0.1.0.tar.gz.
File metadata
- Download URL: torch_slices-0.1.0.tar.gz
- Upload date:
- Size: 12.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eba4d38f4e92216bc6dec952d83f58dc4eb49d635adfd603dce893ec74ab2d2d
|
|
| MD5 |
8cc5fa1683785b152d8e2874d8da6b37
|
|
| BLAKE2b-256 |
0fb43de134b6d6532ea689846e120f67f75efa17524d1664a92a8fb4dfcaef28
|
File details
Details for the file torch_slices-0.1.0-py3-none-any.whl.
File metadata
- Download URL: torch_slices-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
570429f38442b170cfb69f9650224f8ef52e2bce6674f4829bc1ef56610a55a2
|
|
| MD5 |
ea09619ff57ab5e24e29efadaefb8407
|
|
| BLAKE2b-256 |
23cfc80065fa716a806f34774f49083a13292fd51f32bf46664783cb6ddc6429
|