OpenFlowLM-Next icon indicating copy to clipboard operation
OpenFlowLM-Next copied to clipboard

Image generation on the NPU: install FLUX.2 [klein] 4B and run it with `oflm image`

Open Cyronius opened this issue 2 months ago • 3 comments

What this is

OpenFlowLM can now turn a text prompt into an image entirely on the NPU, using FLUX.2 [klein] 4B, a small, fast image model from Black Forest Labs (Apache 2.0). Every step runs on the NPU: reading the prompt, the four denoising steps, and turning the result into pixels. The CPU only hands over the prompt and saves the file.

A PR adding support is in progress. When it lands, anyone can install the model and run it:

oflm image flux2-klein:4b "a red fox in fresh snow" -o fox.png

How fast is it?

Time for one image, 4 steps:

512 × 512 1024 × 1024
Ryzen AI 9 HX 370 NPU (this engine) 5.1 s 13.4 s
Same laptop's Radeon 890M integrated GPU (PyTorch + ROCm) 20.2 s 134 s
Same laptop's CPU ~50 s not measured
NVIDIA RTX 4090 (desktop GPU), bf16 ~0.5–0.8 s (estimate) ~1.5–2 s (estimate)
NVIDIA H100 (datacenter GPU), bf16 0.19 s 0.57 s

The laptop rows were measured on the same machine with nothing else running. The H100 row comes from a third-party benchmark. The RTX 4090 row is an estimate scaled from the H100 result, not a measurement.

A desktop or datacenter GPU is still far faster. The NPU's case is running well on a laptop with no discrete GPU.

The integrated GPU is slower mostly because PyTorch has no fast attention kernel for it yet. The NPU also leaves the CPU almost idle (under 0.1 s of CPU time per image), while the integrated GPU keeps a CPU core busy for the whole image.

Images are coherent and render text legibly. They differ slightly from a reference run on the CPU, which is expected from the NPU's lower-precision math.

What's left to do

  • [ ] Package it as a model. A new q4nx-build --open-diffusion turns the Hugging Face checkpoint into a model folder in one command, and oflm pull flux2-klein:4b downloads it.
  • [ ] Ship the NPU kernels with the installer (about 15 MB), the same way the other open models' kernels ship.
  • [ ] Make the model folder portable. Today the engine uses file paths from the build machine.
  • [ ] Read the prompt in C++. Today a Python script turns the prompt into tokens.
  • [ ] Save compressed PNG or JPEG. Today's PNGs are uncompressed (3 MB at 1024 × 1024).
  • [ ] Add the oflm image command.
  • [ ] Upload the model to Hugging Face as Atomic-Germ/FLUX.2-klein-4B-NPU2 (about 8.9 GB).

Done when

On a fresh machine with only the installer and an internet connection, the command above downloads the model and writes the image.

Open questions

  • How to pass the prompt: as a plain argument (oflm image <model> "prompt") or as a flag (--text "prompt")? Leaning towards the plain argument.
  • Default image size: 1024 × 1024 (about 13 s) or 512 × 512 (about 5 s)? Leaning towards 1024, which matches what the API will use.
  • The Hugging Face upload: who does it?

Not part of this issue

  • The web API (/v1/images/generations): that is #128, which builds on this one.
  • Editing existing images: that needs more NPU work first (encoding an input image).

A note on Q4NX and GGUF

This model doesn't use Q4NX: its folder holds weights repacked for the NPU kernels. Once GGUF support lands (#14), a follow-up could let oflm install from the original checkpoint or a BF16 GGUF and repack it locally, instead of downloading a pre-packed folder. Quantized GGUFs would need new kernels to run at their own precision.

Follow-ups: #129 (running quantized GGUFs natively) and #130 (installing from a transformer GGUF plus a separate text encoder and VAE).

More detail

On branch feat/open-diffusion:

  • the plan: specs/open-diffusion/plans/cli.md
  • the spec, with all measurements: specs/open-diffusion/spec.md

Cyronius avatar Sep 27 '26 19:09 Cyronius

We could marry q4nx-build and oflm-add to get a kind of what you describe there for gguf

Atomic-Germ avatar Oct 15 '26 04:10 Atomic-Germ

5. We could marry q4nx-build and oflm-add to get a kind of what you describe there for gguf

this PR existed before gguf support was merged. I don't know how much we need any of the q4nx stuff. I will update the PR to remove q4nx and just use gguf directly

Cyronius avatar Oct 18 '26 12:10 Cyronius

@Cyronius - It's done in #232 now! Working smoothly. But, rather than oflm-add as a python package, oflm add is a blending of olfm-add and q4nx-build that makes it work and it actually does the container making at install time, links based on geometry

$ oflm add Atomic-Germ/FLUX.2-Klein-4B-OpenFLM-GGUF (it's not actually a special repo, I just don't have unneeded quants in it)

[OFLM]  Kernels (beside the model): /home/atomic-germ/.config/oflm/models/FLUX.2-Klein-4B-OpenFLM-GGUF/open_kernels
[OFLM]  Wrote /home/atomic-germ/Projects/OFLM-GGUF/oflm-12236572173665685734.png
[OFLM]  Seed 12236572173665685734, 4 denoising step(s) at 512x512
[OFLM]  4.8 s on the NPU (text 0.42, steps 3.92, vae 0.44)

So, it's damn fast. The same test for 1024 was 14.9 s. My system is a Framework 13 Ryzen AI 340, which isn't exactly the highest end with the chip in it. This is the only reasonable way to generate images on it really.

Atomic-Germ avatar Nov 06 '26 02:11 Atomic-Germ