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

feat(examples): Support S3 for all StableDiffusionPipeline components #76

Conversation

Eta0
Copy link
Contributor

@Eta0 Eta0 commented Feb 1, 2024

Updated diffusers support in hf_serialization.py

More fixes and improvements for #73.

This change:

  • Adds S3 upload capability for all StableDiffusionPipeline components
    • text_encoder, vae (updated), unet (updated), scheduler (new), tokenizer (new)
    • The scheduler and tokenizer are saved as .zip files containing the directory written by their .save_pretrained() methods
    • Requires nothing from HuggingFace Hub at deserialization time
  • Adds S3 upload capability for transformers tokenizers
  • Adjusts serialization to pass validation checks by using include_non_persistent_buffers=False
  • Merges in the latest changes from main to support using include_non_persistent_buffers=False correctly
  • Avoids re-initializing models from HF for no reason
  • Cleans up misspelled/outdated CLI args and help
  • Adds parameter weight validation for diffusers models
  • Refactors serialize_model substantially
  • Adds logging level command line arguments, and
  • Shifts more outputs to use a logger

(Outdated): I left in the code to generate a test image through diffusers because it is good example code for this repository of how to re-assemble the components of an SD model and was good for testing that these changes work. It could be commented out or deleted later, or changed to be only enabled through a flag.
(Update): The test image generation code for diffusers is now commented out.

Eta0 and others added 6 commits January 31, 2024 16:36
Previously, setting include_non_persistent_buffers=False would only
include persistent buffers, and remove both non-persistent buffers
and parameters. Parameters were not supposed to be affected by this
flag, so this fix changes it to only remove non-persistent buffers.
fix(serialization): Don't drop parameters with non-persistent buffers
This additionally adds parameter weight validation for diffusers
models, refactors `serialize_model` substantially, adds logging level
command line arguments, and shifts more outputs to use a logger.
@Eta0 Eta0 added the enhancement New feature or request label Feb 1, 2024
@Eta0 Eta0 requested review from harubaru and sangstar February 1, 2024 01:46
@Eta0 Eta0 self-assigned this Feb 1, 2024
Copy link
Contributor

@sangstar sangstar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dramatically improved! LGTM. One or two questions on your thought process

Comment on lines +319 to +324
serialize_pretrained(
pipeline.tokenizer, output_prefix, "tokenizer", force=args.force
)
serialize_pretrained(
pipeline.scheduler, output_prefix, "scheduler", force=args.force
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the purpose of serialize_pretrained is specifically to support saving artifacts like SD's scheduler and tokenizer? I assume this is cleaner as they're not configs nor modules.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, pretty much. The scheduler can actually be saved as a single JSON file (around 300 to 400 bytes), so it could have its own special code to avoid a zip file and temporary directory, but it seemed unnecessary at this time.
In summary:

  • text_encoder / vae / unet — tensors & config.json
    • We use tensorizer to save these
    • Potentially very, very large, so the optimizations in tensorizer are important
  • schedulerconfig.json only
    • Supports .save_pretrained(dir) and .from_pretrained(dir)
    • Roughly around 400 bytes, so downloader optimizations aren't a huge deal
  • tokenizer — entire directory of files
    • Supports .save_pretrained(dir) and .from_pretrained(dir)
    • Roughly around 1.5 MB for SD 1.5, and compresses well, so zips work nicely
    • For models that use "fast tokenizers," these can be saved as a single tokenizer.json file instead, but not all models support fast tokenizers

@Eta0 Eta0 requested a review from harubaru February 1, 2024 18:38
@sangstar sangstar merged commit 184d7a6 into sangstar/update-serialization-cl-script-for-container Feb 1, 2024
6 of 7 checks passed
@sangstar sangstar deleted the eta/update-hf-serialization branch February 1, 2024 18:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants