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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
library_name: pytorch
|
3 |
+
license: apache-2.0
|
4 |
+
pipeline_tag: unconditional-image-generation
|
5 |
+
tags:
|
6 |
+
- generative_ai
|
7 |
+
- quantized
|
8 |
+
- android
|
9 |
+
|
10 |
+
---
|
11 |
+
|
12 |
+
![](https://qaihub-public-assets.s3.us-west-2.amazonaws.com/qai-hub-models/models/controlnet_quantized/web-assets/banner.png)
|
13 |
+
|
14 |
+
# ControlNet: Optimized for Mobile Deployment
|
15 |
+
## Generating visual arts from text prompt and input guiding image
|
16 |
+
|
17 |
+
On-device, high-resolution image synthesis from text and image prompts. ControlNet guides Stable-diffusion with provided input image to generate accurate images from given input prompt.
|
18 |
+
|
19 |
+
This model is an implementation of ControlNet found [here](https://github.com/lllyasviel/ControlNet).
|
20 |
+
This repository provides scripts to run ControlNet on Qualcomm® devices.
|
21 |
+
More details on model performance across various devices, can be found
|
22 |
+
[here](https://aihub.qualcomm.com/models/controlnet_quantized).
|
23 |
+
|
24 |
+
|
25 |
+
### Model Details
|
26 |
+
|
27 |
+
- **Model Type:** Image generation
|
28 |
+
- **Model Stats:**
|
29 |
+
- Input: Text prompt and input image as a reference
|
30 |
+
- QNN-SDK: 2.19
|
31 |
+
- Text Encoder Number of parameters: 340M
|
32 |
+
- UNet Number of parameters: 865M
|
33 |
+
- VAE Decoder Number of parameters: 83M
|
34 |
+
- ControlNet Number of parameters: 361M
|
35 |
+
- Model size: 1.4GB
|
36 |
+
|
37 |
+
|
38 |
+
| Device | Chipset | Target Runtime | Inference Time (ms) | Peak Memory Range (MB) | Precision | Primary Compute Unit | Target Model
|
39 |
+
| ---|---|---|---|---|---|---|---|
|
40 |
+
| Samsung Galaxy S23 Ultra (Android 13) | Snapdragon® 8 Gen 2 | QNN Binary | 11.369 ms | 0 - 33 MB | UINT16 | NPU | [TextEncoder_Quantized.bin](https://huggingface.co/qualcomm/ControlNet/blob/main/TextEncoder_Quantized.bin)
|
41 |
+
| Samsung Galaxy S23 Ultra (Android 13) | Snapdragon® 8 Gen 2 | QNN Binary | 386.746 ms | 0 - 4 MB | UINT16 | NPU | [VAEDecoder_Quantized.bin](https://huggingface.co/qualcomm/ControlNet/blob/main/VAEDecoder_Quantized.bin)
|
42 |
+
| Samsung Galaxy S23 Ultra (Android 13) | Snapdragon® 8 Gen 2 | QNN Binary | 259.981 ms | 12 - 14 MB | UINT16 | NPU | [UNet_Quantized.bin](https://huggingface.co/qualcomm/ControlNet/blob/main/UNet_Quantized.bin)
|
43 |
+
| Samsung Galaxy S23 Ultra (Android 13) | Snapdragon® 8 Gen 2 | QNN Binary | 103.748 ms | 0 - 22 MB | UINT16 | NPU | [ControlNet_Quantized.bin](https://huggingface.co/qualcomm/ControlNet/blob/main/ControlNet_Quantized.bin)
|
44 |
+
|
45 |
+
|
46 |
+
## Installation
|
47 |
+
|
48 |
+
This model can be installed as a Python package via pip.
|
49 |
+
|
50 |
+
```bash
|
51 |
+
pip install "qai-hub-models[controlnet_quantized]"
|
52 |
+
```
|
53 |
+
|
54 |
+
|
55 |
+
|
56 |
+
## Configure Qualcomm® AI Hub to run this model on a cloud-hosted device
|
57 |
+
|
58 |
+
Sign-in to [Qualcomm® AI Hub](https://app.aihub.qualcomm.com/) with your
|
59 |
+
Qualcomm® ID. Once signed in navigate to `Account -> Settings -> API Token`.
|
60 |
+
|
61 |
+
With this API token, you can configure your client to run models on the cloud
|
62 |
+
hosted devices.
|
63 |
+
```bash
|
64 |
+
qai-hub configure --api_token API_TOKEN
|
65 |
+
```
|
66 |
+
Navigate to [docs](https://app.aihub.qualcomm.com/docs/) for more information.
|
67 |
+
|
68 |
+
|
69 |
+
|
70 |
+
## Demo on-device
|
71 |
+
|
72 |
+
The package contains a simple end-to-end demo that downloads pre-trained
|
73 |
+
weights and runs this model on a sample input.
|
74 |
+
|
75 |
+
```bash
|
76 |
+
python -m qai_hub_models.models.controlnet_quantized.demo
|
77 |
+
```
|
78 |
+
|
79 |
+
The above demo runs a reference implementation of pre-processing, model
|
80 |
+
inference, and post processing.
|
81 |
+
|
82 |
+
**NOTE**: If you want running in a Jupyter Notebook or Google Colab like
|
83 |
+
environment, please add the following to your cell (instead of the above).
|
84 |
+
```
|
85 |
+
%run -m qai_hub_models.models.controlnet_quantized.demo
|
86 |
+
```
|
87 |
+
|
88 |
+
|
89 |
+
### Run model on a cloud-hosted device
|
90 |
+
|
91 |
+
In addition to the demo, you can also run the model on a cloud-hosted Qualcomm®
|
92 |
+
device. This script does the following:
|
93 |
+
* Performance check on-device on a cloud-hosted device
|
94 |
+
* Downloads compiled assets that can be deployed on-device for Android.
|
95 |
+
* Accuracy check between PyTorch and on-device outputs.
|
96 |
+
|
97 |
+
```bash
|
98 |
+
python -m qai_hub_models.models.controlnet_quantized.export
|
99 |
+
```
|
100 |
+
|
101 |
+
```
|
102 |
+
Profile Job summary of TextEncoder_Quantized
|
103 |
+
--------------------------------------------------
|
104 |
+
Device: Samsung Galaxy S23 Ultra (13)
|
105 |
+
Estimated Inference Time: 11.37 ms
|
106 |
+
Estimated Peak Memory Range: 0.05-33.25 MB
|
107 |
+
Compute Units: NPU (570) | Total (570)
|
108 |
+
|
109 |
+
Profile Job summary of VAEDecoder_Quantized
|
110 |
+
--------------------------------------------------
|
111 |
+
Device: Samsung Galaxy S23 Ultra (13)
|
112 |
+
Estimated Inference Time: 386.75 ms
|
113 |
+
Estimated Peak Memory Range: 0.12-4.28 MB
|
114 |
+
Compute Units: NPU (409) | Total (409)
|
115 |
+
|
116 |
+
Profile Job summary of UNet_Quantized
|
117 |
+
--------------------------------------------------
|
118 |
+
Device: Samsung Galaxy S23 Ultra (13)
|
119 |
+
Estimated Inference Time: 259.98 ms
|
120 |
+
Estimated Peak Memory Range: 12.45-14.35 MB
|
121 |
+
Compute Units: NPU (5434) | Total (5434)
|
122 |
+
|
123 |
+
Profile Job summary of ControlNet_Quantized
|
124 |
+
--------------------------------------------------
|
125 |
+
Device: Samsung Galaxy S23 Ultra (13)
|
126 |
+
Estimated Inference Time: 103.75 ms
|
127 |
+
Estimated Peak Memory Range: 0.19-22.20 MB
|
128 |
+
Compute Units: NPU (2406) | Total (2406)
|
129 |
+
|
130 |
+
|
131 |
+
```
|
132 |
+
## How does this work?
|
133 |
+
|
134 |
+
This [export script](https://github.com/quic/ai-hub-models/blob/main/qai_hub_models/models/ControlNet/export.py)
|
135 |
+
leverages [Qualcomm® AI Hub](https://aihub.qualcomm.com/) to optimize, validate, and deploy this model
|
136 |
+
on-device. Lets go through each step below in detail:
|
137 |
+
|
138 |
+
Step 1: **Upload compiled model**
|
139 |
+
|
140 |
+
Upload compiled models from `qai_hub_models.models.controlnet_quantized` on hub.
|
141 |
+
```python
|
142 |
+
import torch
|
143 |
+
|
144 |
+
import qai_hub as hub
|
145 |
+
from qai_hub_models.models.controlnet_quantized import Model
|
146 |
+
|
147 |
+
# Load the model
|
148 |
+
model = Model.from_precompiled()
|
149 |
+
|
150 |
+
model_textencoder_quantized = hub.upload_model(model.text_encoder.get_target_model_path())
|
151 |
+
model_unet_quantized = hub.upload_model(model.unet.get_target_model_path())
|
152 |
+
model_vaedecoder_quantized = hub.upload_model(model.vae_decoder.get_target_model_path())
|
153 |
+
model_controlnet_quantized = hub.upload_model(model.controlnet.get_target_model_path())
|
154 |
+
```
|
155 |
+
|
156 |
+
|
157 |
+
Step 2: **Performance profiling on cloud-hosted device**
|
158 |
+
|
159 |
+
After uploading compiled models from step 1. Models can be profiled model on-device using the
|
160 |
+
`target_model`. Note that this scripts runs the model on a device automatically
|
161 |
+
provisioned in the cloud. Once the job is submitted, you can navigate to a
|
162 |
+
provided job URL to view a variety of on-device performance metrics.
|
163 |
+
```python
|
164 |
+
|
165 |
+
# Device
|
166 |
+
device = hub.Device("Samsung Galaxy S23")
|
167 |
+
profile_job_textencoder_quantized = hub.submit_profile_job(
|
168 |
+
model=model_textencoder_quantized,
|
169 |
+
device=device,
|
170 |
+
)
|
171 |
+
profile_job_unet_quantized = hub.submit_profile_job(
|
172 |
+
model=model_unet_quantized,
|
173 |
+
device=device,
|
174 |
+
)
|
175 |
+
profile_job_vaedecoder_quantized = hub.submit_profile_job(
|
176 |
+
model=model_vaedecoder_quantized,
|
177 |
+
device=device,
|
178 |
+
)
|
179 |
+
profile_job_controlnet_quantized = hub.submit_profile_job(
|
180 |
+
model=model_controlnet_quantized,
|
181 |
+
device=device,
|
182 |
+
)
|
183 |
+
|
184 |
+
```
|
185 |
+
|
186 |
+
Step 3: **Verify on-device accuracy**
|
187 |
+
|
188 |
+
To verify the accuracy of the model on-device, you can run on-device inference
|
189 |
+
on sample input data on the same cloud hosted device.
|
190 |
+
```python
|
191 |
+
|
192 |
+
input_data_textencoder_quantized = model.text_encoder.sample_inputs()
|
193 |
+
inference_job_textencoder_quantized = hub.submit_inference_job(
|
194 |
+
model=model_textencoder_quantized,
|
195 |
+
device=device,
|
196 |
+
inputs=input_data_textencoder_quantized,
|
197 |
+
)
|
198 |
+
on_device_output_textencoder_quantized = inference_job_textencoder_quantized.download_output_data()
|
199 |
+
|
200 |
+
input_data_unet_quantized = model.unet.sample_inputs()
|
201 |
+
inference_job_unet_quantized = hub.submit_inference_job(
|
202 |
+
model=model_unet_quantized,
|
203 |
+
device=device,
|
204 |
+
inputs=input_data_unet_quantized,
|
205 |
+
)
|
206 |
+
on_device_output_unet_quantized = inference_job_unet_quantized.download_output_data()
|
207 |
+
|
208 |
+
input_data_vaedecoder_quantized = model.vae_decoder.sample_inputs()
|
209 |
+
inference_job_vaedecoder_quantized = hub.submit_inference_job(
|
210 |
+
model=model_vaedecoder_quantized,
|
211 |
+
device=device,
|
212 |
+
inputs=input_data_vaedecoder_quantized,
|
213 |
+
)
|
214 |
+
on_device_output_vaedecoder_quantized = inference_job_vaedecoder_quantized.download_output_data()
|
215 |
+
|
216 |
+
input_data_controlnet_quantized = model.controlnet.sample_inputs()
|
217 |
+
inference_job_controlnet_quantized = hub.submit_inference_job(
|
218 |
+
model=model_controlnet_quantized,
|
219 |
+
device=device,
|
220 |
+
inputs=input_data_controlnet_quantized,
|
221 |
+
)
|
222 |
+
on_device_output_controlnet_quantized = inference_job_controlnet_quantized.download_output_data()
|
223 |
+
|
224 |
+
```
|
225 |
+
With the output of the model, you can compute like PSNR, relative errors or
|
226 |
+
spot check the output with expected output.
|
227 |
+
|
228 |
+
**Note**: This on-device profiling and inference requires access to Qualcomm®
|
229 |
+
AI Hub. [Sign up for early access](https://aihub.qualcomm.com/sign-up).
|
230 |
+
|
231 |
+
|
232 |
+
|
233 |
+
## Deploying compiled model to Android
|
234 |
+
|
235 |
+
|
236 |
+
The models can be deployed using multiple runtimes:
|
237 |
+
- TensorFlow Lite (`.tflite` export): [This
|
238 |
+
tutorial](https://www.tensorflow.org/lite/android/quickstart) provides a
|
239 |
+
guide to deploy the .tflite model in an Android application.
|
240 |
+
|
241 |
+
|
242 |
+
- QNN ( `.so` / `.bin` export ): This [sample
|
243 |
+
app](https://docs.qualcomm.com/bundle/publicresource/topics/80-63442-50/sample_app.html)
|
244 |
+
provides instructions on how to use the `.so` shared library or `.bin` context binary in an Android application.
|
245 |
+
|
246 |
+
|
247 |
+
## View on Qualcomm® AI Hub
|
248 |
+
Get more details on ControlNet's performance across various devices [here](https://aihub.qualcomm.com/models/controlnet_quantized).
|
249 |
+
Explore all available models on [Qualcomm® AI Hub](https://aihub.qualcomm.com/)
|
250 |
+
|
251 |
+
## License
|
252 |
+
- The license for the original implementation of ControlNet can be found
|
253 |
+
[here](https://github.com/lllyasviel/ControlNet/blob/main/LICENSE).
|
254 |
+
- The license for the compiled assets for on-device deployment can be found [here](https://qaihub-public-assets.s3.us-west-2.amazonaws.com/qai-hub-models/Qualcomm+AI+Hub+Proprietary+License.pdf).
|
255 |
+
|
256 |
+
## References
|
257 |
+
* [Adding Conditional Control to Text-to-Image Diffusion Models](https://arxiv.org/abs/2302.05543)
|
258 |
+
* [Source Model Implementation](https://github.com/lllyasviel/ControlNet)
|
259 |
+
|
260 |
+
## Community
|
261 |
+
* Join [our AI Hub Slack community](https://join.slack.com/t/qualcomm-ai-hub/shared_invite/zt-2dgf95loi-CXHTDRR1rvPgQWPO~ZZZJg) to collaborate, post questions and learn more about on-device AI.
|
262 |
+
* For questions or feedback please [reach out to us](mailto:ai-hub-support@qti.qualcomm.com).
|
263 |
+
|
264 |
+
|
265 |
+
## Usage and Limitations
|
266 |
+
|
267 |
+
Model may not be used for or in connection with any of the following applications:
|
268 |
+
|
269 |
+
- Accessing essential private and public services and benefits;
|
270 |
+
- Administration of justice and democratic processes;
|
271 |
+
- Assessing or recognizing the emotional state of a person;
|
272 |
+
- Biometric and biometrics-based systems, including categorization of persons based on sensitive characteristics;
|
273 |
+
- Education and vocational training;
|
274 |
+
- Employment and workers management;
|
275 |
+
- Exploitation of the vulnerabilities of persons resulting in harmful behavior;
|
276 |
+
- General purpose social scoring;
|
277 |
+
- Law enforcement;
|
278 |
+
- Management and operation of critical infrastructure;
|
279 |
+
- Migration, asylum and border control management;
|
280 |
+
- Predictive policing;
|
281 |
+
- Real-time remote biometric identification in public spaces;
|
282 |
+
- Recommender systems of social media platforms;
|
283 |
+
- Scraping of facial images (from the internet or otherwise); and/or
|
284 |
+
- Subliminal manipulation
|
285 |
+
|
286 |
+
|