Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use time step embedding from file #928

Merged
merged 29 commits into from
Jan 14, 2025

Conversation

RSMNYS
Copy link
Contributor

@RSMNYS RSMNYS commented Oct 28, 2024

Timestamp-embedding-parser

anhappdev and others added 4 commits October 15, 2024 13:10
* Add TaskConfig.CustomConfig and pass them to backend

* Add CustomConfig for main.cc

* Use seed and num_steps from CustomConfig for TFLite backend

* Replace std::cout with LOG(INFO)

* Format files
* Add ConvertOutputs() API

* Add ConvertOutputs() for mobile_back_tflite

* Set minimum macos version

* Set minimum macos version to 13.1

* Update _kIphoneOnGitHubAction
Copy link

github-actions bot commented Oct 28, 2024

MLCommons CLA bot All contributors have signed the MLCommons CLA ✍️ ✅

Copy link

@freedomtan
Copy link
Contributor

@mohitmundhragithub to share the code used to generate this embedding.

…#924)

* Add GetConfigValue()

* Add custom setting data-format for Core ML

* Use GetConfigValue() to get stable_diffusion_seed and stable_diffusion_num_steps
…sion (#930)

* Set android:extractNativeLibs="true"

* Set android.bundle.enableUncompressedNativeLibs=false
@mohitmundhragithub
Copy link
Contributor

@RSMNYS, The function where the time step embedding is generated is shared here: https://github.com/mlcommons/submissions_mobile_v4.1/issues/6#issuecomment-2399205941

From QPM Tutorial package, if we go to this file: <qpm_package>\StableDiffusion1.5\model\example1\redefined_modules\diffusers\models\embeddings.py
get_timestep_embedding( ) function generates the time embeddings.

<qpm_package>\StableDiffusion1.5\model\example3\host_linux_target_android_without_native\qnn_model_execution.ipynb
Above function is called from the top level notebook: get_time_embedding( )

image

@RSMNYS
Copy link
Contributor Author

RSMNYS commented Nov 18, 2024

@RSMNYS, The function where the time step embedding is generated is shared here: https://github.com/mlcommons/submissions_mobile_v4.1/issues/6#issuecomment-2399205941

From QPM Tutorial package, if we go to this file: <qpm_package>\StableDiffusion1.5\model\example1\redefined_modules\diffusers\models\embeddings.py
get_timestep_embedding( ) function generates the time embeddings.

<qpm_package>\StableDiffusion1.5\model\example3\host_linux_target_android_without_native\qnn_model_execution.ipynb
Above function is called from the top level notebook: get_time_embedding( )

image

@mohitmundhragithub Shown code is only for the embedding generation. And no operations from the cut part are shown. (fully connected layer, Logistic/Sigmoid activation, and mul operation.

@freedomtan
Copy link
Contributor

@anhappdev to share the QPM Stable Diffusion Jupyter notebook with @RSMNYS

@freedomtan
Copy link
Contributor

@AhmedTElthakeb to provide a small tflite with only 3 operations.

@mohitmundhragithub will try to compare tflite and onnx files.

@AhmedTElthakeb
Copy link
Contributor

@AhmedTElthakeb to provide a small tflite with only 3 operations.
sdv15_time_emb_head_int8.zip

@RSMNYS
Copy link
Contributor Author

RSMNYS commented Dec 2, 2024

Hi guys! So I've used the model prepared by Ahmed to generate the embedding file. Now all works good. Here the link to the colab: https://colab.research.google.com/drive/1LG_rC5dlx2CbW2ZF4EamOnCdwtwLQESv?usp=sharing to prepare the embedding file, and the link to the models and embedding file: https://drive.google.com/drive/folders/1CT6VUWwGaTw34Za6dTJE7ptms20NUKH3?usp=sharing

* master:
  chore: increase Android minSdkVersion from 21 to 30 (#859)
  fix: resolve crash due to permission denied on Android Play Store version (#930)
  refactor: use custom setting in Core ML backend to detect NCHW input. (#924)

# Conflicts:
#	mobile_back_tflite/cpp/backend_tflite/stable_diffusion_pipeline.cc
@freedomtan
Copy link
Contributor

freedomtan commented Dec 3, 2024

please use pickle for saving the embedding.

@mohitmundhragithub please help figure out the difference between the one generated by @RSMNYS and Q's.

@freedomtan
Copy link
Contributor

freedomtan commented Dec 3, 2024

once the pickle is done, please make the embedding part of the files to be downloaded for Stable Diffusion for the TFLite backend.

@RSMNYS
Copy link
Contributor Author

RSMNYS commented Dec 3, 2024

please use .pkl for the save the embedding.

@mohitmundhragithub please help figure out the difference between the one generated by @RSMNYS and Q's.

here you can find 2 files in json format to be able to compare numbers at least: https://drive.google.com/drive/folders/1SO1akyvWd2uYz9Xf_u5OGBJLetuW6x5A?usp=sharing

@RSMNYS
Copy link
Contributor Author

RSMNYS commented Dec 3, 2024

guys, I've updated the colab to generate the pkl file. Also adjusted embedding_utils code to parse this. Here you can find the pkl file: https://drive.google.com/file/d/1pDd5wZje1KbIS4JcWzhDx00aN8GmTBpc/view?usp=share_link

@RSMNYS
Copy link
Contributor Author

RSMNYS commented Dec 3, 2024

once the pickle is done, please make the embedding part of the files to be downloaded for Stable Diffusion for the TFLite backend.

@anhappdev I think for this we only need to upload the new UNET model (without embedding operations), and our embedding file. Right? If yes, can you please help upload those 2 files to the storage, from where we download models and other assets:

https://drive.google.com/file/d/1pDd5wZje1KbIS4JcWzhDx00aN8GmTBpc/view?usp=share_link
https://drive.google.com/file/d/1Sf2lcRDjSfg9jgABWWbV5EeWEmXl5CsJ/view?usp=share_link

@anhappdev
Copy link
Collaborator

@anhappdev I think for this we only need to upload the new UNET model (without embedding operations), and our embedding file. Right? If yes, can you please help upload those 2 files to the storage, from where we download models and other assets:

https://drive.google.com/file/d/1pDd5wZje1KbIS4JcWzhDx00aN8GmTBpc/view?usp=share_link
https://drive.google.com/file/d/1Sf2lcRDjSfg9jgABWWbV5EeWEmXl5CsJ/view?usp=share_link

@RSMNYS Here is the URL for the 2 files you shared
(Please remember to update the checksum in the backend settings)
https://mobile.mlcommons-storage.org/app-resources/models/v4_1/tflite/timestep_embeddings_data.pkl
https://mobile.mlcommons-storage.org/app-resources/models/v4_1/tflite/sd_diffusion_model_dynamic.tflite

And other model files for reference:
https://mobile.mlcommons-storage.org/app-resources/models/v4_1/tflite/sd_decoder_dynamic.tflite
https://mobile.mlcommons-storage.org/app-resources/models/v4_1/tflite/sd_text_encoder_dynamic.tflite

@mohitmundhragithub
Copy link
Contributor

@mohitmundhragithub please share the "final" embedding binary file (so that we can double-check it)

unet_time_step_embeddings_20.pkl.txt
timestep_steps_20_int32_embedding_1x1280_float32.bin.ts.txt

Sharing the .pkl files and the .bin files generated as described in the steps here:
#928 (comment)

Please note that i was unable to upload .pkl and .bin.ts files (github's restriction), so had to append .txt in the filenames. Please remove the .txt extension to use those.

@RSMNYS
Copy link
Contributor Author

RSMNYS commented Jan 3, 2025

@mohitmundhragithub please share the "final" embedding binary file (so that we can double-check it)

unet_time_step_embeddings_20.pkl.txt timestep_steps_20_int32_embedding_1x1280_float32.bin.ts.txt

Sharing the .pkl files and the .bin files generated as described in the steps here: #928 (comment)

Please note that i was unable to upload .pkl and .bin.ts files (github's restriction), so had to append .txt in the filenames. Please remove the .txt extension to use those.

that works good!

RSMNYS added 5 commits January 3, 2025 18:03
* submission-v4.1:
  feat: add icon and description for Stable Diffusion benchmark (#917)
  enable stable diffusion in Pixel backend (#936)
  Update tflite_settings_mtk_mt6989.pbtxt
  Update QTI backend for submission v4.1 (#13)
  Applying linter changes
  Ran make format
  Update seed and num_steps for TFLite SD task (#16)
  Addressing review comments
  Final Submission for code for Qualcomm
  Add a caption_id to coco_gen dataset (#918)
  Enable stable_diffusion tests

# Conflicts:
#	flutter/cpp/datasets/coco_gen.cc
#	mobile_back_apple/dev-utils/Makefile
#	mobile_back_tflite/cpp/backend_tflite/backend_settings/tflite_settings_android.pbtxt
#	mobile_back_tflite/cpp/backend_tflite/stable_diffusion_pipeline.h
@RSMNYS
Copy link
Contributor Author

RSMNYS commented Jan 6, 2025

Hi guys! I can run stable diffusion on my android device (Samsung Galaxy S22), but at the end I have 0 as a result, however I see all the steps in logs. Checking why. Also, during the stable diffusion process we are not updating the progress, so it's hard to understand what is going on.

@mohitmundhragithub
Copy link
Contributor

Hi guys! I can run stable diffusion on my android device (Samsung Galaxy S22), but at the end I have 0 as a result, however I see all the steps in logs. Checking why. Also, during the stable diffusion process we are not updating the progress, so it's hard to understand what is going on.

can you share the loadgen and logcat logs?

@freedomtan
Copy link
Contributor

freedomtan commented Jan 7, 2025

Let's

  1. make sure this app works well (check the app log directory, @RSMNYS )
  2. verify tflite files are the same with what @AhmedTElthakeb provided (https://github.com/mlcommons/mobile_model_closed/releases/tag/alpha-tflite-v0.3)
  3. after we merge this, add tflite files, .ts, files to both the mobile_models file and the CloudFlare bucket (@anhappdev)
  4. add .pickle and tflite files to the mobile_open repo (https://github.com/mlcommons/mobile_open/releases). @anhappdev
  5. 5.0 submission branch

@RSMNYS
Copy link
Contributor Author

RSMNYS commented Jan 7, 2025

Benchmark result.json
here is my benchmark result file. But this for performance only. I think we need the accuracy as well.

@anhappdev anhappdev mentioned this pull request Jan 7, 2025
5 tasks
Copy link

sonarqubecloud bot commented Jan 7, 2025

@anhappdev
Copy link
Collaborator

I tested this PR. Here is the output images of the TFLite backend:
https://drive.google.com/drive/folders/1-2OBHMlfF3fbuXcejeIQzPE-2mD5KPsU?usp=sharing

Many images look "wrong" with the color like this one, please check them out.

Noted that the output images are only saved to disk when we run the accuracy test.
The images should be in /storage/emulated/0/Android/data/org.mlcommons.android.mlperfbench/files/logs/<timestamp>/stable_diffusion-accuracy/cocogen_outputs/

@anhappdev anhappdev changed the title Features/timestamp-embedding-parser use time step embedding from file Jan 13, 2025
@mohitmundhragithub
Copy link
Contributor

I tested this PR. Here is the output images of the TFLite backend: https://drive.google.com/drive/folders/1-2OBHMlfF3fbuXcejeIQzPE-2mD5KPsU?usp=sharing

Many images look "wrong" with the color like this one, please check them out.

Noted that the output images are only saved to disk when we run the accuracy test. The images should be in /storage/emulated/0/Android/data/org.mlcommons.android.mlperfbench/files/logs/<timestamp>/stable_diffusion-accuracy/cocogen_outputs/

almost all the images have such artifacts.
seems more like the output is clipped to some max / min values for some pixels.

@freedomtan
Copy link
Contributor

I tested this PR. Here is the output images of the TFLite backend: https://drive.google.com/drive/folders/1-2OBHMlfF3fbuXcejeIQzPE-2mD5KPsU?usp=sharing

Many images look "wrong" with the color like this one, please check them out.

Noted that the output images are only saved to disk when we run the accuracy test. The images should be in /storage/emulated/0/Android/data/org.mlcommons.android.mlperfbench/files/logs/<timestamp>/stable_diffusion-accuracy/cocogen_outputs/

Most likely, it's caused by quantization. At least, we know timesteps embedding works as expected.
To further confirm which model (text encoder, unet/diffusion, or image decoder) is problematic, we can check fp32/fp16 text encoder and image decoder first.

@freedomtan
Copy link
Contributor

Let's merge this and create a 5.0 submission branch, then work on using fp16 models.

@AhmedTElthakeb please check if you can export fp16 tflite models (https://ai.google.dev/edge/litert/models/post_training_float16_quant).

@freedomtan freedomtan marked this pull request as ready for review January 14, 2025 06:15
@freedomtan freedomtan requested a review from a team as a code owner January 14, 2025 06:15
@freedomtan
Copy link
Contributor

By 5.1, qualcomm backend will use the timestep binary file (so that we have a shared timestep source file). @mohitmundhragithub

@RSMNYS RSMNYS merged commit 48654bd into submission-v4.1 Jan 14, 2025
22 checks passed
@RSMNYS RSMNYS deleted the features/timestamp-embedding-parser branch January 14, 2025 07:02
@github-actions github-actions bot locked and limited conversation to collaborators Jan 14, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants