Skip to main content

Modern PyTorch library for attention mechanisms in transformer models

Project description

Attzoo 🔍

A modern, extensible PyTorch library for attention mechanisms in transformer models and deep learning architectures. Designed for educational and research purposes with clean, well-documented, and modular code.

📋 Attention Mechanisms

✅ Currently Implemented

Mechanism Class Description Use Case
Vanilla Self-Attention VanillaSelfAttention Standard scaled dot-product self-attention Basic transformer building block
Multi-Head Self-Attention MultiHeadSelfAttention Parallel attention heads with different representations Standard transformer layers
Local Self-Attention LocalSelfAttention Windowed attention with configurable window size Long sequences, O(n×w) complexity
Grouped Self-Attention GroupedSelfAttention Memory-efficient attention with shared K,V heads Efficient transformers (GQA/MQA)
Dilated Self-Attention DilatedSelfAttention Sparse attention with dilation patterns Structured sequences, long-range deps
Linear Self-Attention LinearSelfAttention Linear complexity attention using kernel methods O(n) complexity for very long sequences
Block Self-Attention BlockSelfAttention Block-wise sparse attention patterns Hierarchical attention, document modeling
ALiBi Self-Attention ALiBiSelfAttention Attention with linear bias for positions Length extrapolation capabilities
LSH Self-Attention LSHSelfAttention Hash-based bucketed attention within buckets Approximate global attention for long sequences
Gated Self-Attention (Residual) GatedSelfAttention Highway-style gate mixing attention output with the input Learnable residual strength per token
Combined Attention (Mixture) CombinedAttention Learned gate mixes outputs of two attention modules Softly combine local/global or different patterns

🚀 Quick Start

Installation

# Install from source
git clone https://github.com/yourusername/attzoo.git
cd attzoo
uv sync

# For development
uv sync --all-extras --dev

Install from PyPI

Install the latest release from PyPI:

uv add attzoo

Notes:

  • For a brand-new project on PyPI, use an account-scoped token for the first upload.
  • Bump version in pyproject.toml before every release.

Basic Usage

import torch
from attzoo import MultiHeadSelfAttention

# Initialize model and input
d_model = 128
seq_len = 512
batch_size = 4

attn = MultiHeadSelfAttention(d_model=d_model, num_heads=8)
x = torch.randn(batch_size, seq_len, d_model)

# Forward pass
out, weights = attn(x)
print(out.shape)     # [4, 512, 128]
print(weights.shape) # [4, 8, 512, 512]

📝 Documentation

  • Browse the generated docs locally:

    uv run -m mkdocs serve
    

    The site is served at http://127.0.0.1:8000/ with hot reload enabled.

  • Produce a production build:

    uv run -m mkdocs build
    

    Static assets are written to site/ and published automatically by CI as part of the GitHub workflow.

📊 Performance Comparison

Mechanism Time Complexity Memory Complexity Best Use Case
Vanilla O(n²) O(n²) Short sequences (< 512)
Multi-Head O(n²) O(n²) Standard transformer layers
Local O(n×w) O(n×w) Long sequences with local patterns
Grouped O(n²) O(n²/g) Memory-constrained scenarios
Dilated O(n×d) O(n×d) Structured/periodic patterns
Linear O(n) O(n) Very long sequences (> 4K tokens)
Block O(b×(n/b)²) O(b×(n/b)²) Memory-efficient long sequences
ALiBi O(n²) O(n²) Length extrapolation tasks
LSH Sub-quadratic (~O(n×w×h)) Sub-quadratic Approximate long-range attention

Where n=sequence length, w=window size, g=group ratio, d=dilation connections, b=number of blocks

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

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

attzoo-0.1.0.tar.gz (22.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

attzoo-0.1.0-py3-none-any.whl (35.8 kB view details)

Uploaded Python 3

File details

Details for the file attzoo-0.1.0.tar.gz.

File metadata

  • Download URL: attzoo-0.1.0.tar.gz
  • Upload date:
  • Size: 22.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for attzoo-0.1.0.tar.gz
Algorithm Hash digest
SHA256 48006515651b947e3e9462642b47df76960ca441b7a91d61bc0ca5b69a4b99c2
MD5 6e105bbf05cb9d1dd5b51f6aa9060249
BLAKE2b-256 b566b9b2a45dea79f0dccff59183ae7e733b64ccb595de081b4f1cc099214d10

See more details on using hashes here.

File details

Details for the file attzoo-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: attzoo-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 35.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for attzoo-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f67925955a57a155a79004623ac934f1768fef255842e1b35177daece5cb3e64
MD5 ecf97c1caea0dd56db9c269d0873fcf2
BLAKE2b-256 5d1096e1b3c7607c8f3a9d835bad2c5a17d430c33a656e03921eb047586165fe

See more details on using hashes here.

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