# Not One, Not Two, Not Even Three, but Four Ways to Run an ONNX AI Model on GPU with CUDA

DevFeed: [Not One, Not Two, Not Even Three, but Four Ways to Run an ONNX AI Model on GPU with CUDA](<https://devfeed.tech/articles/not-one-not-two-not-even-three-but-four-ways-to-run-an-onnx-ai-model-on-gpu-with-cuda-20728.md>)

Original publisher: [Read original article](<http://dragan.rocks/articles/25/Four-Ways-to-ONNX-on-GPU-in-Clojure-and-CUDA>)

Published: 2025-11-09T17:49:00Z

Content type: tutorial

Language: en

Sources: [Dragan Djuric](<https://devfeed.tech/sources/dragan-djuric.md>)

Topics: [onnx](<https://devfeed.tech/topics/onnx.md>), [CUDA](<https://devfeed.tech/topics/cuda.md>), [onnx runtime](<https://devfeed.tech/topics/onnx-runtime.md>), [cudnn](<https://devfeed.tech/topics/cudnn.md>), [Neural Network](<https://devfeed.tech/topics/neural-network.md>), [Artificial Intelligence](<https://devfeed.tech/topics/ai.md>)

Tags: [ai](<https://devfeed.tech/tags/ai.md>), [ai-model](<https://devfeed.tech/tags/ai-model.md>), [backend](<https://devfeed.tech/tags/backend.md>), [best](<https://devfeed.tech/tags/best.md>), [blog](<https://devfeed.tech/tags/blog.md>), [book](<https://devfeed.tech/tags/book.md>), [built-in](<https://devfeed.tech/tags/built-in.md>), [but](<https://devfeed.tech/tags/but.md>), [can](<https://devfeed.tech/tags/can.md>), [clojure](<https://devfeed.tech/tags/clojure.md>), [core](<https://devfeed.tech/tags/core.md>), [cpu](<https://devfeed.tech/tags/cpu.md>), [cuda](<https://devfeed.tech/tags/cuda.md>), [cudnn](<https://devfeed.tech/tags/cudnn.md>), [deep](<https://devfeed.tech/tags/deep.md>), [diamond](<https://devfeed.tech/tags/diamond.md>), [gpu](<https://devfeed.tech/tags/gpu.md>), [model](<https://devfeed.tech/tags/model.md>), [onnx](<https://devfeed.tech/tags/onnx.md>), [run](<https://devfeed.tech/tags/run.md>), [tensors](<https://devfeed.tech/tags/tensors.md>), [with](<https://devfeed.tech/tags/with.md>)

## AI overview

The excerpt shows four ways to run an MNIST ONNX model with CUDA through Deep Diamond. With the cuDNN factory, tensors use the GPU backend: either wrap the ONNX model in a Deep Diamond network or use an ONNX-created blueprint directly. Alternatively, keep input and output tensors in main memory and set `:ep [:cuda]` in the ONNX configuration; this works both inside a network wrapper and with a standalone blueprint. All four examples return class index 7, and the article presents them as options without preferring one.

## Source excerpt

Two weeks ago, I announced a new Clojure ML library, Diamond ONNX RT, which integrates ONNX Runtime into Deep Diamond. In that post, we explored the classic Hello World example of Neural Networks, MNIST handwritten image recognition, step-by-step. We run that example on the CPU, from main memory. The next logical step is to execute this stuff on the GPU. You'll see that with a little help of ClojureCUDA and Deep Diamond built-in CUDA machinery, this is both easy and simple, requiring almost no effort from a curious Clojure programmer. But don't just trust me; let's fire up your REPL, and we can continue together. Here's how you can evaluate this directly in your REPL (you can use the Hello World that is provided in the ./examples sub-folder of Diamond ONNX RT as a springboard). Require Diamond's namespaces First things first, we refer functions that we're going to use. (require '[uncomplicate.commons.core :refer [with-release]] '[uncomplicate.neanderthal.core :refer [transfer! iamax native]] '[uncomplicate.diamond [tensor :refer [tensor with-diamond]] [dnn :refer [network]] [onnxrt :refer [onnx]]] '[uncomplicate.diamond.internal.dnnl.factory :refer [dnnl-factory]] '[uncomplicate.diamond.internal.cudnn.factory :refer [cudnn-factory]] '[hello-world.native :refer [input-desc input-tz mnist-onnx]]) None of the following ways to run CUDA models has preference, you use the one that best suits your needs. Way one One of the ways to run ONNX models on your GPU is to simply use Deep Diamond's cuDNN factory as the backend for your tensors. Then, the machinery recognizes what you need and proceeds doing everything on the GPU, using the right stream for tensors, Deep Diamond operations, and ONNX Runtime operations. This looks exactly the same as any other Deep Diamond example from this blog or the DLFP book. (with-diamond cudnn-factory [] (with-release [cuda-input-tz (tensor input-desc) mnist (network cuda-input-tz [mnist-onnx]) classify! (mnist cuda-input-tz)] (transfer! input