Skip to content

Commit

Permalink
Interpolate fix on cuda for large output tensors (#10067)
Browse files Browse the repository at this point in the history
* Workaround for upscale with large output tensors.

Fixes #10040.

* Fix scale when output_size is given

* Style

---------

Co-authored-by: Sayak Paul <[email protected]>
  • Loading branch information
pcuenca and sayakpaul authored Dec 2, 2024
1 parent 6db3333 commit 2312b27
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/diffusers/models/upsampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ def forward(self, hidden_states: torch.Tensor, output_size: Optional[int] = None
# if `output_size` is passed we force the interpolation output
# size and do not make use of `scale_factor=2`
if self.interpolate:
# upsample_nearest_nhwc also fails when the number of output elements is large
# https://github.com/pytorch/pytorch/issues/141831
scale_factor = (
2 if output_size is None else max([f / s for f, s in zip(output_size, hidden_states.shape[-2:])])
)
if hidden_states.numel() * scale_factor > pow(2, 31):
hidden_states = hidden_states.contiguous()

if output_size is None:
hidden_states = F.interpolate(hidden_states, scale_factor=2.0, mode="nearest")
else:
Expand Down

0 comments on commit 2312b27

Please sign in to comment.