Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit 3024601

Browse files
authored
Merge pull request #136 from saddam213/convert
StableCascade Model Conversion Script
2 parents 2e998ca + 06e19de commit 3024601

37 files changed

+4211
-0
lines changed

OnnxStack.Converter/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# OnnxStack.Converter
2+
3+
## Requirements
4+
```bash
5+
pip install onnxruntime-directml
6+
pip install olive-ai[directml]
7+
python -m pip install -r requirements.txt
8+
```
9+
10+
## Usage
11+
```bash
12+
convert.py --optimize --model_input '..\stable-diffusion-v1-5' --model_output '..\converted' --controlnet
13+
```
14+
`--optimize` - Run the model optimization
15+
16+
`--model_input` - Safetensor model to convert
17+
18+
`--model_output` - Output for converted ONNX model (NOTE: This folder is deleted before each run)
19+
20+
`--controlnet` - Create a ControlNet enabled Unet model
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/footprints/
2+
/cache/
3+
/result_*.png
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# -------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License.
4+
# --------------------------------------------------------------------------
5+
6+
vae_sample_size = 768
7+
unet_sample_size = 96
8+
cross_attention_dim = 768
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
{
2+
"input_model": {
3+
"type": "PyTorchModel",
4+
"config": {
5+
"model_path": "SimianLuo/LCM_Dreamshaper_v7",
6+
"model_loader": "controlnet_unet_load",
7+
"model_script": "models.py",
8+
"io_config": {
9+
"input_names": [ "sample", "timestep", "encoder_hidden_states", "down_block_0_additional_residual", "down_block_1_additional_residual", "down_block_2_additional_residual", "down_block_3_additional_residual", "down_block_4_additional_residual", "down_block_5_additional_residual", "down_block_6_additional_residual", "down_block_7_additional_residual", "down_block_8_additional_residual", "down_block_9_additional_residual", "down_block_10_additional_residual", "down_block_11_additional_residual", "mid_block_additional_residual", "return_dict" ],
10+
"output_names": [ "out_sample" ],
11+
"dynamic_axes": {
12+
"sample": {"0": "unet_sample_batch", "1": "unet_sample_channels", "2": "unet_sample_height", "3": "unet_sample_width"},
13+
"timestep": {"0": "unet_time_batch"},
14+
"encoder_hidden_states": {"0": "unet_hidden_batch", "1": "unet_hidden_sequence"},
15+
"timestep_cond": { "0": "batch_size" },
16+
"down_block_0_additional_residual": {"0": "cnet_db0_batch", "1": "cnet_db0_channels", "2": "cnet_db0_height", "3": "cnet_db0_width"},
17+
"down_block_1_additional_residual": {"0": "cnet_db1_batch", "1": "cnet_db1_channels", "2": "cnet_db1_height", "3": "cnet_db1_width"},
18+
"down_block_2_additional_residual": {"0": "cnet_db2_batch", "1": "cnet_db2_channels", "2": "cnet_db2_height", "3": "cnet_db2_width"},
19+
"down_block_3_additional_residual": {"0": "cnet_db3_batch", "1": "cnet_db3_channels", "2": "cnet_db3_height2", "3": "cnet_db3_width2"},
20+
"down_block_4_additional_residual": {"0": "cnet_db4_batch", "1": "cnet_db4_channels", "2": "cnet_db4_height2", "3": "cnet_db4_width2"},
21+
"down_block_5_additional_residual": {"0": "cnet_db5_batch", "1": "cnet_db5_channels", "2": "cnet_db5_height2", "3": "cnet_db5_width2"},
22+
"down_block_6_additional_residual": {"0": "cnet_db6_batch", "1": "cnet_db6_channels", "2": "cnet_db6_height4", "3": "cnet_db6_width4"},
23+
"down_block_7_additional_residual": {"0": "cnet_db7_batch", "1": "cnet_db7_channels", "2": "cnet_db7_height4", "3": "cnet_db7_width4"},
24+
"down_block_8_additional_residual": {"0": "cnet_db8_batch", "1": "cnet_db8_channels", "2": "cnet_db8_height4", "3": "cnet_db8_width4"},
25+
"down_block_9_additional_residual": {"0": "cnet_db9_batch", "1": "cnet_db9_channels", "2": "cnet_db9_height8", "3": "cnet_db9_width8"},
26+
"down_block_10_additional_residual": {"0": "cnet_db10_batch", "1": "cnet_db10_channels", "2": "cnet_db10_height8", "3": "cnet_db10_width8"},
27+
"down_block_11_additional_residual": {"0": "cnet_db11_batch", "1": "cnet_db11_channels", "2": "cnet_db11_height8", "3": "cnet_db11_width8"},
28+
"mid_block_additional_residual": {"0": "cnet_mbar_batch", "1": "cnet_mbar_channels", "2": "cnet_mbar_height8", "3": "cnet_mbar_width8"}
29+
}
30+
},
31+
"dummy_inputs_func": "controlnet_unet_conversion_inputs"
32+
}
33+
},
34+
"systems": {
35+
"local_system": {
36+
"type": "LocalSystem",
37+
"config": {
38+
"accelerators": [
39+
{
40+
"device": "gpu",
41+
"execution_providers": [
42+
"DmlExecutionProvider"
43+
]
44+
}
45+
]
46+
}
47+
}
48+
},
49+
"evaluators": {
50+
"common_evaluator": {
51+
"metrics": [
52+
{
53+
"name": "latency",
54+
"type": "latency",
55+
"sub_types": [{"name": "avg"}],
56+
"user_config": {
57+
"user_script": "models.py",
58+
"dataloader_func": "controlnet_unet_data_loader",
59+
"batch_size": 2
60+
}
61+
}
62+
]
63+
}
64+
},
65+
"passes": {
66+
"convert": {
67+
"type": "OnnxConversion",
68+
"config": {
69+
"target_opset": 14,
70+
"save_as_external_data": true,
71+
"all_tensors_to_one_file": true,
72+
"external_data_name": "weights.pb"
73+
}
74+
},
75+
"optimize": {
76+
"type": "OrtTransformersOptimization",
77+
"config": {
78+
"model_type": "unet",
79+
"opt_level": 0,
80+
"float16": true,
81+
"use_gpu": true,
82+
"keep_io_types": false,
83+
"optimization_options": {
84+
"enable_gelu": true,
85+
"enable_layer_norm": true,
86+
"enable_attention": true,
87+
"use_multi_head_attention": true,
88+
"enable_skip_layer_norm": false,
89+
"enable_embed_layer_norm": true,
90+
"enable_bias_skip_layer_norm": false,
91+
"enable_bias_gelu": true,
92+
"enable_gelu_approximation": false,
93+
"enable_qordered_matmul": false,
94+
"enable_shape_inference": true,
95+
"enable_gemm_fast_gelu": false,
96+
"enable_nhwc_conv": false,
97+
"enable_group_norm": true,
98+
"enable_bias_splitgelu": false,
99+
"enable_packed_qkv": true,
100+
"enable_packed_kv": true,
101+
"enable_bias_add": false,
102+
"group_norm_channels_last": false
103+
},
104+
"force_fp32_ops": ["RandomNormalLike"],
105+
"force_fp16_inputs": {
106+
"GroupNorm": [0, 1, 2]
107+
}
108+
}
109+
}
110+
},
111+
"pass_flows": [
112+
["convert", "optimize"]
113+
],
114+
"engine": {
115+
"log_severity_level": 0,
116+
"evaluator": "common_evaluator",
117+
"evaluate_input_model": false,
118+
"host": "local_system",
119+
"target": "local_system",
120+
"cache_dir": "cache",
121+
"output_name": "controlnet",
122+
"output_dir": "footprints"
123+
}
124+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
{
2+
"input_model": {
3+
"type": "PyTorchModel",
4+
"config": {
5+
"model_path": "SimianLuo/LCM_Dreamshaper_v7",
6+
"model_loader": "safety_checker_load",
7+
"model_script": "models.py",
8+
"io_config": {
9+
"input_names": [ "clip_input", "images" ],
10+
"output_names": [ "out_images", "has_nsfw_concepts" ],
11+
"dynamic_axes": {
12+
"clip_input": { "0": "batch", "1": "channels", "2": "height", "3": "width" },
13+
"images": { "0": "batch", "1": "height", "2": "width", "3": "channels" }
14+
}
15+
},
16+
"dummy_inputs_func": "safety_checker_conversion_inputs"
17+
}
18+
},
19+
"systems": {
20+
"local_system": {
21+
"type": "LocalSystem",
22+
"config": {
23+
"accelerators": [
24+
{
25+
"device": "gpu",
26+
"execution_providers": [
27+
"DmlExecutionProvider"
28+
]
29+
}
30+
]
31+
}
32+
}
33+
},
34+
"evaluators": {
35+
"common_evaluator": {
36+
"metrics": [
37+
{
38+
"name": "latency",
39+
"type": "latency",
40+
"sub_types": [{"name": "avg"}],
41+
"user_config": {
42+
"user_script": "models.py",
43+
"dataloader_func": "safety_checker_data_loader",
44+
"batch_size": 1
45+
}
46+
}
47+
]
48+
}
49+
},
50+
"passes": {
51+
"convert": {
52+
"type": "OnnxConversion",
53+
"config": {
54+
"target_opset": 14
55+
}
56+
},
57+
"ov_convert": {
58+
"type": "OpenVINOConversion",
59+
"config": {
60+
"user_script": "models.py",
61+
"example_input_func": "safety_checker_conversion_inputs",
62+
"output_model": "safety_checker"
63+
}
64+
},
65+
"optimize": {
66+
"type": "OrtTransformersOptimization",
67+
"config": {
68+
"model_type": "unet",
69+
"opt_level": 0,
70+
"float16": true,
71+
"use_gpu": true,
72+
"keep_io_types": false,
73+
"optimization_options": {
74+
"enable_gelu": true,
75+
"enable_layer_norm": true,
76+
"enable_attention": true,
77+
"use_multi_head_attention": true,
78+
"enable_skip_layer_norm": false,
79+
"enable_embed_layer_norm": true,
80+
"enable_bias_skip_layer_norm": false,
81+
"enable_bias_gelu": true,
82+
"enable_gelu_approximation": false,
83+
"enable_qordered_matmul": false,
84+
"enable_shape_inference": true,
85+
"enable_gemm_fast_gelu": false,
86+
"enable_nhwc_conv": false,
87+
"enable_group_norm": true,
88+
"enable_bias_splitgelu": false,
89+
"enable_packed_qkv": true,
90+
"enable_packed_kv": true,
91+
"enable_bias_add": false,
92+
"group_norm_channels_last": false
93+
},
94+
"force_fp32_ops": ["RandomNormalLike"],
95+
"force_fp16_inputs": {
96+
"GroupNorm": [0, 1, 2]
97+
}
98+
}
99+
},
100+
"optimize_cuda": {
101+
"type": "OrtTransformersOptimization",
102+
"config": {
103+
"model_type": "unet",
104+
"opt_level": 0,
105+
"float16": true,
106+
"use_gpu": true,
107+
"keep_io_types": false
108+
}
109+
}
110+
},
111+
"pass_flows": [
112+
["convert", "optimize"]
113+
],
114+
"engine": {
115+
"log_severity_level": 0,
116+
"evaluator": "common_evaluator",
117+
"evaluate_input_model": false,
118+
"host": "local_system",
119+
"target": "local_system",
120+
"cache_dir": "cache",
121+
"output_name": "safety_checker",
122+
"output_dir": "footprints"
123+
}
124+
}

0 commit comments

Comments
 (0)