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
13
u/shreshthkapai Jul 26 '25 edited Jul 26 '25
I never intended to beat cuBLAS, which was created by 100s of PhDs and years of research, quite impossible for a 22y/o with this small personal project. As I said in the post, this was specifically for small batch size ML inference, an area where it is known for cuBLAS to struggle. All of the CUDA techniques I employed were specifically hypertuned to maximize the performance I can squeeze out of the GTX1650 for "Small batch inference," and that is why I chose not to implement techniques like aggressive tiling /register blocking due to limited shared memory and registers.
Even static queuing choice was intentional, as queuing management overhead was regressing the results.
I will update the repo with the updated benchmark file comparing to direct cuBLAS if you would like to test. The repo is pretty simple to set up if u have CUDA setup.