CUDA Compute Backend for VSL π₯οΈ
June 1, 2026 Β· View on GitHub
vsl.cuda is a high-performance GPU compute backend for VSL backed by
NVIDIA CUDA (cuBLAS + cuDNN).
π Status
CUDA backend β cuBLAS/cuDNN bindings active when CUDA Toolkit + cuDNN are available at build time (
-d cuda). Operations use GPU kernels where implemented; CPU fallback when CUDA/cuDNN is unavailable.VTL integration (opt-in CUDA Linear/Conv2D): merged in vlang/vtl#93 (issues #89β#91 closed).
| Operation | GPU (CUDA+cuDNN) | Fallback | Tracker |
|---|---|---|---|
gemm | β
cublasDgemm | CPU col-major | β |
gemv | β
cublasDgemv | CPU | β |
relu / sigmoid / tanh | β cuDNN activation | CPU | β |
add_vec | β
cublasDaxpy | CPU | β |
mul_vec | β
cublasDdgmm | CPU | β |
add_scalar / mul_scalar | β cuBLAS/cuDNN path | CPU | β |
softmax | β
cudnnSoftmaxForward | CPU | β |
layernorm | optional -d cudnn_layernorm | CPU | β |
conv2d | β
cudnnConvolutionForward | CPU | β |
conv2d_backward | β
cudnnConvolutionBackward* | CPU | β |
mul_vec uses legacy cublasDdgmm (SIDE_RIGHT, 1Γn row layout;
cublasDdgmm_v2 absent on some distros). Layer norm GPU: build with
-d cudnn_layernorm when libcudnn exports cudnnLayerNormForward (9.1+).
Numerical parity tests: cuda/compute/numerical_validation_test.v (#281).
π Architecture
vsl.cuda
βββ backend.v # CUDABackend (ComputeBackend interface impl)
βββ compute/
β βββ elementwise.v # Activation functions (relu, sigmoid, tanh, ...)
β βββ gemm.v # Public GEMM wrapper (rowβcol conversion)
β βββ gemm_impl.v # Internal GEMM/GEMV implementation + CPU fallbacks
βββ v.mod
Memory layout: cuBLAS is column-major (same as VCL/Vulkan). The
CUDABackend.to_internal() / from_internal() methods handle rowβcolumn-major
conversion at the dispatch boundary.
π― Quick Start
import vsl.compute
// Use CUDA backend automatically when available
ctx := compute.new_context(.cuda)
// All compute operations dispatch to CUDA when the backend is set
a := []f64{len: 6}
b := []f64{len: 6}
// ... fill a, b ...
result := compute.add_vec(ctx, a, b)!
Or directly via the backend:
import vsl.cuda
mut dev := cuda.get_default_device()!
mut backend := cuda.new_cuda_backend()
result := backend.relu(my_data)!
π§ Requirements
Runtime (at runtime)
- NVIDIA GPU with compute capability β₯ 5.0 (Maxwell or newer)
- NVIDIA Driver β₯ 525.60 (for CUDA 12.x)
- CUDA Toolkit β₯ 11.8
- cuDNN β₯ 8.0
Build time (for CUDA GPU builds)
nvcc(NVIDIA C compiler) in$PATH- CUDA Toolkit headers (
cuda.h,cublas.h,cudnn.h) - cuDNN headers
π¦ Installation
Arch Linux
# NVIDIA driver (verify with nvidia-smi)
sudo pacman -S nvidia nvidia-utils
# CUDA Toolkit (includes cuBLAS)
sudo pacman -S cuda
# cuDNN (must match CUDA version)
sudo pacman -S cudnn
# Verify
nvcc --version # should show 12.x or 13.x
nvidia-smi # should show your GPU
Ubuntu / Debian
# NVIDIA driver
sudo apt-get install nvidia-driver-535
# CUDA Toolkit
wget https://developer.downloads.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt-get update
sudo apt-get install cuda-toolkit-12-2
# cuDNN
sudo apt-get install libcudnn8 libcudnn8-dev
macOS
CUDA Toolkit is no longer supported on macOS arm64 (Apple Silicon). For GPU acceleration on Apple Silicon, use the VCL/OpenCL backend instead.
ποΈ Compiling with CUDA
VSL uses conditional compilation ($if cuda ?) to include CUDA code:
# Compile with CUDA backend
v -d cuda run your_app.v
# Run tests with CUDA
v -d cuda test .
# Lint with CUDA
v -d cuda vet .
Set CUDNN_PATH if cuDNN is not in a standard location:
CUDNN_PATH=/opt/cuda v -d cuda run your_app.v
β‘ Performance Notes
- GEMM (matrix-matrix) β the highest-impact primitive. cuBLAS
dgemmis heavily optimized for NVIDIA Tensor Cores on Ampere+ GPUs. - cuDNN activations β fused kernels for ReLU, Sigmoid, Tanh provide ~2-5x speedup over CPU for large tensors.
- Memory layout β cuBLAS uses column-major; VSL uses row-major. The
to_internal()/from_internal()conversion adds overhead for small tensors but is negligible for large matrices (β₯ 256Γ256).
π§ͺ Examples
See the cuda/examples/ directory:
# Run CUDA backend smoke test
v -d cuda run cuda/examples/relu_example.v
πΊοΈ Roadmap
| Phase | Description |
|---|---|
| β Phase B | Infrastructure ready; CPU fallback path |
| β Phase C | cuBLAS/cuDNN kernels for GEMM/GEMV, activations, softmax, Conv2D, LayerNorm |
| ποΈ Phase D | Device discovery, multi-GPU support |
| π§ Phase E | GPU memory management (avoid CPUβGPU copies) |
| β Phase F | Numerical validation vs reference for key kernels |
π Resources
- VSL Documentation
- cuBLAS Documentation
- cuDNN Documentation
- CUDA Toolkit Download
- VSL ADR-001: Multi-backend GPU compute
- VTL CUDA example
Accelerate your scientific computing with NVIDIA GPUs! π