


import argparse
import os


A_														=      {
    "pytorch": {
        "1.13": {
            "torch": "1.13.1",
            "torchvision": "0.14.1",
            "torchaudio": "0.13.1",
            "python": 3.9,
            "cuda": "cu116",
            "install": (
                "python3 -m pip install --no-cache-dir -U torch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1"
                " --extra-index-url https://download.pytorch.org/whl/cu116"
            ),
            "base_image": "nvidia/cuda:11.6.2-cudnn8-devel-ubuntu20.04",
        },
        "1.12": {
            "torch": "1.12.1",
            "torchvision": "0.13.1",
            "torchaudio": "0.12.1",
            "python": 3.9,
            "cuda": "cu113",
            "install": (
                "python3 -m pip install --no-cache-dir -U torch==1.12.1 torchvision==0.13.1 torchaudio==0.12.1"
                " --extra-index-url https://download.pytorch.org/whl/cu113"
            ),
            "base_image": "nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04",
        },
        "1.11": {
            "torch": "1.11.0",
            "torchvision": "0.12.0",
            "torchaudio": "0.11.0",
            "python": 3.9,
            "cuda": "cu113",
            "install": (
                "python3 -m pip install --no-cache-dir -U torch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0"
                " --extra-index-url https://download.pytorch.org/whl/cu113"
            ),
            "base_image": "nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04",
        },
        "1.10": {
            "torch": "1.10.2",
            "torchvision": "0.11.3",
            "torchaudio": "0.10.2",
            "python": 3.9,
            "cuda": "cu113",
            "install": (
                "python3 -m pip install --no-cache-dir -U torch==1.10.2 torchvision==0.11.3 torchaudio==0.10.2"
                " --extra-index-url https://download.pytorch.org/whl/cu113"
            ),
            "base_image": "nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04",
        },
        # torchaudio < 0.10 has no CUDA-enabled binary distributions
        "1.9": {
            "torch": "1.9.1",
            "torchvision": "0.10.1",
            "torchaudio": "0.9.1",
            "python": 3.9,
            "cuda": "cu111",
            "install": (
                "python3 -m pip install --no-cache-dir -U torch==1.9.1 torchvision==0.10.1 torchaudio==0.9.1"
                " --extra-index-url https://download.pytorch.org/whl/cu111"
            ),
            "base_image": "nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04",
        },
    },
    "tensorflow": {
        "2.11": {
            "tensorflow": "2.11.1",
            "install": "python3 -m pip install --no-cache-dir -U tensorflow==2.11.1",
            "base_image": "nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04",
        },
        "2.10": {
            "tensorflow": "2.10.1",
            "install": "python3 -m pip install --no-cache-dir -U tensorflow==2.10.1",
            "base_image": "nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04",
        },
        "2.9": {
            "tensorflow": "2.9.3",
            "install": "python3 -m pip install --no-cache-dir -U tensorflow==2.9.3",
            "base_image": "nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04",
        },
        "2.8": {
            "tensorflow": "2.8.2",
            "install": "python3 -m pip install --no-cache-dir -U tensorflow==2.8.2",
            "base_image": "nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04",
        },
        "2.7": {
            "tensorflow": "2.7.3",
            "install": "python3 -m pip install --no-cache-dir -U tensorflow==2.7.3",
            "base_image": "nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04",
        },
        "2.6": {
            "tensorflow": "2.6.5",
            "install": "python3 -m pip install --no-cache-dir -U tensorflow==2.6.5",
            "base_image": "nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04",
        },
        "2.5": {
            "tensorflow": "2.5.3",
            "install": "python3 -m pip install --no-cache-dir -U tensorflow==2.5.3",
            "base_image": "nvidia/cuda:11.2.2-cudnn8-devel-ubuntu20.04",
        },
    },
}


if __name__ == "__main__":
		A_														=      argparse.ArgumentParser("Choose the framework and version to install")
		parser.add_argument(
		    "--framework", help="The framework to install. Should be `torch` or `tensorflow`", type=str, required=True
		)
		parser.add_argument("--version", help="The version of the framework to install.", type=str, required=True)
		A_														=      parser.parse_args()

		A_														=      past_versions_testing[args.framework][args.version]

		os.system(F'echo "export INSTALL_CMD=\'{info["install"]}\'" >> ~/.profile')
		print(F'echo "export INSTALL_CMD=\'{info["install"]}\'" >> ~/.profile')

		A_														=      ""
		if args.framework == "pytorch":
				A_														=      info["cuda"]
		os.system(F'echo \"export CUDA=\'{cuda}\'\" >> ~/.profile')
		print(F'echo \"export CUDA=\'{cuda}\'\" >> ~/.profile')






import tempfile
import unittest

import torch

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


enable_full_determinism()

class 						snake_case			(       unittest.TestCase	):
			'''simple docstring'''

			UpperCAmelCase       :       Union[str, Any]       = """hf-internal-testing/tiny-stable-diffusion-pipe"""
			UpperCAmelCase       :       int       = 1
			UpperCAmelCase       :       Optional[Any]       = 77
			UpperCAmelCase       :       Any       = 32
			UpperCAmelCase       :       str       = 4
			UpperCAmelCase       :       Optional[int]       = 64
			UpperCAmelCase       :       Tuple       = torch.manual_seed(0	)

			def _lowercase (	self     :     str							,							lowerCAmelCase_     :     str=0.9_999					)					->					int:




								"""simple docstring"""






								SCREAMING_SNAKE_CASE_	      =       UNetaDConditionModel.from_pretrained(self.model_id							,							subfolder='''unet'''					)
								SCREAMING_SNAKE_CASE_	      =       unet.to(lowerCAmelCase_					)
								SCREAMING_SNAKE_CASE_	      =       EMAModel(unet.parameters()							,							decay=lowerCAmelCase_							,							model_cls=lowerCAmelCase_							,							model_config=unet.config					)
								return unet, ema_unet

			def _lowercase (	self     :     str					)					->					List[str]:




								"""simple docstring"""






								SCREAMING_SNAKE_CASE_	      =       torch.randn(
								    self.batch_size							,							self.num_in_channels							,							self.latent_height							,							self.latent_width							,							generator=self.generator					).to(lowerCAmelCase_					)
								SCREAMING_SNAKE_CASE_	      =       torch.randint(0							,							1_000							,							size=(self.batch_size,)							,							generator=self.generator					).to(lowerCAmelCase_					)
								SCREAMING_SNAKE_CASE_	      =       torch.randn(
								    self.batch_size							,							self.prompt_length							,							self.text_encoder_hidden_dim							,							generator=self.generator					).to(lowerCAmelCase_					)
								return noisy_latents, timesteps, encoder_hidden_states

			def _lowercase (	self     :     str							,							lowerCAmelCase_     :     Tuple					)					->					str:




								"""simple docstring"""






								SCREAMING_SNAKE_CASE_	      =       {}
								for k, param in unet.state_dict().items():
													SCREAMING_SNAKE_CASE_	      =       torch.randn_like(lowerCAmelCase_					) + (param * torch.randn_like(lowerCAmelCase_					))
													updated_state_dict.update({k: updated_param}					)
								unet.load_state_dict(lowerCAmelCase_					)
								return unet

			def _lowercase (	self     :     Dict					)					->					str:




								"""simple docstring"""






								SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       self.get_models()
								# Take the first (hypothetical) EMA step.
								ema_unet.step(unet.parameters()					)
								assert ema_unet.optimization_step == 1

								# Take two more.
								for _ in range(2					):
													ema_unet.step(unet.parameters()					)
								assert ema_unet.optimization_step == 3

			def _lowercase (	self     :     Union[str, Any]					)					->					List[str]:




								"""simple docstring"""






								SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       self.get_models()
								# Since the `unet` is not being updated (i.e., backprop'd)
								# there won't be any difference between the `params` of `unet`
								# and `ema_unet` even if we call `ema_unet.step(unet.parameters())`.
								ema_unet.step(unet.parameters()					)
								SCREAMING_SNAKE_CASE_	      =       list(unet.parameters()					)
								for s_param, param in zip(ema_unet.shadow_params							,							lowerCAmelCase_					):
													assert torch.allclose(lowerCAmelCase_							,							lowerCAmelCase_					)

								# The above holds true even if we call `ema.step()` multiple times since
								# `unet` params are still not being updated.
								for _ in range(4					):
													ema_unet.step(unet.parameters()					)
								for s_param, param in zip(ema_unet.shadow_params							,							lowerCAmelCase_					):
													assert torch.allclose(lowerCAmelCase_							,							lowerCAmelCase_					)

			def _lowercase (	self     :     Any					)					->					Union[str, Any]:




								"""simple docstring"""






								SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       self.get_models()
								# Here we simulate the parameter updates for `unet`. Since there might
								# be some parameters which are initialized to zero we take extra care to
								# initialize their values to something non-zero before the multiplication.
								SCREAMING_SNAKE_CASE_	      =       self.simulate_backprop(lowerCAmelCase_					)

								# Take the EMA step.
								ema_unet.step(unet_pseudo_updated_step_one.parameters()					)

								# Now the EMA'd parameters won't be equal to the original model parameters.
								SCREAMING_SNAKE_CASE_	      =       list(unet_pseudo_updated_step_one.parameters()					)
								for s_param, param in zip(ema_unet.shadow_params							,							lowerCAmelCase_					):
													assert ~torch.allclose(lowerCAmelCase_							,							lowerCAmelCase_					)

								# Ensure this is the case when we take multiple EMA steps.
								for _ in range(4					):
													ema_unet.step(unet.parameters()					)
								for s_param, param in zip(ema_unet.shadow_params							,							lowerCAmelCase_					):
													assert ~torch.allclose(lowerCAmelCase_							,							lowerCAmelCase_					)

			def _lowercase (	self     :     Any					)					->					Tuple:




								"""simple docstring"""






								SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       self.get_models()

								# First backprop + EMA
								SCREAMING_SNAKE_CASE_	      =       self.simulate_backprop(lowerCAmelCase_					)
								ema_unet.step(unet_step_one.parameters()					)
								SCREAMING_SNAKE_CASE_	      =       ema_unet.shadow_params

								# Second backprop + EMA
								SCREAMING_SNAKE_CASE_	      =       self.simulate_backprop(lowerCAmelCase_					)
								ema_unet.step(unet_step_two.parameters()					)
								SCREAMING_SNAKE_CASE_	      =       ema_unet.shadow_params

								for step_one, step_two in zip(lowerCAmelCase_							,							lowerCAmelCase_					):
													assert ~torch.allclose(lowerCAmelCase_							,							lowerCAmelCase_					)

			def _lowercase (	self     :     str					)					->					Optional[Any]:




								"""simple docstring"""






								SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       self.get_models(decay=0.0					)
								SCREAMING_SNAKE_CASE_	      =       self.simulate_backprop(lowerCAmelCase_					)
								ema_unet.step(unet_step_one.parameters()					)
								SCREAMING_SNAKE_CASE_	      =       ema_unet.shadow_params

								SCREAMING_SNAKE_CASE_	      =       self.simulate_backprop(lowerCAmelCase_					)
								ema_unet.step(unet_step_two.parameters()					)
								SCREAMING_SNAKE_CASE_	      =       ema_unet.shadow_params

								for step_one, step_two in zip(lowerCAmelCase_							,							lowerCAmelCase_					):
													assert torch.allclose(lowerCAmelCase_							,							lowerCAmelCase_					)




			@skip_mps
			def _lowercase (	self     :     str					)					->					List[Any]:




								"""simple docstring"""






								SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       self.get_models()
								SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_      ,SCREAMING_SNAKE_CASE_	      =       self.get_dummy_inputs()

								with tempfile.TemporaryDirectory() as tmpdir:
													ema_unet.save_pretrained(lowerCAmelCase_					)
													SCREAMING_SNAKE_CASE_	      =       UNetaDConditionModel.from_pretrained(lowerCAmelCase_							,							model_cls=lowerCAmelCase_					)
													SCREAMING_SNAKE_CASE_	      =       loaded_unet.to(unet.device					)

								# Since no EMA step has been performed the outputs should match.
								SCREAMING_SNAKE_CASE_	      =       unet(lowerCAmelCase_							,							lowerCAmelCase_							,							lowerCAmelCase_					).sample
								SCREAMING_SNAKE_CASE_	      =       loaded_unet(lowerCAmelCase_							,							lowerCAmelCase_							,							lowerCAmelCase_					).sample

								assert torch.allclose(lowerCAmelCase_							,							lowerCAmelCase_							,							atol=1e-4					)


