Linear Complexity in Attention Mechanism: A step-by-step implementation in PyTorch
About this lesson
In our last video, we explored eight distinct algorithms aimed at improving the efficiency of the attention mechanism by minimizing its memory and arithmetic complexity. In this video, we'll be presenting a PyTorch implementation of four of those algorithms that have linear complexity wrt sequence length This demonstration, is intended to help you get familiar with the underlying concepts. Link to notebooks: - Blank notebook: https://github.com/PyML-studio/mlstudio/blob/main/Notebooks/linear-attention-pytorch/linear-attention-pytorch.ipynb - Solution notebook: https://github.com/PyML-studio/mlstudio/blob/main/Notebooks/linear-attention-pytorch/linear-attention-pytorch_solution.ipynb ~~~~~~~~ Note on Tensor.matmul() vs. torch.bmm() ~~~~~~~~~~~ torch.bmm is specialized for batch matrix-matrix multiplication of 3D tensors, whereas Tensor.matmul is more versatile and can handle 2D matrix multiplication as well as batch matrix multiplication with broadcasting support for higher-dimensional tensors.
DeepCamp AI