Skip to content

Latest commit

 

History

History
245 lines (161 loc) · 10.3 KB

BENCHMARKS.md

File metadata and controls

245 lines (161 loc) · 10.3 KB

MacroTorch Benchmarks

Comprehensive performance benchmarks for MacroTorch custom CUDA kernels.

All benchmarks use torch.cuda.Event for precise GPU kernel timing. All tests and benchmarks were performed on an NVIDIA Tesla T4 GPU.


Forward Pass (im2col + custom Matmul Tiled)

Configuration: Batch=2, In Channels=4, Out Channels=8, 64×64 Input, 3×3 Kernel, Padding=1

Precision SciPy (CPU) PyTorch (GPU) MacroTorch (GPU) MT vs CPU Max Error
FP32 13.14 ms 0.06 ms 1.30 ms 10.1x faster 9.54e-06
FP16 13.13 ms 0.06 ms 1.34 ms 9.8x faster 7.63e-06

MacroTorch matches PyTorch accuracy exactly (~1e-06 error).


Input Gradient Backward Pass (custom Matmul Tiled + col2im)

Configuration: Batch=2, In Channels=4, Out Channels=8, 64×64 Input, 3×3 Kernel

Precision SciPy (CPU) PyTorch (GPU) MacroTorch (GPU) MT vs CPU
FP32 13.49 ms 0.09 ms 0.89 ms 15.1x faster
FP16 14.35 ms 0.11 ms 0.94 ms 15.2x faster

MacroTorch matches PyTorch accuracy exactly (7.63e-06 error in FP32).


Bias Gradient Backward Pass

Configuration: Batch=32, Channels=128, Spatial=64×64

Precision NumPy (CPU) PyTorch (GPU) MacroTorch (GPU) MT vs CPU
FP32 28.47 ms 0.34 ms 0.94 ms 30.4x faster
FP16 59.53 ms 0.32 ms 0.94 ms 63.0x faster

MacroTorch achieves better FP16 accuracy (1.83e-04) than PyTorch (2.27e-01).


Weight Gradient Backward Pass (im2col + custom Matmul Tiled)

torch.cuda.Event profiling for precise GPU kernel timing.

Small Configuration (Batch=2, Cin=4, Cout=8, 32×32, 3×3 Kernel)

Precision SciPy (CPU) PyTorch (GPU) MacroTorch (GPU) MT vs CPU Max Error
FP32 2.05 ms 0.08 ms 1.02 ms 2.0x faster 1.98e-04
FP16 2.26 ms 0.07 ms 0.97 ms 2.3x faster 1.60e-04

Large Configuration (Batch=8, Cin=32, Cout=64, 128×128, 3×3 Kernel)

Precision SciPy (CPU) PyTorch (GPU) MacroTorch (GPU) MT vs CPU Max Error
FP32 6995.59 ms 2.98 ms 19.66 ms 355.9x faster 2.04e-02
FP16 7317.20 ms 2.98 ms 15.87 ms 461.1x faster 1.93e-02

Weight Gradient Backward Pass (2D Legacy Kernel)

Configuration: Batch=8, 128×128 Input, 3×3 Kernel, Padding=1

Important

MacroTorch's 2D Legacy Kernel beats PyTorch significantly!

Precision SciPy (CPU) PyTorch (GPU) MacroTorch (GPU) MT vs PT Max Error
FP32 3.02 ms 0.48 ms 0.12 ms 4.1x faster 1.10e-03
FP16 3.79 ms 0.46 ms 0.13 ms 3.5x faster 1.01e-03

This specialized kernel demonstrates that custom CUDA implementations can significantly outperform cuDNN for specific use cases.


ReLU Activation

Configuration: 1024×1024 array, FP32

Pass NumPy (CPU) PyTorch (GPU) MacroTorch (GPU) MT vs PT Error
Forward 0.87 ms 0.05 ms 0.11 ms 2.5x slower 0.00e+00
Backward 0.93 ms 0.19 ms 0.11 ms 1.7x faster 0.00e+00

MacroTorch ReLU backward is 1.7x faster than PyTorch, while both achieve exact accuracy.


MaxPool2D (4D Multi-Channel)

Configuration: Batch=8, Channels=64, 128×128 Input, Pool Size=2, FP32

Operation PyTorch (GPU) MacroTorch (GPU) MT vs PT Max Error
Forward 0.35 ms 0.84 ms 0.4x faster 0.00e+00
Backward 1.31 ms 0.50 ms 2.6x faster 0.00e+00

Important

MacroTorch's MaxPool2D Backward is 2.6x faster than PyTorch!

Both forward and backward produce identical numerical results (exact 0.00 error).


Softmax

Small Configuration (Batch=8, Classes=10, Spatial=1×1, FP32)

Operation NumPy (CPU) PyTorch (GPU) MacroTorch (GPU) MT vs PT Max Error
Forward 0.08 ms 0.02 ms 0.06 ms - 2.98e-08
Backward 0.01 ms 0.19 ms 0.07 ms 2.7x faster 2.98e-08

Large Configuration (Batch=8, Classes=10, Spatial=28×28, FP32)

Operation NumPy (CPU) PyTorch (GPU) MacroTorch (GPU) MT vs PT Max Error
Forward 0.17 ms 0.03 ms 0.08 ms - 1.79e-07
Backward 0.10 ms 0.20 ms 0.08 ms 2.5x faster 1.19e-07

Important

MacroTorch's Softmax Backward is up to 2.7x faster than PyTorch!

MacroTorch achieves 2.5x speedup over CPU with exact numerical accuracy matching PyTorch.


Matrix Multiplication (Tiled)

Tiled matrix multiplication using 16×16 shared memory tiles with FP32 accumulation.

Small Configuration (256×256 × 256×256)

Precision NumPy (CPU) PyTorch (GPU) MacroTorch (GPU) MT vs CPU Max Error
FP32 0.38 ms 0.07 ms 0.18 ms 2.1x faster 0.00e+00
FP16 0.34 ms 0.04 ms 0.20 ms 1.7x faster 0.00e+00

Large Configuration (1024×1024 × 1024×1024)

Precision NumPy (CPU) PyTorch (GPU) MacroTorch (GPU) MT vs CPU Max Error
FP32 19.08 ms 0.85 ms 5.96 ms 3.2x faster 2.21e-04
FP16 17.91 ms 0.14 ms 4.79 ms 3.7x faster 1.98e-04

MacroTorch's tiled matmul achieves 2-4x speedup over CPU. PyTorch uses cuBLAS/Tensor Cores which are highly optimized for GEMM operations.

Note

The matmul kernel achieves perfect accuracy (0.00 error) on small matrices. FP16 inputs are converted to FP32 for accumulation, providing better precision than native FP16 compute.


Cross-Entropy Loss

Cross-entropy loss for multi-class classification with CUDA kernels.

Small Configuration (Batch=32, Classes=10, FP32)

Pass NumPy (CPU) PyTorch (GPU) MacroTorch (GPU) MT vs PT Max Error
Forward 0.02 ms 0.05 ms 0.07 ms 1.4x slower 0.00e+00
Backward 0.02 ms 0.18 ms 0.07 ms 2.6x faster 0.00e+00

Large Configuration (Batch=256, Classes=1000, FP32)

Pass NumPy (CPU) PyTorch (GPU) MacroTorch (GPU) MT vs PT Max Error
Forward 0.02 ms 0.04 ms 0.07 ms 1.8x slower 0.00e+00
Backward 0.19 ms 0.26 ms 0.12 ms 🏆 2.2x faster 0.00e+00

Important

MacroTorch's Cross-Entropy Backward is 2.2x faster than PyTorch!

The cross-entropy kernel achieves perfect accuracy (0.00 error) on all configurations.


Summary

MacroTorch demonstrates:

  • ✅ 10-15x speedup over CPU for forward/backward passes
  • ✅ 30-63x speedup over CPU for bias gradient computation
  • ✅ 355-461x speedup over CPU for weight gradient (large tensors)
  • ✅ 2-3.7x speedup over CPU for tiled matrix multiplication
  • ✅ 2.6x faster than PyTorch for MaxPool2D backward (Specific Config)
  • ✅ 4.1x faster than PyTorch for 2D Legacy weight gradient backward (Simpler Kernel)
  • ✅ 2.7x faster than PyTorch for Softmax backward (Specific Config)
  • ✅ 2.2x faster than PyTorch for Cross-Entropy backward (Specific Config)
  • ✅ 1.7x faster than PyTorch for ReLU backward (Specific Config)
  • ✅ Better FP16 precision than PyTorch in bias gradient operations
  • ✅ Excellent accuracy with max error ~1e-03 to 1e-05

Specific Configurations Faster than PyTorch

Kernel Configuration Speedup vs PyTorch
MaxPool2D Backward 8×64×128×128, pool=2 2.6x faster
Softmax Backward 8×10×1×1, FP32 2.7x faster
Softmax Backward 8×10×28×28, FP32 2.5x faster
Weight Backward (2D Legacy) 8×128×128, 3×3, FP32 4.1x faster
Weight Backward (2D Legacy) 8×128×128, 3×3, FP16 3.5x faster
ReLU Backward 1024×1024, FP32 1.7x faster
Cross-Entropy Backward 256×1000, FP32 2.2x faster

End-to-End Training: MNIST (1 Epoch)

Comparison of full model training time (1 epoch) between MacroTorch and PyTorch across various batch sizes.

Batch Size PyTorch (s) MacroTorch (s) Slowdown
32 3.44s 57.13s 16.61x slower
64 1.80s 34.30s 19.04x slower
128 1.08s 19.42s 18.01x slower
256 0.83s 13.56s 16.43x slower
512 0.69s 10.88s 15.80x slower
1024 0.66s 9.30s 14.19x slower

Note: While individual kernels are competitive, the end-to-end slowdown is primarily due to PyTorch's internal kernel fusion and graph optimizations. MacroTorch focuses on raw kernel transparency and research flexibility.

Note on PyTorch Comparison

PyTorch uses highly optimized cuDNN for most operations. MacroTorch outperforms PyTorch only on specific configurations listed above, typically involving simpler memory access patterns or specific tensor shapes.

Note on 2D Legacy Kernel: The "2D Legacy" weight gradient kernel is faster than PyTorch because it processes 3D inputs (N, H, W) treating them as a batch of 2D images, whereas PyTorch's convolution is inherently 4D (N, C, H, W). The MacroTorch 2D kernel is performing less computational work and memory indexing than the full 4D convolution.


🎯 Precision & Numerical Stability

MacroTorch uses FP32 accumulation for its CUDA kernels even when processing FP16 inputs. This design choice provides several benefits:

  1. Higher Accuracy: Minimizes rounding errors during the large summations required for convolution and matrix multiplication.
  2. Stable Gradients: As shown in the Bias Backward FP16 results, MacroTorch (1.83e-04 error) is significantly more accurate than standard FP16 PyTorch implementations (2.27e-01 error) for deep accumulations.
  3. Consistency: Ensures that switching between FP32 and FP16 modes results in nearly identical numerical outputs, aiding in debugging and model verification.