Skip to content

Conversation

@wkpark
Copy link
Contributor

@wkpark wkpark commented Oct 20, 2024

based on https://github.com/lllyasviel/stable-diffusion-webui-forge/blob/main/modules_forge/main_thread.py

original author's comment in the source code:

This file is the main thread that handles all gradio calls for major t2i or i2i processing.
Other gradio calls (like those from extensions) are not influenced.
By using one single thread to process all major calls, model moving is significantly faster.

and it's commit:
lllyasviel/stable-diffusion-webui-forge@f06ba8e

from commit message:

original author said:
This will move all major gradio calls into the main thread rather than random gradio threads.
This ensures that all torch.module.to() are performed in main thread to completely possible avoid GPU fragments.
In my test now model moving is 0.7 ~ 1.2 seconds faster, which means all 6GB/8GB VRAM users will get 0.7 ~ 1.2 seconds faster per image on SDXL.

  • classified
  • fix workflow (all tests have been passed but coverage not work)
  • this way, gc.collect() will work as expected

extracted from PR #16484

Comment on lines +73 to +80
while True:
time.sleep(0.01)
if current_id in self.finished_tasks:
finished = self.finished_tasks.pop(current_id)
if finished.exception is not None:
raise finished.exception

return finished.result

Choose a reason for hiding this comment

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

Wrap modifications of pending_tasks and access to finished_tasks in self.lock.

def __init__(self):
self.lock = threading.Lock()

def work(self, task):

Choose a reason for hiding this comment

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

To avoid memory bloat, optionally limit or expire old entries in finished_tasks

Comment on lines +46 to +54
def main_loop(self):
self.running = True
while self.running:
time.sleep(0.01)
if len(self.pending_tasks) > 0:
with self.lock:
task = self.pending_tasks.pop(0)

self.work(task)

Choose a reason for hiding this comment

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

Optionally increase sleep time when no tasks are pending, or use threading.Condition() for efficiency:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants