c-llm/dev/cuda
Andrej 80e0c3b8d9
Merge pull request #512 from gordicaleksa/refactor_encoder_bwd_kernel
Remove redundant CPU computation in encoder bwd
2024-06-02 16:43:10 -07:00
..
adamw.cu update the -lcublas -lcublasLt flag in the comment 2024-05-09 22:04:02 -07:00
attention_backward.cu update the -lcublas -lcublasLt flag in the comment 2024-05-09 22:04:02 -07:00
attention_forward.cu update the -lcublas -lcublasLt flag in the comment 2024-05-09 22:04:02 -07:00
benchmark_on_modal.py Modal benchmarking script updated to replace deprecated calls 2024-05-29 20:01:41 +05:30
classifier_fused.cu Removed unnecesary shared memory due to blockreduce using static defined shared memory 2024-05-30 03:01:52 +00:00
common.h utilities for Packed and more vectorization 2024-05-27 13:15:36 +03:00
crossentropy_forward.cu update the -lcublas -lcublasLt flag in the comment 2024-05-09 22:04:02 -07:00
crossentropy_softmax_backward.cu update the -lcublas -lcublasLt flag in the comment 2024-05-09 22:04:02 -07:00
encoder_backward.cu Remove redundant CPU computation 2024-06-01 18:27:24 +02:00
encoder_forward.cu update the -lcublas -lcublasLt flag in the comment 2024-05-09 22:04:02 -07:00
fused_residual_forward.cu update the -lcublas -lcublasLt flag in the comment 2024-05-09 22:04:02 -07:00
gelu_backward.cu update the -lcublas -lcublasLt flag in the comment 2024-05-09 22:04:02 -07:00
gelu_forward.cu update the -lcublas -lcublasLt flag in the comment 2024-05-09 22:04:02 -07:00
global_norm.cu small touchups to grad clip 2024-05-19 17:07:55 +00:00
layernorm_backward.cu amend 2024-05-27 15:13:11 -07:00
layernorm_forward.cu update the -lcublas -lcublasLt flag in the comment 2024-05-09 22:04:02 -07:00
Makefile added a useful mixed precision utility for dev/cuda 2024-05-18 22:45:44 +03:00
matmul_backward.cu update the -lcublas -lcublasLt flag in the comment 2024-05-09 22:04:02 -07:00
matmul_backward_bias.cu update the utils function and assert 2024-05-20 08:00:39 -07:00
matmul_forward.cu mixed precision utilities for dev/cuda 2024-05-01 22:35:55 +03:00
nccl_all_reduce.cu update the -lcublas -lcublasLt flag in the comment 2024-05-09 22:04:02 -07:00
README.md Update compile cmd in the dev/cuda README 2024-06-01 16:46:32 +02:00
residual_forward.cu update the -lcublas -lcublasLt flag in the comment 2024-05-09 22:04:02 -07:00
softmax_forward.cu Merge pull request #383 from KarhouTam/feature/online-softmax-forward-without-cgs 2024-05-16 21:23:56 +01:00
trimat_forward.cu update the -lcublas -lcublasLt flag in the comment 2024-05-09 22:04:02 -07:00

dev/cuda

This directory is scratch space for developing various versions of the needed CUDA kernels. Each file develops a kernel, and usually multiple versions of that kernel that could have different running times and of different code or time complexity.

See the top of each file for how to compile and run the kernel. Alternatively, the commands are also all grouped in the Makefile in this directory for convenience.

For example, we can look at the top of layernorm_forward.cu to build the forward pass kernels for the LayerNorm:

nvcc -O3 --use_fast_math -lcublas -lcublasLt layernorm_forward.cu -o layernorm_forward

or simply

make layernorm_forward

The comments at the top then document the different versions of this kernel available, usually these are in increasing complexity and decreasing running times. For example, inspecting the comments in the file on top, the most naive kernel we can then run as:

./layernorm_forward 1

You'll see that this first forwards the reference code on the CPU, then it runs kernel 1 on the GPU, compares the results to check for correctness, and then runs a number of configurations of this kernel (most often and most notably the block size), to time the kernel in these launch configurations. We can then run one of the faster kernels (kernel 4) instead:

./layernorm_forward 4

You'll see that this matches all the CPU results but runs much much faster. The typical process from here on is we copy paste the kernel that ran fastest, adjust it manually (e.g. to hardcode the best block size) and drop it into the training code file, e.g. train_gpt2.cu.

To add a new version of a kernel, add the kernel to the corresponding file and adjust the docs. To add a new kernel, add the new file and adjust the Makefile. Run make clean to clean up binaries from your directory.

If you do not have a GPU or is having trouble with CUDA dependencies, you can run the benchmarks on the Modal platform. For example, to run the benchmark for the attention forward pass on an A100 GPU with 80GB of memory, you can run the following command:

GPU_MEM=80 modal run benchmark_on_modal.py --compile-command "nvcc -O3 --use_fast_math attention_forward.cu -o attention_forward -lcublas" --run-command "./attention_forward 1"