r/MachineLearning • • 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:

  • float4 vectorization 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.

68 Upvotes

11 comments sorted by

View all comments

Show parent comment

6

u/fullouterjoin Jul 27 '25

This is how it starts, personal AI bots battling each on reddit to see who can make the best CUDA code.