Home

Awesome

Flowformer (ICML 2022)

Flowformer: Linearizing Transformers with Conservation Flows

:triangular_flag_on_post:News (2024.07) Mobile-Attention, a mobile-device-tailored version of Flowformer, has been published in ICML 2024. The attention code can be found here. You can obtain a faster model by just replacing the canonical Attention Mechanism with our Mobile-Attention.

Transformers have achieved impressive success in various areas. However, the attention mechanism has quadratic complexity, significantly impeding Transformers from dealing with numerous tokens and scaling up to bigger models. In pursuing the linear complexity and task-universal foundation model, we propose Flowformer [paper] with the following merits:

Flow-Attention Design

We cast the attention mechanism into flow network, where the information flow is aggregated from the sources (values) to the sinks (results) through the learned flow capacities (attentions).

By conducting the conservation in both source and sink ascpects, we can bring competition into Flow-Attention design to avoid trivial attention in the spirit that "fixed resource will cause competition''.

<p align="center"> <img src=".\pic\Flow-Attention.png" height = "250" alt="" align=center /> <br><br> <b>Figure 1.</b> Flow-Attention with Competition and Allocation mechanisms. </p>

Get Started

  1. Please refer to different folders for detailed experiment instructions.

    Note: We have suffered a lot in configuring environments for different tasks. If you also have problems in solving the environment, feel free to contact us and discuss about it.

  2. List of benchmarks

Main Results

See the [paper] for detailed results, including nearly 20 comparing baselines.

TaskMetricsFlowformerPerformerReformerVanilla<br>Transformer
Long Sequence Modeling <br> (LRA)Avg Acc (%) $\uparrow$56.4851.4150.67OOM
Vision Recognization<br>(ImageNet-1K)Top-1 Acc (%) $\uparrow$80.678.179.678.7
Language Modeling<br>(WikiText-103)Perplexity $\downarrow$30.837.533.633.0
Time series classification<br>(UEA)Avg Acc (%) $\uparrow$73.071.571.971.9
Offline RL<br>(D4RL)Avg Reward $\uparrow$ <br>Avg Deviation $\downarrow$73.5 $\pm$ 2.963.8 $\pm$ 7.663.9 $\pm$ 2.972.2 $\pm$ 2.6

Vanilla Transformer means Decision Transorfomer in RL.

Attention Visualization

<p align="center"> <img src=".\pic\Attention-visualization.png" height = "300" alt="" align=center /> <br><br> <b>Figure 2.</b> Attention visualization. Flowformer can capture the essential parts successfully. </p>

Citation

If you find this repo useful, please cite our paper.

@inproceedings{wu2022flowformer,
  title={Flowformer: Linearizing Transformers with Conservation Flows},
  author={Haixu Wu and Jialong Wu and Jiehui Xu and Jianmin Wang and Mingsheng Long},
  booktitle={International Conference on Machine Learning},
  year={2022}
}

Contact

If you have any questions or want to use the code, please contact wuhx23@mails.tsinghua.edu.cn.