Skip to main content

A linear-attention transformer implementation with KV caching.

Project description

Trim Transformer

trim-transformer is a lightweight PyPI package that replicates the familiar interface of torch.nn.TransformerEncoder, but with an attention function of the form Attn(Q,K,V) = QK^TV, which we call multi-linear attention. This implementation has time complexity O(nd^2), where n is the sequence length and d is the model dimension. Since the time complexity is linear in the sequence length, this implementation is well suited for high sequence length tasks. Attention in this form has shown success in operator learning tasks, see Choose a Transformer: Fourier or Galerkin.

This implementation is particularly relevent for training physics models where high sequence length can come from large grid sizes, long time periods, or both.

Additionally, this implementation supports key-value caching for inference that is also linear in the number of tokens generated. Finally, this implementation supports custom weight initialization functions for the query, key, and value projection matrices, and custom normalization layers for the query, key, and value activations.

PyPI License


Installation

The package is published on PyPI and only depends on PyTorch:

pip install trim-transformer

Alternatively, install the latest commit from GitHub:

pip install git+https://github.com/eg-trim/trim-transformer.git

Benchmarks

Below are some benchmark plots demonstrating model performance and resource usage on the Navier-Stokes dataset from https://arxiv.org/abs/2010.08895:

The Trim Transformer achives more than 90% reduction in memory usage compared to a standard Pytorch transformer using softmax attention and 3.5x faster time per epoch while maintaining very similar validation loss. As grid size and sequence length increase these gains become even more drastic. Memory Usage

Time per Epoch Training Loss


Quickstart

import torch
from trim_transformer.transformer_layers import TrimTransformerEncoderLayer, TrimTransformerEncoder

layer = TrimTransformerEncoderLayer(d_model=EMBED_DIM, nhead=NUM_HEADS, batch_first=True)
model = TrimTransformerEncoder(layer, num_layers=NUM_LAYERS)

x = torch.randn(8, 2048, 512)  # (batch, seq_len, dim)

# Standard forward pass (causal mask optional)
out = model(x, is_causal=True)  # (batch, seq_len, dim)

See tutorial_notebook.ipynb for demonstrations of each feature. And see trim_vs_softmax.ipynb for an example of a complete pipeline and a comparison to a PyTorch baseline.

Masking

A significant departure from PyTorch syntax is the structure of the mask. Multi-linear attention with arbitrary boolean masks cannot be computed in time linear in the sequence length. Instead, this package supports masks such that the i-th query attends to all keys up to index m_i. Such masks can be specified by an integer array of length n, with values in [0, n-1], where n is the sequence length.

For example, a causal mask of length n is given by torch.arange(n). To illustrate further, consider the one-dimensional mask [2, 0, 1]. This corresponds to the following two-dimensional mask, where, following the PyTorch convention, True indicates that an element is masked.

Key 0 Key 1 Key 2
Query 0 False False False
Query 1 False True True
Query 2 False False True

Key-value caching

Inference with key-value caching can be performed with a simple loop.

def generate(model, initial_tokens, num_new_tokens=5):
    """Autoregressive generation with caching"""
    model.eval()  # optional
    generated_sequence = []
    new_token = initial_tokens.clone()

    for _ in range(num_new_tokens):
        # Must set use_kv_cache=True and update_kv_cache=True to use key-value caching.
        # use_kv_cache=True means that the model will key-value cache that is already stored.
        # update_kv_cache=True means that the model will update the key-value cache with the
        # keys and values from the new token.
        output = model(new_token, is_causal=True, use_kv_cache=True, update_kv_cache=True)
        generated_sequence.append(output)

    # Clear the key-value cache after generation. If you don't clear the cache, then if
    # use_kv_cache=True in the future, the model will continue to use the key-value cache
    # on future forward passes.
    model.clear_kv_cache()
    return generated_sequence

License

trim-transformer is released under the MIT License. See LICENSE for the full text.

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

trim_transformer-0.1.3.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

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

trim_transformer-0.1.3-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file trim_transformer-0.1.3.tar.gz.

File metadata

  • Download URL: trim_transformer-0.1.3.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for trim_transformer-0.1.3.tar.gz
Algorithm Hash digest
SHA256 e3c7b2018ae519087d68cd23f2ce4fcd07f3314fd21c15ba5271da7066af23ca
MD5 91ec3870a939b59bc5fbcdb10aadef53
BLAKE2b-256 2a97e7e5d49d4576a77fa930bdcea30a6920ea358f7dd884ce540372ba186aab

See more details on using hashes here.

File details

Details for the file trim_transformer-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for trim_transformer-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a50b07fc7614478ddc6fcbf8ca7a87ed51018a34aa52d3b07ca8984da86a809e
MD5 22edeb767116fd190e0c1194fb52e572
BLAKE2b-256 45183f42e21853d77cacffc2d2f47cbb77d447884dcc55d3ffa324daea1be58e

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