Back to all posts
Coffee Codex - Trainium
Meshan Khosla ·

Coffee Codex - Trainium

Introduction

I’m at North of Main Cafe in Bellevue, WA, and today I’m learning about AWS Trainium chips.

Coffee

I was watching re:Invent this past week, and Amazon’s new Ergo model stole a lot of the show. It’s open-weight and is claimed to run at 2,200 tok/s on the new Trainium 3SR chips. Trainium chips were designed for training workloads, but it turns out they have since been very effective for inference workloads as well.

Inference

I covered this in my transformers post, but inference is split into 2 phases: prefill and decode. Prefill does a lot of the upfront work of creating the KV cache and generating the first token, thus making it compute intensive. Decode generates the following tokens. Since it needs to read from memory a lot (KV cache, weights), it is memory intensive.

The Trainium 3SR chip is designed to handle the second flow. The “SR” is for static random access memory (SRAM), which is memory that lives on the chip, making it faster than High Bandwidth Memory (HBM). This means the Trainium3 chips can be used for prefill and then handed off using EFA to Trainium 3SR for decode. With Trainium 3 alone, the tok/s went from 2,200 to 400. OK, let’s learn more.

Handoff

The diagram shows the flow of the prompt’s KV cache between the two stages.

Why SRAM

So AWS hasn’t really released a lot of information about Trainium 3SR, so instead let’s discuss why SRAM would be better for decode and what the alternatives are.

The structure of a GPU with memory looks something like this:

Structure

The real thing looks like this:

H100 package

Image from SemiAnalysis.

Looking at this image, the package on that board holds the GH100 processor chip and the HBM3 memory chips next to it. The big silver rectangle in the middle is the GH100. It contains the hardware that does the math, the interconnect, and the memory controllers. The L1 cache, shared memory, and L2 cache are SRAM on that same chip. HBM3 is a type of DRAM, and it lives on separate chips inside the same package.

Here’s what AWS Trainium3 looks like:

Trainium3

Image from About Amazon.

Which brings up an interesting question. How do we choose what goes into SRAM and what goes into HBM? We discussed earlier that SRAM is great for memory-bound workloads like decode, but what goes in there?

There are really two options: the model weights and the KV cache.

At first, I thought it should be the KV cache. With full attention, each new token needs the keys and values for all the earlier tokens. But even if we’re generating just one token, we still need the weights across the model to run that forward pass.

Say one decode step reads 8 GB of weights and 1 GB of KV cache from HBM, and we have 1 GB of SRAM free. If we keep the whole KV cache in SRAM, we still read 8 GB of weights from HBM. If we keep 1 GB of weights in SRAM, we read 7 GB of weights and 1 GB of KV cache from HBM. Either way, we save 1 GB of HBM reads on that step.

In my 8 GB example, it’s a tie for one decode step. I’d keep the weights in SRAM if I were serving lots of short requests. The next request uses the same weights, but it usually needs a new KV cache. If I’m decoding a long conversation, I can reuse the same cache across steps, and there’s more of it to read as the conversation grows. Cool stuff

References