Skip to main content

Stack Attention (wip)

For following a line of research that augments attention with a differentiable stack, beginning with DuSell et al. at ETH Zurich

Install

$ pip install stack-attention-pytorch

Usage

StackTransLayer

import torch
from stack_attention import StackTransLayer

tokens = torch.randn(2, 512, 256)

layer = StackTransLayer(256)

out1, state = layer(tokens)

out2, state = layer(
    out1,
    stack_state = state
)

assert out1.shape == out2.shape == tokens.shape

DataStructureTransLayer

Or declare a custom differentiable data structure

from stack_attention import DataStructureTransLayer, data_structure

counter = data_structure(
    init = lambda: torch.zeros(1),
    actions = dict(
        inc  = lambda s: s + 1.,
        dec  = lambda s: s - 1.,
        noop = lambda s: s,
    ),
    readout = lambda s: s
)

layer = DataStructureTransLayer(dim = 256, data_structure = counter)

tokens = torch.randn(2, 512, 256)
out, state = layer(tokens, recurrent = True)

Citations

@misc{dusell2024stackattentionimprovingability,
    title   = {Stack Attention: Improving the Ability of Transformers to Model Hierarchical Patterns},
    author  = {Brian DuSell and David Chiang},
    year    = {2024},
    eprint  = {2310.01749},
    archivePrefix = {arXiv},
    primaryClass = {cs.CL},
    url     = {https://arxiv.org/abs/2310.01749},
}
@misc{zhang2025stacktranslargelanguagemodel,
    title    = {StackTrans: From Large Language Model to Large Pushdown Automata Model},
    author   = {Kechi Zhang and Ge Li and Jia Li and Huangzhao Zhang and Yihong Dong and Jia Li and Jingjing Xu and Zhi Jin},
    year     = {2025},
    eprint   = {2507.15343},
    archivePrefix = {arXiv},
    primaryClass = {cs.SE},
    url      = {https://arxiv.org/abs/2507.15343},
}
@inproceedings{joulin2015inferring,
    author    = {Armand Joulin and Tom{\'a}{\v{s}} Mikolov},
    title     = {Inferring Algorithmic Patterns with Stack-Augmented Recurrent Nets},
    booktitle = {Advances in Neural Information Processing Systems 28 (NIPS 2015)},
    year      = {2015}
}

Metadata

Release files for stack-attention-pytorch 0.0.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stack-attention-pytorch 0.0.9
File Size Uploaded
stack_attention_pytorch-0.0.9.tar.gz 13.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for stack-attention-pytorch 0.0.9
File Interpreter ABI Platform
stack_attention_pytorch-0.0.9-py3-none-any.whl Python 3 none any Details

Total release size: 27.3 kB

Release files / stack_attention_pytorch-0.0.9.tar.gz

Download URL stack_attention_pytorch-0.0.9.tar.gz
Size 13.0 kB
Tags Source
SHA-256 checksum
How to use checksums
8335857f988fa302d47c91000985b7624eaac43239da664ad23a18db819503e4
BLAKE2b-256 checksum
How to use checksums
19037a0d8f9e2f5a18fe6059416fe77dad07756d8ad865c7e8de4fff5c789a7a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.17

Release files / stack_attention_pytorch-0.0.9-py3-none-any.whl

Download URL stack_attention_pytorch-0.0.9-py3-none-any.whl
Size 14.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f4965b663159cc5ca9f54eb8b317abd045d786eeecfa4fa0076d37ba18931838
BLAKE2b-256 checksum
How to use checksums
fef8ab4f0a9e073e632c445f840ca1341b87192affa63f909ffe97bb4b3f873b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.17

Release history Release notifications | RSS feed

0.0.12

2 release files

0.0.11

2 release files

This release

0.0.9 This release

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page