Stable diffusion models with Ort or candle

Hello,

I have a very low understanding of machine learning, but I’m tinkering with several models from hugging face to see what is possible. To get an idea of how practical software depending on ML-based AI is, I’d like to write a small/medium-sized application that uses it. The hugging face repository of models and the seamless Python interface is great for quickly experimenting, but for a larger project, I’d rather write it in Rust. I was wondering if it were possible to integrate any of these models into a Rust application.

For the first test, I’ve tried text to image models (as I find it very impressive to be able to generate photorealistic images in tens of seconds out of thin air). I’ve browsed the hug, and selected some with various fine-tuning to compare them. Not all of them worked out of the box, some only provide a .safetensors file, so I had to download them and load them directly with StableDiffusionXLPipeline.from_single_file. It works great. Now, I’d like to be able to do the same in Rust. By googling this, I’ve fallen on ort, which seems to be an ONNX binding library for Rust. They say I just need to convert the models to the ONNX format, and then I’m good to go, but (after painfully managing to do the conversion without the model_index.json and config.json files, which optimum-cli apparently really wants) I get tons of files in the ONNX format, not a single .safetensors file. So I’m a bit lost about what I’m supposed do to next to.

I’ve seen there is an alternative to Ort, that is, candle, which is developed by Hugging Face, so maybe folks around here have more knowledge about it. I’m also open to hints on how to make this work with candle.

Concretely, say I’d like to have the PixelGen model working in Rust. Is there a tutorial somewhere on how to do that? Or could you provide a MWE with it?

1 Like

I haven’t tried Rust yet. For Candle, there’s a dedicated channel on the HF Discord, so it’s best to ask technical questions there.

However, for this time, I think the samples for StableDiffusion and StableDiffusion3 on GitHub can be reused almost as-is.