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