Skip to content

Commit

Permalink
Fix prepare latent image ids and vae sample generators for flux (#9981)
Browse files Browse the repository at this point in the history
* fix

* update expected slice
  • Loading branch information
a-r-r-o-w authored Nov 21, 2024
1 parent e564abe commit cd6ca9d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/diffusers/pipelines/flux/pipeline_flux.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ def prepare_latents(
shape = (batch_size, num_channels_latents, height, width)

if latents is not None:
latent_image_ids = self._prepare_latent_image_ids(batch_size, height, width, device, dtype)
latent_image_ids = self._prepare_latent_image_ids(batch_size, height // 2, width // 2, device, dtype)
return latents.to(device=device, dtype=dtype), latent_image_ids

if isinstance(generator, list) and len(generator) != batch_size:
Expand Down
20 changes: 17 additions & 3 deletions src/diffusers/pipelines/flux/pipeline_flux_controlnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,20 @@ def calculate_shift(
return mu


# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.retrieve_latents
def retrieve_latents(
encoder_output: torch.Tensor, generator: Optional[torch.Generator] = None, sample_mode: str = "sample"
):
if hasattr(encoder_output, "latent_dist") and sample_mode == "sample":
return encoder_output.latent_dist.sample(generator)
elif hasattr(encoder_output, "latent_dist") and sample_mode == "argmax":
return encoder_output.latent_dist.mode()
elif hasattr(encoder_output, "latents"):
return encoder_output.latents
else:
raise AttributeError("Could not access latents of provided encoder_output")


# Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.retrieve_timesteps
def retrieve_timesteps(
scheduler,
Expand Down Expand Up @@ -512,7 +526,7 @@ def prepare_latents(
shape = (batch_size, num_channels_latents, height, width)

if latents is not None:
latent_image_ids = self._prepare_latent_image_ids(batch_size, height, width, device, dtype)
latent_image_ids = self._prepare_latent_image_ids(batch_size, height // 2, width // 2, device, dtype)
return latents.to(device=device, dtype=dtype), latent_image_ids

if isinstance(generator, list) and len(generator) != batch_size:
Expand Down Expand Up @@ -772,7 +786,7 @@ def __call__(
controlnet_blocks_repeat = False if self.controlnet.input_hint_block is None else True
if self.controlnet.input_hint_block is None:
# vae encode
control_image = self.vae.encode(control_image).latent_dist.sample()
control_image = retrieve_latents(self.vae.encode(control_image), generator=generator)
control_image = (control_image - self.vae.config.shift_factor) * self.vae.config.scaling_factor

# pack
Expand Down Expand Up @@ -810,7 +824,7 @@ def __call__(

if self.controlnet.nets[0].input_hint_block is None:
# vae encode
control_image_ = self.vae.encode(control_image_).latent_dist.sample()
control_image_ = retrieve_latents(self.vae.encode(control_image_), generator=generator)
control_image_ = (control_image_ - self.vae.config.shift_factor) * self.vae.config.scaling_factor

# pack
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ def __call__(
)
height, width = control_image.shape[-2:]

control_image = self.vae.encode(control_image).latent_dist.sample()
control_image = retrieve_latents(self.vae.encode(control_image), generator=generator)
control_image = (control_image - self.vae.config.shift_factor) * self.vae.config.scaling_factor

height_control_image, width_control_image = control_image.shape[2:]
Expand Down Expand Up @@ -832,7 +832,7 @@ def __call__(
)
height, width = control_image_.shape[-2:]

control_image_ = self.vae.encode(control_image_).latent_dist.sample()
control_image_ = retrieve_latents(self.vae.encode(control_image_), generator=generator)
control_image_ = (control_image_ - self.vae.config.shift_factor) * self.vae.config.scaling_factor

height_control_image, width_control_image = control_image_.shape[2:]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ def __call__(
controlnet_blocks_repeat = False if self.controlnet.input_hint_block is None else True
if self.controlnet.input_hint_block is None:
# vae encode
control_image = self.vae.encode(control_image).latent_dist.sample()
control_image = retrieve_latents(self.vae.encode(control_image), generator=generator)
control_image = (control_image - self.vae.config.shift_factor) * self.vae.config.scaling_factor

# pack
Expand Down Expand Up @@ -979,7 +979,7 @@ def __call__(

if self.controlnet.nets[0].input_hint_block is None:
# vae encode
control_image_ = self.vae.encode(control_image_).latent_dist.sample()
control_image_ = retrieve_latents(self.vae.encode(control_image_), generator=generator)
control_image_ = (control_image_ - self.vae.config.shift_factor) * self.vae.config.scaling_factor

# pack
Expand Down
2 changes: 1 addition & 1 deletion tests/pipelines/controlnet_flux/test_controlnet_flux.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def test_controlnet_flux(self):
assert image.shape == (1, 32, 32, 3)

expected_slice = np.array(
[0.7348633, 0.41333008, 0.6621094, 0.5444336, 0.47607422, 0.5859375, 0.44677734, 0.4506836, 0.40454102]
[0.47387695, 0.63134766, 0.5605469, 0.61621094, 0.7207031, 0.7089844, 0.70410156, 0.6113281, 0.64160156]
)

assert (
Expand Down

0 comments on commit cd6ca9d

Please sign in to comment.