Skip to content

Commit

Permalink
fix F.interpolate() for large batch sizes (huggingface#1006)
Browse files Browse the repository at this point in the history
* fix `upsample_nearest_nhwc` for large bsz

* fix `upsample_nearest_nhwc` for large bsz
  • Loading branch information
NouamaneTazi authored Oct 28, 2022
1 parent 1e07b6b commit ab079f2
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/diffusers/models/resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def forward(self, hidden_states, output_size=None):
if dtype == torch.bfloat16:
hidden_states = hidden_states.to(torch.float32)

# upsample_nearest_nhwc fails with large batch sizes. see https://github.com/huggingface/diffusers/issues/984
if hidden_states.shape[0] >= 64:
hidden_states = hidden_states.contiguous()

# if `output_size` is passed we force the interpolation output
# size and do not make use of `scale_factor=2`
if output_size is None:
Expand Down Expand Up @@ -376,6 +380,10 @@ def forward(self, input_tensor, temb):
hidden_states = self.nonlinearity(hidden_states)

if self.upsample is not None:
# upsample_nearest_nhwc fails with large batch sizes. see https://github.com/huggingface/diffusers/issues/984
if hidden_states.shape[0] >= 64:
input_tensor = input_tensor.contiguous()
hidden_states = hidden_states.contiguous()
input_tensor = self.upsample(input_tensor)
hidden_states = self.upsample(hidden_states)
elif self.downsample is not None:
Expand Down

0 comments on commit ab079f2

Please sign in to comment.