Building fast transformer kernels
Elliot Arledge

#LLM
#CUDA
#GPU
#NVIDIA
#AI
⚡ برنامهنویسی CUDA؛ از اولین Kernel تا Flash Attention
💭 «یک راهنمای پایهای برای CUDA، معماری مدرن GPU و کاربردهای آنها در حوزه هوش مصنوعی.»
— برایس ادلستین لِلباخ، NVIDIA
🚀 فرق بین مدلی که استنتاج را در چند میلیثانیه بهجای چند ثانیه انجام میده یا در چند روز بهجای چند هفته آموزش میبینه، به نحوه استفاده از سختافزار برمیگرده. برای بیرون کشیدن تمام توان پردازشی GPUهای NVIDIA باید Kernelهای اختصاصی CUDA نوشته بشن و این کتاب دقیقا همین مهارت را آموزش میده.
الیوت آرلج در ۴۰۸ صفحه مصور و پر از کد، برنامهنویسی مدرن GPU را از اولین Kernel شروع میکنه و تا Flash Attention، هستههای Tensor و آموزش توزیعشده روی چند GPU پیش میبره. مطالب براساس جدیدترین سختافزارهای NVIDIA نوشته شدن، اما اصولی را آموزش میدن که با عوض شدن نسل GPUها همچنان کاربردی باقی میمونن.
🧩 چون برنامهنویسی GPU پیچیده است، کتاب مهارتها را لایهبهلایه و با یک ساختار ششمرحلهای برای بهینهسازی Kernel جلو میبره. کار با نوشتن یک Kernel برای جمع برداری شروع میشه تا مشخص بشه Grid، Block و Thread چطور روی سختافزار واقعی اجرا میشن.
بعد نوبت پیادهسازی نسخههای ابتدایی Kernelهایی میرسه که تقریبا همه مدلهای Deep Learning به آنها وابسته هستن؛ از GEMM و Softmax گرفته تا کانولوشن یکبعدی و دوبعدی، Max Pooling و ترانهاده ماتریس. در ادامه یک شبکه عصبی کامل در شش نسخه ساخته و آموزش داده میشه که هر نسخه از قبلی سریعتره. Backpropagation از پایه استخراج و کدنویسی میشه و کار با cuBLAS بهصورت عملی پیش میره.
🛠️ تمرکز اصلی کتاب روی بهینهسازی واقعی است. یک Transformer برای اجرای Inference به CUDA منتقل میشه و Kernelهای آن یکییکی بهینه میشن. رمزگشای GEMV با Coalesced Access، کاهش در سطح Block و Vectorization سریعتر میشه. Softmax به نسخههای Online، مبتنی بر Warp Shuffle و Fused ارتقا پیدا میکنه. GEMM هم با Thread Remapping، کاشیبندی Shared Memory، Block Tiling یکبعدی و دوبعدی و بارگذاری برداری بهبود داده میشه.
تمام بهینهسازیها Profile میشن و نتیجه هر Profile توضیح داده میشه تا مشخص باشه هر تغییر دقیقا چه اثری روی عملکرد GPU میذاره.
🔥 فصلهای پایانی سراغ فناوریهای پیشرفتهتر میرن. برنامهنویسی هستههای Tensor با مجموعهدستورهای WMMA و WGMMA برای سختافزار Hopper آموزش داده میشه و مشخص میشه چطور جابهجایی داده تحت مدیریت سختافزار میتونه جای Tiling دستی را بگیره.
همچنین Flash Attention با Tiling و Online Softmax پیادهسازی میشه، مدلها Quantize میشن و مقیاسپذیری تا ۱۶ GPU با Tensor Parallelism، Pipeline Parallelism و NCCL بررسی میشه. در پایان، بیش از ۵۰ Kernel قابل اجرا در اختیار خواننده قرار میگیره و دید فنی لازم برای طراحی Kernel روی نسلهای آینده سختافزار شکل میگیره.
• آموزش CUDA و برنامهنویسی GPU از پایه
• اجرای Tensor Parallelism و Pipeline Parallelism روی چند GPU
• ساخت افزونههای ++PyTorch C برای عرضه Kernelهای اختصاصی
۱. وقتی PyTorch دیگر کافی نیست
۲. اولین برنامه CUDA
۳. Kernelهای ابتدایی
۴. آموزش و اعتبارسنجی شبکههای عصبی با CUDA خام
۵. اجرای Inference مدل Transformer در CUDA
۶. بهینهسازی Kernelهای Transformer
۷. هستههای Tensor
۸. Flash Attention
۹. Quantization
۱۰. محاسبات توزیعشده
۱۱. CUTLASS
۱۲. حالا چه کار کنیم؟
ضمیمه A. نصب و راهاندازی
ضمیمه B. اجرای نمونهکدها
ضمیمه C. عیبیابی و روشهای توسعه
ضمیمه D. مرجع CUDA؛ دستورات، سختافزار، Templateها و Bindingها
ضمیمه E. منابع تکمیلی
این کتاب برای مهندسان نرمافزار و هوش مصنوعی آشنا با C یا ++C نوشته شده و برای شروع آن نیازی به تجربه قبلی کار با CUDA نیست.
الیوت آرلج مهندس عملکرد GPU و مدرس برنامهنویسی است. تمام Kernelهای کتاب از روند واقعی کار او در نوشتن، Profile کردن و اعتبارسنجی CUDA روی سختافزارهای مصرفی و دیتاسنتری به دست اومدن. او دوره رایگان ۱۲ ساعته CUDA در freeCodeCamp را ساخته و در زمینه بهینهسازی عملکرد Deep Learning مشاوره میده.
“A fundamental guide for CUDA and modern GPU architecture and their application in the field of AI.”
—Bryce Adelstein Lelbach, NVIDIA
The difference between a model that infers in milliseconds instead of seconds, or one that trains in days instead of weeks, is in how effectively you use your hardware. To squeeze every last teraflop out of an NVIDIA GPU, you need to write your own CUDA kernels, and this book will get you there! This book is a friendly hands-on tour of the modern GPU programming stack, written for the latest NVIDIA hardware, and grounded in fundamentals that will outlast any single generation of chips. In 408 fully-illustrated, code-rich pages, author Elliot Arledge guides you from your first kernel all the way to Flash Attention, tensor cores, and multi-GPU distributed training.
GPU programming is complicated, so this book builds your skills one layer at a time using a six-level kernel optimization stack. You’ll start by writing a vector addition kernel, learning how grids, blocks, and threads map onto real hardware. From there, you’ll implement the naive versions of the kernels every deep learning model depends on: GEMM, softmax, 1D and 2D convolution, max pooling, and matrix transpose. Then you’ll build and train a full neural network in six progressively faster implementations and dig into cuBLAS, with backpropagation derived and coded from scratch.
Throughout this book, your focus will stay on real optimization work. You’ll port a transformer to CUDA inference and attack each kernel in turn. Your GEMV decoder gets coalesced access, block-level reduction, and vectorization. Softmax becomes online, warp-shuffled, and fused. GEMM progresses through thread remapping, shared memory tiling, 1D and 2D blocktiling, and vectorized loads. Every optimization is profiled, and every profile is explained.
The final chapters take you to the frontier. You’ll program tensor cores using the WMMA and WGMMA instruction sets for Hopper hardware and see how manual tiling gets replaced by hardware-managed data movement. You’ll implement Flash Attention with tiling and online softmax, quantize models, and explore scaling up to 16 GPUs with tensor and pipeline parallelism over NCCL. By the end, you’ll have both a portfolio of more than 50 working kernels and the mental models to write new ones for hardware that doesn’t exist yet.
What's inside
• CUDA and GPU programming from the ground up
• Tensor and pipeline parallelism across multiple GPUs
• PyTorch C++ extensions for shipping custom kernels
Table of Contents
1. When PyTorch Just Isn't Enough
2. Your First CUDA Program
3. Naive Kernels
4. Training and Verifying Neural Nets in Raw CUDA
5. Transformer Inference in CUDA
6. Optimizing Our Transformer Kernels
7. Tensor Cores
8. Flash Attention
9. Quantization
10. Distributed Computing
11. CUTLASS
12. Now What?
Appendix A. Installation and Setup
Appendix B. Running the Code Examples
Appendix C. Troubleshooting and Development Practices
Appendix D. CUDA Reference: Commands, Hardware, Templates, and Bindings
Appendix E. Additional Resources
About the reader
For software and AI engineers comfortable with C/C++. No prior CUDA experience required.
About the author
Elliot Arledge is a GPU performance engineer and educator. Every kernel in this book comes from his workflow of writing, profiling, and verifying CUDA on both consumer and datacenter hardware. He created freeCodeCamp’s 12-hour CUDA course and consults on deep learning performance.









