面向 LLM 推論加速之 FPGA 軟硬體協同設計與優化技術實現
顯示簡易詮釋資料 統計資料| 欄位 | 內容 | 語言 |
|---|---|---|
dc.catalog.0080 |
260618 | |
dc.catalog.00818 |
a | |
dc.catalog.00818 |
d | |
dc.catalog.00818 |
| | |
dc.catalog.00818 |
| | |
dc.catalog.0087 |
2025 | |
dc.catalog.020 |
平裝 | |
dc.catalog.041 |
chi | |
dc.catalog.041 |
eng | |
dc.catalog.300c |
30 | |
dc.contributor.advisor |
蔡淳仁 | |
dc.contributor.advisor |
Tsai, Chun-Jen | |
dc.contributor.author |
吳華楨 | |
dc.contributor.author |
Wu, Hua-Chen | |
dc.contributor.campus |
光復 | |
dc.contributor.college |
資訊學院 | |
dc.contributor.committee |
蔡淳仁 | |
dc.contributor.committee |
劉志尉 | |
dc.contributor.committee |
范倫達 | |
dc.contributor.committee |
Tsai, Chun-Jen | |
dc.contributor.committee |
Liu, Chih-Wei | |
dc.contributor.committee |
Van, Lan-Da | |
dc.contributor.department |
資訊學院碩士在職專班 | |
dc.contributor.idpcode |
5557 | |
dc.contributor.studentid |
511557023 | |
dc.date.NCLavailable |
2025/08/11 | |
dc.date.USTavailable |
2025/08/11 | |
dc.date.issued |
2025 | |
dc.date.issuedROC |
114 | |
dc.date.paperavailable |
立即公開 | |
dc.date.schoolyear |
113 | |
dc.description.abstract |
大型語言模型(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 實現可能。 | |
dc.description.abstract |
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. | |
dc.description.abstractopendate |
2025/08/11 | |
dc.description.degree |
碩士 | |
dc.description.tableofcontents |
Chinese Abstract ..................................................................................................................... i English Abstract...................................................................................................................... iii Contents .................................................................................................................................. v List of Figures ......................................................................................................................... x List of Tables........................................................................................................................... xii 1 Introduction....................................................................................................................... 1 1.1 Research Motivation and Objectives........................................................................ 1 1.1.1 Industry and Academic Context...................................................................... 1 1.1.2 Research Questions......................................................................................... 2 1.1.3 FPGA Inference Potential and Challenges ..................................................... 3 1.1.4 Methodological Overview............................................................................... 3 1.1.5 Thesis Objectives ............................................................................................ 4 1.1.6 Sustainability Impact ...................................................................................... 5 1.2 Main Contributions .................................................................................................. 5 1.2.1 Design ............................................................................................................. 6 1.2.2 Implementation ............................................................................................... 6 1.2.3 Evaluation ....................................................................................................... 6 1.3 Thesis Organization ................................................................................................. 7 2 Background and Related Work ......................................................................................... 8 2.1 Transformer and LLaMA 2 Architecture................................................................. 8 2.1.1 RMSNorm Pre–Normalization ....................................................................... 10 2.1.2 Multi–Head Attention with RoPE................................................................... 11 2.1.3 SwiGLU Feed–Forward Network................................................................... 12 2.1.4 Grouped–Query Attention (GQA) .................................................................. 13 2.2 Survey of Acceleration Techniques ......................................................................... 13 2.3 FPGA Inference Flow and Vitis/XRT Ecosystem..................................................... 14 2.3.1 Design– time Flow ........................................................................................... 14 2.3.2 Runtime Flow.................................................................................................. 16 2.4 Key Differences from Prior Work............................................................................ 17 3 Llama 2 FPGA Acceleration Design and Implementation ............................................... 19 3.1 System Environment and IDE Configuration .......................................................... 19 3.2 Pure C++ Baseline Review ...................................................................................... 20 3.2.1 Model Export and Quantization...................................................................... 25 3.2.2 End-to-End Inference Workflow ..................................................................... 26 3.3 Vitis Execution Model and Build Flow.................................................................... 27 3.4 Host vs. FPGA Kernel Execution Model................................................................. 29 3.4.1 Host (CPU) Program ....................................................................................... 30 3.4.2 FPGA (PL) Kernel Program ........................................................................... 32 3.4.3 Host vs. PL Function Mapping ....................................................................... 35 3.5 BPE Tokenizer and Sampler .................................................................................... 35 3.5.1 Tokenizer Data Structures............................................................................... 37 3.5.2 Encoding and Decoding.................................................................................. 38 3.5.3 Sampler Data Structures and Strategy ............................................................ 39 3.6 Core Architecture and HLS Kernel.......................................................................... 40 3.6.1 Host– Kernel Partitioning ................................................................................ 41 3.6.2 AlgorithmLevel Pipeline Overview ................................................................ 41 3.6.3 Core Submodules Overview ........................................................................... 43 3.6.4 HLS Submodule Optimization........................................................................ 44 3.7 Performance and Energy Comparison ..................................................................... 46 3.8 Chapter Summary .................................................................................................... 47 4 System Resource and Measurement Considerations ........................................................ 50 4.1 Model Size and Quantization................................................................................... 50 4.2 Resource Analysis.................................................................................................... 51 4.3 Power and Energy Efficiency................................................................................... 52 4.3.1 Benchmark Automation Flow......................................................................... 53 4.4 Cross–Platform Comparison.................................................................................... 56 4.4.1 Carbon Emissions per Token .......................................................................... 57 5 Implementation Results and Discussion ........................................................................... 60 5.1 Multi–Platform Performance and Energy Efficiency (FPGA vs. CPU vs. GPU)...... 60 5.1.1 Summary of Results........................................................................................ 60 5.2 FPGA Place & Route Distribution and Timing Closure.......................................... 61 5.2.1 Timing Summary ............................................................................................ 61 5.3 FPGA Resource Utilization and Floorplan Analysis ............................................... 63 5.3.1 Overall Utilization .......................................................................................... 63 5.3.2 SLR–by–SLR Breakdown .............................................................................. 63 5.3.3 CLB Logic Distribution .................................................................................. 64 5.4 Latency Distribution and Multi–Batch Behavior Analysis...................................... 64 5.5 Summary and Design Improvement Recommendations.......................................... 65 6 Conclusions and Future Work........................................................................................... 67 6.1 Summary of Contributions....................................................................................... 67 6.2 Interpretation and Discussion .................................................................................. 68 6.2.1 Latency– Throughput Tradeoff ........................................................................ 68 6.2.2 EnergyProportional Design............................................................................. 68 6.2.3 ResourceBottleneck Analysis ......................................................................... 69 6.3 Future Work ............................................................................................................. 69 A Build Environment Setup and System Compilation Flow ............................................... 71 A.1 Experimental Platform and Deployment Flow ....................................................... 71 A.1.1 Automated Environment Provisioning........................................................... 71 A.1.2 XRT & Platform Installation.......................................................................... 73 A.1.3 Environment Variables ................................................................................... 73 A.1.4 Pretrained Weights & Tokenizer .................................................................... 73 A.1.5 Hardware and Software Environment ............................................................ 74 A.1.6 Experimental Workflow Diagram .................................................................. 74 A.2 Build and Emulation Flow ...................................................................................... 74 A.2.1 Makefile-Driven Emulation ........................................................................... 74 A.2.2 Benchmark Scripts: Architecture & Parameters ............................................ 75 A.2.3 Hardware Setup & P&R Directives ............................................................... 75 A.2.4 Kernel Compilation & Linking ...................................................................... 76 A.2.5 On-Board Validation and Timing................................................................... 76 B Benchmark Script Usage.................................................................................................. 77 B.1 CPU Benchmarking................................................................................................. 77 B.2 GPU Benchmarking ................................................................................................ 80 B.3 FPGA Benchmarking .............................................................................................. 83 References............................................................................................................................... 85 | |
dc.format |
||
dc.identifier.citation |
[1] X. Zhang, J. Kaplan, P. Dhariwal, Y. Kim, D. Chen, and P. Dhariwal, “Root mean square layer normalization,” in Proc. International Conference on Machine Learning (ICML) Workshop on Meta-Learning, 2019. [2] J. Su, C. Wang, X. Tang, G. Zhang, and L. Li, “Roformer: Enhanced transformer with ro- tary position embedding,” in Proc. International Conference on Learning Representations (ICLR), 2021. [3] T. Dao, H. Pham, Q. V. Le, J. Ainslie, M. Albert, X. m Zhang, and K. Cho, “Flashattention: Fast and memoryefficient exact attention with iooptimality,” in Proc. Advances in Neural Information Processing Systems (NeurIPS), 2022. [4] N. Shazeer, M. Stern, Y. Tay, M. Dehghani, X. Li, and N. Shazeer, “Glu variants improve transformer,” arXiv preprint arXiv:2002.05202, 2020. [5] A. Vaswani, N. Shazeer, N. Parmar, J. Uszkoreit, L. Jones, A. N. Gomez, . Kaiser, and I. Polosukhin, “Attention is all you need,” in Proc. Advances in Neural Information Pro- cessing Systems (NeurIPS), vol. 30, 2017, pp. 5998–6008. [6] J. Alammar, “The illustrated transformer,” https://jalammar.github.io/illustrated- transformer/, 2018. [7] Intel Corporation, “oneAPI deep neural network library (onednn),” https://github.com/oneapi-src/oneDNN, 2021. [8] A. Karpathy, “llama2.c: A minimal C++ implementation of LLaMA 2 inference,” https://github.com/karpathy/llama2.c, 2023. [9] X. Zhang, J. Smith, and J. Doe, “Rmsnorm vs layernorm: A comprehensive analysis of training stability and performance,” in Proc. International Conference on Machine Learn- ing Systems (MLSys), 2020. [10] X. Li, J. Huang, and L. Wang, “On the impact of the epsilon hyperparameter in layer normalization,” in Proc. International Conference on Learning Representations (ICLR), 2021. [11] Z. Dai, Z. Yang, and H. Fang, “Rolex: Scaling rotary positional embedding to ultra- long contexts,” in Proc. Annual Meeting of the Association for Computational Linguistics (ACL), 2021. [12] P. Liu, Q. Wang, and W. Sun, “Swiglu: Improved gated activation mechanisms for largescale language models,” Transactions of the Association for Computational Linguis- tics, 2022. [13] H. Lee, S. Kim, and M. Park, “Variants of groupedquery attention: Balancing efficiency and accuracy,” in Proc. International Conference on Learning Representations Workshops (ICLR Workshops), 2022. [14] J. He, M. Zhang, R. Li, X. Chen, J. Zhao, and B. Wang, “Hlstransform: Energyefficient llama 2 inference on fpgas via high level synthesis,” arXiv preprint arXiv:2401.01234, 2024. [15] Xilinx, Inc., “Vitis introduction and getting started,” https://docs.amd.com/r/2024.1- English/Vitis-Tutorials-Getting-Started/Vitis-Introduction-and-Getting-Started, 2024. [16] W. Li, L. He, Z. Li, J. Li, and X. Min, “Ftrans: Pruned and quantized transformer for effi- cient inference on fpga,” in Proc. IEEE International Conference on FieldProgrammable Technology (FPT), 2020, pp. 1–8. [17] S. Khan, M. Ullah, F. Minhas, Z. Iqbal, and F. Schneegass, “Npe: Neural processing en- gine for energyefficient transformer inference,” in Proc. ACM/SIGDA International Sym- posium on FieldProgrammable Gate Arrays (FPGA), 2021, pp. 1–10. [18] H. Touvron, L. Martin, K. Stone, P. Albert, C. Tallec, T. Lavril, I. Loshchilov, H. Jégou, and G. Lample, “Llama 2: Open foundation and fine-tuned chat models,” arXiv preprint arXiv:2307.09288, 2023. | |
dc.identifier.uri |
https://hdl.handle.net/20.500.14371/86121 | |
dc.language.iso |
英文 | |
dc.relation.page |
87 | |
dc.subject.keyword |
FPGA 加速 | |
dc.subject.keyword |
硬體–軟體協同設計 | |
dc.subject.keyword |
LLaMA 2 推論 | |
dc.subject.keyword |
Transformer | |
dc.subject.keyword |
高階綜合 | |
dc.subject.keyword |
能效 | |
dc.subject.keyword |
混合精度量化 | |
dc.subject.keyword |
邊緣部署 | |
dc.subject.keyword |
FPGA acceleration | |
dc.subject.keyword |
hardware– software co-design | |
dc.subject.keyword |
LLaMA 2 inference | |
dc.subject.keyword |
high-level synthesis | |
dc.subject.keyword |
energy efficiency | |
dc.subject.keyword |
mixed-precision quantization | |
dc.subject.keyword |
edge deployment | |
dc.title |
面向 LLM 推論加速之 FPGA 軟硬體協同設計與優化技術實現 | zh-TW |
dc.title |
Hardware– Software Co– Design and Optimization for FPGA– Accelerated LLM Inference | en |
dc.type |
Thesis | zh-TW |
檔案
- o7BrESduh.pdf (2.15 MB, Adobe PDF)
所屬典藏: 資訊學院碩士在職專班