Single GPU Inference Arithmetic on Qwen3.5-4B: Part 1 - the Math
15 Nov 2026
Intro
Models that can do edge/local inference have been super interesting to me. It’s how we can get intelligence to environments that are
a. Hard to reach, and thus insulated from the internet in some capacity
b. Require “tiered” intelligence. Less intelligence sometimes, but for much faster decisions.
Inspired by a friend who had to deploy stuff on big boats (a) and didn’t have access to the internet, I decided to make this write-up on a popular such model, and how I estimate it’d perform on an H100 SXM. This article assumes some basic knowledge of Transformers (The Illustrated Transformer, Transformer Inference Arithmetic).
LOCAL?! Inference
When you make a ChatGPT request, your token is actually batched with a bunch of other “sequences” of tokens.

This intuitively changes the math of how things are computed and how long things take.
When you run a model locally for yourself, more often it is one active request.

That’s the case I’ll focus on, and it makes the math a bit easier. The model processes the prompt in prefill, then generates the rest of the response one token at a time in decode, and we must do these two calculations separately.
Prefill
For simplicity, let’s assume a 2048-token prompt (including the system message, formatting, and all that good stuff)
Generally, prefill is going to be “compute-bound” (more time spent doing math than loading weights), and its purpose is to fill up the KV cache for generation to use. Still, for the article’s sake, let’s do the calculation for both memory and compute to develop this intuition of how much longer compute takes.
Also, an important thing to keep in mind is that Qwen3.5-4B is a “hybrid” model.
The model has 32 layers. For 8 of those, we do regular attention. For the other 24, we use “DeltaNet” layers (hence “hybrid”).
For our purposes, the main thing to understand about DeltaNet is how it carries information from earlier tokens. Each of Qwen’s eight attention layers builds a KV cache that grows with the prompt. More tokens generated result in a greater KV cache to pull in.
On the other hand, each of its 24 Gated DeltaNet layers updates a fixed-size memory matrix (along with a small buffer for the sliding window calculation, more to come).

This is great! KV caches usually grow with sequence size, which is generally bad. DeltaNet does not.
Finally, we still have an MLP in every layer, so the regular W_up, W_gate, and W_down matrices apply. I like to think of the MLP (Multilayer Perceptron) as the “classic” neural network component of the model.
Let’s start! First, we’ll calculate the timings behind the easiest part shared by all 32 layers: the MLP.
Each token enters the MLP as a vector of 2560 numbers, or d_model. (2560 is a chosen feature of the model, and most “initial” numbers you see in this article are also chosen features and part of the Qwen architecture). We first normalize this initial vector.
Then, W_up [2560, 9216] projects this vector to a dimension of 9216.
Then we have a second matrix, W_gate. This one also has shape [2560, 9216]. It takes the same original normalized 2560-number vector and produces another 9216-number vector.
So we now have two vectors of length 9216. We apply SiLU, an activation function, to the gate vector only. Then we element-wise multiply the two final vectors.
W_down, with shape [9216, 2560], projects that result back to 2560 numbers. And last, we add this result back to our original vector of size 2560 (residual connection).

So… how much total math is this?
Matrix multiplication (Matmul) of two matrices of dimensions [N, K] by [K, M] is going to take 2 NKM FLOPs (2 is because we have to both multiply and add). Keep this in mind for the rest of the article.
Recall also that we are processing all 2048 tokens together as we’re in the prefill stage. Thus, for W_up this gives us [2048, 2560] x [2560, 9216] = 2 * 2048 * 2560 * 9216 = about 96.64 billion FLOPs.
W_gate has the same dimensions, so we get the same FLOPs. W_down swaps the input and output dimensions, but the total is the same.
And we do this for 32 layers.
So 32 * 3 * 96.64 billion = about 9.3 trillion FLOPs.
Now, we’re going to do what all great lazy engineers like to do, and throw away the parts that don’t matter much for the final timings - Normalization, SiLU, the element-wise multiplication, etc. You can double-check it yourself, but these do not contribute much to the total FLOPs (though they can still affect overall latency somewhat… it’s fine).
Ok, so MLP is done; now the more difficult parts - attention and DeltaNet.
Let’s start with attention.
So Qwen3.5-4B uses what’s called gated grouped-query attention (GQA). In classic transformers, we have the same number of Query, Key, and Value vectors. This is great for simplicity, but it doesn’t map well to GPU hardware. KV caches kill speed, especially as sequence sizes grow. As such, Qwen3.5-4B actually only has 4 key/value heads and 16 query heads. This means 4 query heads attend to the same 1 key head, but we only have to store in memory the values from the 4 K/V heads.
Anyways, let’s look at one token first. We can generalize to 2048 afterward.
The token enters as a 2560-dimensional vector, which we normalize (sound familiar?). We then multiply it by W_k, a matrix of shape [2560, 1024] (a model parameter). This produces 1024 key values that are split into four groups of 256 numbers. These are our 4 key heads; each head is a 256-dimensional vector.
Then, for the values, we do the same thing, but with W_v. We get 4 256-dimensional value vectors in the same way.
Then, for the query-and-gate part, which is sort of interesting and differs from standard attention (where it’s just generating the query).
We take the 2560-dimensional vector and multiply it by the learned query-and-gate matrix of shape [2560, 8192]. This produces 4096 query values and 4096 gate values. We split the query values into 16 query heads of 256 numbers each.
The other 4096 values form the gate, which are also split into 16 vectors of size 256 to match the attention output from the 16 query heads. We apply sigmoid to these gate vectors, putting individual values between 0 and 1. The idea is that attention (softmax of query x keys) tells us how much a certain value matters, but the softmax values must sum up to one (definition of softmax). The gate allows the model to learn, “Hey, this output feature actually doesn’t matter at all at this point”.

Now for the actual attention mechanism. We’ve only been looking at one token, but attention fundamentally involves other tokens. In fact, the whole sequence of past tokens.
Let’s take one of our 16 query heads, a 256-dimensional vector. Its dot product with one token’s 256-dimensional key produces one scalar. For the last token of our 2048-token prompt, comparing it with all positions, including itself, produces 2048 scores. We scale these scores and apply softmax, giving us 2048 attention weights. These are the values that must add up to 1, as mentioned previously. Each weight multiplies every corresponding token’s 256-dimensional value vector. We then add those 2048 weighted results element by element, producing one 256-dimensional output for this query head.
We repeat this for all 16 query heads, giving us 16 outputs of 256 numbers each. We apply sigmoid to the 4096 gate values we calculated earlier, then multiply each of the 4096 attention-output values by its corresponding gate.
We concatenate these gated outputs into one 4096-dimensional vector and multiply it by W_o, which has the dimension of [4096, 2560]. This projects the result back to 2560 dimensions. At the end, we add it to the original, unnormalized input through a residual connection.
This is for one token. During prefill, we do this for all 2048 tokens at the same time (causal masking).
So, in order, for one full-attention layer:
- Produce the query and gate values. [2048, 2560] x [2560, 8192]. That’s 2 * 2048 * 2560 * 8192 = about 85.90 billion FLOPs. This produces 4096 query values and 4096 gate values per token.
- Produce the key values. [2048, 2560] x [2560, 1024]. That’s 2 * 2048 * 2560 * 1024 = about 10.74 billion FLOPs.
- Dot products of queries and keys. For each query head, [2048, 256] x [256, 2048] gives us [2048, 2048] scores. With causal masking, we only need 2048 * 2049 / 2 token pairs. Across 16 query heads, that’s 16 * 2 * 256 * (2048 * 2049 / 2) = about 17.19 billion FLOPs, assuming the implementation skips masked pairs. We then scale and apply softmax to get attention weights.
- Produce the value vectors. [2048, 2560] x [2560, 1024]. That’s 2 * 2048 * 2560 * 1024 = about 10.74 billion FLOPs.
- Multiply the attention weights by the values. For each query head, [2048, 2048] x [2048, 256] gives us [2048, 256]. Counting only the causally allowed contributions across all 16 heads gives us another 17.19 billion FLOPs.
- Apply the gates. Apply sigmoid to the gate values, then element-wise multiply the attention outputs by them. Both have shape [2048, 16, 256]. That’s 2048 * 16 * 256 = about 8.39 million multiplications, plus the sigmoid computation. We already counted producing the gates in step 1.
- The final W_o projection. We concatenate the heads into [2048, 4096], then multiply by W_o. [2048, 4096] x [4096, 2560] gives us [2048, 2560]. That’s 2 * 2048 * 4096 * 2560 = about 42.95 billion FLOPs.
- Adding up the main matrix operations gives us about 184.70 billion FLOPs per attention layer. We have 8 of these layers, so 8 * 184.70 billion = about 1.48 trillion FLOPs.
As I said before, we’re lazy engineers, and ignoring all the normalization, positional encoding, softmax, sigmoid, etc., slop (granted, this stuff is actually very important).
Now for DeltaNet.
I won’t lie, DeltaNet is hard. I started writing out all the math and realized this blog would become way too large. As such, I’m going to include the math at the bottom. For now, though, in short, there are three key things to understand about DeltaNet.
- It starts with a sliding window calculation, mixing info from itself and the 3 previous tokens. This gives it local context. HOWEVER, it also maintains a memory of earlier tokens, which it updates as it reads the sequence.
- Each Gated DeltaNet layer has 32 value heads, each with its own [128, 128] memory matrix per sequence. These matrices are this longer memory of earlier tokens, each head sort of keeping track of what it’s “seen”. Each new token can weaken old information and write new information into this summary. Importantly, these matrices stay the same size even as the sequence gets longer.
- The current token’s query retrieves information from each head’s updated memory (so our token updates it first, then we retrieve). Each head produces 128 numbers, giving us 32 * 128 = 4096 numbers in total. We normalize and gate these outputs, then project them back to 2560 dimensions. (Classic classic- we’ve seen this before). We then add that result to the layer’s input through a residual connection.
As far as math goes, prefill can use a chunking algorithm to group prompt tokens into blocks and turn much of the work into efficient Matmuls, even if the algorithm itself isn’t easily mapped to large matrix multiplication. More on this at the end, where I did end up including the math. I just don’t think it’s overly important for the time estimation and is a bit verbose, so we can skip it here, but feel free to read it now or after!
Anyways, most of the arithmetic still comes from the QKV projections. Per layer, we have the combined QKV projection [2560, 8192], the output-gate projection [2560, 4096], two memory-control projections of [2560, 32] each, and the output projection [4096, 2560]. Across 2048 tokens and 24 layers, that’s 24 * 2 * 2048 * 2560 * (8192 + 4096 + 32 + 32 + 4096) = about 4.14 trillion FLOPs.
I will do another “engineer” thing here and estimate the extra arithmetic using the reference implementation with 64-token chunks. The chunked implementation will add about 0.3 trillion FLOPs for the main DeltaNet operations (notice how it’s a much smaller percent of projections), and then the short convolutions don’t really add much at all, so we have about 4.4 trillion FLOPs for all 24 Gated DeltaNet layers.
RESULTS: FLOPs for PREFILL
Adding it all up, we get 9.28 + 1.48 + 4.40 = about 15.16 trillion FLOPs for our 2048-token prompt. The H100 SXM’s peak dense BF16 throughput is about 989 trillion FLOPs per second, giving us 15.16 / 989 = about 0.0153 seconds, or 15.3 ms. BTW, we use the BF16 throughput because we’re assuming BF16 projection computations. Applying the GPU’s peak rate to the whole FLOP count is a simplification; not every operation runs at that precision or throughput, but it’s ok for a simple estimation.
Anyways, this is a “sunny day” estimate. Actual latency will depend on kernel efficiency, memory traffic, and the precision used by each operation.
Memory for PREFILL
For prefill, we’ll start by estimating the memory traffic from reading the model weights. We’re processing a new prompt, so there’s no KV cache to load. We build that cache as we process the prompt (more to say in the generation section).
As far as the weights go, this includes W_gate, W_up, and W_down in every MLP; the query, key, value, gate, and output projections in attention; and the projections and convolution weights in DeltaNet. For a simple first estimate, we’ll count one read of each required weight. We also move intermediate activations and write the KV caches and DeltaNet states, so weights alone don’t capture all the memory traffic, but we’re going to estimate that away.
For the MLPs, we have 32 * 3 * 2560 * 9216 = about 2.265 billion weights. For the 8 attention layers, we have 8 * 2560 * (8192 + 1024 + 1024 + 4096) = about 0.294 billion weights. For the 24 DeltaNet layers, the projections contribute 24 * 2560 * (8192 + 4096 + 32 + 32 + 4096) = about 1.011 billion weights.
Together, that’s about 3.5 billion weights for these layer operations. Each BF16 weight takes 2 bytes, so reading them once moves about 3.5 billion * 2 = 7 GB. This, again, excludes a bunch of smaller stuff.
The H100 SXM has a published memory bandwidth of 3.35 TB/s, or 3350 GB/s. So our ideal weight-reading time is 7 / 3350 = about 0.00209 seconds, or 2.09 ms. That’s wayyyyy below our 15.3 ms compute estimate, even if we estimated some stuff away.
An interesting part about GPUs doing inference is that, more often than not, memory loading from HBM can be done in parallel with compute. This is why, as I said earlier, for most of our prefill, we’re going to be “compute bound”, meaning FLOPs time is going to outweigh loading time.
So…our ideal prefill estimate is max(t_compute, t_loading) = about 15.3 ms. Actual prefill will take longer, but we can see when we actually benchmark all this.
Decode
Ok, so now prefill is done. We have our KV caches and our DeltaNet states (8 layers’ worth of KV caches and 24 * 32 = 768 DeltaNet memory matrices, plus a small rolling convolution buffer for each of the 24 DeltaNet layers). Now we’re repeatedly (autoregressively) feeding the chosen token back into the model.
During decode, we process just one new token per step. For example, our W_up multiplication goes from [2048, 2560] x [2560, 9216] to [1, 2560] x [2560, 9216]. That’s 2048 times less math for this projection, but we still need the same weight matrix! This is why single-request decode tends to become memory-bound.
So what do the timings look like?
Let’s start with the MLPs again. For one decode step, our three projections across all 32 layers take 32 * 3 * 2 * 1 * 2560 * 9216 = about 4.53 billion FLOPs. Dividing by the H100’s peak BF16 throughput gives 4.53 billion / 989 trillion = about 0.00458 ms. But those matrices still contain about 2.265 billion weights, or 4.53 GB in BF16. Reading them takes 4.53 / 3350 = about 1.35 ms. So our simplified estimate for MLP is max(0.00458, 1.35) = 1.35 ms.
Next, the projections in our 8 full-attention layers. For one token, these take 8 * 2 * 2560 * (8192 + 1024 + 1024 + 4096) = about 0.587 billion FLOPs. Dividing by 989 trillion gives about 0.000594 ms of ideal compute time. The corresponding weights occupy about 0.587 GB in BF16, taking 0.587 / 3350 = about 0.175 ms to read: max(0.000594, 0.175) = 0.175 ms.
But we also have to read the KV values of the previous tokens.
Unlike the projections, reading the KV cache depends on the context length. Let S be the number of tokens already cached before the current decode step. Across our 8 attention layers, we have 8 * S * 4 * 256 * 2 numbers, where the last 2 accounts for both keys and values. Each BF16 number takes 2 bytes, so that’s 32768 * S bytes to read. Assuming each cached entry is read once and shared across its 4 query heads, loading time is (32768 * S) / (3.35 * 10¹²) seconds, or about 0.00000978 * S ms. At S = 2048, that’s about 0.0200 ms.

And as you guessed, as far as math goes, we will be memory-bound for attention too.
Next, the 24 DeltaNet layers. Their projections take 24 * 2 * 2560 * (8192 + 4096 + 32 + 32 + 4096) = about 2.02 billion FLOPs per decode step. Dividing by 989 trillion gives about 0.00204 ms of ideal projection compute time. The corresponding BF16 weights occupy about 2.02 GB, taking 2.02 / 3350 = about 0.603 ms to read. We also have 24 * 32 = 768 memory matrices of shape [128, 128]. Assuming BF16 states for this estimate, reading and writing each matrix once moves 768 * 128 * 128 * 2 * 2 = about 0.0503 GB, adding about 0.0150 ms. So, ignoring the smaller convolution and recurrent-update compute costs, our simplified estimate is max(0.00204, 0.603 + 0.0150) = about 0.618 ms. Unlike full attention, DeltaNet’s per-step work doesn’t grow with S: each new token updates the same fixed-size memory matrices and rolling convolution buffers.
One last step I missed - turning the final 2560 numbers into an actual token. After all 32 layers, we apply a final RMSNorm, then multiply by the vocab projection [2560, 248320]. This gives us one score (logit) per vocabulary entry. Softmax turns these scores into probabilities, from which we select the next token.
For one output position, that’s 2 * 2560 * 248320 = about 1.27 billion FLOPs and 1.27 GB of BF16 weights to read, or about 0.38 ms at our assumed bandwidth. The totals below include this extra 0.38 ms per decode step. We should have done the same with prefill, by the way, but there this part isn’t as important to the final result.
Adding it all up, our ideal decode time per token, including the 32 decoder blocks and vocabulary output projection, is 1.35 + 0.175 + 0.618 + 0.38 + 0.00000978 * S = about 2.523 + 0.00000978 * S ms. At S = 2048, that’s about 2.54 ms per token.
But note that as S grows, we spend more time on each new token.
At S = 65536, our ideal decode time becomes 2.523 + 0.00000978 * 65536 = about 3.16 ms per token.

To grow the cached context from 2048 to 65536 tokens, we need 65536 − 2048 = 63488 decode steps. Since our estimated time per step grows linearly with S, we can use the average context length across those steps: (2048 + 65535) / 2 = 33791.5. Our total decode time, including the vocabulary projection at every step, is therefore 63488 * (2.523 + 0.00000978 * 33791.5) = about 181000 ms, or 181 seconds: about 3.02 minutes!!
These are obviously idealized estimations. Also, we have techniques we can use to speed this up.
- Quantization: Use fewer bits per weight
- Speculative decoding: Try to predict several tokens cheaply with some fast model, then verify them together with the main model.
- Kernel fusion: Lower-level GPU stuff - but we can combine operations to avoid writing and loading intermediate results over and over.
- Efficient attention kernels: Compute attention in blocks, reusing on-chip data and reducing memory traffic.
- And more
Oh, and one thing I forgot to add. The H100 SXM has 80 GB of HBM, and the full Qwen3.5-4B checkpoint contains about 9.32 GB of weight tensors, so we will have no problem loading the full model into one GPU (hence, edge inference).
Anyways, let’s bench it…in a bit
Benchmarks coming in part 2~
DeltaNet Math
As usual, let’s start with one token entering one layer. The token enters as a 2560-sized vector (that we normalize). We multiply it by W_q [2560, 2048], W_k [2560, 2048], and W_v [2560, 4096] (all those dimensions are model-chosen parameters; we usually combine those matrices), and our result is a 2048-sized query vector, a 2048-sized key vector, and a 4096-sized value vector.
The convolution then takes this 8192-sized vector (combined) and mixes it with the corresponding vectors from the previous three tokens (this is the sliding window I mentioned). This gives us a matrix of shape [8192, 4]. We multiply it element-wise by a learned weight matrix of shape [8192, 4], then sum each row’s four results (can’t really do this with Matmul). This produces one 8192-dimensional vector.
This takes about 2 * 4 * 8192 = 65536 FLOPs per token (barely anything…). We then apply SiLU to the resulting 8192-dimensional vector.
Now we split that result back into 2048 query numbers, 2048 key numbers, and 4096 value numbers. We reshape these into 16 query heads, 16 key heads, and 32 value heads, each containing 128 numbers. We normalize each query and key vector (notice that each query/key pair is shared by two value heads).
We then create two “gates” for each head: one controls how much OLD memory to retain (so existing values in this memory matrix), and the other controls how strongly to write the NEW information. We get these from the original normalized 2560-dimensional vector, using two separate matrices of shape [2560, 32].
Each produces a 32-value vector (one per head). We apply sigmoid to the write-gate numbers, and a softplus-and-exponential transformation to the retention-gate numbers, giving us factors between 0 and 1. These two calculations take 2 * 2 * 2560 * 32 = 327680 FLOPs per token across all 32 heads.
Ok, so two 32-value vectors now, right?
Well, let’s focus on one of the 32 value heads; it has its own 128-dimensional value vector and a [128, 128] memory matrix carried forward from the previous token (there is one of these per head), plus a 128-dimensional query and key shared with one other value head.
We multiply every entry in our head’s [128, 128] memory matrix by its “retention factor” (a scalar, one of the 32 numbers from the retention vector). This takes 128 * 128 = 16384 multiplications.
Next, we multiply the current token’s/head’s key [1, 128] by the memory matrix [128, 128], after applying the retention factor. This gives us 128 numbers. We subtract those numbers from the current token’s 128-dimensional value vector, element by element. This matrix multiplication takes about 2 * 128 * 128 = 32768 FLOPs, followed by 128 subtractions.
Then we reshape the head’s key into [128, 1] and multiply it by the write-gate scalar, and then by those 128 differences, arranged as [1, 128], producing a [128, 128] update matrix. We add this update to the memory matrix we already multiplied by the retention factor, taking another 128 * 128 = 16384 operations, and in total we did 32768 operations for the outer product and addition combined (and 128 for the write-gate).
Now we use this head’s query to read from the updated memory. We scale the already-normalized query by 1 / √128, then multiply it [1, 128] by the updated memory matrix [128, 128], producing 128 output numbers. This matrix multiplication takes about 2 * 128 * 128 = 32768 FLOPs. We do this for all 32 value heads, giving us 32 * 128 = 4096 output numbers for the current token.
Next, we apply RMSNorm to each head’s 128 output numbers (separately) and apply an output gate. We calculate this gate from the token’s original normalized 2560-dimensional vector using ANOTHER separate projection [2560, 4096], taking 2 * 2560 * 4096 = 20971520 FLOPs. We apply SiLU to those 4096 gate numbers, then multiply them element-wise by the 4096 numbers we got from the combined RMSNorm calculations. Finally, we multiply the combined result [1, 4096] by W_out [4096, 2560], taking another 20971520 FLOPs and producing a 2560-dimensional vector. We add this to the layer’s original, unnormalized input through the residual connection, and the MLP follows. FINALLY! BTW - you might have noticed there is a lot of “fragmentation” in this math. As in, it’s hard to map this to big Matmuls. You’d be correct. In modern implementations, this is done with chunking algorithms that find ways to do this efficiently with Matmuls somehow.