cgel
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    $$ \newcommand{\R}{\mathbb{R}} \newcommand{\Z}{\mathbb{Z}} \newcommand{\N}{\mathbb{N}} \newcommand{\sft}{\text{softmax}} \newcommand{\List}{\text{List}} \newcommand{\Seq}{\text{Seq}} \newcommand{\SeqT}{\text{SeqT}} \newcommand{\CSeqT}{\text{CSeqT}} \newcommand{\Dist}{\text{Dist}} \newcommand{\SM}{\text{SM}} \newcommand{\Fn}{\text{Fn}} \newcommand{\Tok}{\text{Tok}} \newcommand{\Aij}{ A_{[i,j]}} $$ # Linear Self Attention is Faster # 0) Introduction What would happen if you took an exact replica GPT2 using flash attention 2 and made one simple modification: remove all the exponentials that appear in the attention layers (due to the softmax). Essentially, just change all the terms $e^{K_j^T Q_i} \to K_j^T Q_i$. [^1] In the plot below you can see the results for the 4 Transformer scales of the GPT2 paper which range from $120$ million to $1.5$ billions parameters. [^1]: To avoid dividing by $0$ in the normalization of the atteniton layer we also applied a positive nonlinearity when computing $K, Q$ to ensure all terms $K_j^T Q_i >0$. See section X for all the details. ![Screen Shot 2023-11-18 at 5.09.54 AM](https://hackmd.io/_uploads/BJcQKhB4a.png) Well, it looks like we found a way to make GPT2 slighly worse. But there is a catch. Note that the x axis is the number of training updates, not the time elapsed. See what happens if you click on the `time` x_axis? Now our modified transformer is way better at minimizing the loss. This is because the new transformer, running on equivalent hardware, is 20x faster. Look at what hapens for context sizes larger and smaller than 4096. As the context size grows, the speedup only gets more extreme, to the point that for the largest context size we testd, 100k, it's 200x faster. But how can such a tiny modification have such radical impact on the performance of the architecture? Section X contains all the details and performance analysis, but we can explain the core of the idea quite simply. So let's get on with it. ## 0.1) Linear Self Attention The inputs to the layer are sequences of $Q_i, K_i, V_i \in \R^d$ of query, key and values, where $i$ ranges from $1$ to $t$, the sequence length. The job of an attention layer is to compute another sequence of outputs $Y_i\in \R^d$. The core of the transformer attention layer is $$ \hat{Y}_i = \sum_{j=0}^i e^{K_j^T Q_i} V_j $$ Let's think about the cost of computing $Y_i$. We'll need to perform inner products $K_j^T Q_i$, each requiring $O(d)$ operations. And to compute $Y_i$ we'll need to perform $i$ such inner products, one for each $j\le i$. The inner products dominate, so the cost of computing $Y_i$ is $O(id)$. Since we need to compute the entire output sequence for $0\le i < t$, the total cost will be $O(d + 2d +\cdots + td) = O(t^2d)$. TODO: explain that the normalization factor is actually important for performance. The experiments we show include it. But normalization doesn't have any impact on the deep ideas behind the reason RSA can be so much faster than classical transformers. So we omit them for now to not clutter the notation. To see details see the full description on section 2.X. The story doesn't change much for the modified attention layer. It has the equation $$ Y_i = \sum_{j=0}^i K_j^T Q_i V_j $$ All the same inner products appear in this linear attention layer, so the cost should also be $O(t^2d)$. The optimized way to implement attention is with specialized kernels but the core of the idea is that you do ```python! def linear_attention(Q, K, V): """ TODO: explain shapes of the inputs """ M = causal_mask(K.shape[1]) A = (K.T @ Q) * M return V @ A ``` The difference with the standard transformer layer is that we can perform the follwoing factorization: $$ Y_i = \sum_{j=0}^i K_j^T Q_i V_j = \underbrace{ \left ( \sum_{j=0}^i V_j K_j^T\right )}_{S_i} \; \; Q_i $$ Where $S_i \in \R^{d\times d}$ can be thought of as a state summarizing all the relevant information up to time $i$. We can further massage the formula into the following recurrent equations $$ S_i = S_{i-1} + V_i K_i^T \;\;\;\;\;\;\;\; Y_{i} = S_i Q_i $$ where we assume $S_{-1} = 0$. Let's think about the computational cost of this approach. Every step, when we are trying to compute $Y_i$ we already have access to $S_{i-1}$. We need to compute $V_i K_i^T$ which takes $O(d^2)$ operations, adding it to $S_{i-1}$ is also $O(d^2)$ and the matrix vector multiplication $S_i Q_i$ is also $O(d^2)$. Thus, to compute the entire output sequence it will require $O(td^2)$ operations. The efficient way to implement this is just ```python! def linear_attention(Q, K, V): S = cumsum(V.T @ K) return Q @ S ``` We have a choice between two modes of computation! The first one mirrors the intuitions of self attention, each output $Y_i$ is computed by seeing how $Q_i$ interacts with all the previous keys $K_j$ for $0\ge j \ge i$. This **self attention mode** has cost $O(t^2 d)$. The other mode is sequential in nature, there is nothing in it that involves the interaction of every moment on time with all the privious ones. The **sequential mode** has cost $O(td^2)$. The 1.5B parameter model has $d=64$ and the context size $t$ would tipically be $1024$ or greater. Since $t$ is usually much larger than $d$, one would expect the sequential mode to be preferable over the attention approach which has a factor $t^2$ in the cost. Let's empirically measure this. The plot below shows the times to perform a training update for the 1.5B model with context sizes varying from 1024 to 132k EXPERIMENTS COMPARING SPEEDS OF THE TWO MODES It's perhaps surprizing how large the context size needs to be before the sequential approach becomes faster than the attention one. It turns out that attention is really well suited for GPUs, specially when using really well optimized kernels like flash attention. The attention mode is more parallelizable and requires less memory reads and writes. $t$ being much greater than $d$ does mean that the attention mode requires more floating point operations, but that turns out not to be the bottleneck for GPUs. At least it isn't until very large context sizes, $t=64k$ in the experiment above. Two things are evident here. * The attention formulation is curcial to fully utilize the power of GPUs * For large $t$ the quadratic term in $O(t^2d)$ really starts to hurt us. So much so that we reach a point where even the inneficient sequential implementation ends up being faster But why not get the best of both worlds? The great GPU utilization of attention together with the good growth in $t$ of the sequential mode. Let $c \in \Z^+$ be a positive integer that we call the chunk size. The key idea is that we are only only compute a subset of all states $S_0, S_c, S_{2c}, \cdots$ and we'll use attention of cost $O(c^2d)$ to compute all the in between outputs. For any $i$ find the $n\in \Z$ s.t. $cn < i \le c(n+1)$. Then we can easily see that the following equations are equivalent to the previous ones. $$ Y_{i} = S_{cn}Q_i + \sum_{j=cn+1}^i K_j^T Q_i V_j \;\;\;\;\;\;\;\; S_{c(n+1)} = S_{cn} + \sum_{j=cn+1}^{c(n+1)} V_j K_j^T $$ TODO: write pseudo code for the hybrid method We can pick $c$ empirically. For the 1.5B transformer, the optimal value of the chunk size $c$ was $512$. So let's see how this hybrid approach compares with the other two: EXPERIMENTS COMPARING SPEEDS OF THE THREE MODES And now you can see why in the initial experiment SARNN was so much faster than a standard transofrmer. TODO: add discusion of the hypotheis of why transformer is better when the x axis is updates: * We haven't put a lot of work tunning the LSA architecute. Just used all standard hyperparameters that were tunned for GPT2. We'll push a bit further to see if we can close the gap * Self attention with the softmax has more powerful representational capacity. In section X we present some theoretical evidence supporting this theory. If the exponential inside the softmax is appoximated with finitely many terms $e^{x} \simeq \sum_{k=0}^n \frac{x^k}{k!}$ then we can implement the classical transformer as an RNN just like we can for the linear self atteniton. The catch is that instead of having a state of size $d^2$, the state has size on the order of $O(d^n)$. So perhaps, when we are comparing a GPT2 vs RSA-GPT2 we are comparing two models with the same number of parameters with very different state sizes. Regardless of what the gap reason is, the fact RSA is so much faster than attention with the softmax makes it somewhat of a mute point. For large context sizes, the speed up enables one to train a much larger model with the same amount of compute, and the gains from the extra TODO: address BIG question about our work. Does the ability of RSA to utilize long context get worse as the context size grows? Since we are using datasets that don't contain long term information we aren't really testing that the models are capable of integrating large amounts of long term information. Essentially, it could be that the gap gets larger as the context size grows on tasks where there really is long context signal. Mention that this is ongoing work and we will update with more information once we have it. This is probably the biggest weakness of the work as is. ## 0.2) Sampling costs But when working with language models, efficinet training is not the only performance consideration. Once the model is trained, what are the costs of using it? The process of sampling looks something like this: we might have an initial sequence of input tokens $x_1 \cdots x_t$. We pass them to our model, which gives us a distirbution $p$ assigning probabilities to the next token . We sample from this distribution and append the new sample to the sequence and repeat the process with the sequence $x_1 \cdots x_{t+1}$. During training time, transformers are great because they compute the next token distribuiton $p_1, \cdots, p_{t}$ all at the same time in parallel. This advantage is more than enough to overcome the quadratic costs $O(t^2)$, at least for short context sizes. But when we are sampling we are left with all the disadvantages but can't reap the parallelization benefits due to the sequential nature of the problem. With the linear self attention, though, we have access to the recurrent formulation. It's has the advantage of lower cost EXPERIMENT: Experiment comparing speeds of sampling between RSA-GPT2 NOTE: The comparasion is not completely fair. Our KV cache implementation could be improved quite a lot by using specialized kernels while it's unclear if much improvement is possible on the simple unrolling of the RNN. But the pattern will remain the same. TODO: Make small point that GPT2 is not suited for sampling for arbitrarily long context due to the positional encodings.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully