Running Llama 3.1 8B on encrypted data
FHE natively handles additions, multiplications and rotations on packed data. A transformer relies on far more complex operations: large matrix multiplications, attention over a growing key-value (KV) cache and non-polynomial functions in every layer. This section walks through how these operations are expressed within FHE's computing model.
argmax picks the next token
scores all 128,256 tokens
SwiGLU, matrix multiplications
matrix multiplication
softmax over earlier tokens
matrix multiplications
token to vector
every token
Llama 3.1 8B has 8 billion parameters, arranged in a stack of 32 transformer layers. Inside the model, each token is represented by a vector of 4,096 values, known as the hidden dimension. Generating a single token requires one pass through all 32 layers, which in plaintext takes on the order of 8 billion multiply-and-add operations. Each layer combines five steps:
- RMS normalization rescales each token's representation before the attention and feed-forward blocks described below. It requires an inverse square root, an operation that is not native to FHE.
- Projections prepare each token's representation for attention, as attention queries, attention keys and attention values, and map the attention output back to feed-forward activations. Attention keys are part of the model and are not encryption keys. Each projection is computed as a large matrix multiplication between encrypted activations and plaintext weights.
- Rotary position embeddings encode where each token sits in the sequence by transforming its queries and keys according to their position.
- Attention lets each token compare its query with the attention keys of every earlier token through a similarity score. These attention keys are kept in the KV cache, which therefore grows with every token generated. A softmax, another non-polynomial operation that is costly under FHE, turns these scores into a probability distribution over the earlier tokens.
- Feed-forward network expands each token's representation, applies the non-polynomial SwiGLU activation and projects it back. Together with the projections, it accounts for most of the arithmetic.
Two more important components sit outside the transformer layers:
- Embeddings. The input embedding maps each token into a vector of 4,096 values, which enter the first layer. The output embedding performs the inverse operation: it scores the output of the last layer against all 128,256 tokens in Llama's vocabulary. With approximately half a billion parameters each, the embeddings themselves require large matrix multiplications.
- Token selection converts the scores of the output embedding into the next token, through either greedy decoding or sampling. Greedy decoding takes the highest-scoring token, whereas sampling draws a token from the resulting probabilities derived from the scores. In both approaches, the server must compare or exponentiate 128,256 encrypted scores, both of which are non-polynomial operations.
A request passes through the model in two phases (Figure 8). In the first, the model receives the whole prompt and processes all of its tokens at once. This phase, known as prefill, fills the KV cache and produces the first token, and therefore determines the time to first token. The second phase is decode, in which the model generates the rest of the answer one token at a time, reusing the KV cache at each step. Decode sets the throughput in tokens per second (tok/s) and the time to last token.
The two phases call for different kinds of parallelism. Prefill is relatively easier to parallelize at the algorithmic level, because all tokens of the prompt can be processed in parallel. Decode offers no such option, since each new token depends on the one before it. Reducing the latency of decode therefore requires fine-grained, low-level parallelism within the computation for a single token.