Skip to content
This repository was archived by the owner on Feb 7, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions generative/networks/nets/autoencoderkl.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,18 @@ def __init__(
)

def forward(self, x: torch.Tensor) -> torch.Tensor:
# Cast to float32 to as 'upsample_nearest2d_out_frame' op does not support bfloat16
# https://github.com/pytorch/pytorch/issues/86679
dtype = x.dtype
if dtype == torch.bfloat16:
x = x.to(torch.float32)

x = F.interpolate(x, scale_factor=2.0, mode="nearest")

# If the input is bfloat16, we cast back to bfloat16
if dtype == torch.bfloat16:
x = x.to(dtype)

x = self.conv(x)
return x

Expand Down
18 changes: 18 additions & 0 deletions generative/networks/nets/diffusion_model_unet.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,19 @@ def __init__(
def forward(self, x: torch.Tensor, emb: Optional[torch.Tensor] = None) -> torch.Tensor:
del emb
assert x.shape[1] == self.num_channels

# Cast to float32 to as 'upsample_nearest2d_out_frame' op does not support bfloat16
# https://github.com/pytorch/pytorch/issues/86679
dtype = x.dtype
if dtype == torch.bfloat16:
x = x.to(torch.float32)

x = F.interpolate(x, scale_factor=2.0, mode="nearest")

# If the input is bfloat16, we cast back to bfloat16
if dtype == torch.bfloat16:
x = x.to(dtype)

if self.use_conv:
x = self.conv(x)
return x
Expand Down Expand Up @@ -1783,13 +1795,19 @@ def forward(
"""
# 1. time
t_emb = get_timestep_embedding(timesteps, self.block_out_channels[0])

# timesteps does not contain any weights and will always return f32 tensors
# but time_embedding might actually be running in fp16. so we need to cast here.
# there might be better ways to encapsulate this.
t_emb = t_emb.to(dtype=x.dtype)
emb = self.time_embed(t_emb)

# 2. class
if self.num_class_embeds is not None:
if class_labels is None:
raise ValueError("class_labels should be provided when num_class_embeds > 0")
class_emb = self.class_embedding(class_labels)
class_emb = class_emb.to(dtype=x.dtype)
emb = emb + class_emb

# 3. initial convolution
Expand Down