invoke-ai / InvokeAI

InvokeAI is a leading creative engine for Stable Diffusion models, empowering professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry leading WebUI, supports terminal use through a CLI, and serves as the foundation for multiple commercial products.
https://invoke-ai.github.io/InvokeAI/
Apache License 2.0
23.07k stars 2.39k forks source link

[MM] Make controlnet image processors use MM cache system #6564

Open lstein opened 3 months ago

lstein commented 3 months ago

Summary

This PR changes the loading of most controlnet image processors to use the model manager caching system. This should result in a performance improvement when multiple different control images are processed using the same processor. However, the application of the processed image during image generation will not be affected.

A couple of notes here.

  1. The controlnet_aux module, which defines the image processors, uses an odd from_pretrained() pattern in which the same HuggingFace repo is used for each of the processor classes. Internally, each processor class looks for a different hard-coded model checkpoint file. To use the MM caching system, I figured out which model was being loaded, and created a mapping from the processor class to the checkpoint file in controlnet_image_processors.py. If at some point one of the processors in controlnet_aux is updated to use a different file name, a compensatory change will need to be made to controlnet_image_processors.py.
  2. I had to make a small change to the model installer's download_and_cache_model() method in order to accommodate the way that the SegmentAnything class looks for its model in a subfolder.
  3. I was unable to migrate the Leres processor due to the fact that its from_pretrained() method loads two different models, but download_and_cache_model() doesn't accept more than a single model file at a time.

Related Issues / Discussions

QA Instructions

In the workflow editor, execute the following controlnet processors and confirm that they load and work as expected:

The remaining processors (canny, shuffle, etc) do not download models from HF.

Merge Plan

Merge when approved.

Checklist

psychedelicious commented 2 months ago

This is a good step in the right direction. Eventually we should finish the move away from controlnet_aux as it is not actively maintained.

lstein commented 2 months ago

Overall, makes sense to me. A few comments:

  • I smoke tested the lineart processor. I trust that you have tested them all?

Yes, all tested successfully.

I've added memory calculations for the controlnet processors as well as the scheduler and tokenizer. Hopefully we won't see any more complaints about size 0.

  • The lineart processor (and probably others) don't run on the GPU, because they don't have a .to(...) method. Is that expected?

Some of the controlnet processors run in GPU and others in CPU. The CPU models only runs once when the encoded controlnet image is first calculated and cached, so it's not a high priority to fix at the moment.

  • @brandonrising can you review for potential commercial implications?
brandonrising commented 2 months ago
Screenshot 2024-07-23 at 3 29 04 PM

Hmm I did get this error when testing each of the controlnets the first time, but I don't seem to be able to reproduce it again.

Otherwise, this all worked really well and commercial would be improved by having these in the cache

brandonrising commented 2 months ago

Screenshot 2024-07-23 at 3 29 04 PM Hmm I did get this error when testing each of the controlnets the first time, but I don't seem to be able to reproduce it again.

Otherwise, this all worked really well and commercial would be improved by having these in the cache

oh ignore that error message, I thought it was downloading a preprocessor but it was probably just from when I kicked off installs in MM