Comfy-Org / Comfy-Org/ComfyUI

(sd-webui-comfyui) inference with checkpoint/vae/clip using models located in a different process

Open
#2,407 6 comments 0 reactions 0 assignees View on GitHub
User Support
Dominant language
Python
Stars
133k
Forks
15.7k
Avg merge
1d 6h
Merged PRs (30d)
155

Description

Hi, I am a maintainer of the [sd-webui-comfyui](https://github.com/ModelSurge/sd-webui-comfyui) extension for a1111.

The extension provides a comfyui node that reuses the webui checkpoint already loaded in memory to allow people to reuse resources:
https://github.com/ModelSurge/sd-webui-comfyui/wiki/Webui-Nodes#webui-checkpoint

The way it has been implemented is very fragile and breaks whenever the implementation of the `Model`/`VAE`/`Clip` (along with `ModelPatcher`) classes change. It would be very convenient if it was possible for external code to be written against a more stable interface to provide custom checkpoint sources.

Using the checkpoint/vae/clip classes as-is is not really an option since their constructors allocate memory and rely on the assumption that an actual model reference will be available in the current process.

Particularly, the current code uses `.state_dict()` in numerous places, which makes it impractical to hold any model in a different process. This is because, since the webui model is in a different process than comfyui, we would need to make a copy of the model every time to move it to the comfyui process.

In the case of our a1111 extension, an alternative solution we found to be a good tradeoff is to keep the webui model in the webui process and instead copy the latents back and forth between the webui and comfyui processes. This is done by moving the latents to cpu, then serializing them, then copying the serialized tensor over to the other process, then deserializing, then moving to the right device for inference.

This is how we implemented the unet/vae/clip webui proxy classes currently. As you can see, it is a very tedious process:
https://github.com/ModelSurge/sd-webui-comfyui/blob/ebed0c432c8264b164b78da6e887d253e9a7aa6c/lib_comfyui/webui/proxies.py

Also, it is very fragile:
https://github.com/ModelSurge/sd-webui-comfyui/issues/195

TL;DR: We need a way to infer outputs with the unet/vae/text encoder classes using models loaded in a different process than comfyui.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.