I wanted to understand the Transformer architecture in depth and implement it with CUDA. However, during this process, I realized that the cost of learning C++ was too high and decided to turn to more practical optimization tools. This project will be public, so you can always contribute.
This project is a modular, from-scratch Transformer implementation using LibTorch (PyTorch C++ API) and custom CUDA kernels for acceleration.
You’ll find pure CUDA C++ modules for:
- Input Embedding
- Positional Encoding
- Layer Normalization
- Residual Connection
- FeedForward
- Softmax (custom CUDA kernel)
- Classic Self Attention
- Docker CE
- NVIDIA Container Toolkit
- Compatible CUDA driver installed on your host (must match container CUDA version)
Your host CUDA driver version must match the container’s CUDA version (e.g., 12.4.0):
You may install some packages like nano, code-server for working inside of container etc.
- Download LibTorch for your CUDA version.
- Extract and place it inside the project directory.
Then add it to your environment:
echo 'export CMAKE_PREFIX_PATH=/workspace/libtorch' >> ~/.bashrc
source ~/.bashrcYou can also look at CMakeLists.txt for reference.
- Create a
CMakeLists.txtfile (already included if cloned). - Create a build directory:
mkdir build
cd build
cmake ..
make -j- Run the executable:
./mainAlso look at build_and_run.sh for building and running the script without executing multiple lines.
Each module (e.g., feed_forward.cpp, layer_norm.cpp, etc.) is implemented as a separate class.
CUDA kernels (e.g., softmax.cu) are invoked using custom launcher functions.
See main.cpp for usage examples and test cases for each component.
- This project is GPU-only.
- All tensors are allocated on
torch::kCUDA. - You’ll need to manually uncomment desired test blocks in
main.cppfor validation.
Input shape: [2, 4, 512]
Output shape: [2, 4, 512]- Implement full Encoder and Decoder blocks using the completed modules.
- Introduce further CUDA kernel optimizations for Attention and other bottlenecks.
- Train a Transformer model end-to-end using this modular repo as the base.