Going Further with CUDA for Python Programmers

  Рет қаралды 10,766

Jeremy Howard

Jeremy Howard

Күн бұрын

This technical talk by Jeremy Howard explores advanced programming techniques for maximizing performance when using CUDA with Python. The focus is on optimizing memory usage with a specific emphasis on effectively leveraging fast shared memory in CUDA. It assumes you have already watched this "Getting Started" video: • Getting Started With C...
The video begins with foundational concepts by comparing shared memory to global memory and demonstrates strategies like tiling to address shared memory capacity limitations. It demonstrates core ideas through a matrix multiplication example.
Jeremy compares pure Python, Python with simulated 'shared memory', Numba, and raw CUDA implementations, using ChatGPT for guided code conversion. While initial Numba-based code may exhibit some overhead, it serves as a fast development pathway compared to raw CUDA.
Resources
The notebook for this lesson is in the "lecture5" folder at: github.com/cuda-mode/lectures. Special thanks to Kerem Turgutlu for help preparing it.
See also this video for more information about GPU memory optimisation: • Lecture 4 Compute and ... .
Timestamps
- 0:00 Introduction to Optimized Matrix Multiplication
- 12:04 Shared Memory Techniques for CUDA
- 20:12 Implementing Shared Memory Optimization in Python
- 42:15 Translating Python to CUDA and Performance Considerations
- 55:55 Numba: Bringing Python and CUDA Together
- 1:11:46 The Future of AI in Coding
Thanks to @wolpumba4099 for initial summary and timestamps.

Пікірлер: 17
@pinchedsquare
@pinchedsquare 4 ай бұрын
@Jeremy love you so so much for being such an amazing educator. I am a professional SWE with 15+ years of experience and I find myself learning from every single video of yours. I am sure I and thousand other are grateful for what you are doing. 🙏
@alinour7488
@alinour7488 4 ай бұрын
Thank you for the Great video! Are there any plans for a practical deeplearning for coders for 2024?
@gfickel
@gfickel 4 ай бұрын
Excelent video! And the explenation of the runtime differences between static and dynamic shared memory at ~ 54:00 was great, specially how to somewhat circumvent it using the template/switch/lambda approach.
@paxdriver
@paxdriver 4 ай бұрын
Very much appreciate the edit for the explanation and demo at ~50:00 👏
@ThanhPham-xz2yo
@ThanhPham-xz2yo 4 ай бұрын
Thanks for sharing!
@RajeevRanjan-hd3no
@RajeevRanjan-hd3no 4 ай бұрын
Thanks for this great video
@rickharold7884
@rickharold7884 4 ай бұрын
Cool
@wolpumba4099
@wolpumba4099 4 ай бұрын
*Abstract* This technical talk explores advanced programming techniques for maximizing performance when using CUDA with Python. The focus is on optimizing memory usage with a specific emphasis on effectively leveraging fast shared memory in CUDA. The video begins with foundational concepts by comparing shared memory to global memory and demonstrates strategies like tiling to address shared memory capacity limitations. It demonstrates core ideas through a matrix multiplication example. The presenter compares pure Python, Python with simulated 'shared memory', Numba, and raw CUDA implementations. While providing code examples, the speaker underscores the value of debugging with simpler models and using Pythonic constructs to simulate CUDA-like concurrency when possible. They also discuss using ChatGPT for guided code conversion. While initial Numba-based code may exhibit some overhead, it serves as a fast development pathway compared to raw CUDA. In its final segment, the video discusses the evolving role of AI in software development, compares approaches like Numba and Triton for CUDA programming, and emphasizes the continued importance of understanding core CUDA concepts even as increasingly sophisticated tools emerge. *Keywords:* CUDA, Python, shared memory, performance optimization, Numba, ChatGPT *Chapter Titles* *I. Introduction to Optimized Matrix Multiplication (**0:00**)* - 0:00 Introduction - 1:03 Understanding Shared Memory vs. Global Memory - 6:21 Pure Python Matrix Multiplication *II. Shared Memory Techniques for CUDA (**12:04**)* - 12:04 Tiling for Shared Memory Optimization - 15:26 CUDA Matrix Multiplication Using Shared Memory - 15:35 Shared Memory for Tiling Optimization - 19:23 Shared Memory vs Views in Python *III. Implementing Shared Memory Optimization in Python (**20:12**)* - 20:12 Python Implementation of Shared Memory Optimization - 30:49 Debugging Tip - 31:08 Code Refactoring - 31:35 Managing Concurrent Threads - 34:40 CUDA Execution Model - 35:14 Python Threading (Simulating CUDA) - 38:27 Final Kernel Runner (Python) *IV. Translating Python to CUDA and Performance Considerations (**42:15**)* - 42:15 ChatGPT: Automated Python to CUDA conversion - 42:47 CUDA-Specific Syntax - 45:11 CUDA Code Structure & Shared Memory - 46:53 CUDA Execution, Compilation, and the Mystery of Dynamic Shared Memory *V. Numba: Bringing Python and CUDA Together (**55:55**)* - 55:55 Introducing Numba for Python-Based CUDA - 59:26 Advantages of Using Numba - 1:00:38 Numba's CUDA Simulator - 1:01:36 Optimizing Performance - 1:02:41 ChatGPT's Capabilities *VI. The Future of AI in Coding (**1:11:46**)* - 1:11:46 The Future of Developers and Tools like ChatGPT - 1:13:46 The Future of AI in Software Development - 1:13:59 Comparing Numba and Triton - 1:15:51 The Value of Learning CUDA - 1:16:47 Additional Notes *Summary 1/3* *Introduction* - *0:00* This video demonstrates advanced CUDA techniques for Python programmers, building upon previous CUDA knowledge. - *0:34* Focuses on optimizing memory usage with incredibly fast shared memory in CUDA. *Understanding Shared Memory vs. Global Memory* - *1:03* Global memory: Default type used in basic CUDA (slower but larger capacity). - *1:47* Shared memory: Limited to threads within a single block (10x faster than global). - *2:32* Using shared memory effectively is crucial for optimizing CUDA code execution.
@wolpumba4099
@wolpumba4099 4 ай бұрын
*Summary 2/3* *Pure Python Matrix Multiplication* - *6:21* The example focuses on matrix multiplication (dimensions: 5120 x 256 and 256 x 5120). - *6:46* A pure Python implementation is very slow, serving as a benchmark for improvement. *Tiling for Shared Memory Optimization* - *12:04* Shared memory has limited capacity, hence the need for "tiling". - *13:13* Tiling splits matrices into smaller tiles and calculates dot products iteratively in chunks. - *13:53* Tiles of data are loaded into shared memory. Threads across the block calculate partial dot products and aggregate them in global memory. *Cuda Matrix Multiplication Using Shared Memory* - *15:26* Shared memory allows us to reuse loaded data from the matrix without accessing slow global memory repeatedly. - *15:45* Threads collaborate on a sub-section of the matrix in shared memory, greatly enhancing performance. *Shared Memory for Tiling Optimization* - *15:35* Tiling breaks the matrix multiplication into smaller chunks for shared memory usage. - *15:48* Tiles of rows and columns are loaded into shared memory, processed, and added to the output. - *16:15* This reuse of tile data from shared memory avoids repeated, slow reads from global memory. *Shared Memory vs Views in Python* - *19:23* Python 'views' of tensors (numpy / PyTorch) allow modifications that reflect in the original tensor. - *19:58* This behavior simulates shared memory, where multiple threads access the same memory for efficiency. *Python Implementation of Shared Memory Optimization* - *20:12* The kernel runner is adapted to include a shared memory allocation step. - *20:39* Shared memory blocks are created as views into a larger contiguous memory allocation. - *21:02* Threads work in two stages: - Loading matrix tiles into shared memory. - Calculating dot products of the tiles from shared memory. - *22:30* `Mshared` and `Nshared` hold tile data. - *23:22* Looping logic calculates indices (`PH`, `idx`) to track tiling positions. - *26:01* Thread coordinates (`TR`, `TC`) and indices are combined to locate tile elements. - *28:37* 'Padding' with zeros handles tiles extending beyond matrix bounds. - *30:49* *Debugging Tip:* Use plain Python as a simplified model to debug core logic due to ease of inspection. - *31:08* *Code Refactoring:* Break down the process into clear functions mimicking thread behavior (`fill_shared_memory`, `do_dot_product`). - *31:35* *Managing Concurrent Threads:* Python's `threading` module is used to simulate CUDA-like concurrency: - *32:01* Each tile corresponds to a function executing across all threads of a block. - *32:31* *Relationship between CUDA blocks and tile size:* Blocks in CUDA are semantically mapped to tiles in the output for efficient use of shared memory. - *34:40* *Cuda Execution Model:* Unlike sequential Python loops, in CUDA, code within a kernel conceptually executes concurrently across all threads. - *35:14* *Python Threading (Simulating CUDA):* - *35:45* The `threading.Thread` class is used to launch parallel computations. - *36:19* The `threadpool.map` function executes a task across a pool of threads. - *37:27* *Thread Synchronization:* The `threading.Barrier` class enforces synchronization points within concurrent threads. - *38:27* *Final Kernel Runner (Python):* Combines shared memory handling and dot product logic, using barriers for synchronization. This model closely resembles CUDA code. - *40:39* *Synchronization's Importance:* Barriers prevent threads from overwriting shared memory or running ahead, ensuring correct calculations. - *42:15* *ChatGPT: Automated Python to CUDA conversion* with minor guidance and cleanup. - *42:47* *CUDA-Specific Syntax:* - *43:06* Data typing for inputs and outputs. - *43:38* `__shared__` keyword for explicit shared memory declaration. - *44:52* `syncthreads()` for thread synchronization within a block. *CUDA Code Structure & Shared Memory* - *45:11* `syncthreads()` ensures all threads have finished a code block before proceeding. It replaces Python's barrier object. - *45:29* CUDA has special syntax for shared memory (`__shared__`) and synchronization. - *45:48* Kernel calls use triple angle brackets `>` to specify blocks, threads per block, and shared memory size. - *46:30* Shared memory size calculation: - tile width * tile width * 2 (for input tiles) * size of float *CUDA Execution, Compilation, and the Mystery of Dynamic Shared Memory* - *46:53* Cuda version, as expected, yields the same result. - *46:59* *Mystery:* Dynamic shared memory version is slightly slower than the hardcoded tile-size version. This is counterintuitive and likely a code error. - *47:11* Book mentions, but doesn't fully explain, how to calculate optimal shared memory size / tile width. - *47:42* `cudaGetDeviceProperties` provides runtime information like max threads per block, shared memory per block, which can be used for optimization. *Introducing Numba for Python-Based CUDA* - *55:55* Numba allows writing CUDA code directly in Python for simpler syntax. - *56:23* *Performance Note:* Initial Numba version is somewhat slower, possibly due to dynamic shared memory usage. Optimization may be needed. - *57:03* Despite slower initial performance, Numba runs at CUDA-like speeds. - *58:31* *Key Advantage of Numba:* Faster compilation / iteration speed compared to raw C/C++ CUDA, making development easier.
@wolpumba4099
@wolpumba4099 4 ай бұрын
*Summary 3/3* *Additional Notes from 'Jeremy from the future' Segment* - *Mystery of Slow Dynamic Shared Memory Solved:* - CUDA can't optimize for unknown tile width at compile time. It falls back to a less optimized execution path. - *Workaround:* C++ templates allow different kernels to be generated for a fixed set of tile widths. *Advantages of Using Numba* - *59:26* Numba simplifies CUDA integration: Array handling is streamlined, eliminating manual flattening. - *59:53* Convenient indexing notation ([].shape). - *1:00:09* Python threading simulation in Numba mirrors CUDA behavior. *Numba's CUDA Simulator* - *1:00:38* `number.enable_cuda_sim=1` activates the simulator for CPU-based execution, similar to the manual Python version. - *1:01:02* This is excellent for debugging and development due to breakpoints and print statements. - *1:01:16* Important: Simulator performance mirrors Python (slow), use small data subsets. *Optimizing Performance* - *1:01:36* The Numba workflow: Develop and debug using the simulator, then disable it for actual GPU execution. - *1:02:13* ChatGPT can assist in converting Numba code to CUDA C/C++ code. - *1:02:41* Deployment strategies: - Use Numba directly (consider ease of use vs. potential CUDA toolkit dependency). - Auto-convert to CUDA and package (using load Cuda approach for easy distribution). *ChatGPT's Capabilities* - *1:06:07* Speaker hasn't explored performance optimization comparisons (CUBLAS, PyTorch) due to their complexity. - *1:07:15* Community efforts could bridge this gap and optimize the demonstrated techniques. - *1:09:30* Challenges with hardware-specific optimizations (Nvidia Tensor Cores and consumer GPUs). *The Future of Developers and Tools like ChatGPT* - *1:11:46* Exploring fusion optimizations with ChatGPT is an interesting area to experiment with. - *1:12:38* ChatGPT's strengths: - API usage for unfamiliar languages/frameworks - Replicating well-known algorithms - *1:13:14* Limitations: ChatGPT has not proven useful for novel algorithm development in the speaker's research-oriented work. Here's a bullet-point summary of the video transcript, divided into sections, with timestamps: *Sections* - *The Future of AI in Software Development* - *Comparing Numba and Triton* - *The Value of Learning CUDA* *The Future of AI in Software Development* - *1:13:46* The speaker believes AI tools will definitively play a role in the future of software development. *Comparing Numba and Triton* - *1:13:59* The speakers discuss the relative merits of Numba and Triton: - *Triton:* - Sophisticated library, capable of internal optimizations in kernels. - Result of recent PhD research. - *Numba:* - Simpler, providing a direct mapping of CUDA concepts to Python. - *1:14:54* Both share similarities (decorators, converting Python to GPU code). - *1:15:01* Key difference: Triton is more powerful for complex optimizations, while Numba is easier to learn. - *1:15:07* Triton has limitations: It doesn't represent the full Cuda programming model (example: 4-bit discretization was difficult to express). *The Value of Learning CUDA* - *1:15:51* A common question is whether a tool like Triton can negate the need to learn CUDA. - *1:16:03* Understanding CUDA is likely always necessary to maximize performance/flexibility. - *1:16:09* It might be difficult to use Triton effectively without prior CUDA knowledge. - *1:16:34* The iterative, notebook-based approach to CUDA development makes it approachable. *Additional Notes* - *1:16:47* Mention of an internal OpenAI tool, a potential example of the increasing role of AI in software development. - *1:17:09* Both Triton and Numba offer similar iteration speed benefits. Disclaimer: I used gemini ultra 1.0 (2024.02.08) to summarize the video transcript. This method may make mistakes in recognizing words.
@EvanBurnetteMusic
@EvanBurnetteMusic 4 ай бұрын
I'm having trouble with Ninja installation on colab
@sndrstpnv8419
@sndrstpnv8419 4 ай бұрын
can you share link to code pls
@forheuristiclifeksh7836
@forheuristiclifeksh7836 2 ай бұрын
30:99
@philippmuller2086
@philippmuller2086 3 ай бұрын
Hi Jeremy, your kaggle notebook from the first lesson (practical deep learning for coders) doesnt work. is your course from fastai outdated or still relevant?
@dangomushi983
@dangomushi983 2 ай бұрын
why do you write if or for statements in one line frequently? it's not recommended coding style...
@aintgonhappen
@aintgonhappen 3 күн бұрын
didn't watch his practical deep learning for coders?
@adnanwahab4191
@adnanwahab4191 27 күн бұрын
Amazing content thank you so much !
Getting Started With CUDA for Python Programmers
1:17:56
Jeremy Howard
Рет қаралды 50 М.
Sigma Girl Past #funny #sigma #viral
00:20
CRAZY GREAPA
Рет қаралды 24 МЛН
Дибала против вратаря Легенды
00:33
Mr. Oleynik
Рет қаралды 3,6 МЛН
Mojo🔥: a deep dive on ownership with Chris Lattner
48:50
15 Python Libraries You Should Know About
14:54
ArjanCodes
Рет қаралды 368 М.
How much faster has Mojo's dictionary gotten?
7:40
EKB PhD
Рет қаралды 917
The Most Important Algorithm in Machine Learning
40:08
Artem Kirsanov
Рет қаралды 292 М.
Advanced C: The UB and optimizations that trick good programmers.
1:12:34
Eskil Steenberg
Рет қаралды 159 М.
Modern Python logging
21:32
mCoding
Рет қаралды 156 М.
How to Do 90% of What Plugins Do (With Just Vim)
1:14:03
thoughtbot
Рет қаралды 870 М.
Claudette source walk-thru - Answer.AI dev chat #1
1:48:15
Jeremy Howard
Рет қаралды 3,5 М.
Popular Technologies that Won't be Around Much Longer...
14:36
Sideprojects
Рет қаралды 146 М.
Sigma Girl Past #funny #sigma #viral
00:20
CRAZY GREAPA
Рет қаралды 24 МЛН