Skip to content
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

Flux Vae broke for float16, force bfloat16 or float32 were compatible #7213

Merged
merged 13 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions invokeai/backend/flux/modules/autoencoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,12 +312,25 @@ def encode(self, x: Tensor, sample: bool = True, generator: torch.Generator | No
Tensor: Encoded latent tensor. Shape: (batch_size, z_channels, latent_height, latent_width).
"""

# VAE is broken in float16, use same logic in model loading to pick bfloat16 or float32
if x.dtype == torch.float16:
try:
x = x.to(torch.bfloat16)
except TypeError:
x = x.to(torch.float32)
RyanJDick marked this conversation as resolved.
Show resolved Hide resolved
z = self.reg(self.encoder(x), sample=sample, generator=generator)
z = self.scale_factor * (z - self.shift_factor)
return z

def decode(self, z: Tensor) -> Tensor:
z = z / self.scale_factor + self.shift_factor

# VAE is broken in float16, use same logic in model loading to pick bfloat16 or float32
if z.dtype == torch.float16:
try:
z = z.to(torch.bfloat16)
except TypeError:
z = z.to(torch.float32)
Vargol marked this conversation as resolved.
Show resolved Hide resolved
return self.decoder(z)

def forward(self, x: Tensor) -> Tensor:
Expand Down
1 change: 1 addition & 0 deletions invokeai/backend/model_manager/load/load_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(
self._logger = logger
self._ram_cache = ram_cache
self._torch_dtype = TorchDevice.choose_torch_dtype()
self._torch_device = TorchDevice.choose_torch_device()

def load_model(self, model_config: AnyModelConfig, submodel_type: Optional[SubModelType] = None) -> LoadedModel:
"""
Expand Down
10 changes: 9 additions & 1 deletion invokeai/backend/model_manager/load/model_loaders/flux.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,15 @@ def _load_model(
model = AutoEncoder(ae_params[config.config_path])
sd = load_file(model_path)
model.load_state_dict(sd, assign=True)
model.to(dtype=self._torch_dtype)
# VAE is broken in float16, which mps defaults too
Vargol marked this conversation as resolved.
Show resolved Hide resolved
if self._torch_dtype == torch.float16:
try:
vae_dtype = torch.tensor([1.0], dtype=torch.bfloat16, device=self._torch_device).dtype
except TypeError:
vae_dtype = torch.tensor([1.0], dtype=torch.float32, device=self._torch_device).dtype
Vargol marked this conversation as resolved.
Show resolved Hide resolved
else:
vae_dtype = self._torch_dtype
model.to(vae_dtype)

return model

Expand Down