Skip to content

Commit 1eb40c6

Browse files
Resnet only use contiguous in training mode. (#12977)
* fix contiguous Signed-off-by: jiqing-feng <jiqing.feng@intel.com> * update tol Signed-off-by: jiqing-feng <jiqing.feng@intel.com> * bigger tol Signed-off-by: jiqing-feng <jiqing.feng@intel.com> * fix tests Signed-off-by: jiqing-feng <jiqing.feng@intel.com> * update tol Signed-off-by: jiqing-feng <jiqing.feng@intel.com> --------- Signed-off-by: jiqing-feng <jiqing.feng@intel.com> Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
1 parent bff672f commit 1eb40c6

File tree

3 files changed

+12
-1
lines changed

3 files changed

+12
-1
lines changed

src/diffusers/models/resnet.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,12 @@ def forward(self, input_tensor: torch.Tensor, temb: torch.Tensor, *args, **kwarg
366366
hidden_states = self.conv2(hidden_states)
367367

368368
if self.conv_shortcut is not None:
369-
input_tensor = self.conv_shortcut(input_tensor.contiguous())
369+
# Only use contiguous() during training to avoid DDP gradient stride mismatch warning.
370+
# In inference mode (eval or no_grad), skip contiguous() for better performance, especially on CPU.
371+
# Issue: https://github.com/huggingface/diffusers/issues/12975
372+
if self.training:
373+
input_tensor = input_tensor.contiguous()
374+
input_tensor = self.conv_shortcut(input_tensor)
370375

371376
output_tensor = (input_tensor + hidden_states) / self.output_scale_factor
372377

tests/pipelines/kandinsky2_2/test_kandinsky_inpaint.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ def test_inference_batch_single_identical(self):
248248
def test_float16_inference(self):
249249
super().test_float16_inference(expected_max_diff=5e-1)
250250

251+
def test_save_load_dduf(self):
252+
super().test_save_load_dduf(atol=1e-3, rtol=1e-3)
253+
251254
@is_flaky()
252255
def test_model_cpu_offload_forward_pass(self):
253256
super().test_inference_batch_single_identical(expected_max_diff=8e-4)

tests/pipelines/kandinsky3/test_kandinsky3_img2img.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ def test_float16_inference(self):
191191
def test_inference_batch_single_identical(self):
192192
super().test_inference_batch_single_identical(expected_max_diff=1e-2)
193193

194+
def test_save_load_dduf(self):
195+
super().test_save_load_dduf(atol=1e-3, rtol=1e-3)
196+
194197

195198
@slow
196199
@require_torch_accelerator

0 commit comments

Comments
 (0)