-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Add thread-safe wrappers for components in pipeline (examples/server-async/utils/requestscopedpipeline.py) #12515
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
Open
+173
−47
Open
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
bbfc5f4
Basic implementation of request scheduling
FredyRivera-dev a308e3e
Basic editing in SD and Flux Pipelines
FredyRivera-dev 4799b8e
Small Fix
FredyRivera-dev eda5847
Fix
FredyRivera-dev 6b5e6be
Update for more pipelines
FredyRivera-dev df2933f
Add examples/server-async
FredyRivera-dev 5c7c7c6
Add examples/server-async
FredyRivera-dev e3cd368
Merge branch 'huggingface:main' into main
FredyRivera-dev 09bf796
Merge branch 'huggingface:main' into main
FredyRivera-dev bd3e48a
Updated RequestScopedPipeline to handle a single tokenizer lock to av...
FredyRivera-dev 534710c
Fix
FredyRivera-dev 4d7c64f
Fix _TokenizerLockWrapper
FredyRivera-dev 18db9e6
Fix _TokenizerLockWrapper
FredyRivera-dev 8f0efb1
Delete _TokenizerLockWrapper
FredyRivera-dev b479039
Fix tokenizer
FredyRivera-dev e676b34
Merge branch 'huggingface:main' into main
FredyRivera-dev 0beab1c
Update examples/server-async
FredyRivera-dev 840f0e4
Fix server-async
FredyRivera-dev bb41c2b
Merge branch 'huggingface:main' into main
FredyRivera-dev 8a238c3
Merge branch 'huggingface:main' into main
FredyRivera-dev ed617fe
Optimizations in examples/server-async
FredyRivera-dev b052d27
We keep the implementation simple in examples/server-async
FredyRivera-dev 0f63f4d
Update examples/server-async/README.md
FredyRivera-dev a9666b1
Update examples/server-async/README.md for changes to tokenizer locks...
FredyRivera-dev 06bb136
The changes to the diffusers core have been undone and all logic is b...
FredyRivera-dev a519915
Update examples/server-async/utils/*
FredyRivera-dev 7cfee77
Fix BaseAsyncScheduler
FredyRivera-dev e574f07
Rollback in the core of the diffusers
FredyRivera-dev 05d7936
Merge branch 'huggingface:main' into main
FredyRivera-dev 1049663
Update examples/server-async/README.md
FredyRivera-dev 5316620
Complete rollback of diffusers core files
FredyRivera-dev 0ecdfc3
Simple implementation of an asynchronous server compatible with SD3-3...
FredyRivera-dev ac5c9e6
Update examples/server-async/README.md
FredyRivera-dev 72e0215
Fixed import errors in 'examples/server-async/serverasync.py'
FredyRivera-dev edd550b
Flux Pipeline Discard
FredyRivera-dev 6b69367
Update examples/server-async/README.md
FredyRivera-dev 5598557
Merge branch 'main' into main
sayakpaul 7c4f883
Apply style fixes
github-actions[bot] c91e6f4
Merge branch 'huggingface:main' into main
FredyRivera-dev f2e9f02
Add thread-safe wrappers for components in pipeline
FredyRivera-dev 489da5d
Add wrappers.py
FredyRivera-dev 8072fba
Merge branch 'main' into main
sayakpaul 581847f
Apply style fixes
github-actions[bot] 7f01e69
Merge branch 'main' into main
sayakpaul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
examples/server-async/utils/wrappers.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| class ThreadSafeTokenizerWrapper: | ||
| def __init__(self, tokenizer, lock): | ||
| self._tokenizer = tokenizer | ||
| self._lock = lock | ||
|
|
||
| self._thread_safe_methods = { | ||
| "__call__", | ||
| "encode", | ||
| "decode", | ||
| "tokenize", | ||
| "encode_plus", | ||
| "batch_encode_plus", | ||
| "batch_decode", | ||
| } | ||
|
|
||
| def __getattr__(self, name): | ||
| attr = getattr(self._tokenizer, name) | ||
|
|
||
| if name in self._thread_safe_methods and callable(attr): | ||
|
|
||
| def wrapped_method(*args, **kwargs): | ||
| with self._lock: | ||
| return attr(*args, **kwargs) | ||
|
|
||
| return wrapped_method | ||
|
|
||
| return attr | ||
|
|
||
| def __call__(self, *args, **kwargs): | ||
| with self._lock: | ||
| return self._tokenizer(*args, **kwargs) | ||
|
|
||
| def __setattr__(self, name, value): | ||
| if name.startswith("_"): | ||
| super().__setattr__(name, value) | ||
| else: | ||
| setattr(self._tokenizer, name, value) | ||
|
|
||
| def __dir__(self): | ||
| return dir(self._tokenizer) | ||
|
|
||
|
|
||
| class ThreadSafeVAEWrapper: | ||
| def __init__(self, vae, lock): | ||
| self._vae = vae | ||
| self._lock = lock | ||
|
|
||
| def __getattr__(self, name): | ||
| attr = getattr(self._vae, name) | ||
| if name in {"decode", "encode", "forward"} and callable(attr): | ||
|
|
||
| def wrapped(*args, **kwargs): | ||
| with self._lock: | ||
| return attr(*args, **kwargs) | ||
|
|
||
| return wrapped | ||
| return attr | ||
|
|
||
| def __setattr__(self, name, value): | ||
| if name.startswith("_"): | ||
| super().__setattr__(name, value) | ||
| else: | ||
| setattr(self._vae, name, value) | ||
|
|
||
|
|
||
| class ThreadSafeImageProcessorWrapper: | ||
| def __init__(self, proc, lock): | ||
| self._proc = proc | ||
| self._lock = lock | ||
|
|
||
| def __getattr__(self, name): | ||
| attr = getattr(self._proc, name) | ||
| if name in {"postprocess", "preprocess"} and callable(attr): | ||
|
|
||
| def wrapped(*args, **kwargs): | ||
| with self._lock: | ||
| return attr(*args, **kwargs) | ||
|
|
||
| return wrapped | ||
| return attr | ||
|
|
||
| def __setattr__(self, name, value): | ||
| if name.startswith("_"): | ||
| super().__setattr__(name, value) | ||
| else: | ||
| setattr(self._proc, name, value) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.