Skip to content

Commit 51843fd

Browse files
Refactor full determinism (#3485)
* up * fix more * Apply suggestions from code review * fix more * fix more * Check it * Remove 16:8 * fix more * fix more * fix more * up * up * Test only stable diffusion * Test only two files * up * Try out spinning up processes that can be killed * up * Apply suggestions from code review * up * up
1 parent 49ad61c commit 51843fd

File tree

58 files changed

+158
-170
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+158
-170
lines changed

src/diffusers/training_utils.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import contextlib
22
import copy
3-
import os
4-
import random
3+
from random import random
54
from typing import Any, Dict, Iterable, Optional, Union
65

76
import numpy as np
@@ -14,26 +13,6 @@
1413
import transformers
1514

1615

17-
def enable_full_determinism(seed: int):
18-
"""
19-
Helper function for reproducible behavior during distributed training. See
20-
- https://pytorch.org/docs/stable/notes/randomness.html for pytorch
21-
"""
22-
# set seed first
23-
set_seed(seed)
24-
25-
# Enable PyTorch deterministic mode. This potentially requires either the environment
26-
# variable 'CUDA_LAUNCH_BLOCKING' or 'CUBLAS_WORKSPACE_CONFIG' to be set,
27-
# depending on the CUDA version, so we set them both here
28-
os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
29-
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":16:8"
30-
torch.use_deterministic_algorithms(True)
31-
32-
# Enable CUDNN deterministic mode
33-
torch.backends.cudnn.deterministic = True
34-
torch.backends.cudnn.benchmark = False
35-
36-
3716
def set_seed(seed: int):
3817
"""
3918
Args:

src/diffusers/utils/testing_utils.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,3 +514,21 @@ def __exit__(self, *exc):
514514

515515
def __repr__(self):
516516
return f"captured: {self.out}\n"
517+
518+
519+
def enable_full_determinism():
520+
"""
521+
Helper function for reproducible behavior during distributed training. See
522+
- https://pytorch.org/docs/stable/notes/randomness.html for pytorch
523+
"""
524+
# Enable PyTorch deterministic mode. This potentially requires either the environment
525+
# variable 'CUDA_LAUNCH_BLOCKING' or 'CUBLAS_WORKSPACE_CONFIG' to be set,
526+
# depending on the CUDA version, so we set them both here
527+
os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
528+
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":16:8"
529+
torch.use_deterministic_algorithms(True)
530+
531+
# Enable CUDNN deterministic mode
532+
torch.backends.cudnn.deterministic = True
533+
torch.backends.cudnn.benchmark = False
534+
torch.backends.cuda.matmul.allow_tf32 = False

tests/models/test_layers_utils.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
from diffusers.utils import torch_device
2828

2929

30-
torch.backends.cuda.matmul.allow_tf32 = False
31-
32-
3330
class EmbeddingsTests(unittest.TestCase):
3431
def test_timestep_embeddings(self):
3532
embedding_dim = 256

tests/models/test_models_unet_1d.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
from .test_modeling_common import ModelTesterMixin
2424

2525

26-
torch.backends.cuda.matmul.allow_tf32 = False
27-
28-
2926
class UNet1DModelTests(ModelTesterMixin, unittest.TestCase):
3027
model_class = UNet1DModel
3128

tests/models/test_models_unet_2d.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121

2222
from diffusers import UNet2DModel
2323
from diffusers.utils import floats_tensor, logging, slow, torch_all_close, torch_device
24+
from diffusers.utils.testing_utils import enable_full_determinism
2425

2526
from .test_modeling_common import ModelTesterMixin
2627

2728

2829
logger = logging.get_logger(__name__)
29-
torch.backends.cuda.matmul.allow_tf32 = False
30-
torch.use_deterministic_algorithms(True)
30+
31+
enable_full_determinism()
3132

3233

3334
class Unet2DModelTests(ModelTesterMixin, unittest.TestCase):

tests/models/test_models_unet_2d_condition.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@
3333
torch_device,
3434
)
3535
from diffusers.utils.import_utils import is_xformers_available
36+
from diffusers.utils.testing_utils import enable_full_determinism
3637

3738
from .test_modeling_common import ModelTesterMixin
3839

3940

4041
logger = logging.get_logger(__name__)
41-
torch.backends.cuda.matmul.allow_tf32 = False
42-
torch.use_deterministic_algorithms(True)
42+
43+
enable_full_determinism()
4344

4445

4546
def create_lora_layers(model, mock_weights: bool = True):

tests/models/test_models_unet_3d_condition.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@
2929
torch_device,
3030
)
3131
from diffusers.utils.import_utils import is_xformers_available
32+
from diffusers.utils.testing_utils import enable_full_determinism
3233

3334
from .test_modeling_common import ModelTesterMixin
3435

3536

37+
enable_full_determinism()
38+
3639
logger = logging.get_logger(__name__)
37-
torch.backends.cuda.matmul.allow_tf32 = False
38-
torch.use_deterministic_algorithms(True)
3940

4041

4142
def create_lora_layers(model, mock_weights: bool = True):

tests/models/test_models_vae.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
from diffusers import AutoencoderKL
2323
from diffusers.utils import floats_tensor, load_hf_numpy, require_torch_gpu, slow, torch_all_close, torch_device
2424
from diffusers.utils.import_utils import is_xformers_available
25+
from diffusers.utils.testing_utils import enable_full_determinism
2526

2627
from .test_modeling_common import ModelTesterMixin
2728

2829

29-
torch.backends.cuda.matmul.allow_tf32 = False
30-
torch.use_deterministic_algorithms(True)
30+
enable_full_determinism()
3131

3232

3333
class AutoencoderKLTests(ModelTesterMixin, unittest.TestCase):

tests/models/test_models_vq.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
from diffusers import VQModel
2121
from diffusers.utils import floats_tensor, torch_device
22+
from diffusers.utils.testing_utils import enable_full_determinism
2223

2324
from .test_modeling_common import ModelTesterMixin
2425

2526

26-
torch.backends.cuda.matmul.allow_tf32 = False
27-
torch.use_deterministic_algorithms(True)
27+
enable_full_determinism()
2828

2929

3030
class VQModelTests(ModelTesterMixin, unittest.TestCase):

tests/others/test_ema.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@
2020

2121
from diffusers import UNet2DConditionModel
2222
from diffusers.training_utils import EMAModel
23-
from diffusers.utils.testing_utils import skip_mps, torch_device
23+
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps, torch_device
2424

2525

26-
torch.backends.cuda.matmul.allow_tf32 = False
27-
torch.use_deterministic_algorithms(True)
26+
enable_full_determinism()
2827

2928

3029
class EMAModelTests(unittest.TestCase):

tests/pipelines/altdiffusion/test_alt_diffusion.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@
2626
RobertaSeriesModelWithTransformation,
2727
)
2828
from diffusers.utils import slow, torch_device
29-
from diffusers.utils.testing_utils import require_torch_gpu
29+
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu
3030

3131
from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
3232
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin
3333

3434

35-
torch.backends.cuda.matmul.allow_tf32 = False
36-
torch.use_deterministic_algorithms(True)
35+
enable_full_determinism()
3736

3837

3938
class AltDiffusionPipelineFastTests(PipelineLatentTesterMixin, PipelineTesterMixin, unittest.TestCase):

tests/pipelines/altdiffusion/test_alt_diffusion_img2img.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@
3333
RobertaSeriesModelWithTransformation,
3434
)
3535
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
36-
from diffusers.utils.testing_utils import require_torch_gpu
36+
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu
3737

3838

39-
torch.backends.cuda.matmul.allow_tf32 = False
40-
torch.use_deterministic_algorithms(True)
39+
enable_full_determinism()
4140

4241

4342
class AltDiffusionImg2ImgPipelineFastTests(unittest.TestCase):

tests/pipelines/audio_diffusion/test_audio_diffusion.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@
3030
UNet2DModel,
3131
)
3232
from diffusers.utils import slow, torch_device
33-
from diffusers.utils.testing_utils import require_torch_gpu
33+
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu
3434

3535

36-
torch.backends.cuda.matmul.allow_tf32 = False
37-
torch.use_deterministic_algorithms(True)
36+
enable_full_determinism()
3837

3938

4039
class PipelineFastTests(unittest.TestCase):

tests/pipelines/audioldm/test_audioldm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@
3737
UNet2DConditionModel,
3838
)
3939
from diffusers.utils import slow, torch_device
40+
from diffusers.utils.testing_utils import enable_full_determinism
4041

4142
from ..pipeline_params import TEXT_TO_AUDIO_BATCH_PARAMS, TEXT_TO_AUDIO_PARAMS
4243
from ..test_pipelines_common import PipelineTesterMixin
4344

4445

45-
torch.backends.cuda.matmul.allow_tf32 = False
46-
torch.use_deterministic_algorithms(True)
46+
enable_full_determinism()
4747

4848

4949
class AudioLDMPipelineFastTests(PipelineTesterMixin, unittest.TestCase):

tests/pipelines/controlnet/test_controlnet.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet import MultiControlNetModel
3333
from diffusers.utils import load_image, load_numpy, randn_tensor, slow, torch_device
3434
from diffusers.utils.import_utils import is_xformers_available
35-
from diffusers.utils.testing_utils import require_torch_gpu
35+
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu
3636

3737
from ..pipeline_params import (
3838
TEXT_TO_IMAGE_BATCH_PARAMS,
@@ -41,8 +41,7 @@
4141
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin
4242

4343

44-
torch.backends.cuda.matmul.allow_tf32 = False
45-
torch.use_deterministic_algorithms(True)
44+
enable_full_determinism()
4645

4746

4847
class ControlNetPipelineFastTests(PipelineLatentTesterMixin, PipelineTesterMixin, unittest.TestCase):

tests/pipelines/controlnet/test_controlnet_img2img.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet import MultiControlNetModel
3636
from diffusers.utils import floats_tensor, load_image, load_numpy, randn_tensor, slow, torch_device
3737
from diffusers.utils.import_utils import is_xformers_available
38-
from diffusers.utils.testing_utils import require_torch_gpu
38+
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu
3939

4040
from ..pipeline_params import (
4141
TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
@@ -44,8 +44,7 @@
4444
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin
4545

4646

47-
torch.backends.cuda.matmul.allow_tf32 = False
48-
torch.use_deterministic_algorithms(True)
47+
enable_full_determinism()
4948

5049

5150
class ControlNetImg2ImgPipelineFastTests(PipelineLatentTesterMixin, PipelineTesterMixin, unittest.TestCase):

tests/pipelines/controlnet/test_controlnet_inpaint.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet import MultiControlNetModel
3636
from diffusers.utils import floats_tensor, load_image, load_numpy, randn_tensor, slow, torch_device
3737
from diffusers.utils.import_utils import is_xformers_available
38-
from diffusers.utils.testing_utils import require_torch_gpu
38+
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu
3939

4040
from ..pipeline_params import (
4141
TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
@@ -44,8 +44,7 @@
4444
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin
4545

4646

47-
torch.backends.cuda.matmul.allow_tf32 = False
48-
torch.use_deterministic_algorithms(True)
47+
enable_full_determinism()
4948

5049

5150
class ControlNetInpaintPipelineFastTests(PipelineLatentTesterMixin, PipelineTesterMixin, unittest.TestCase):

tests/pipelines/dance_diffusion/test_dance_diffusion.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121

2222
from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNet1DModel
2323
from diffusers.utils import slow, torch_device
24-
from diffusers.utils.testing_utils import require_torch_gpu, skip_mps
24+
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps
2525

2626
from ..pipeline_params import UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS, UNCONDITIONAL_AUDIO_GENERATION_PARAMS
2727
from ..test_pipelines_common import PipelineTesterMixin
2828

2929

30-
torch.backends.cuda.matmul.allow_tf32 = False
30+
enable_full_determinism()
3131

3232

3333
class DanceDiffusionPipelineFastTests(PipelineTesterMixin, unittest.TestCase):

tests/pipelines/ddim/test_ddim.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
import torch
2020

2121
from diffusers import DDIMPipeline, DDIMScheduler, UNet2DModel
22-
from diffusers.utils.testing_utils import require_torch_gpu, slow, torch_device
22+
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device
2323

2424
from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS
2525
from ..test_pipelines_common import PipelineTesterMixin
2626

2727

28-
torch.backends.cuda.matmul.allow_tf32 = False
28+
enable_full_determinism()
2929

3030

3131
class DDIMPipelineFastTests(PipelineTesterMixin, unittest.TestCase):

tests/pipelines/ddpm/test_ddpm.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
import torch
2020

2121
from diffusers import DDPMPipeline, DDPMScheduler, UNet2DModel
22-
from diffusers.utils.testing_utils import require_torch_gpu, slow, torch_device
22+
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device
2323

2424

25-
torch.backends.cuda.matmul.allow_tf32 = False
25+
enable_full_determinism()
2626

2727

2828
class DDPMPipelineFastTests(unittest.TestCase):

tests/pipelines/dit/test_dit.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, DPMSolverMultistepScheduler, Transformer2DModel
2323
from diffusers.utils import is_xformers_available, load_numpy, slow, torch_device
24-
from diffusers.utils.testing_utils import require_torch_gpu
24+
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu
2525

2626
from ..pipeline_params import (
2727
CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS,
@@ -30,7 +30,7 @@
3030
from ..test_pipelines_common import PipelineTesterMixin
3131

3232

33-
torch.backends.cuda.matmul.allow_tf32 = False
33+
enable_full_determinism()
3434

3535

3636
class DiTPipelineFastTests(PipelineTesterMixin, unittest.TestCase):

tests/pipelines/karras_ve/test_karras_ve.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
import torch
2020

2121
from diffusers import KarrasVePipeline, KarrasVeScheduler, UNet2DModel
22-
from diffusers.utils.testing_utils import require_torch, slow, torch_device
22+
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device
2323

2424

25-
torch.backends.cuda.matmul.allow_tf32 = False
25+
enable_full_determinism()
2626

2727

2828
class KarrasVePipelineFastTests(unittest.TestCase):

tests/pipelines/latent_diffusion/test_latent_diffusion.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,20 @@
2121
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer
2222

2323
from diffusers import AutoencoderKL, DDIMScheduler, LDMTextToImagePipeline, UNet2DConditionModel
24-
from diffusers.utils.testing_utils import load_numpy, nightly, require_torch_gpu, slow, torch_device
24+
from diffusers.utils.testing_utils import (
25+
enable_full_determinism,
26+
load_numpy,
27+
nightly,
28+
require_torch_gpu,
29+
slow,
30+
torch_device,
31+
)
2532

2633
from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
2734
from ..test_pipelines_common import PipelineTesterMixin
2835

2936

30-
torch.backends.cuda.matmul.allow_tf32 = False
37+
enable_full_determinism()
3138

3239

3340
class LDMTextToImagePipelineFastTests(PipelineTesterMixin, unittest.TestCase):

0 commit comments

Comments
 (0)