Skip to main content

Compositional Attention

PyPI Upload Python Package Code style: black

GitHub stars GitHub followers Twitter Follow

This repository is an implementation of Compositional Attention: Disentangling Search and Retrieval by MILA. Revisiting standard Multi-head attention through the lens of multiple parallel and independent search and retrieval mechanisms, this leads to static pairings between searches and retrievals, often leading to redundancy of parameters. They reframe the "heads" of multi-head attention as "searches", and once the multi-headed/searched values are aggregated, there is an extra retrieval step (using attention) off the searched results. The experiments establish this as an easy drop-in replacement for Multi-head attention.

Installation

Run the following to install:

pip install compositional-attention

Developing compositional-attention

To install compositional-attention, along with tools you need to develop and test, run the following in your virtualenv:

git clone https://github.com/Rishit-dagli/compositional-attention.git
# or clone your own fork

cd compositional-attention
pip install -e .[dev]

Usage

import tensorflow as tf
from compositional_attention import CompositionalAttention

attn = CompositionalAttention(
    dim = 1024,            # input dimension
    dim_head = 64,         # dimension per attention 'head' - head is now either search or retrieval
    num_searches = 8,      # number of searches
    num_retrievals = 2,    # number of retrievals
    dropout = 0.1,          # dropout of attention of search and retrieval
)

tokens = tf.random.uniform([1, 512, 1024])  # tokens
mask = tf.ones([1, 512], dtype=tf.dtypes.bool)  # mask

out = attn(tokens, mask = mask) # (1, 512, 1024)

Want to Contribute 🙋‍♂️?

Awesome! If you want to contribute to this project, you're always welcome! See Contributing Guidelines. You can also take a look at open issues for getting more information about current or upcoming tasks.

Want to discuss? 💬

Have any questions, doubts or want to present your opinions, views? You're always welcome. You can start discussions.

Citations

Mittal, Sarthak, et al. ‘Compositional Attention: Disentangling Search and Retrieval’. ArXiv:2110.09419 [Cs], Feb. 2022. arXiv.org, http://arxiv.org/abs/2110.09419.

Official PyTorch implmentation and Phil Wang's PyTorch implmenetation.

Metadata

Release files for compositional-attention 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for compositional-attention 0.1.0
File Size Uploaded
compositional-attention-0.1.0.tar.gz 8.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for compositional-attention 0.1.0
File Interpreter ABI Platform
compositional_attention-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.7 kB

Release files / compositional-attention-0.1.0.tar.gz

Download URL compositional-attention-0.1.0.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
661a400fdd98622c6908e69dcfeeae6990914a515b6a10a44eea400df5c486c6
BLAKE2b-256 checksum
How to use checksums
a178b095aef5110f68e669833321302ed3aeb9d3f29c05144b52aa51ddf5ae25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.13

Release files / compositional_attention-0.1.0-py3-none-any.whl

Download URL compositional_attention-0.1.0-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dd265174da1eef2b48b8c782d1adc17e25db1a787bc45c938f298d2b862f9fcd
BLAKE2b-256 checksum
How to use checksums
ff32a55817ea22ea3d7a0bd4359474fbc2e6d81c45f8af7f9c7ab312e5cbc579
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.9.13

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page