面向 LLM 推論加速之 FPGA 軟硬體協同設計與優化技術實現
Hardware– Software Co– Design and Optimization for FPGA– Accelerated LLM Inference
摘要
大型語言模型(LLM),如 LLaMA 2,在自然語言理解與生成任務上展現了最先進
的性能,但其大規模部署卻因龐大的計算與功耗需求而受限。本論文首先針對純 C 實
現的 LLaMA 2 引擎提出一套詳細的性能剖析方法,發現高維度矩陣相乘與量化迴圈佔
據超過 70% 的運行時間;基於此洞察,我們設計了一個硬體–軟體協同設計框架,將控
制、I/O 與 BPE 分詞留在主機 CPU 上執行,同時將包含 RMSNorm、旋轉式位置編碼、
多頭注意力、softmax、SwiGLU 及分類投影等運算的推論流水線卸載至 FPGA;接
著,我們引入一套 HLS 優化策略,結合本地緩衝、ARRAY_PARTITION、PIPELINE
與 UNROLL pragma,以及雙緩衝 PCIe 傳輸,實現單週期啟動間隔並重疊資料傳輸與計
算;作為概念驗證,本研究於 Xilinx Alveo U200 上實現一端到端加速器,採用片上 KV
快取支援序列長度 256,並相較 CPU 基準實現達到約 2.5× 的端到端吞吐量提升;全面
跨平臺評測顯示 FPGA 可達 22.3 令牌/秒 @ 25.4 W(0.88 令牌/秒·W−1),其能效較
Intel Core i7-1185G7 CPU(約 9 令牌/秒 @ 28 W)高約 2.8×,但較 NVIDIA RTX 3060
GPU(5.35 令牌/秒·W−1)約低 6×;在 INT8 量化下,整體準確度損失小於 1%,單
令牌中位延遲約 4.3 ms(99% 線上約 6.0 ms),在批次大小為 8 時降至約 1.1 ms/令牌
且抖動極小;最後,後佈線時序與資源分析顯示:在 98 MHz 時脈約束下,最差負時
序裕度為 +0.08 ns;DSP 使用率約 79%(LUT 約 35%);LUT/BRAM 均仍保有大量餘
裕,暗示未來可進行混合精度與基於 LUT 的優化。本設計特別適用於實時邊緣推論,
並已開源所有程式碼、Dockerfiles 及腳本(Vitis 2023.2、XRT 2023.2),以確保
結果可完全復現;未來方向包括將混合精度運算映射至 LUT、加深資料流流水線、透
過 HBM/DDR 階層式快取實現片上緩衝、自適應批次調度,以及遷移至 Versal ACAP,
以在維持能源優勢的同時進一步縮小與 GPU 的效能差距。
此外,若以本 FPGA 驗證的 LLM 架構為藍本,並透過與 RTX 3060 相同之 8 nm
CMOS 製程進行電路級實現,可望實現約 10 倍的能效提升——從而開啟超越現有 GPU
的 ASIC 實現可能。
的性能,但其大規模部署卻因龐大的計算與功耗需求而受限。本論文首先針對純 C 實
現的 LLaMA 2 引擎提出一套詳細的性能剖析方法,發現高維度矩陣相乘與量化迴圈佔
據超過 70% 的運行時間;基於此洞察,我們設計了一個硬體–軟體協同設計框架,將控
制、I/O 與 BPE 分詞留在主機 CPU 上執行,同時將包含 RMSNorm、旋轉式位置編碼、
多頭注意力、softmax、SwiGLU 及分類投影等運算的推論流水線卸載至 FPGA;接
著,我們引入一套 HLS 優化策略,結合本地緩衝、ARRAY_PARTITION、PIPELINE
與 UNROLL pragma,以及雙緩衝 PCIe 傳輸,實現單週期啟動間隔並重疊資料傳輸與計
算;作為概念驗證,本研究於 Xilinx Alveo U200 上實現一端到端加速器,採用片上 KV
快取支援序列長度 256,並相較 CPU 基準實現達到約 2.5× 的端到端吞吐量提升;全面
跨平臺評測顯示 FPGA 可達 22.3 令牌/秒 @ 25.4 W(0.88 令牌/秒·W−1),其能效較
Intel Core i7-1185G7 CPU(約 9 令牌/秒 @ 28 W)高約 2.8×,但較 NVIDIA RTX 3060
GPU(5.35 令牌/秒·W−1)約低 6×;在 INT8 量化下,整體準確度損失小於 1%,單
令牌中位延遲約 4.3 ms(99% 線上約 6.0 ms),在批次大小為 8 時降至約 1.1 ms/令牌
且抖動極小;最後,後佈線時序與資源分析顯示:在 98 MHz 時脈約束下,最差負時
序裕度為 +0.08 ns;DSP 使用率約 79%(LUT 約 35%);LUT/BRAM 均仍保有大量餘
裕,暗示未來可進行混合精度與基於 LUT 的優化。本設計特別適用於實時邊緣推論,
並已開源所有程式碼、Dockerfiles 及腳本(Vitis 2023.2、XRT 2023.2),以確保
結果可完全復現;未來方向包括將混合精度運算映射至 LUT、加深資料流流水線、透
過 HBM/DDR 階層式快取實現片上緩衝、自適應批次調度,以及遷移至 Versal ACAP,
以在維持能源優勢的同時進一步縮小與 GPU 的效能差距。
此外,若以本 FPGA 驗證的 LLM 架構為藍本,並透過與 RTX 3060 相同之 8 nm
CMOS 製程進行電路級實現,可望實現約 10 倍的能效提升——從而開啟超越現有 GPU
的 ASIC 實現可能。
Large language models (LLMs) such as LLaMA 2 deliver state-of-the-art performance in
natural language understanding and generation, yet their deployment at scale is hindered by
immense compute and power requirements. In this thesis, we first develop a detailed profiling
methodology for a pure-C LLaMA 2 engine, revealing that high-dimensional matrix multi-
plies and quantization loops consume over 70% of runtime; guided by these insights, we design
a hardware– software co-design framework that retains control, I/O, and BPE tokenization on the
host CPU while offloading the compute-intensive forward pipeline—comprising RMSNorm,
rotary positional encoding, multi-head attention, softmax, SwiGLU, and classifier projection
—to the FPGA; we then introduce an HLS optimization recipe—incorporating local buffering,
ARRAY_PARTITION, PIPELINE, and UNROLL pragmas as well as double-buffered PCIe
transfers—that achieves single-cycle initiation intervals and overlaps data movement with com-
putation; as a proof of concept, we implement an end-to-end accelerator on a Xilinx Alveo
U200 with on-chip KV caching for sequence length 256 and demonstrate an ≈ 2.5× end-to-
end throughput improvement over the CPU baseline; a comprehensive cross-platform evalua-
tion shows 22.3 tokens/s at 25.4 W (0.88 tokens/s W−1)—2.8× more energy-efficient than an
Intel Core i7-1185G7 CPU (≈ 9 tokens/s at 28 W) while trailing an NVIDIA RTX 3060 GPU
(5.35 tokens/s W−1) by ≈ 6×—incurring < 1 % end-to-end accuracy loss under INT8 quan-
tization and achieving a single-token median latency of ≈ 4.3 ms (99th percentile ≈ 6.0 ms)
that drops to ≈ 1.1 ms per token under batch-8 with minimal jitter; finally, post-route timing
and resource analysis reports a +0.08 ns worst-negative slack (post– route, 98 MHz constraint),
79 % DSP utilization (vs. 35 % LUT), and substantial LUT/BRAM headroom—pointing to
future mixed-precision and LUT-based optimizations. Our design is particularly suited for real-
time edge inference, and we open-source all code, Dockerfiles, and scripts (Vitis 2023.2,
XRT 2023.2) to ensure full reproducibility; future directions include mixed-precision arith-
metic mapped to LUTs, deeper dataflow pipelining, on-card buffering via HBM/DDR partitions,
adaptive batching, and migration to Versal ACAP to further narrow the performance gap with
GPUs while preserving energy advantages.
Furthermore, by taking the FPGA-validated LLM architecture as a blueprint and mapping
it through a cell-based 8 nm CMOS design flow (the same process node as the RTX 3060),
one can expect an order-of-magnitude improvement in power efficiency—enabling ASIC im-
plementations that surpass current GPUs.
natural language understanding and generation, yet their deployment at scale is hindered by
immense compute and power requirements. In this thesis, we first develop a detailed profiling
methodology for a pure-C LLaMA 2 engine, revealing that high-dimensional matrix multi-
plies and quantization loops consume over 70% of runtime; guided by these insights, we design
a hardware– software co-design framework that retains control, I/O, and BPE tokenization on the
host CPU while offloading the compute-intensive forward pipeline—comprising RMSNorm,
rotary positional encoding, multi-head attention, softmax, SwiGLU, and classifier projection
—to the FPGA; we then introduce an HLS optimization recipe—incorporating local buffering,
ARRAY_PARTITION, PIPELINE, and UNROLL pragmas as well as double-buffered PCIe
transfers—that achieves single-cycle initiation intervals and overlaps data movement with com-
putation; as a proof of concept, we implement an end-to-end accelerator on a Xilinx Alveo
U200 with on-chip KV caching for sequence length 256 and demonstrate an ≈ 2.5× end-to-
end throughput improvement over the CPU baseline; a comprehensive cross-platform evalua-
tion shows 22.3 tokens/s at 25.4 W (0.88 tokens/s W−1)—2.8× more energy-efficient than an
Intel Core i7-1185G7 CPU (≈ 9 tokens/s at 28 W) while trailing an NVIDIA RTX 3060 GPU
(5.35 tokens/s W−1) by ≈ 6×—incurring < 1 % end-to-end accuracy loss under INT8 quan-
tization and achieving a single-token median latency of ≈ 4.3 ms (99th percentile ≈ 6.0 ms)
that drops to ≈ 1.1 ms per token under batch-8 with minimal jitter; finally, post-route timing
and resource analysis reports a +0.08 ns worst-negative slack (post– route, 98 MHz constraint),
79 % DSP utilization (vs. 35 % LUT), and substantial LUT/BRAM headroom—pointing to
future mixed-precision and LUT-based optimizations. Our design is particularly suited for real-
time edge inference, and we open-source all code, Dockerfiles, and scripts (Vitis 2023.2,
XRT 2023.2) to ensure full reproducibility; future directions include mixed-precision arith-
metic mapped to LUTs, deeper dataflow pipelining, on-card buffering via HBM/DDR partitions,
adaptive batching, and migration to Versal ACAP to further narrow the performance gap with
GPUs while preserving energy advantages.
Furthermore, by taking the FPGA-validated LLM architecture as a blueprint and mapping
it through a cell-based 8 nm CMOS design flow (the same process node as the RTX 3060),
one can expect an order-of-magnitude improvement in power efficiency—enabling ASIC im-
plementations that surpass current GPUs.