Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.
TypeScript ai-artartificial-intelligencegenerative-artimage-generationimg2imginpaintinglatent-diffusionlinuxmacosoutpaintingstable-diffusiontxt2imgwindows
Fix arguments for hidiffusion hook (#9679)
StAlKeR7779f8b1bf7097a5
diff --git a/invokeai/backend/stable_diffusion/diffusion_backend.py b/invokeai/backend/stable_diffusion/diffusion_backend.pyindex be3800411a..5cb356744e 100644--- a/invokeai/backend/stable_diffusion/diffusion_backend.py+++ b/invokeai/backend/stable_diffusion/diffusion_backend.py@@ -142,4 +142,8 @@ class StableDiffusionBackend:return noise_preddef _unet_forward(self, **kwargs) -> torch.Tensor:- return self.unet(**kwargs).sample+ # First three args should be positional, not keywords, so torch hooks can see them.+ sample = kwargs.pop("sample")+ timestep = kwargs.pop("timestep")+ encoder_hidden_states = kwargs.pop("encoder_hidden_states")+ return self.unet(sample, timestep, encoder_hidden_states, **kwargs).samplediff --git a/tests/backend/stable_diffusion/test_diffusion_backend.py b/tests/backend/stable_diffusion/test_diffusion_backend.pynew file mode 100644index 0000000000..76137e5a67--- /dev/null+++ b/tests/backend/stable_diffusion/test_diffusion_backend.py@@ -0,0 +1,55 @@+from types import SimpleNamespace++import torch+from torch import nn++from invokeai.backend.stable_diffusion import diffusion_backend as diffusion_backend_module+from invokeai.backend.stable_diffusion.diffusion_backend import StableDiffusionBackend+++class HookAwareUNet(nn.Module):+ def __init__(self):+ super().__init__()+ self.info = {}+ self.received_kwargs = None++ def forward(self, sample, timestep, encoder_hidden_states, **kwargs):+ self.received_kwargs = kwargs+ return SimpleNamespace(sample=sample)+++def test_unet_forward_passes_required_inputs_positionally(monkeypatch):+ monkeypatch.setattr(+ diffusion_backend_module,+ "get_config",+ lambda: SimpleNamespace(sequential_guidance=False),+ )+ unet = HookAwareUNet()+ hook_args = []++ def hidiffusion_style_pre_hook(module, args):+ hook_args.append(args)+ module.info["size"] = (args[0].shape[2], args[0].shape[3])++ unet.register_forward_pre_hook(hidiffusion_style_pre_hook)+ backend = StableDiffusionBackend(unet=unet, scheduler=object())++ sample = torch.ones((1, 4, 8, 8))+ timestep = torch.tensor(1)+ encoder_hidden_states = torch.ones((1, 2, 3))+ cross_attention_kwargs = {"scale": 0.5}++ result = backend._unet_forward(+ sample=sample,+ timestep=timestep,+ encoder_hidden_states=encoder_hidden_states,+ cross_attention_kwargs=cross_attention_kwargs,+ )++ assert unet.info["size"] == (8, 8)+ assert len(hook_args[0]) == 3+ assert hook_args[0][0] is sample+ assert hook_args[0][1] is timestep+ assert hook_args[0][2] is encoder_hidden_states+ assert unet.received_kwargs == {"cross_attention_kwargs": cross_attention_kwargs}+ assert result is sample