Skip to main content

Attention with QK distance using KL divergence

Project description

kl-div-attention

Just another attention variant, where the query key distance uses KL divergence, for testing Gitlab workflow

Install

pip install kl-div-attention

Usage

import torch
from kl_div_attention import KLDivAttention

attn = KLDivAttention(
    dim = 512,
    heads = 8,
    dim_head = 64,
    causal = True,
    prenorm = True, 
    fused_mode = 'flash'    # use fused triton flash attention
).cuda()

tokens = torch.randn(1, 1024, 512).cuda()

out = attn(tokens) + tokens

assert out.shape == tokens.shape

out.sum().backward()

Training

You can train a small transformer on Enwik8 with KL-divergence attention using the provided training script.

Eg. to use the fused Triton kernel

uv run train_enwik8.py --fused-mode flash

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

kl_div_attention-0.1.4.tar.gz (36.6 MB view details)

Uploaded Source

Built Distribution

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

kl_div_attention-0.1.4-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file kl_div_attention-0.1.4.tar.gz.

File metadata

  • Download URL: kl_div_attention-0.1.4.tar.gz
  • Upload date:
  • Size: 36.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for kl_div_attention-0.1.4.tar.gz
Algorithm Hash digest
SHA256 3be31adb91a2874c97370761c3b943a785330b81f2c356c5771dfe13a5d3514c
MD5 016c34b81c888a0514f219a51b28ecb5
BLAKE2b-256 d7be7ee07f02c2c5245a0964f7730762962808b5c88b5cc929b78b3ec25fab66

See more details on using hashes here.

File details

Details for the file kl_div_attention-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for kl_div_attention-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 17ac316b1084dfe0defd304a8adb7c2930c486af5289704bd0822b9ef025c314
MD5 ee887318179304ca5b289a2686cd1382
BLAKE2b-256 c16b813572e7d0f9c810fa036764812235a71a776198f4cf9a546b7469285a8f

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