-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Make torch.compile LoRA/key-compatible #8213
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ae78eda
Make torch compile node use wrapper instead of object_patch for the e…
Kosinkadink 920f6d7
Moved torch compile code into comfy_api so it can be used by custom n…
Kosinkadink c4a7e8f
Refactor set_torch_compile_wrapper to support a list of keys instead …
Kosinkadink 5789068
remove unused import
Kosinkadink b212f06
Moved torch compile kwargs to be stored in model_options instead of a…
Kosinkadink c64281a
Add some comments
Kosinkadink 8514e96
Remove random line of code, not sure how it got there
Kosinkadink 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from .torch_compile import set_torch_compile_wrapper | ||
|
|
||
| __all__ = [ | ||
| "set_torch_compile_wrapper", | ||
| ] |
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,69 @@ | ||
| from __future__ import annotations | ||
| import torch | ||
|
|
||
| import comfy.utils | ||
| from comfy.patcher_extension import WrappersMP | ||
| from typing import TYPE_CHECKING, Callable, Optional | ||
| if TYPE_CHECKING: | ||
| from comfy.model_patcher import ModelPatcher | ||
| from comfy.patcher_extension import WrapperExecutor | ||
|
|
||
|
|
||
| COMPILE_KEY = "torch.compile" | ||
| TORCH_COMPILE_KWARGS = "torch_compile_kwargs" | ||
|
|
||
|
|
||
| def apply_torch_compile_factory(compiled_module_dict: dict[str, Callable]) -> Callable: | ||
| ''' | ||
| Create a wrapper that will refer to the compiled_diffusion_model. | ||
| ''' | ||
| def apply_torch_compile_wrapper(executor: WrapperExecutor, *args, **kwargs): | ||
| try: | ||
| orig_modules = {} | ||
| for key, value in compiled_module_dict.items(): | ||
| orig_modules[key] = comfy.utils.get_attr(executor.class_obj, key) | ||
| comfy.utils.set_attr(executor.class_obj, key, value) | ||
| return executor(*args, **kwargs) | ||
| finally: | ||
| for key, value in orig_modules.items(): | ||
| comfy.utils.set_attr(executor.class_obj, key, value) | ||
| return apply_torch_compile_wrapper | ||
|
|
||
|
|
||
| def set_torch_compile_wrapper(model: ModelPatcher, backend: str, options: Optional[dict[str,str]]=None, | ||
| mode: Optional[str]=None, fullgraph=False, dynamic: Optional[bool]=None, | ||
| keys: list[str]=["diffusion_model"], *args, **kwargs): | ||
| ''' | ||
| Perform torch.compile that will be applied at sample time for either the whole model or specific params of the BaseModel instance. | ||
|
|
||
| When keys is None, it will default to using ["diffusion_model"], compiling the whole diffusion_model. | ||
| When a list of keys is provided, it will perform torch.compile on only the selected modules. | ||
| ''' | ||
| # clear out any other torch.compile wrappers | ||
| model.remove_wrappers_with_key(WrappersMP.APPLY_MODEL, COMPILE_KEY) | ||
| # if no keys, default to 'diffusion_model' | ||
| if not keys: | ||
| keys = ["diffusion_model"] | ||
| # create kwargs dict that can be referenced later | ||
| compile_kwargs = { | ||
| "backend": backend, | ||
| "options": options, | ||
| "mode": mode, | ||
| "fullgraph": fullgraph, | ||
| "dynamic": dynamic, | ||
| } | ||
| # get a dict of compiled keys | ||
| compiled_modules = {} | ||
| for key in keys: | ||
| compiled_modules[key] = torch.compile( | ||
| model=model.get_model_object(key), | ||
| **compile_kwargs, | ||
| ) | ||
| # add torch.compile wrapper | ||
| wrapper_func = apply_torch_compile_factory( | ||
| compiled_module_dict=compiled_modules, | ||
| ) | ||
| # store wrapper to run on BaseModel's apply_model function | ||
| model.add_wrapper_with_key(WrappersMP.APPLY_MODEL, COMPILE_KEY, wrapper_func) | ||
| # keep compile kwargs for reference | ||
| model.model_options[TORCH_COMPILE_KWARGS] = compile_kwargs | ||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @Kosinkadink I found if the image shape is changed, the patch of torch.compile will not work. Do you have any clue ? I‘d like to know how to trigger the patching when image shape is changed. thank you