Resources:
| Name | Description |
|---|---|
| 01. Vector addition | Simple example to get everything working. |
| 02. Matrix multiplication SIMT | Block tiling, thread tiling, warp tiling. |
| 02. Matrix multiplication SM80 | Inline PTX, cvta, ldmatrix, mma. |
| 02. Matrix multiplication SM100 | tcgen05 |
| 02. Matrix multiplication SM120 | |
| 02. Matrix multiplication CDNA3 | |
| 03. Sum | Reduction in general. Prepare for softmax (max and sum). |
| 04. Softmax | Naive (safe) softmax, online softmax. atomicCAS(). Single-block and multi-block per row. |
| 05. FP6 | FP6 primitives (FP32/FP16/BF16<->FP6). |
| 06. Box blur | 2D CUDA blocks/threads. TODO: optimize with separable filters, moving average. |
| 07. Attention | Flash attention |
| 08. Row-scaled matmul | Simple epilogue |
| 09. Block-scaled matmul | MXFP8 |
# profile a CUDA kernel
# remember to compile with -lineinfo
ncu --set full --import-source on -o profile python main.py
# debug illegal memory access
compute-sanitizer python main.py
and open the generated profile.ncu-rep file in Nsight Compute. See more here: https://docs.nvidia.com/nsight-compute/NsightComputeCli/index.html
To profile PyTorch program, run
with torch.profiler.profile() as prof:
# PyTorch program here
...
print(prof.key_averages().table(sort_by="cuda_time_total", row_limit=10))
prof.export_chrome_trace("trace.json")
and open the saved trace.json file at https://ui.perfetto.dev/. See more here: https://pytorch.org/docs/stable/profiler.html#torch.profiler.profile
To support syntax highlighting and code suggestion in IDE, add the following include paths (can be seen in the command that PyTorch uses to compile inline C++ code).
"/home/thien/miniconda3/envs/dev/lib/python3.10/site-packages/torch/include",
"/home/thien/miniconda3/envs/dev/lib/python3.10/site-packages/torch/include/torch/csrc/api/include",
"/home/thien/miniconda3/envs/dev/lib/python3.10/site-packages/torch/include/THC",
"/usr/local/cuda/include",
"/home/thien/miniconda3/envs/dev/include/python3.10"
Change the paths appropriately for your system. For Windows, the paths are slightly different, but then again, you can see them in the PyTorch compile command. For VSCode, add the paths to .vscode/c_cpp_properties.json (VSCode will prompt you to create one if it does not exist).
cudaMalloc() and write data to it with cudaMemcpy().__shared__ float shmem[64]; for static-size array, or extern __shared__ float shmem[]; for dynamically allocated shared memory, which is set by kernel call execution configuration kernel<<<n_blocks, n_threads, shmem_size>>>().cudaSetFuncAttribute().float4). For direct global->shared memory copy without using registers, we can use cp.async.tl.dot(allow_tf32=False). With fp32 inputs, the outputs are wrong. There are no clear reasons for this. With fp16/bf16, this flag doesn't matter.270 commits
1 commits
Cuda
59.8%
Python
28.9%
C++
11.3%
Resources:
| Name | Description |
|---|---|
| 01. Vector addition | Simple example to get everything working. |
| 02. Matrix multiplication SIMT | Block tiling, thread tiling, warp tiling. |
| 02. Matrix multiplication SM80 | Inline PTX, cvta, ldmatrix, mma. |
| 02. Matrix multiplication SM100 | tcgen05 |
| 02. Matrix multiplication SM120 | |
| 02. Matrix multiplication CDNA3 | |
| 03. Sum | Reduction in general. Prepare for softmax (max and sum). |
| 04. Softmax | Naive (safe) softmax, online softmax. atomicCAS(). Single-block and multi-block per row. |
| 05. FP6 | FP6 primitives (FP32/FP16/BF16<->FP6). |
| 06. Box blur | 2D CUDA blocks/threads. TODO: optimize with separable filters, moving average. |
| 07. Attention | Flash attention |
| 08. Row-scaled matmul | Simple epilogue |
| 09. Block-scaled matmul | MXFP8 |
# profile a CUDA kernel
# remember to compile with -lineinfo
ncu --set full --import-source on -o profile python main.py
# debug illegal memory access
compute-sanitizer python main.py
and open the generated profile.ncu-rep file in Nsight Compute. See more here: https://docs.nvidia.com/nsight-compute/NsightComputeCli/index.html
To profile PyTorch program, run
with torch.profiler.profile() as prof:
# PyTorch program here
...
print(prof.key_averages().table(sort_by="cuda_time_total", row_limit=10))
prof.export_chrome_trace("trace.json")
and open the saved trace.json file at https://ui.perfetto.dev/. See more here: https://pytorch.org/docs/stable/profiler.html#torch.profiler.profile
To support syntax highlighting and code suggestion in IDE, add the following include paths (can be seen in the command that PyTorch uses to compile inline C++ code).
"/home/thien/miniconda3/envs/dev/lib/python3.10/site-packages/torch/include",
"/home/thien/miniconda3/envs/dev/lib/python3.10/site-packages/torch/include/torch/csrc/api/include",
"/home/thien/miniconda3/envs/dev/lib/python3.10/site-packages/torch/include/THC",
"/usr/local/cuda/include",
"/home/thien/miniconda3/envs/dev/include/python3.10"
Change the paths appropriately for your system. For Windows, the paths are slightly different, but then again, you can see them in the PyTorch compile command. For VSCode, add the paths to .vscode/c_cpp_properties.json (VSCode will prompt you to create one if it does not exist).
cudaMalloc() and write data to it with cudaMemcpy().__shared__ float shmem[64]; for static-size array, or extern __shared__ float shmem[]; for dynamically allocated shared memory, which is set by kernel call execution configuration kernel<<<n_blocks, n_threads, shmem_size>>>().cudaSetFuncAttribute().float4). For direct global->shared memory copy without using registers, we can use cp.async.tl.dot(allow_tf32=False). With fp32 inputs, the outputs are wrong. There are no clear reasons for this. With fp16/bf16, this flag doesn't matter.270 commits
1 commits
Cuda
59.8%
Python
28.9%
C++
11.3%