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.5.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.5-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kl_div_attention-0.1.5.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.5.tar.gz
Algorithm Hash digest
SHA256 e853833d2a7898adc4e479925e055abbdd13a7494c9a3cbc7b283c0e64598e8a
MD5 c7efaf22112a0b1dd7d42e8a4b587850
BLAKE2b-256 4c69c7916b96febd843eb75e7b63c49fea9486494d62cb390951998611ee7db4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for kl_div_attention-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 41bbecddc1a1161ae3ba8922eaeab1d460da82c24000bd2047a3ed5cd295e41d
MD5 4cf57bf67026a18f1261769f95d29cb9
BLAKE2b-256 251591293ca15679e905a3d8d3ac6a53912e10d00234cad06a17349a51666b35

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