|
| 1 | +# Qwen3.5-0.8B on AWS FPGA (F2) |
| 2 | + |
| 3 | +Running batch-1 decode of `Qwen/Qwen3.5-0.8B` (text only) on an AWS F2 instance, |
| 4 | +which carries an AMD Virtex UltraScale+ HBM part (VU47P) with 16 GiB of HBM. |
| 5 | +The work covers a bit-exact software model, an HLS hardware datapath verified |
| 6 | +against it, a cycle-accurate HBM bandwidth study, a real Vitis synthesis pass on |
| 7 | +the actual chip, and a full end to end validation of the FPGA delivery path on |
| 8 | +real silicon. |
| 9 | + |
| 10 | +## The core idea |
| 11 | + |
| 12 | +Single token decode reads every weight from memory once, does one multiply with |
| 13 | +it, and moves on. It is limited by memory bandwidth, not by math. So the token |
| 14 | +rate is set by one equation: |
| 15 | + |
| 16 | +``` |
| 17 | +tokens per second = HBM bandwidth / bytes read per token |
| 18 | +``` |
| 19 | + |
| 20 | +For this model at int8 that is about 0.7586 GB per token. This is why the whole |
| 21 | +project centers on HBM bandwidth, and why the hybrid architecture of this model |
| 22 | +matters: only 6 of its 24 layers keep a KV cache, so its per token memory traffic |
| 23 | +stays flat as context grows, unlike a dense model whose KV cache eventually |
| 24 | +dominates. |
| 25 | + |
| 26 | +## Model shape |
| 27 | + |
| 28 | +Qwen3.5-0.8B, text path only (the vision tower and the MTP head are dropped). |
| 29 | +24 layers in the pattern `[linear_attention x3, full_attention] x6`. |
| 30 | + |
| 31 | +- **Gated DeltaNet layers (18 of 24):** 16 heads, key and value head dim 128, a |
| 32 | + short causal conv of width 4, no growing KV cache. The recurrent state is |
| 33 | + 16 x 128 x 128 x 18 layers = 18 MB in fp32, constant regardless of context |
| 34 | + length. That fits in the VU47P on-chip URAM, so it never touches HBM. |
| 35 | +- **Full attention layers (6 of 24):** 8 query heads, 2 key/value heads, head |
| 36 | + dim 256, QK-norm, partial rotary on the first 64 of 256 dims, and an output |
| 37 | + gate. KV traffic is 6 KB per token, against 56 KB per token for a comparable |
| 38 | + dense model. |
| 39 | +- hidden size 1024, vocab 248320, tied embedding and output head. |
| 40 | + |
| 41 | +## Repository layout |
| 42 | + |
| 43 | +``` |
| 44 | +golden/ pure-numpy fp32 and int8 decode, checked against HF transformers |
| 45 | +hls/ HLS C++ kernels and testbenches, plus the Vitis wrapper and configs |
| 46 | +sim/ DRAMsim3 HBM bandwidth harness driven by the real weight layout |
| 47 | +scripts/ weight packing, offset generation, prompt batch, eval |
| 48 | +docs/ architecture, bandwidth study, synthesis estimate, silicon validation |
| 49 | +artifacts/ generated outputs and the real Vitis reports |
| 50 | +``` |
| 51 | + |
| 52 | +## Results |
| 53 | + |
| 54 | +### 1. The software model matches PyTorch |
| 55 | + |
| 56 | +`golden/` is a from-scratch numpy decode transcribed from the Qwen3.5 modeling |
| 57 | +code and checked step by step against Hugging Face transformers. |
| 58 | + |
| 59 | +``` |
| 60 | +worst hidden deviation 1.9e-06 |
| 61 | +worst logit deviation 7.5e-06 |
| 62 | +result: numpy model matches torch |
| 63 | +``` |
| 64 | + |
| 65 | +Two things this caught, both of which would have produced fluent nonsense in |
| 66 | +hardware without an error: the norm uses `(1 + weight)` because the checkpoint |
| 67 | +stores gamma centered at zero, and the DeltaNet gated norm does not, so the two |
| 68 | +norms in the model use different conventions. And the recurrent form of the delta |
| 69 | +rule matches the chunked form to 7.6e-06, so the hardware needs a single |
| 70 | +recurrent datapath, not a separate prefill kernel. |
| 71 | + |
| 72 | +### 2. int8 holds up, and int8 activations make it fit |
| 73 | + |
| 74 | +Per output channel int8 weights, checked against fp32 over several prompts: |
| 75 | + |
| 76 | +| datapath | top-1 agreement | mean KL | bytes per token | |
| 77 | +|---|---|---|---| |
| 78 | +| int8 weights, fp32 activations | 97.9% | 1.2e-3 | 758 MB | |
| 79 | +| int8 weights and int8 activations | 94.8% | 1.2e-2 | 758 MB | |
| 80 | + |
| 81 | +int8 activations cost a few points of agreement but drop the compute cost by |
| 82 | +roughly 10x, because the multiply becomes int8 by int8. Quantizing the tied |
| 83 | +embedding and output head is free on quality and halves the per token bytes, so |
| 84 | +it is done. |
| 85 | + |
| 86 | +### 3. The full HLS decode matches the model |
| 87 | + |
| 88 | +Every operation is written as synthesizable HLS C++ and composed end to end: |
| 89 | +embedding, both norms, the int8 GEMV weight streamer, the causal conv, the gated |
| 90 | +delta rule, partial rotary, gated attention, SwiGLU, and the tied head. Checked |
| 91 | +against the int8 software model over 11 decode steps: |
| 92 | + |
| 93 | +``` |
| 94 | +argmax matches at all 11 steps |
| 95 | +step-0 logits agree to 6.6e-05 (fp summation order only) |
| 96 | +result: HLS decode matches the software model |
| 97 | +``` |
| 98 | + |
| 99 | +The DeltaNet kernel also has a standalone check on a warm state, where the output |
| 100 | +matches to 3.7e-09 and the evolved recurrent state to 6.0e-08 over 262,144 |
| 101 | +elements. |
| 102 | + |
| 103 | +### 4. HBM bandwidth study (cycle accurate model) |
| 104 | + |
| 105 | +Since real silicon was not yet in hand for this step, the memory system was |
| 106 | +modeled with DRAMsim3 using an HBM2 config matched to the F2 device on both |
| 107 | +capacity and bandwidth, driven by the actual 758 MB weight layout in the order |
| 108 | +the decoder reads it. Two design choices decide everything: |
| 109 | + |
| 110 | +| layout | shallow queue | deep queue | |
| 111 | +|---|---|---| |
| 112 | +| naive sequential | 88 tok/s | 391 tok/s | |
| 113 | +| channel interleaved | 221 tok/s | 559 tok/s | |
| 114 | + |
| 115 | +The channel interleaved layout with a deep outstanding read queue reaches 424 |
| 116 | +GB/s, which is 92 percent of the 460 GB/s device peak, at a 96.8 percent row |
| 117 | +buffer hit rate. This becomes two concrete hardware requirements: stripe the |
| 118 | +weights across HBM pseudo-channels, and keep a few hundred reads in flight. |
| 119 | + |
| 120 | +### 5. Real Vitis synthesis on the VU47P |
| 121 | + |
| 122 | +An HBM bandwidth kernel and the model kernels were run through real Vitis |
| 123 | +synthesis and place and route on the actual chip. Genuine numbers from the tools: |
| 124 | + |
| 125 | +- The int8 GEMV closes timing at 342 MHz and uses about 3 percent of the DSPs. |
| 126 | +- The DeltaNet head as first written failed timing at 66 MHz because a 128 wide |
| 127 | + fp32 reduction was forced into one cycle. Tiling the value dimension so the |
| 128 | + tool can register the arithmetic fixes this, verified in the software checks. |
| 129 | +- The 32 channel HBM bandwidth kernel mapped to the real part at about 39,400 |
| 130 | + LUTs (3.2 percent), 272 BRAM (14 percent), 4 DSP, 0 URAM. |
| 131 | + |
| 132 | +These reports are saved under `artifacts/vitis_reports/`. |
| 133 | + |
| 134 | +### 6. The FPGA delivery path works on real silicon |
| 135 | + |
| 136 | +The Vitis flow cannot create an AFI on F2 today, so the route to silicon is the |
| 137 | +HDK / RTL flow. The full chain was validated on real hardware with small designs |
| 138 | +before committing to the model RTL. Build to checkpoint, checkpoint to AFI, AFI |
| 139 | +loaded onto an f2, and host driving the logic and reading correct results back, |
| 140 | +all confirmed. Details in `docs/silicon_validation.md`. |
| 141 | + |
| 142 | +### 7. Measured HBM bandwidth on real silicon |
| 143 | + |
| 144 | +The AWS HBM bandwidth benchmark `cl_mem_perf` was built through the HDK flow, |
| 145 | +turned into an AFI, and run on a real f2.6xlarge. It closed timing at 450 MHz on |
| 146 | +all 32 HBM channels. Measured on the chip: |
| 147 | + |
| 148 | +``` |
| 149 | +read bandwidth 426.14 GB/s |
| 150 | +write bandwidth 433.69 GB/s |
| 151 | +read latency 268 ns |
| 152 | +``` |
| 153 | + |
| 154 | +Read is 92.6 percent of the 460 GB/s device peak. Decode reads the weights, so |
| 155 | +the read number is the one that sets the token rate: |
| 156 | + |
| 157 | +``` |
| 158 | +426.14 GB/s / 0.7586 GB per token = 562 tokens per second |
| 159 | +``` |
| 160 | + |
| 161 | +The DRAMsim3 model in section 4 predicted 424 GB/s and 559 tokens per second. |
| 162 | +The real chip measured 426 GB/s and 562 tokens per second. The model was right, |
| 163 | +now confirmed on silicon. Full output is in |
| 164 | +`artifacts/vitis_reports/f2_hbm_bandwidth.txt`. |
| 165 | + |
| 166 | +### 8. Generations |
| 167 | + |
| 168 | +Full prompts, complete answers, token counts, and timings are in |
| 169 | +`artifacts/generations.md`. The text comes from the verified int8 model path, the |
| 170 | +same arithmetic the hardware datapath uses. The token rate the hardware sustains |
| 171 | +comes from the measured HBM bandwidth above, not from these CPU timings. |
| 172 | + |
| 173 | +## Reproduce |
| 174 | + |
| 175 | +Everything except the FPGA build runs on a laptop. |
| 176 | + |
| 177 | +```bash |
| 178 | +python3 -m venv .venv && ./.venv/bin/pip install torch transformers safetensors numpy |
| 179 | + |
| 180 | +./.venv/bin/python golden/dump_torch_ref.py # torch reference |
| 181 | +python3 golden/verify.py # numpy vs torch |
| 182 | +python3 golden/eval_quant.py # int8 quality |
| 183 | +python3 scripts/pack_weights.py --check # pack int8 weights for HBM |
| 184 | +make -C hls csim_decode # full HLS decode vs model |
| 185 | +python3 scripts/run_batch.py # prompt batch and timings |
| 186 | + |
| 187 | +# HBM bandwidth model (builds DRAMsim3 once) |
| 188 | +cmake -S third_party/DRAMsim3 -B third_party/DRAMsim3/build -DCMAKE_POLICY_VERSION_MINIMUM=3.5 |
| 189 | +make -C third_party/DRAMsim3/build dramsim3 |
| 190 | +python3 scripts/emit_access.py && make -C sim && ./run_all.sh hbm |
| 191 | +``` |
| 192 | + |
| 193 | +The FPGA build and silicon steps are in `docs/silicon_validation.md` and |
| 194 | +`docs/build_afi.md`. |
| 195 | + |
| 196 | +## Status and what is next |
| 197 | + |
| 198 | +Done: the software model, the HLS datapath, the bandwidth study, a real Vitis |
| 199 | +synthesis pass, and full validation of the silicon delivery path on real F2 |
| 200 | +hardware. Next is the actual model on chip, the Qwen decode datapath written in |
| 201 | +RTL on top of the proven HBM template, so the FPGA generates tokens directly. |