Skip to content

Commit 52110fb

Browse files
Fix LoRA hot-swapping recompilation with different_shapes_for_compilation (#14297)
[tests] fix hot-swapping recompilation with different_shapes_for_compilation `test_hotswapping_compiled_model_linear` and `test_hotswapping_compiled_model_both_linear_and_other` failed for every model setting `different_shapes_for_compilation`, e.g.: RecompileError: tensor 'hidden_states' size mismatch at index 1. expected 16, actual 32 The dummy inputs are `(batch, height * width, channels)` with `channels = 16` and the first traced shape is `(4, 4)`, so the image sequence length equals the channel count. Duck shaping assigns both dims the same symbol, which `img_in` (an `nn.Linear` with constant `in_features`) then specializes to 16, forcing a recompile on the next shape. `use_duck_shape = False` was already set in `test_compile_on_different_shapes` and `test_hotswapping_compile_on_different_shapes` (#11327) but the other two multi-shape hot-swapping tests were missed. Move it into `_check_model_hotswap` so all of them are covered, and drop the now-redundant duplicate. Also drop the two `xfail(strict=True)` markers on `TestQwenImageTransformerLoRAHotSwap` that were masking this bug. Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
1 parent 6009225 commit 52110fb

2 files changed

Lines changed: 6 additions & 12 deletions

File tree

tests/models/testing_utils/lora.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ def _check_model_hotswap(
321321
fine.
322322
"""
323323
different_shapes = self.different_shapes_for_compilation
324+
if different_shapes is not None:
325+
# Specifying `use_duck_shape=False` instructs the compiler if it should use the same symbolic
326+
# variable to represent input sizes that are the same. For more details,
327+
# check out this [comment](https://github.com/huggingface/diffusers/pull/11327#discussion_r2047659790).
328+
torch.fx.experimental._config.use_duck_shape = False
329+
324330
# create 2 adapters with different ranks and alphas
325331
torch.manual_seed(0)
326332
init_dict = self.get_init_dict()
@@ -557,10 +563,6 @@ def test_hotswapping_compile_on_different_shapes(self, tmp_path, rank0, rank1):
557563
different_shapes_for_compilation = self.different_shapes_for_compilation
558564
if different_shapes_for_compilation is None:
559565
pytest.skip(f"Skipping as `different_shapes_for_compilation` is not set for {self.__class__.__name__}.")
560-
# Specifying `use_duck_shape=False` instructs the compiler if it should use the same symbolic
561-
# variable to represent input sizes that are the same. For more details,
562-
# check out this [comment](https://github.com/huggingface/diffusers/pull/11327#discussion_r2047659790).
563-
torch.fx.experimental._config.use_duck_shape = False
564566

565567
target_modules = ["to_q", "to_k", "to_v", "to_out.0"]
566568
with torch._dynamo.config.patch(error_on_recompile=True):

tests/models/transformers/test_models_transformer_qwenimage.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -305,14 +305,6 @@ class TestQwenImageTransformerLoRA(QwenImageTransformerTesterConfig, LoraTesterM
305305
class TestQwenImageTransformerLoRAHotSwap(QwenImageTransformerTesterConfig, LoraHotSwappingForModelTesterMixin):
306306
"""LoRA hot-swapping tests for QwenImage Transformer."""
307307

308-
@pytest.mark.xfail(True, reason="Recompilation issues.", strict=True)
309-
def test_hotswapping_compiled_model_linear(self):
310-
super().test_hotswapping_compiled_model_linear()
311-
312-
@pytest.mark.xfail(True, reason="Recompilation issues.", strict=True)
313-
def test_hotswapping_compiled_model_both_linear_and_other(self):
314-
super().test_hotswapping_compiled_model_both_linear_and_other()
315-
316308
@property
317309
def different_shapes_for_compilation(self):
318310
return [(4, 4), (4, 8), (8, 8)]

0 commit comments

Comments
 (0)