PyTorch implementation of Entangled State Attention (ESA) for causal sequence modeling.
Project description
Entangled State Attention
Entangled State Attention (ESA) is a PyTorch library for causal sequence modeling using a scan-friendly recurrent state update instead of a full token-token causal attention matrix.
This repository is the first clean library release of the ESA mechanism described in:
Entangled State Attention: Associative State Scanning for Causal Sequence Modeling
DOI: https://doi.org/10.5281/zenodo.20973958
Status: early research library, v0.1.0.
Naming
Install package:
pip install entangled-state-attention
Python import:
from esa import ESA, EntangledStateAttention, ESALayer
Recommended short usage:
from esa import ESA, ESALayer
ESAis a short alias forEntangledStateAttention.EntangledStateAttentionis the core ESA layer.ESALayeris a stackable residual layer using ESA.
Core idea
For an input sequence x, ESA computes token-local gates and values, then builds a causal state sequence.
Direct ESA:
A_t = \sigma(x_t W_A)
V_t = x_t W_V
E_t = A_t \odot E_{t-1} + V_t
Readout:
q_t = x_t W_Q
y_t = W_O(q_t \odot \mathrm{Norm}(E_t))
Controlled ESA:
g_t = \sigma(x_t W_g)
E_t = g_t \odot E_{t-1} + (1 - g_t) \odot V_t
The controlled form couples old-state retention with new-content writing: when the keep gate is high, the write amount is low.
Direct vs controlled mode
ESA supports two recurrence modes:
ESA(dim=256, gate_mode="controlled")
ESA(dim=256, gate_mode="direct")
Use gate_mode="controlled" for most experiments and downstream models. It is the safer default because the same gate controls both how much old state is retained and how much new content is written. This creates a clear tradeoff:
high keep gate -> more old state, less new writing
low keep gate -> less old state, more new writing
Use gate_mode="direct" when you want to reproduce or study the original direct ESA recurrence. In direct mode, the old-state gate controls retention, but the new value is added directly. This is useful for ablations, research comparison, and testing the original formulation, but it is less constrained than controlled mode.
Recommended default:
from esa import ESA
layer = ESA(dim=256, gate_mode="controlled")
Installation
From source:
pip install -e .
With development tools:
pip install -e ".[dev]"
After PyPI release:
pip install entangled-state-attention
Minimal usage
import torch
from esa import ESA
x = torch.randn(2, 128, 256) # batch, sequence, hidden dimension
esa = ESA(
dim=256,
state_dim=256,
gate_mode="controlled",
)
y = esa(x)
print(y.shape) # torch.Size([2, 128, 256])
The full class name is also available:
from esa import EntangledStateAttention
esa = EntangledStateAttention(dim=256, state_dim=256)
ESA layer
ESALayer is the stackable layer form. It contains normalization, Entangled State Attention, residual connections, and a feed-forward network.
import torch
from esa import ESALayer
x = torch.randn(2, 128, 256)
layer = ESALayer(dim=256, state_dim=256)
y = layer(x)
You can stack ESA layers:
import torch.nn as nn
from esa import ESALayer
model = nn.Sequential(
ESALayer(dim=256, state_dim=256),
ESALayer(dim=256, state_dim=256),
ESALayer(dim=256, state_dim=256),
)
Tiny language model example
from esa import TinyESALanguageModel
model = TinyESALanguageModel(
vocab_size=5000,
block_size=256,
dim=256,
n_layer=4,
)
This tiny model is included for smoke tests and simple experiments. It is not meant to be the official ESA-SLM release.
Future ESA-based language models should install this library with pip and live in separate model repositories.
Run examples
python examples/minimal_esa.py
python examples/tiny_lm_demo.py
Run tests
python -m pytest
Included tests:
- ESA output shape
- ESA layer shape
- tiny LM forward/loss
- causal behavior
- direct scan equivalence
- controlled scan equivalence
- public alias imports
Project scope
Version 0.1.0 intentionally includes only the standalone ESA mechanism:
ESAEntangledStateAttentionControlledEntangledStateAttentionESALayerTinyESALanguageModel- examples
- tests
It does not include SOUP, Entangled Router, ObserverTransformer, or other architecture projects. Those should remain separate projects.
Citation
@misc{hussain2026entangledstateattention,
title = {Entangled State Attention: Associative State Scanning for Causal Sequence Modeling},
author = {Hussain, Zameer},
year = {2026},
publisher = {Zenodo},
doi = {10.5281/zenodo.20973958},
url = {https://doi.org/10.5281/zenodo.20973958}
}
License
Apache License 2.0.
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 entangled_state_attention-0.1.0.tar.gz.
File metadata
- Download URL: entangled_state_attention-0.1.0.tar.gz
- Upload date:
- Size: 12.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7061da25f49ae3518ac30f1679031ca338124625f3758d9e7002f048f099fe31
|
|
| MD5 |
0a5e7b949119630035e76054883132a0
|
|
| BLAKE2b-256 |
16e368feefd0f2a8d262164dedf1c76f1ba80b4ae7931dbe37dc45554dd9fc4a
|
File details
Details for the file entangled_state_attention-0.1.0-py3-none-any.whl.
File metadata
- Download URL: entangled_state_attention-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
63af3ea98036099ed7a6b908d57e5796313120ce51724a8377aed10a2e98a485
|
|
| MD5 |
e1c389fa14ad5dee0fb6fb0d9a9e08b7
|
|
| BLAKE2b-256 |
1e9cb2d34fe2b521c06a96a45180ef67977c11a88136707d30e2f01311d85068
|