-
Notifications
You must be signed in to change notification settings - Fork 6.7k
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
Hooks Part 2 - TransformerOptionsHook and AdditionalModelsHook #6377
base: master
Are you sure you want to change the base?
Conversation
…about the full scope of current sampling run, fix Hook Keyframes' guarantee_steps=1 inconsistent behavior with sampling split across different Sampling nodes/sampling runs by referencing 'sigmas'
…ches to use target_dict instead of target so that more information can be provided about the current execution environment if needed
… to separate out Wrappers/Callbacks/Patches into different hook types (all affect transformer_options)
… hook_type, modified necessary code to no longer need to manually separate out hooks by hook_type
…ptions to not conflict with the "sigmas" that will overwrite "sigmas" in _calc_cond_batch
…ade AddModelsHook operational and compliant with should_register result, moved TransformerOptionsHook handling out of ModelPatcher.register_all_hook_patches, support patches in TransformerOptionsHook properly by casting any patches/wrappers/hooks to proper device at sample time
…nsHook are not yet operational
…ops nodes by properly caching between positive and negative conds, make hook_patches_backup behave as intended (in the case that something pre-registers WeightHooks on the ModelPatcher instead of registering it at sample time)
…added some doc strings and removed a so-far unused variable
…ok to InjectionsHook (not yet implemented, but at least getting the naming figured out)
This sounds like it's fixing a problem I hit trying to implement scheduled attention masking. I'll try and test if I can make things work with this PR. |
Transformer patches get applied properly now, at least. It's pretty slow and probably buggy, but it appears to work as expected. You can have two conds with different masks attached and it seems to be swapping the transformer options properly so that the masks don't get mixed. |
The first Hooks PR made the code changes necessary to support hooks possible and fully added support for WeightHooks, enabling masked and scheduled weights bound to conditioning.
This PR follows up on that and cleans up the hooks implementation, adding support for two new types of hooks: TransformerOptionsHooks and AdditionalModelsHooks.
transformer_options
at sample time) to be added totransformer_options
either at registration time (apply to all conds) or during the sampling of the specific hooked conds;hook_scope
is the variable that stores the enum corresponding to the desired behavior. The storedtransformers_dict
is a dict that will be merged withtransformer_options
. Any wrappers/callbacks/patches will be cast to the model's runtime dtype/device to comply with expectations for patches on thetransformers_options
within a ModelPatcher'smodel_options
. Previously was called WrapperHook in anticipation of wrappers/callbacks/patches needing separate Hook types, but after giving it some thought my initial implementation was enough to do all transformer_options-related things.additional_models
param to conds, the AdditionalModelsHook allows the inclusion of models to be determined at sample time, in case the model is only needed if some other hook or param on the ModelPatcher is present thanks to Hook'sshould_register
function.Cleanup of hook infrastructure:
list[Hook]
anddict[EnumHookType, list[Hook]
.target
that was just an EnumWeightTarget is nowtarget_dict
, containing an EnumWeightTarget as well as any other potential inputs. This will allow for more advanced filtering of WeightHooks for non-core features, but otherwise does not affect any previous behavior and is more of a refactor.Fixes:
Miscellaneous:
from __future__ import annotations
takes away the need for the'QuotesAroundType'
workaround for typehinting in certain scenarios, so I've removed the quotes around types where the workaround would have been needed otherwise.Future Work:
model
input that impacted all conds.TL;DR: With the addition of TransformerOptionsHook and AdditionalModelsHook, custom code can now be executed on a per-cond basis by assigning wrappers/callbacks/patches to only run on specific conds, or on all conds but without the need to know the model used in sampling via a model input. Something like GLIGEN that has special ComfyUI core code to support per-cond behavior for it could instead be written using these new hooks.