Skip to main content

Multihead Temporal Latent Attention implementation

Project description

MTLA: Multi-head Temporal Latent Attention

MTLA

Multi-head Temporal Latent Attention
Keqi Deng, Philip C. Woodland
📄 Paper on arXiv
🎉 Accepted at NeurIPS 2025!

About

MTLA is a novel attention mechanism building on DeepSeek MLA, with a key innovation: temporal compression of the key-value cache. This enables more efficient self-attention and significantly reduces memory footprint during inference, making it particularly valuable for decoder-only architectures such as LLMs. Built on PyTorch, this project also serves as an open-source, decoder-only toolkit for end-to-end speech and language processing, covering tasks such as text summarisation, speech translation, speech recognition, spoken language understanding, and so on, with fully featured setup recipes.

Key Features

Supported Attention Mechanisms

Complete Setup Recipes

  • Tasks: speech translation (MuST-C), speech recognition (AMI), spoken language understanding (SLURP), and text summarisation (XSum)
  • Data Processing: Fairseq-style Fbank feature extraction and compression into zip file, and ESPnet2-style speech data processing with raw audio saved in flac or ark format
  • Feature Extraction: Fbank online/offline extraction, and self-supervised learning representations as features, using upstream models in S3PRL
  • Notebook Demo: Open In Colab

Evaluation

  • Parallel Inference: Fairseq-style parallel beam search over batches containing multiple data samples
  • Quality Evaluation: BLEU, WER, classification accuracy, and ROUGE (ROUGE-1, ROUGE-2, and ROUGE-L)
  • Efficiency Evaluation: inference time spent, and GPU memory (including activation memory and the storage of key-value cache) consumed on inference

Installation and Usage

  • If you only need the Python MTLA module, simply clone this repository or pip install:

    pip install mtla
    

    Then refer to the following example:

    import torch
    from MTLA import MultiheadTemporalLatentAttention
    
    batch, length, dim = 2, 64, 512
    x = torch.randn(batch, length, dim)
    pos = torch.arange(0, length).float().view(1, -1) # Position information
    model = MultiheadTemporalLatentAttention(
        embed_dim=dim, # Model dimension
        num_heads=8,  # Attention heads of queries
    )
    y = model(query=x, key=x, value=x, position=pos)
    assert y.shape == x.shape
    

    A notebook demo of training with MTLA and performing beam search inference refers to Open In Colab

  • Optional: FlashAttention backend for MTLA inference. We provide an optional FlashAttention backend to accelerate MTLA inference. This feature is disabled by default. To enable it, please install our customised FlashAttention fork:

    git clone https://github.com/D-Keqi/flash-attention.git
    cd flash-attention
    python setup.py install
    
    • FlashAttention requires a CUDA-capable GPU with PyTorch 2.7.0 and CUDA 12.6 (tested working versions).
    • Only fp16 (torch.float16) or bf16 (torch.bfloat16) dtypes are supported.
    • If FlashAttention is not installed, MTLA will automatically fall back to the standard PyTorch implementation.

    Refer to the example below to use our extended FlashAttention for MTLA inference:

    import torch
    from MTLA import MultiheadTemporalLatentAttention
    
    batch, length, dim = 2, 16, 512
    dtype = torch.float16  # or torch.bfloat16
    device = "cuda"
    
    x = torch.randn(batch, length, dim, device=device, dtype=dtype)
    pos = torch.arange(0, length, device=device, dtype=torch.float32).view(1, -1)
    
    model = MultiheadTemporalLatentAttention(
        embed_dim=dim,
        num_heads=8,
    ).to(device, dtype=dtype)
    model.eval()
    
    # Incremental inference with FlashAttention-based MTLA
    incremental_state = {}
    outputs = []
    for t in range(length):
        out = model(
            query=x[:, t:t+1],
            key=x[:, t:t+1],
            value=x[:, t:t+1],
            position=pos[:, t:t+1],
            incremental_state=incremental_state,
            use_flashattn_infer=True,  # Enable FlashAttention
        )
        outputs.append(out)
    
    y = torch.cat(outputs, dim=1)
    print("Output shape:", y.shape)  # should be [batch, length, dim]
    
  • If you intend to run the full experiments, please install the project as described below before proceeding to the examples in the experiments directory.

    • PyTorch version >= 1.10.0
    • Python version >= 3.8
    cd experiments/tools/fairseq
    pip install --editable ./
    

Citation

If you use this codebase, or otherwise find our work valuable, please cite MTLA:

@article{mtla,
  title={Multi-head Temporal Latent Attention},
  author={Deng, Keqi and Woodland, Philip C},
  journal={arXiv preprint arXiv:2505.13544},
  year={2025}
}

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

mtla-0.2.0.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

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

mtla-0.2.0-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

Details for the file mtla-0.2.0.tar.gz.

File metadata

  • Download URL: mtla-0.2.0.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.18

File hashes

Hashes for mtla-0.2.0.tar.gz
Algorithm Hash digest
SHA256 9bb18e8c37369931f14ae10441b249407573dae231105e5fd2badb5f5823dd2f
MD5 1eab0b25cf45024afa00474b87be56f7
BLAKE2b-256 2be45d06ced6c31ecb8792b5f1d7112008b0c4e7c8fd50b58ac69420dce8998c

See more details on using hashes here.

File details

Details for the file mtla-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: mtla-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 14.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.18

File hashes

Hashes for mtla-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2ca7001c9593b300a7fe39fd07f27d828050b2a78e04724ad93d2e7c217cada2
MD5 84406f6b8e3825c910dea88976a28e26
BLAKE2b-256 31fbb1a6990ea215e3559978902d8c427fc21020ae1cc70a8a253f7c75567442

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