Skip to content

Refactor full determinism #3485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 1 addition & 22 deletions src/diffusers/training_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import contextlib
import copy
import os
import random
from random import random
from typing import Any, Dict, Iterable, Optional, Union

import numpy as np
Expand All @@ -14,26 +13,6 @@
import transformers


def enable_full_determinism(seed: int):
"""
Helper function for reproducible behavior during distributed training. See
- https://pytorch.org/docs/stable/notes/randomness.html for pytorch
"""
# set seed first
set_seed(seed)

# Enable PyTorch deterministic mode. This potentially requires either the environment
# variable 'CUDA_LAUNCH_BLOCKING' or 'CUBLAS_WORKSPACE_CONFIG' to be set,
# depending on the CUDA version, so we set them both here
os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":16:8"
torch.use_deterministic_algorithms(True)

# Enable CUDNN deterministic mode
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False


def set_seed(seed: int):
"""
Args:
Expand Down
18 changes: 18 additions & 0 deletions src/diffusers/utils/testing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,21 @@ def __exit__(self, *exc):

def __repr__(self):
return f"captured: {self.out}\n"


def enable_full_determinism():
"""
Helper function for reproducible behavior during distributed training. See
- https://pytorch.org/docs/stable/notes/randomness.html for pytorch
"""
# Enable PyTorch deterministic mode. This potentially requires either the environment
# variable 'CUDA_LAUNCH_BLOCKING' or 'CUBLAS_WORKSPACE_CONFIG' to be set,
# depending on the CUDA version, so we set them both here
os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":16:8"
torch.use_deterministic_algorithms(True)

# Enable CUDNN deterministic mode
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
torch.backends.cuda.matmul.allow_tf32 = False
3 changes: 0 additions & 3 deletions tests/models/test_layers_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
from diffusers.utils import torch_device


torch.backends.cuda.matmul.allow_tf32 = False


class EmbeddingsTests(unittest.TestCase):
def test_timestep_embeddings(self):
embedding_dim = 256
Expand Down
3 changes: 0 additions & 3 deletions tests/models/test_models_unet_1d.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
from .test_modeling_common import ModelTesterMixin


torch.backends.cuda.matmul.allow_tf32 = False


class UNet1DModelTests(ModelTesterMixin, unittest.TestCase):
model_class = UNet1DModel

Expand Down
5 changes: 3 additions & 2 deletions tests/models/test_models_unet_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@

from diffusers import UNet2DModel
from diffusers.utils import floats_tensor, logging, slow, torch_all_close, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin


logger = logging.get_logger(__name__)
torch.backends.cuda.matmul.allow_tf32 = False
torch.use_deterministic_algorithms(True)

enable_full_determinism()


class Unet2DModelTests(ModelTesterMixin, unittest.TestCase):
Expand Down
5 changes: 3 additions & 2 deletions tests/models/test_models_unet_2d_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@
torch_device,
)
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin


logger = logging.get_logger(__name__)
torch.backends.cuda.matmul.allow_tf32 = False
torch.use_deterministic_algorithms(True)

enable_full_determinism()


def create_lora_layers(model, mock_weights: bool = True):
Expand Down
5 changes: 3 additions & 2 deletions tests/models/test_models_unet_3d_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
torch_device,
)
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin


enable_full_determinism()

logger = logging.get_logger(__name__)
torch.backends.cuda.matmul.allow_tf32 = False
torch.use_deterministic_algorithms(True)


def create_lora_layers(model, mock_weights: bool = True):
Expand Down
4 changes: 2 additions & 2 deletions tests/models/test_models_vae.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
from diffusers import AutoencoderKL
from diffusers.utils import floats_tensor, load_hf_numpy, require_torch_gpu, slow, torch_all_close, torch_device
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin


torch.backends.cuda.matmul.allow_tf32 = False
torch.use_deterministic_algorithms(True)
enable_full_determinism()


class AutoencoderKLTests(ModelTesterMixin, unittest.TestCase):
Expand Down
4 changes: 2 additions & 2 deletions tests/models/test_models_vq.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@

from diffusers import VQModel
from diffusers.utils import floats_tensor, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin


torch.backends.cuda.matmul.allow_tf32 = False
torch.use_deterministic_algorithms(True)
enable_full_determinism()


class VQModelTests(ModelTesterMixin, unittest.TestCase):
Expand Down
5 changes: 2 additions & 3 deletions tests/others/test_ema.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@

from diffusers import UNet2DConditionModel
from diffusers.training_utils import EMAModel
from diffusers.utils.testing_utils import skip_mps, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps, torch_device


torch.backends.cuda.matmul.allow_tf32 = False
torch.use_deterministic_algorithms(True)
enable_full_determinism()


class EMAModelTests(unittest.TestCase):
Expand Down
5 changes: 2 additions & 3 deletions tests/pipelines/altdiffusion/test_alt_diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@
RobertaSeriesModelWithTransformation,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import require_torch_gpu
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


torch.backends.cuda.matmul.allow_tf32 = False
torch.use_deterministic_algorithms(True)
enable_full_determinism()


class AltDiffusionPipelineFastTests(PipelineLatentTesterMixin, PipelineTesterMixin, unittest.TestCase):
Expand Down
5 changes: 2 additions & 3 deletions tests/pipelines/altdiffusion/test_alt_diffusion_img2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@
RobertaSeriesModelWithTransformation,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import require_torch_gpu
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


torch.backends.cuda.matmul.allow_tf32 = False
torch.use_deterministic_algorithms(True)
enable_full_determinism()


class AltDiffusionImg2ImgPipelineFastTests(unittest.TestCase):
Expand Down
5 changes: 2 additions & 3 deletions tests/pipelines/audio_diffusion/test_audio_diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,10 @@
UNet2DModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import require_torch_gpu
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


torch.backends.cuda.matmul.allow_tf32 = False
torch.use_deterministic_algorithms(True)
enable_full_determinism()


class PipelineFastTests(unittest.TestCase):
Expand Down
4 changes: 2 additions & 2 deletions tests/pipelines/audioldm/test_audioldm.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@
UNet2DConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from ..pipeline_params import TEXT_TO_AUDIO_BATCH_PARAMS, TEXT_TO_AUDIO_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


torch.backends.cuda.matmul.allow_tf32 = False
torch.use_deterministic_algorithms(True)
enable_full_determinism()


class AudioLDMPipelineFastTests(PipelineTesterMixin, unittest.TestCase):
Expand Down
5 changes: 2 additions & 3 deletions tests/pipelines/controlnet/test_controlnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet import MultiControlNetModel
from diffusers.utils import load_image, load_numpy, randn_tensor, slow, torch_device
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import require_torch_gpu
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
TEXT_TO_IMAGE_BATCH_PARAMS,
Expand All @@ -41,8 +41,7 @@
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


torch.backends.cuda.matmul.allow_tf32 = False
torch.use_deterministic_algorithms(True)
enable_full_determinism()


class ControlNetPipelineFastTests(PipelineLatentTesterMixin, PipelineTesterMixin, unittest.TestCase):
Expand Down
5 changes: 2 additions & 3 deletions tests/pipelines/controlnet/test_controlnet_img2img.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet import MultiControlNetModel
from diffusers.utils import floats_tensor, load_image, load_numpy, randn_tensor, slow, torch_device
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import require_torch_gpu
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
Expand All @@ -44,8 +44,7 @@
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


torch.backends.cuda.matmul.allow_tf32 = False
torch.use_deterministic_algorithms(True)
enable_full_determinism()


class ControlNetImg2ImgPipelineFastTests(PipelineLatentTesterMixin, PipelineTesterMixin, unittest.TestCase):
Expand Down
5 changes: 2 additions & 3 deletions tests/pipelines/controlnet/test_controlnet_inpaint.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet import MultiControlNetModel
from diffusers.utils import floats_tensor, load_image, load_numpy, randn_tensor, slow, torch_device
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import require_torch_gpu
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
Expand All @@ -44,8 +44,7 @@
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


torch.backends.cuda.matmul.allow_tf32 = False
torch.use_deterministic_algorithms(True)
enable_full_determinism()


class ControlNetInpaintPipelineFastTests(PipelineLatentTesterMixin, PipelineTesterMixin, unittest.TestCase):
Expand Down
4 changes: 2 additions & 2 deletions tests/pipelines/dance_diffusion/test_dance_diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@

from diffusers import DanceDiffusionPipeline, IPNDMScheduler, UNet1DModel
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import require_torch_gpu, skip_mps
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

from ..pipeline_params import UNCONDITIONAL_AUDIO_GENERATION_BATCH_PARAMS, UNCONDITIONAL_AUDIO_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


torch.backends.cuda.matmul.allow_tf32 = False
enable_full_determinism()


class DanceDiffusionPipelineFastTests(PipelineTesterMixin, unittest.TestCase):
Expand Down
4 changes: 2 additions & 2 deletions tests/pipelines/ddim/test_ddim.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
import torch

from diffusers import DDIMPipeline, DDIMScheduler, UNet2DModel
from diffusers.utils.testing_utils import require_torch_gpu, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device

from ..pipeline_params import UNCONDITIONAL_IMAGE_GENERATION_BATCH_PARAMS, UNCONDITIONAL_IMAGE_GENERATION_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


torch.backends.cuda.matmul.allow_tf32 = False
enable_full_determinism()


class DDIMPipelineFastTests(PipelineTesterMixin, unittest.TestCase):
Expand Down
4 changes: 2 additions & 2 deletions tests/pipelines/ddpm/test_ddpm.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import torch

from diffusers import DDPMPipeline, DDPMScheduler, UNet2DModel
from diffusers.utils.testing_utils import require_torch_gpu, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, slow, torch_device


torch.backends.cuda.matmul.allow_tf32 = False
enable_full_determinism()


class DDPMPipelineFastTests(unittest.TestCase):
Expand Down
4 changes: 2 additions & 2 deletions tests/pipelines/dit/test_dit.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

from diffusers import AutoencoderKL, DDIMScheduler, DiTPipeline, DPMSolverMultistepScheduler, Transformer2DModel
from diffusers.utils import is_xformers_available, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import require_torch_gpu
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
CLASS_CONDITIONED_IMAGE_GENERATION_BATCH_PARAMS,
Expand All @@ -30,7 +30,7 @@
from ..test_pipelines_common import PipelineTesterMixin


torch.backends.cuda.matmul.allow_tf32 = False
enable_full_determinism()


class DiTPipelineFastTests(PipelineTesterMixin, unittest.TestCase):
Expand Down
4 changes: 2 additions & 2 deletions tests/pipelines/karras_ve/test_karras_ve.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
import torch

from diffusers import KarrasVePipeline, KarrasVeScheduler, UNet2DModel
from diffusers.utils.testing_utils import require_torch, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


torch.backends.cuda.matmul.allow_tf32 = False
enable_full_determinism()


class KarrasVePipelineFastTests(unittest.TestCase):
Expand Down
11 changes: 9 additions & 2 deletions tests/pipelines/latent_diffusion/test_latent_diffusion.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,20 @@
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, LDMTextToImagePipeline, UNet2DConditionModel
from diffusers.utils.testing_utils import load_numpy, nightly, require_torch_gpu, slow, torch_device
from diffusers.utils.testing_utils import (
enable_full_determinism,
load_numpy,
nightly,
require_torch_gpu,
slow,
torch_device,
)

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


torch.backends.cuda.matmul.allow_tf32 = False
enable_full_determinism()


class LDMTextToImagePipelineFastTests(PipelineTesterMixin, unittest.TestCase):
Expand Down
Loading