You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This guide provides the steps to convert the `stabilityai/stable-diffusion-3-medium` model to the ONNX format for use with the CUDA execution provider. It also includes a step to address an issue with mixed-precision nodes that may occur during the conversion process.
4
+
5
+
## 1. Prerequisites and Installation
6
+
7
+
Install the required Python packages using the following `requirements.txt` content:
You can save this to a `requirements.txt` file and install it with:
20
+
```bash
21
+
pip install -r requirements.txt
22
+
```
23
+
This will install `onnxruntime-gpu` with the CUDA execution provider, which is necessary for model conversion.
24
+
25
+
## 2. Model Conversion
26
+
27
+
Run the following command to export the model to ONNX format. This command uses `optimum-cli` to convert the model to half-precision (`fp16`) on a CUDA device.
28
+
29
+
```bash
30
+
optimum-cli export onnx --model stabilityai/stable-diffusion-3-medium --dtype fp16 --device cuda fp16_optimum
31
+
```
32
+
33
+
This will download the model and convert it into multiple ONNX files in the `fp16_optimum` directory.
34
+
35
+
## 3. Correcting FP64 Nodes
36
+
37
+
The PyTorch model may contain some `fp64` nodes, which are exported as-is during the conversion. If you encounter issues with these nodes, you can use the provided `Replace_fp64.py` script to replace them with `fp32` nodes. This script will process all `.onnx` files in the input directory and save the corrected files to the output directory.
This will create a `corrected_model` directory with the FP64 nodes converted to FP32.
43
+
44
+
## 4. Using a Custom ONNX Runtime
45
+
46
+
If you have a locally built ONNX Runtime wheel with specific optimizations (e.g., for NvTensorRTRTXExecutionProvider), ensure that you install it in your environment before running inference. Additionally, be sure to uninstall the default `onnxruntime` package installed via `requirements.txt` to avoid any conflicts.
47
+
48
+
## 5. Running Inference
49
+
50
+
To run inference with the converted ONNX model, use the provided `RunSd.py` script. This script loads the ONNX model and generates an image based on a prompt.
51
+
52
+
Here is an example command to run the script:
53
+
```bash
54
+
python run_sd.py --model_path corrected_model --prompt "A beautiful landscape painting of a waterfall in a lush forest" --output_dir generated_images
55
+
```
56
+
57
+
### Command-line Arguments
58
+
59
+
The `RunSd.py` script accepts several arguments to customize the image generation process:
60
+
61
+
*`--model_path`: Path to the directory containing the ONNX models (e.g., `corrected_model`). (Required)
62
+
*`--prompt`: The text prompt to generate the image from.
63
+
*`--negative_prompt`: The prompt not to guide the image generation.
64
+
*`--height`: The height of the generated image (default: 512).
65
+
*`--width`: The width of the generated image (default: 512).
66
+
*`--steps`: The number of inference steps (default: 50).
67
+
*`--guidance_scale`: Guidance scale for the prompt (default: 7.5).
68
+
*`--seed`: A seed for reproducibility.
69
+
*`--output_dir`: The directory to save the generated images (default: `generated_images`).
70
+
*`--execution_provider`: The ONNX Runtime execution provider to use (default: `NvTensorRTRTXExecutionProvider`).
71
+
72
+
For a full list of arguments, you can run:
73
+
```bash
74
+
python run_sd.py --help
75
+
```
76
+
77
+
The generated image will be saved in the specified output directory.
0 commit comments