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. The attention kernel has the form Attn(Q,K,V) = QK^TV. This implementation has complexity O(nd^2), where n is the sequence length and d is the model dimension.

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/emanuel-nuclearsoftware/trim-transformer.git

Quickstart

import torch
from trim_transformer.transformer_layers import CumulativeTransformerKV

layer = CumulativeTransformerEncoderLayerKV(d_model=EMBED_DIM, nhead=NUM_HEADS, batch_first=True)
model = CumulativeTransformerEncoderKV(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 demo_vs_baseline 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). And the one dimensional mask [2, 0, 1] corresponds to the two dimensional mask [[False, False, False], [False, True, True], [False, False, True]], following the PyTorch convention that True means to set that element of the attention matrix to 0.

Key-value caching

Inference with key value 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.0.tar.gz (9.8 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.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for trim_transformer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dafe19fb115b85f4d44d1b0c5274101f4119979ca1fa33c0e0115081a3f52b95
MD5 3b9b55ab3737abc5626e7169c4d7c291
BLAKE2b-256 6ff387b2e5f877517d8d85dbbc5259bb372086203318c9a0e779109805ba9336

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for trim_transformer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 721b3749450e63ae4cf113e1231a8c6bd564e47658a77894ee30e27900513545
MD5 94829c1ed463f7e74303ff1959e162b3
BLAKE2b-256 530fd4014d84665b3db5c88a017c927b41b852c7be54b451b728df2598cbbcae

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