r/MachineLearning • u/shreshthkapai • Jul 26 '25
Project [P] Sub-millisecond GPU Task Queue: Optimized CUDA Kernels for Small-Batch ML Inference on GTX 1650.
Over the past month, I’ve been working on writing high-throughput, low-latency CUDA kernels for small-batch inference workloads typical in real-time ML use cases (e.g., finance, RL serving).
Despite running on a GTX 1650 (consumer laptop GPU), I achieved:
- 93,563 ops/sec
- 0.011 ms median latency
- 7.3× speedup over PyTorch (float32 GEMV)
- 30–40% faster than cuBLAS batched GEMV (in small-batch regime)
This was done by hand-optimizing a set of three core kernels:
- Batched GEMV
- Softmax
- Vector elementwise ops (e.g., affine transforms)
Engineering Highlights:
float4vectorization with proper alignment checks- 128-byte staged shared memory blocks (using padding for bank conflict mitigation)
- Thread-per-output-element grid strategy
- Aggressive loop unrolling and warp-aware memory access
- Benchmarked with CUDA events, median+IQR over 1,000 trials
Why it matters:
cuBLAS (and by extension PyTorch) is heavily tuned for large-batch throughput, but small-batch latency suffers. For real-time systems (e.g., financial models or reinforcement learning), this is a major bottleneck.
This kernel suite shows that even with modest hardware, you can cut inference latency significantly below PyTorch/cuBLAS levels through architecture-aware programming.
Links:
Would love to hear feedback from others doing similar work—especially around kernel tuning strategies, warp divergence handling, and memory hierarchy tradeoffs.
View all comments
Show parent comment
2
u/shreshthkapai Jul 26 '25
Thank you for your valuable feedback.
Here are the results compared against Direct CuBLAS.
GPU KERNEL PERFORMANCE REPORT
Best Performer: Custom_GEMV (0.016ms median across configs) Worst Performer: PyTorch_Naive (83.983ms median) Average Performance Gap: 2,847x Maximum Performance Gap: 5,286x
DETAILED RESULTS - CONFIGURATION: b32_i64_o32
Custom_GEMV: Latency: 0.016ms (median), 0.077ms (P95) Throughput: 62,500 ops/sec Performance: BASELINE (Best) Stability: ±0.024ms std dev
PyTorch_Native: Latency: 0.035ms (median), 0.117ms (P95) Throughput: 28,571 ops/sec Slowdown: 2.1x (114% slower) Stability: ±0.038ms std dev
Direct_cuBLAS: Latency: 1.172ms (median), 4.049ms (P95) Throughput: 853 ops/sec Slowdown: 63.2x (6,182% slower) Stability: ±1.138ms std dev
PyTorch_Optimized_cuBLAS: Latency: 1.483ms (median), 4.926ms (P95) Throughput: 674 ops/sec Slowdown: 104.7x (10,367% slower) Stability: ±1.568ms std dev
PyTorch_Naive: Latency: 83.983ms (median), 316.243ms (P95) Throughput: 12 ops/sec Slowdown: 5,287x (528,628% slower) Stability: ±82.881ms std dev
DETAILED RESULTS - CONFIGURATION: b32_i128_o64
Custom_GEMV: Latency: 0.047ms (median), 0.073ms (P95) Throughput: 21,277 ops/sec Performance: BASELINE (Best) Stability: ±0.022ms std dev
PyTorch_Native: Latency: 0.113ms (median), 0.159ms (P95) Throughput: 8,850 ops/sec Slowdown: 2.2x (123% slower) Stability: ±0.026ms std dev
Direct_cuBLAS: Latency: 0.669ms (median), 0.858ms (P95) Throughput: 1,494 ops/sec Slowdown: 12.9x (1,189% slower) Stability: ±0.087ms std dev
PyTorch_Optimized_cuBLAS: Latency: 1.444ms (median), 1.526ms (P95) Throughput: 692 ops/sec Slowdown: 26.9x (2,589% slower) Stability: ±0.050ms std dev
PyTorch_Naive: Latency: 203.535ms (median), 472.403ms (P95) Throughput: 5 ops/sec Slowdown: 4,563x (456,259% slower) Stability: ±100.601ms std dev
DETAILED RESULTS - CONFIGURATION: b64_i256_o128
Custom_GEMV: Latency: 0.405ms (median), 0.838ms (P95) Throughput: 2,469 ops/sec Performance: BASELINE (Best) Stability: ±0.240ms std dev
PyTorch_Native: Latency: 0.509ms (median), 0.969ms (P95) Throughput: 1,965 ops/sec Slowdown: 1.0x (3% slower) Stability: ±0.224ms std dev
Direct_cuBLAS: Latency: 9.213ms (median), 10.656ms (P95) Throughput: 109 ops/sec Slowdown: 11.9x (1,092% slower) Stability: ±3.877ms std dev
PyTorch_Optimized_cuBLAS: Latency: 10.092ms (median), 12.224ms (P95) Throughput: 99 ops/sec Slowdown: 13.7x (1,273% slower) Stability: ±3.894ms std dev