
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
    <channel>
        <title><![CDATA[ The Cloudflare Blog ]]></title>
        <description><![CDATA[ Get the latest news on how products at Cloudflare are built, technologies used, and join the teams helping to build a better Internet. ]]></description>
        <link>https://blog.cloudflare.com</link>
        <atom:link href="https://blog.cloudflare.com/" rel="self" type="application/rss+xml"/>
        <language>en-us</language>
        <image>
            <url>https://blog.cloudflare.com/favicon.png</url>
            <title>The Cloudflare Blog</title>
            <link>https://blog.cloudflare.com</link>
        </image>
        <lastBuildDate>Mon, 13 Apr 2026 21:50:38 GMT</lastBuildDate>
        <item>
            <title><![CDATA[Making Workers AI faster and more efficient: Performance optimization with KV cache compression and speculative decoding]]></title>
            <link>https://blog.cloudflare.com/making-workers-ai-faster/</link>
            <pubDate>Thu, 26 Sep 2024 13:00:00 GMT</pubDate>
            <description><![CDATA[ With a new generation of data center accelerator hardware and using optimization techniques such as KV cache compression and speculative decoding, we’ve made large language model (LLM) ]]></description>
            <content:encoded><![CDATA[ <p>During Birthday Week 2023, <a href="https://blog.cloudflare.com/workers-ai/"><u>we launched Workers AI</u></a>. Since then, we have been listening to your feedback, and one thing we’ve heard consistently is that our customers want Workers AI to be faster. In particular, we hear that large language model (LLM) generation needs to be faster. Users want their interactive chat and agents to go faster, developers want faster help, and users do not want to wait for applications and generated website content to load. Today, we’re announcing three upgrades we’ve made to Workers AI to bring faster and more efficient inference to our customers: upgraded hardware, KV cache compression, and speculative decoding.</p>
    <div>
      <h3>Watch on Cloudflare TV</h3>
      <a href="#watch-on-cloudflare-tv">
        
      </a>
    </div>
    <div>
  
</div><p>Thanks to Cloudflare’s <a href="https://blog.cloudflare.com/gen-12-servers/"><u>12th generation compute servers</u></a>, our network now supports a newer generation of GPUs capable of supporting larger models and faster inference. Customers can now use <a href="https://developers.cloudflare.com/workers-ai/models/llama-3.2-11b-vision-instruct"><u>Meta Llama 3.2 11B</u></a>, Meta’s <a href="https://ai.meta.com/blog/llama-3-2-connect-2024-vision-edge-mobile-devices/"><u>newly released</u></a> multi-modal model with vision support, as well as Meta Llama 3.1 70B on Workers AI. Depending on load and time of day, customers can expect to see two to three times the throughput for Llama 3.1 and 3.2 compared to our previous generation Workers AI hardware. More performance information for these models can be found in today’s post: <a href="https://blog.cloudflare.com/workers-ai-bigger-better-faster"><i><u>Cloudflare’s Bigger, Better, Faster AI platform</u></i></a>.</p>
    <div>
      <h2>New KV cache compression methods, now open source</h2>
      <a href="#new-kv-cache-compression-methods-now-open-source">
        
      </a>
    </div>
    <p>In our effort to deliver low-cost low-latency inference to the world, Workers AI has been developing novel methods to boost efficiency of LLM inference. Today, we’re excited to announce a technique for KV cache compression that can help increase throughput of an inference platform. And we’ve made it open source too, so that everyone can benefit from our research.</p>
    <div>
      <h3>It’s all about memory</h3>
      <a href="#its-all-about-memory">
        
      </a>
    </div>
    <p>One of the main bottlenecks when running LLM inference is the amount of vRAM (memory) available. Every word that an LLM processes generates a set of vectors that encode the meaning of that word in the context of any earlier words in the input that are used to generate new tokens in the future. These vectors are stored in the <i>KV cache</i>, causing the memory required for inference to scale linearly with the total number of tokens of all sequences being processed. This makes memory a bottleneck for a lot of transformer-based models. Because of this, the amount of memory an instance has available limits the number of sequences it can generate concurrently, as well as the maximum token length of sequences it can generate.</p>
    <div>
      <h3>So what is the KV cache anyway?</h3>
      <a href="#so-what-is-the-kv-cache-anyway">
        
      </a>
    </div>
    <p>LLMs are made up of layers, with an <a href="https://en.wikipedia.org/wiki/Attention_(machine_learning)"><u>attention</u></a> operation occurring in each layer. Within each layer’s attention operation, information is collected from the representations of all previous tokens that are stored in cache. This means that vectors in the KV cache are organized into layers, so that the active layer’s attention operation can only query vectors from the corresponding layer of KV cache. Furthermore, since attention within each layer is parallelized across multiple attention “heads”, the KV cache vectors of a specific layer are further subdivided into groups corresponding to each attention head of that layer.</p><p>The diagram below shows the structure of an LLM’s KV cache for a single sequence being generated. Each cell represents a KV and the model’s representation for a token consists of all KV vectors for that token across all attention heads and layers. As you can see, the KV cache for a single layer is allocated as an M x N matrix of KV vectors where M is the number of attention heads and N is the sequence length. This will be important later!</p>
          <figure>
          <img src="https://cf-assets.www.cloudflare.com/zkvhlag99gkb/3ZagFp9yy3E55SR8GKRBvh/9e37f5890165e758ccaebf77464be483/BLOG-2571_2.png" />
          </figure><p>For a deeper look at attention, see the original “<a href="https://arxiv.org/abs/1706.03762"><u>Attention is All You Need</u></a>” paper. </p>
    <div>
      <h3>KV-cache compression — “use it or lose it”</h3>
      <a href="#kv-cache-compression-use-it-or-lose-it">
        
      </a>
    </div>
    <p>Now that we know what the KV cache looks like, let’s dive into how we can shrink it!</p><p>The most common approach to compressing the KV cache involves identifying vectors within it that are unlikely to be queried by future attention operations and can therefore be removed without impacting the model’s outputs. This is commonly done by looking at the past attention weights for each pair of key and value vectors (a measure of the degree with which that KV’s representation has been queried during past attention operations) and selecting the KVs that have received the lowest total attention for eviction. This approach is conceptually similar to a LFU (least frequently used) cache management policy: the less a particular vector is queried, the more likely it is to be evicted in the future.</p>
    <div>
      <h3>Different attention heads need different compression rates</h3>
      <a href="#different-attention-heads-need-different-compression-rates">
        
      </a>
    </div>
    <p>As we saw earlier, the KV cache for each sequence in a particular layer is allocated on the GPU as a <i># attention heads X sequence length</i> tensor. This means that the total memory allocation scales with the <i>maximum</i> sequence length for all attention heads of the KV cache. Usually this is not a problem, since each sequence generates the same number of KVs per attention head.</p><p>When we consider the problem of eviction-based KV cache compression, however, this forces us to remove an equal number of KVs from each attention head when doing the compression. If we remove more KVs from one attention head alone, those removed KVs won’t actually contribute to lowering the memory footprint of the KV cache on GPU, but will just add more empty “padding” to the corresponding rows of the tensor. You can see this in the diagram below (note the empty cells in the second row below):</p>
          <figure>
          <img src="https://cf-assets.www.cloudflare.com/zkvhlag99gkb/68Q5hVbfRF1vhqyeGNzB1Q/91056db8208c5e74be00e0add147b3e9/BLOG-2571_3.png" />
          </figure><p>The extra compression along the second head frees slots for two KVs, but the cache’s shape (and memory footprint) remains the same.</p><p>This forces us to use a fixed compression rate for all attention heads of KV cache, which is very limiting on the compression rates we can achieve before compromising performance.</p>
    <div>
      <h3>Enter PagedAttention</h3>
      <a href="#enter-pagedattention">
        
      </a>
    </div>
    <p>The solution to this problem is to change how our KV cache is represented in physical memory. <a href="https://arxiv.org/abs/2309.06180"><u>PagedAttention</u></a> can represent N x M tensors with padding efficiently by using an N x M block table to index into a series of “blocks”.</p>
          <figure>
          <img src="https://cf-assets.www.cloudflare.com/zkvhlag99gkb/1Sia3ZKKzBaHEfI8qLYr8o/57edb68d61ff916d322502aeb406c88c/BLOG-2571_4.png" />
          </figure><p>This lets us retrieve the i<sup>th</sup> element of a row by taking the i<sup>th</sup> block number from that row in the block table and using the block number to lookup the corresponding block, so we avoid allocating space to padding elements in our physical memory representation. In our case, the elements in physical memory are the KV cache vectors, and the <i>M </i>and <i>N</i> that define the shape of our block table are the number of attention heads and sequence length, respectively. Since the block table is only storing integer indices (rather than high-dimensional KV vectors), its memory footprint is negligible in most cases.</p>
    <div>
      <h3>Results</h3>
      <a href="#results">
        
      </a>
    </div>
    <p>Using paged attention lets us apply different rates of compression to different heads in our KV cache, giving our compression strategy more flexibility than other methods. We tested our compression algorithm on <a href="https://arxiv.org/abs/2308.14508"><u>LongBench</u></a> (a collection of long-context LLM benchmarks) with Llama-3.1-8B and found that for most tasks we can retain over 95% task performance while reducing cache size by up to 8x (left figure below). Over 90% task performance can be retained while further compressing up to 64x. That means you have room in memory for 64 times as many tokens!</p>
          <figure>
          <img src="https://cf-assets.www.cloudflare.com/zkvhlag99gkb/pdz5rPhYdfnMmn6cxhczo/29b69bb65aea8989fc1f50283e8ecbc5/BLOG-2571_5.png" />
          </figure><p>This lets us increase the number of requests we can process in parallel, increasing the total throughput (total tokens generated per second) by 3.44x and 5.18x for compression rates of 8x and 64x, respectively (right figure above).</p>
    <div>
      <h3>Try it yourself!</h3>
      <a href="#try-it-yourself">
        
      </a>
    </div>
    <p>If you’re interested in taking a deeper dive check out our <a href="https://github.com/IsaacRe/vllm-kvcompress"><u>vLLM fork</u></a> and get compressing!!</p>
    <div>
      <h2>Speculative decoding for faster throughput</h2>
      <a href="#speculative-decoding-for-faster-throughput">
        
      </a>
    </div>
    <p>A new inference strategy that we implemented is speculative decoding, which is a very popular way to get faster throughput (measured in tokens per second). LLMs work by predicting the next expected token (a token can be a word, word fragment or single character) in the sequence with each call to the model, based on everything that the model has seen before. For the first token generated, this means just the initial prompt, but after that each subsequent token is generated based on the prompt plus all other tokens that have been generated. Typically, this happens one token at a time, generating a single word, or even a single letter, depending on what comes next.</p><p>But what about this prompt:</p><blockquote><p><i>Knock, knock!</i></p></blockquote><p>If you are familiar with knock-knock jokes, you could very accurately predict more than one token ahead. For an English language speaker, what comes next is a very specific sequence that is four to five tokens long: “Who’s there?” or “Who is there?” Human language is full of these types of phrases where the next word has only one, or a few, high probability choices. Idioms, common expressions, and even basic grammar are all examples of this. So for each prediction the model makes, we can take it a step further with speculative decoding to predict the next <i>n</i> tokens. This allows us to speed up inference, as we’re not limited to predicting one token at a time.</p><p>There are several different implementations of speculative decoding, but each in some way uses a smaller, faster-to-run model to generate more than one token at a time. For Workers AI, we have applied <a href="https://github.com/apoorvumang/prompt-lookup-decoding"><u>prompt-lookup decoding</u></a> to some of the LLMs we offer. This simple method matches the last <i>n </i>tokens of generated text against text in the prompt/output and predicts candidate tokens that continue these identified patterns as candidates for continuing the output. In the case of knock-knock jokes, it can predict all the tokens for <i>“Who’s there</i>” at once after seeing “<i>Knock, knock!</i>”, as long as this setup occurs somewhere in the prompt or previous dialogue already. Once these candidate tokens have been predicted, the model can verify them all with a single forward-pass and choose to either accept or reject them. This increases the generation speed of llama-3.1-8b-instruct by up to 40% and the 70B model by up to 70%.</p><p>Speculative decoding has tradeoffs, however. Typically, the results of a model using speculative decoding have a lower quality, both when measured using benchmarks like <a href="https://paperswithcode.com/dataset/mmlu"><u>MMLU</u></a> as well as when compared by humans. More aggressive speculation can speed up sequence generation, but generally comes with a greater impact to the quality of the result. Prompt lookup decoding offers one of the smallest overall quality impacts while still providing performance improvements, and we will be adding it to some language models on Workers AI including <a href="https://developers.cloudflare.com/workers-ai/models/llama-3-8b-instruct"><u>@cf/meta/llama-3.1-8b-instruct</u></a>.</p><p>And, by the way, here is one of our favorite knock-knock jokes, can you guess the punchline?</p><blockquote><p><i>Knock, knock!</i></p><p><i>Who’s there?</i></p><p><i>Figs!</i></p><p><i>Figs who?</i></p><p><i>Figs the doorbell, it’s broken!</i></p></blockquote>
    <div>
      <h2>Keep accelerating</h2>
      <a href="#keep-accelerating">
        
      </a>
    </div>
    <p>As the AI industry continues to evolve, there will be new hardware and software that allows customers to get faster inference responses. Workers AI is committed to researching, implementing, and making upgrades to our services to help you get fast inference. As an Inference-as-a-Service platform, you’ll be able to benefit from all the optimizations we apply, without having to hire your own team of ML researchers and SREs to manage inference software and hardware deployments.

We’re excited for you to try out some of these new releases we have and let us know what you think! Check out our full-suite of AI announcements <a href="https://blog.cloudflare.com/tag/ai/"><u>here</u></a> and check out the <a href="https://developers.cloudflare.com/workers-ai/"><u>developer docs</u></a> to get started.</p> ]]></content:encoded>
            <category><![CDATA[Birthday Week]]></category>
            <category><![CDATA[Product News]]></category>
            <category><![CDATA[Cloudflare Workers]]></category>
            <category><![CDATA[Developers]]></category>
            <category><![CDATA[Developer Platform]]></category>
            <category><![CDATA[LLM]]></category>
            <guid isPermaLink="false">29PAMer5L0do12OtNa557I</guid>
            <dc:creator>Isaac Rehg</dc:creator>
            <dc:creator>Jesse Kipp</dc:creator>
        </item>
        <item>
            <title><![CDATA[Meta Llama 3 available on Cloudflare Workers AI]]></title>
            <link>https://blog.cloudflare.com/meta-llama-3-available-on-cloudflare-workers-ai/</link>
            <pubDate>Thu, 18 Apr 2024 20:58:33 GMT</pubDate>
            <description><![CDATA[ We are thrilled to give developers around the world the ability to build AI applications with Meta Llama 3 using Workers AI. We are proud to be a launch partner with Meta for their newest 8B Llama 3 model ]]></description>
            <content:encoded><![CDATA[ <p></p><p>We are thrilled to give developers around the world the ability to build AI applications with Meta Llama 3 using Workers AI. We are proud to be a launch partner with Meta for their newest 8B Llama 3 model, and excited to continue our partnership to bring the best of open-source models to our inference platform.</p>
    <div>
      <h2>Workers AI</h2>
      <a href="#workers-ai">
        
      </a>
    </div>
    <p><a href="/workers-ai">Workers AI’s initial launch</a> in beta included support for Llama 2, as it was one of the most requested open source models from the developer community. Since that initial launch, we’ve seen developers build all kinds of innovative applications including knowledge sharing <a href="https://workers.cloudflare.com/built-with/projects/ai.moda/">chatbots</a>, creative <a href="https://workers.cloudflare.com/built-with/projects/Audioflare/">content generation</a>, and automation for <a href="https://workers.cloudflare.com/built-with/projects/Azule/">various workflows</a>.  </p><p>At Cloudflare, we know developers want simplicity and flexibility, with the ability to build with multiple AI models while optimizing for accuracy, performance, and cost, among other factors. Our goal is to make it as easy as possible for developers to use their models of choice without having to worry about the complexities of hosting or deploying models.</p><p>As soon as we learned about the development of Llama 3 from our partners at Meta, we knew developers would want to start building with it as quickly as possible. Workers AI’s serverless inference platform makes it extremely easy and cost-effective to start using the latest large language models (LLMs). Meta’s commitment to developing and growing an open AI-ecosystem makes it possible for customers of all sizes to use AI at scale in production. All it takes is a few lines of code to run inference to Llama 3:</p>
            <pre><code>export interface Env {
  // If you set another name in wrangler.toml as the value for 'binding',
  // replace "AI" with the variable name you defined.
  AI: any;
}

export default {
  async fetch(request: Request, env: Env) {
    const response = await env.AI.run('@cf/meta/llama-3-8b-instruct', {
        messages: [
{role: "user", content: "What is the origin of the phrase Hello, World?"}
	 ]
      }
    );

    return new Response(JSON.stringify(response));
  },
};</code></pre>
            
    <div>
      <h2>Built with Meta Llama 3</h2>
      <a href="#built-with-meta-llama-3">
        
      </a>
    </div>
    <p>Llama 3 offers leading performance on a wide range of industry benchmarks. You can learn more about the architecture and improvements on Meta’s <a href="https://ai.meta.com/blog/meta-llama-3/">blog post</a>. Cloudflare Workers AI supports <a href="https://developers.cloudflare.com/workers-ai/models/llama-3-8b-instruct/">Llama 3 8B</a>, including the instruction fine-tuned model.</p><p>Meta’s testing shows that Llama 3 is the most advanced open LLM today on <a href="https://github.com/meta-llama/llama3/blob/main/eval_details.md?cf_target_id=1F7E4663A460CE17F25CF8ADDF6AB9F1">evaluation benchmarks</a> such as MMLU, GPQA, HumanEval, GSM-8K, and MATH. Llama 3 was trained on an increased number of training tokens (15T), allowing the model to have a better grasp on language intricacies. Larger context windows doubles the capacity of Llama 2, and allows the model to better understand lengthy passages with rich contextual data. Although the model supports a context window of 8k, we currently only support 2.8k but are looking to support 8k context windows through quantized models soon. As well, the new model introduces an efficient new <a href="https://github.com/openai/tiktoken">tiktoken</a>-based tokenizer with a vocabulary of 128k tokens, encoding more characters per token, and achieving better performance on English and multilingual benchmarks. This means that there are 4 times as many parameters in the embedding and output layers, making the model larger than the previous Llama 2 generation of models.</p><p>Under the hood, Llama 3 uses <a href="https://arxiv.org/abs/2305.13245">grouped-query attention</a> (GQA), which improves inference efficiency for longer sequences and also renders their 8B model architecturally equivalent to <a href="https://developers.cloudflare.com/workers-ai/models/mistral-7b-instruct-v0.1/">Mistral-7B</a>. For tokenization, it uses byte-level <a href="https://huggingface.co/learn/nlp-course/en/chapter6/5">byte-pair encoding (BPE)</a>, similar to OpenAI’s GPT tokenizers. This allows tokens to represent any arbitrary byte sequence — even those without a valid utf-8 encoding. This makes the end-to-end model much more flexible in its representation of language, and leads to improved performance.</p><p>Along with the base Llama 3 models, Meta has released a suite of offerings with tools such as <a href="https://ai.meta.com/blog/meta-llama-3/">Llama Guard 2, Code Shield, and CyberSec Eval 2,</a> which we are hoping to release on our Workers AI platform shortly.</p>
    <div>
      <h2>Try it out now</h2>
      <a href="#try-it-out-now">
        
      </a>
    </div>
    <p>Meta Llama 3 8B is available in the <a href="https://developers.cloudflare.com/workers-ai/models/">Workers AI Model Catalog</a> today! Check out the <a href="https://developers.cloudflare.com/workers-ai/models/llama-3-8b-instruct/">documentation here</a> and as always if you want to share your experiences or learn more, join us in the <a href="https://discord.cloudflare.com">Developer Discord</a>.</p> ]]></content:encoded>
            <category><![CDATA[Llama]]></category>
            <category><![CDATA[Developers]]></category>
            <category><![CDATA[Developer Platform]]></category>
            <category><![CDATA[Workers AI]]></category>
            <category><![CDATA[Cloudflare Workers]]></category>
            <category><![CDATA[Product News]]></category>
            <guid isPermaLink="false">3FHdMMB8JzNt8hkAYDcqVL</guid>
            <dc:creator>Michelle Chen</dc:creator>
            <dc:creator>Davina Zamanzadeh</dc:creator>
            <dc:creator>Isaac Rehg</dc:creator>
            <dc:creator>Nikhil Kothari</dc:creator>
        </item>
        <item>
            <title><![CDATA[Workers AI Update: Hello, Mistral 7B!]]></title>
            <link>https://blog.cloudflare.com/workers-ai-update-hello-mistral-7b/</link>
            <pubDate>Tue, 21 Nov 2023 14:00:58 GMT</pubDate>
            <description><![CDATA[ Today we’re excited to announce that we’ve added the Mistral-7B-v0.1-instruct to Workers AI ]]></description>
            <content:encoded><![CDATA[ <p></p>
            <figure>
            
            <img src="https://cf-assets.www.cloudflare.com/zkvhlag99gkb/1vOF3ZSPVvD4CPRyt6PCZi/10554230ab60f8f2252ca5ee0eae47c7/Mistral-1.png" />
            
            </figure><p>Today we’re excited to announce that we’ve added the Mistral-7B-v0.1-instruct to Workers AI. Mistral 7B is a 7.3 billion parameter language model with a number of unique advantages. With some help from the founders of Mistral AI, we’ll look at some of the highlights of the Mistral 7B model, and use the opportunity to dive deeper into “attention” and its variations such as multi-query attention and grouped-query attention.</p>
    <div>
      <h2>Mistral 7B tl;dr:</h2>
      <a href="#mistral-7b-tl-dr">
        
      </a>
    </div>
    <p>Mistral 7B is a 7.3 billion parameter model that puts up <a href="https://mistral.ai/news/announcing-mistral-7b/">impressive numbers on benchmarks</a>. The model:</p><ul><li><p>Outperforms comparable 13B model on all benchmarks</p></li><li><p>Outperforms comparable 34B on many benchmarks,</p></li><li><p>Approaches CodeLlama 7B performance on code, while remaining good at English tasks, and</p></li><li><p>The chat fine-tuned version we’ve deployed outperforms comparable 2 13B chat in the benchmarks provided by Mistral.</p></li></ul><p>Here’s an example of using streaming with the <a href="https://developers.cloudflare.com/workers-ai/get-started/rest-api/">REST API</a>:</p>
            <pre><code>curl -X POST \
“https://api.cloudflare.com/client/v4/accounts/{account-id}/ai/run/@cf/mistral/mistral-7b-instruct-v0.1” \
-H “Authorization: Bearer {api-token}” \
-H “Content-Type:application/json” \
-d '{ “prompt”: “What is grouped query attention”, “stream”: true }'

API Response: { response: “Grouped query attention is a technique used in natural language processing  (NLP) and machine learning to improve the performance of models…” }</code></pre>
            <p>And here’s an example using a Worker script:</p>
            <pre><code>import { Ai } from '@cloudflare/ai';
export default {
    async fetch(request, env) {
        const ai = new Ai(env.AI);
        const stream = await ai.run('@cf/mistral/mistral-7b-instruct-v0.1', {
            prompt: 'What is grouped query attention',
            stream: true
        });
        return Response.json(stream, { headers: { “content-type”: “text/event-stream” } });
    }
}</code></pre>
            <p>Mistral takes advantage of <a href="https://arxiv.org/abs/2305.13245">grouped-query attention</a> for faster inference. This recently-developed technique improves the speed of inference without compromising output quality. For 7 billion parameter models, we can generate close to 4x as many tokens per second with Mistral as we can with Llama, thanks to Grouped-Query attention.</p><p>You don’t need any information beyond this to start using Mistral-7B, you can test it out today <a href="https://ai.cloudflare.com">ai.cloudflare.com</a>. To learn more about attention and Grouped-Query attention, read on!</p>
    <div>
      <h2>So what is “attention” anyway?</h2>
      <a href="#so-what-is-attention-anyway">
        
      </a>
    </div>
    <p>The basic mechanism of attention, specifically “Scaled Dot-Product Attention” as introduced in the landmark paper <a href="https://arxiv.org/abs/1706.03762">Attention Is All You Need</a>, is fairly simple:</p><blockquote><p>We call our particular attention “Scale Dot-Product Attention”. The input consists of query and keys of dimension d_k, and values of dimension d_v. We compute the dot products of the query with all the keys, divide each by sqrt(d_k) and apply a softmax function to obtain the weights on the values.</p></blockquote><p>More concretely, this looks like this:</p>
            <figure>
            
            <img src="https://cf-assets.www.cloudflare.com/zkvhlag99gkb/7gLX5h3XWjtQugoqf4Yqjh/9f4879fc9078fa291da9caacab1925d7/Screenshot-2023-11-21-at-09.12.30.png" />
            
            </figure><p><a href="https://arxiv.org/abs/1706.03762">source</a></p><p>In simpler terms, this allows models to focus on important parts of the input. Imagine you are reading a sentence and trying to understand it. Scaled dot product attention enables you to pay more attention to certain words based on their relevance. It works by calculating the similarity between each word (K) in the sentence and a query (Q). Then, it scales the similarity scores by dividing them by the square root of the dimension of the query. This scaling helps to avoid very small or very large values. Finally, using these scaled similarity scores, we can determine how much attention or importance each word should receive. This attention mechanism helps models identify crucial information (V) and improve their understanding and translation capabilities.</p><p>Easy, right? To get from this simple mechanism to an AI that can write a “Seinfeld episode in which Jerry learns the bubble sort algorithm,” we’ll need to make it more complex. In fact, everything we’ve just covered doesn’t even have any learned parameters — constant values learned during model training that customize the output of the attention block!Attention blocks in the style of <i>Attention is All You Need</i> add mainly three types of complexity:</p>
    <div>
      <h3>Learned parameters</h3>
      <a href="#learned-parameters">
        
      </a>
    </div>
    <p>Learned parameters refer to values or weights that are adjusted during the training process of a model to improve its performance. These parameters are used to control the flow of information or attention within the model, allowing it to focus on the most relevant parts of the input data. In simpler terms, learned parameters are like adjustable knobs on a machine that can be turned to optimize its operation.</p>
    <div>
      <h3>Vertical stacking - layered attention blocks</h3>
      <a href="#vertical-stacking-layered-attention-blocks">
        
      </a>
    </div>
    <p>Vertical layered stacking is a way to stack multiple attention mechanisms on top of each other, with each layer building on the output of the previous layer. This allows the model to focus on different parts of the input data at different levels of abstraction, which can lead to better performance on certain tasks.</p>
    <div>
      <h3>Horizontal stacking - aka Multi-Head Attention</h3>
      <a href="#horizontal-stacking-aka-multi-head-attention">
        
      </a>
    </div>
    <p>The figure from the paper displays the full multi-head attention module. Multiple attention operations are carried out in parallel, with the Q-K-V input for each generated by a unique linear projection of the same input data (defined by a unique set of learned parameters). These parallel attention blocks are referred to as “attention heads”. The weighted-sum outputs of all attention heads are concatenated into a single vector and passed through another parameterized linear transformation to get the final output.</p>
            <figure>
            
            <img src="https://cf-assets.www.cloudflare.com/zkvhlag99gkb/5IhI1kh7JOS9ERb3aFuVHg/95e10b554d0f5029bf08fb10262cc29b/Screenshot-2023-11-21-at-09.13.49.png" />
            
            </figure><p><a href="https://arxiv.org/abs/1706.03762">source</a></p><p>This mechanism allows a model to focus on different parts of the input data concurrently. Imagine you are trying to understand a complex piece of information, like a sentence or a paragraph. In order to understand it, you need to pay attention to different parts of it at the same time. For example, you might need to pay attention to the subject of the sentence, the verb, and the object, all simultaneously, in order to understand the meaning of the sentence. Multi-headed attention works similarly. It allows a model to pay attention to different parts of the input data at the same time, by using multiple "heads" of attention. Each head of attention focuses on a different aspect of the input data, and the outputs of all the heads are combined to produce the final output of the model.</p>
    <div>
      <h2>Styles of attention</h2>
      <a href="#styles-of-attention">
        
      </a>
    </div>
    <p>There are three common arrangements of attention blocks used by large language models developed in recent years: multi-head attention, grouped-query attention and multi-query attention. They differ in the number of K and V vectors relative to the number of query vectors. <b>Multi-head attention</b> uses the same number of K and V vectors as Q vectors, denoted by “N” in the table below. <b>Multi-query attention</b> uses only a single K and V vector. <b>Grouped-query attention</b>, the type used in the Mistral 7B model, divides the Q vectors evenly into groups containing “G” vectors each, then uses a single K and V vector for each group for a total of N divided by G sets of K and V vectors. This summarizes the differences, and we’ll dive into the implications of these below.</p><table>
	<tbody>
		<tr>
			<td> </td>
			<td>
			<p><span><span><span><strong>Number of Key/Value Blocks</strong></span></span></span></p>
			</td>
			<td>
			<p><span><span><span><strong>Quality</strong></span></span></span></p>
			</td>
			<td>
			<p><span><span><span><strong>Memory Usage</strong></span></span></span></p>
			</td>
		</tr>
		<tr>
			<td>
			<p><span><span><span><strong>Multi-head attention (MHA)</strong></span></span></span></p>
			</td>
			<td>
			<p><span><span><span>N</span></span></span></p>
			</td>
			<td>
			<p><span><span><span>Best</span></span></span></p>
			</td>
			<td>
			<p><span><span><span>Most</span></span></span></p>
			</td>
		</tr>
		<tr>
			<td>
			<p><span><span><span><strong>Grouped-query attention (GQA)</strong></span></span></span></p>
			</td>
			<td>
			<p><span><span><span>N / G</span></span></span></p>
			</td>
			<td>
			<p><span><span><span>Better</span></span></span></p>
			</td>
			<td>
			<p><span><span><span>Less</span></span></span></p>
			</td>
		</tr>
		<tr>
			<td>
			<p><span><span><span><strong>Multi-query attention (MQA)</strong></span></span></span></p>
			</td>
			<td>
			<p><span><span><span>1</span></span></span></p>
			</td>
			<td>
			<p><span><span><span>Good</span></span></span></p>
			</td>
			<td>
			<p><span><span><span>Least</span></span></span></p>
			</td>
		</tr>
	</tbody>
</table>
<p><span><span><span>Summary of attention styles</span></span></span></p><p>And this diagram helps illustrate the difference between the three styles:</p>
            <figure>
            
            <img src="https://cf-assets.www.cloudflare.com/zkvhlag99gkb/2n8Qcj00djVDN2KEGUB7Ul/4cbd4f808b5d51e3f470852ecaff8214/image1-6.png" />
            
            </figure><p><a href="https://arxiv.org/pdf/2305.13245.pdf">source</a></p>
    <div>
      <h3>Multi-Query Attention</h3>
      <a href="#multi-query-attention">
        
      </a>
    </div>
    <p>Multi-query attention was described in 2019 in the paper from Google: <a href="https://arxiv.org/abs/1911.02150">Fast Transformer Decoding: One Write-Head is All You Need</a>. The idea is that instead of creating separate K and V entries for every Q vector in the attention mechanism, as in multi-head attention above, only a single K and V vector is used for the entire set of Q vectors. Thus the name, multiple queries combined into a single attention mechanism. In the paper, this was benchmarked on a translation task and showed performance equal to multi-head attention on the benchmark task.</p><p>Originally the idea was to reduce the total size of memory that is accessed when performing inference for the model. Since then, as generalized models have emerged and grown in number of parameters, the GPU memory needed is often the bottleneck which is the strength of multi-query attention, as it requires the least accelerator memory of the three types of attention. However, as models grew in size and generality, performance of multi-query attention fell relative to multi-head attention.</p>
    <div>
      <h3>Grouped-Query Attention</h3>
      <a href="#grouped-query-attention">
        
      </a>
    </div>
    <p>The newest of the bunch — and the one used by Mistral — is grouped-query attention, as described in the paper <a href="https://arxiv.org/abs/2305.13245">GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints</a> that was published on arxiv.org in May 2023. Grouped-query attention combines the best of both worlds: the quality of multi-headed attention with the speed and low memory usage of multi-query attention. Instead of either a single set of K and V vectors or one set for every Q vector, a fixed ratio of 1 set of K and V vectors for every Q vector is used, reducing memory usage but retaining high performance on many tasks.</p><p>Often choosing a model for a production task is not just about picking the best model available because we must consider tradeoffs between performance, memory usage, batch size, and available hardware (or cloud costs). Understanding these three styles of attention can help guide those decisions and understand when we might choose a particular model given our circumstances.</p>
    <div>
      <h2>Enter Mistral — try it today</h2>
      <a href="#enter-mistral-try-it-today">
        
      </a>
    </div>
    <p>Being one of the first large language models to leverage grouped-query attention and combining it with sliding window attention, Mistral seems to have hit the goldilocks zone — it’s low latency, high-throughput, and it performs really well on benchmarks even when compared to bigger models (13B). All this to say is that it packs a punch for its size, and we couldn't be more excited to make it available to all developers today, via Workers AI.</p><p>Head over to our <a href="https://developers.cloudflare.com/workers-ai/models/text-generation/">developer docs</a> to get started, and if you need help, want to give feedback, or want to share what you’re building just pop into our <a href="https://discord.com/invite/cloudflaredev">Developer Discord</a>!</p><p>The Workers AI team is also expanding and hiring; check our <a href="https://www.cloudflare.com/careers/jobs/">jobs page</a> for open roles if you’re passionate about AI engineering and want to help us build and evolve our global, serverless GPU-powered inference platform.</p> ]]></content:encoded>
            <category><![CDATA[Workers AI]]></category>
            <category><![CDATA[Cloudflare Workers]]></category>
            <category><![CDATA[Developers]]></category>
            <category><![CDATA[Developer Platform]]></category>
            <guid isPermaLink="false">4QmaH8GOH4hQA5STcgIbJS</guid>
            <dc:creator>Jesse Kipp</dc:creator>
            <dc:creator>Isaac Rehg</dc:creator>
        </item>
    </channel>
</rss>