r/CUDA • • 4d ago

exact-gpu-jpeg: CUDA JPEG decode/encode that’s byte-identical to libjpeg-turbo and Pillow

GPU JPEG decoders are fast, but their pixels differ from the CPU libraries'. On 102 public test images, nvJPEG matched libjpeg-turbo on none of them, by up to 110 levels at 4:2:0. That quietly breaks reproducible ML pipelines, test baselines and caches whenever decoding moves between CPU and GPU.

exact-gpu-jpeg decodes on the GPU and returns libjpeg-turbo's pixels byte for byte (accurate IDCT, fancy upsampling), and its encoder writes the same bytes as Pillow. Anything it can't reproduce exactly, progressive files for example, goes to libjpeg-turbo itself, so the output is always the reference.

On a 2016 GTX 1070: a 30 MP camera photo decodes in 11.3 ms, against 185 ms for libjpeg-turbo on one core and 164 ms for nvJPEG on the same card. Batches of small images: 7,310 images/s. Encoding: 5.5x faster than Pillow with identical files.

Where it loses is listed near the top of the README: one small image at a time, small encodes against 8 CPU threads, progressive files. Everything so far is from one machine, so results from other GPUs are very welcome: `python bench/reproduce.py --quick`, and there's a pinned issue for them.

The parallel Huffman stage (Weißenberger & Schmidt's self-synchronising method) is drawn in docs/huffman-sync.md, and there's a white paper in docs/ covering the method, the evidence and the limits.

https://github.com/mrluker/exact-gpu-jpeg

8 Upvotes

6 comments sorted by

2

u/mherf 4d ago

Very nice!

fwiw, nvJPEG is not really accelerated on the GTX/RTX GPUs, they only accelerated Huffman on the A100 and similar datacenter GPUs, see https://developer.nvidia.com/blog/leveraging-hardware-jpeg-decoder-and-nvjpeg-on-a100/

I ran your benchmark on RTX 5070ti - here is bench/reproduce.py - GPU utilization overall is low. ~1ms for 24MP operations is amazing.

## Decode, one image at a time (ms per image, lower is better)
| size | file | libjpeg-turbo (simplejpeg), CPU | exact-gpu-jpeg | speed-up | decoded on | pixels equal to libjpeg-turbo's |
|---|---|---|---|---|---|---|
| 0.25 MP (616x408) | 35.7 kB | 0.39 | 0.52 | 0.8x | GPU | yes |
| 1 MP (1224x816) | 136.3 kB | 1.52 | 0.59 | 2.6x | GPU | yes |
| 2 MP (1736x1160) | 279.2 kB | 3.04 | 0.69 | 4.4x | GPU | yes |
| 6 MP (3000x2000) | 840 kB | 9.16 | 0.83 | 11.1x | GPU | yes |
| 12 MP (4240x2824) | 1655 kB | 18.02 | 0.94 | 19.1x | GPU | yes |
| 24 MP (6000x4000) | 3359.7 kB | 39.57 | 1.40 | 28.3x | GPU | yes |

## Decode, 64 different images per call (images per second, higher is better)

sizedecode() in a loopdecode_batch()speed-uppixels equal
0.25 MP (616x408)1,91831,02616.2xyes
1 MP (1224x816)1,58516,97710.7xyes

## Encode, quality 90, 4:2:0 (ms per image, lower is better)

sizePillow (libjpeg-turbo), CPUexact-gpu-jpegspeed-upfile equal to Pillow's
0.25 MP (616x408)0.440.114.1xyes
1 MP (1224x816)1.700.189.2xyes
2 MP (1736x1160)3.370.2712.7xyes
6 MP (3000x2000)10.090.4323.4xyes
12 MP (4240x2824)21.120.6930.5xyes
24 MP (6000x4000)60.531.1950.7xyes

1

u/Sure_Resource4753 4d ago

Thanks a lot for running it! This is the first result from a card other than my GTX 1070, and it's great to see every output identical on an RTX 5070 Ti too!

You're right about nvJPEG: the hardware JPEG engine is only on A100-class datacenter GPUs, so on GeForce cards nvJPEG runs on the regular CUDA cores. The README says the GTX 1070 has no hardware engine and that A100-class cards weren't measured; I'll make that clearer.

Low utilization for single images makes sense: one image at a time is mostly fixed per-call cost (launches and a host round trip), which is why decode_batch gets so much more out of the card (31k images/s at 0.25 MP on yours!).

Would you mind pasting the full reproduce.py output (it includes your driver/OS/device report) in the pinned "Results on other GPUs" issue? https://github.com/mrluker/exact-gpu-jpeg/issues/1 I'd love to add it to the README with credit.

2

u/mherf 4d ago

posted

1

u/Sure_Resource4753 4d ago

Summary Image

1

u/Sure_Resource4753 4d ago edited 4d ago

UnHuff: faster Huffman stage decoding’s parallel huffman diagram

1

u/TQ_GPU 3d ago

"30 MP camera photo decodes in 11.3 ms" --> is this 12 GB/s decode speed?