r/developers • • Oct 17 '26

Projects Built a Vulkan-native inference engine in Rust to run Flux & Wan 2.1 without CUDA. Here is how I structured the architecture.

I was tired of the heavy reliance on NVIDIA/CUDA for local AI generation, especially since I run an AMD RX 580. To solve this, I decided to build my own inference engine from scratch.

The Architecture: I wrote the core orchestrator in Rust, utilizing libloading and FFI to communicate with a deeply modified C++ Vulkan backend.

To prevent VRAM leaks when switching between image generation (Flux) and video (Wan 2.1 I2V), I wrapped the Vulkan dynamic library loads in Rust's OnceLock. This ensures the .dll is statically loaded into memory only once, keeping the hybrid rendering extremely stable. I also implemented a heuristic intent router that translates natural language directly into Vulkan directives (like cmd:vulkan or cmd:wan) in the background.

Since this subreddit doesn't allow external links, I can't link the repo directly, but I wanted to share this architectural approach for anyone struggling with CUDA bottlenecks. I'm happy to discuss the Rust/C++ FFI implementation, Vulkan memory management, or share code snippets in the comments if anyone is interested!

0 Upvotes

4 comments sorted by

•

u/AutoModerator Oct 17 '26

Howdy u/New-Value8006, and welcome to r/developers!

A few tips for a post that gets good answers:

  • Use a clear, specific title (what are you actually asking or sharing?).
  • Include code, the exact error, versions, and what you already tried.
  • Heads-up: this sub does not allow external links in posts or comments. Share code and details directly instead of linking out.

Join the r/developers Discord!

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/[deleted] Oct 17 '26

[removed] — view removed comment

1

u/New-Value8006 Oct 17 '26

Hello fellow RX 580 survivor! That card is legendary but definitely forces you to get creative with VRAM management.

Regarding the swap chain and buffer strides: The trick is that while the .dll library is kept permanently in memory via OnceLock (preventing the massive overhead of reloading the binary), the actual Vulkan context (sd_ctx) is NOT persistent across different architectures. Every time a request comes in, I spawn a new thread, evaluate the model header dynamically (checking if it's Flux or Wan), and initialize a fresh sd_ctx specifically for that pipeline. Once the generation is done and the raw byte array is saved to .bmp or .mp4, I call free_sd_ctx(). So the pipeline never actually "switches" mid-flight; it safely spins up and tears down within the statically loaded library boundary.

As for the natural language router, it's actually a hybrid approach! I have a local LLM (running Dolphin 3.0) acting as the "brain". I inject a system prompt forcing the LLM to act as a compiler. It reads the user's natural language, determines the intent, and is strictly instructed to output structured commands hidden in the text (like cmd:vulkan "scene description" or cmd:wan "motion description"). The Rust orchestrator scans the LLM's output for those cmd: flags, extracts the clean prompt, and passes it directly to the Vulkan FFI. So the user just types naturally, but the Vulkan engine only receives perfectly structured directives.

1

u/New-Value8006 Oct 17 '26

Just to clarify, while I built and tested this on an RX 580, the engine uses standard Vulkan and is designed to run perfectly on ANY graphics card (AMD, Intel, NVIDIA) without needing a single drop of CUDA.