mirror of
https://github.com/huggingface/diffusers.git
synced 2026-10-24 20:59:27 +03:00
Make Stable Diffusion device-map comparisons deterministic (#15010)
Description: The balanced device-map tests compare images from single-device and multi-device inference. On XPU, repeated fp16 inference can produce different results even with the same seed, causing the pixel comparison to fail. Enable PyTorch deterministic algorithms for the two image-comparison tests and restore the previous setting afterward. This keeps other tests unaffected. Validation: Both affected tests passed on a multi-XPU runner (RUN_NIGHTLY=1 RUN_SLOW=1). Signed-off-by: Wang, Yi A <yi.a.wang@intel.com> Co-authored-by: Sayak Paul <spsayakpaul@gmail.com>
This commit is contained in:
@@ -1189,6 +1189,16 @@ class TestStableDiffusionPipelineDeviceMap:
|
||||
gc.collect()
|
||||
backend_empty_cache(torch_device)
|
||||
|
||||
@pytest.fixture
|
||||
def deterministic_algorithms(self):
|
||||
enabled = torch.are_deterministic_algorithms_enabled()
|
||||
warn_only = torch.is_deterministic_algorithms_warn_only_enabled()
|
||||
torch.use_deterministic_algorithms(True)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
torch.use_deterministic_algorithms(enabled, warn_only=warn_only)
|
||||
|
||||
def get_inputs(self, generator_device="cpu", seed=0):
|
||||
generator = torch.Generator(device=generator_device).manual_seed(seed)
|
||||
inputs = {
|
||||
@@ -1212,7 +1222,7 @@ class TestStableDiffusionPipelineDeviceMap:
|
||||
|
||||
return no_device_map_image
|
||||
|
||||
def test_forward_pass_balanced_device_map(self):
|
||||
def test_forward_pass_balanced_device_map(self, deterministic_algorithms):
|
||||
no_device_map_image = self.get_pipeline_output_without_device_map()
|
||||
|
||||
sd_pipe_with_device_map = StableDiffusionPipeline.from_pretrained(
|
||||
@@ -1232,7 +1242,7 @@ class TestStableDiffusionPipelineDeviceMap:
|
||||
|
||||
assert len(set(sd_pipe_with_device_map.hf_device_map.values())) >= 2
|
||||
|
||||
def test_max_memory(self):
|
||||
def test_max_memory(self, deterministic_algorithms):
|
||||
no_device_map_image = self.get_pipeline_output_without_device_map()
|
||||
|
||||
sd_pipe_with_device_map = StableDiffusionPipeline.from_pretrained(
|
||||
|
||||
Reference in New Issue
Block a user