




import unittest

import numpy as np
import torch

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


enable_full_determinism()




class 	UpperCamelCase__			(unittest.TestCase  ):

				'''simple docstring'''



				@property
				def 					_lowercase  (					self  )  ->   Union[str, Any]:
											torch.manual_seed(0  )
											lowerCamelCase     :   Optional[int]    				=      UNetaDModel(
											    block_out_channels=(32, 64)  ,						layers_per_block=2  ,						sample_size=32  ,						in_channels=3  ,						out_channels=3  ,						down_block_types=("DownBlock2D", "AttnDownBlock2D")  ,						up_block_types=("AttnUpBlock2D", "UpBlock2D")  ,						)
											return model







				def 					_lowercase  (					self  )  ->   List[Any]:
											lowerCamelCase     :   Tuple    				=      "cpu"
											lowerCamelCase     :   List[str]    				=      self.dummy_uncond_unet
											lowerCamelCase     :   str    				=      DDPMScheduler()

											lowerCamelCase     :   List[str]    				=      DDPMPipeline(unet=UpperCamelCase__  ,						scheduler=UpperCamelCase__  )
											ddpm.to(UpperCamelCase__  )
											ddpm.set_progress_bar_config(disable=UpperCamelCase__  )

											lowerCamelCase     :   Union[str, Any]    				=      torch.Generator(device=UpperCamelCase__  ).manual_seed(0  )
											lowerCamelCase     :   str    				=      ddpm(generator=UpperCamelCase__  ,						num_inference_steps=2  ,						output_type="numpy"  ).images

											lowerCamelCase     :   Dict    				=      torch.Generator(device=UpperCamelCase__  ).manual_seed(0  )
											lowerCamelCase     :   Tuple    				=      ddpm(generator=UpperCamelCase__  ,						num_inference_steps=2  ,						output_type="numpy"  ,						return_dict=UpperCamelCase__  )[0]

											lowerCamelCase     :   Tuple    				=      image[0, -3:, -3:, -1]
											lowerCamelCase     :   Tuple    				=      image_from_tuple[0, -3:, -3:, -1]

											assert image.shape == (1, 32, 32, 3)
											lowerCamelCase     :   List[Any]    				=      np.array(
											    [9.956e-01, 5.785e-01, 4.675e-01, 9.930e-01, 0.0, 1.000, 1.199e-03, 2.648e-04, 5.101e-04]  )

											assert np.abs(image_slice.flatten() - expected_slice  ).max() < 1e-2
											assert np.abs(image_from_tuple_slice.flatten() - expected_slice  ).max() < 1e-2







				def 					_lowercase  (					self  )  ->   Dict:
											lowerCamelCase     :   Union[str, Any]    				=      self.dummy_uncond_unet
											lowerCamelCase     :   str    				=      DDPMScheduler(prediction_type="sample"  )

											lowerCamelCase     :   Tuple    				=      DDPMPipeline(unet=UpperCamelCase__  ,						scheduler=UpperCamelCase__  )
											ddpm.to(UpperCamelCase__  )
											ddpm.set_progress_bar_config(disable=UpperCamelCase__  )

											lowerCamelCase     :   Any    				=      torch.manual_seed(0  )
											lowerCamelCase     :   int    				=      ddpm(generator=UpperCamelCase__  ,						num_inference_steps=2  ,						output_type="numpy"  ).images

											lowerCamelCase     :   Tuple    				=      torch.manual_seed(0  )
											lowerCamelCase     :   Dict    				=      ddpm(generator=UpperCamelCase__  ,						num_inference_steps=2  ,						output_type="numpy"  )[0]

											lowerCamelCase     :   Optional[Any]    				=      image[0, -3:, -3:, -1]
											lowerCamelCase     :   Tuple    				=      image_eps[0, -3:, -3:, -1]

											assert image.shape == (1, 32, 32, 3)
											lowerCamelCase     :   Optional[int]    				=      1e-2 if torch_device != "mps" else 3e-2
											assert np.abs(image_slice.flatten() - image_eps_slice.flatten()  ).max() < tolerance




@slow
@require_torch_gpu
class 	UpperCamelCase__			(unittest.TestCase  ):

				'''simple docstring'''



				def 					_lowercase  (					self  )  ->   int:
											lowerCamelCase     :   Optional[int]    				=      "google/ddpm-cifar10-32"

											lowerCamelCase     :   Union[str, Any]    				=      UNetaDModel.from_pretrained(UpperCamelCase__  )
											lowerCamelCase     :   int    				=      DDPMScheduler.from_pretrained(UpperCamelCase__  )

											lowerCamelCase     :   Optional[Any]    				=      DDPMPipeline(unet=UpperCamelCase__  ,						scheduler=UpperCamelCase__  )
											ddpm.to(UpperCamelCase__  )
											ddpm.set_progress_bar_config(disable=UpperCamelCase__  )

											lowerCamelCase     :   Union[str, Any]    				=      torch.manual_seed(0  )
											lowerCamelCase     :   Optional[int]    				=      ddpm(generator=UpperCamelCase__  ,						output_type="numpy"  ).images

											lowerCamelCase     :   List[str]    				=      image[0, -3:, -3:, -1]

											assert image.shape == (1, 32, 32, 3)
											lowerCamelCase     :   Union[str, Any]    				=      np.array([0.4200, 0.3588, 0.1939, 0.3847, 0.3382, 0.2647, 0.4155, 0.3582, 0.3385]  )
											assert np.abs(image_slice.flatten() - expected_slice  ).max() < 1e-2












import os
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
       import torch

       from transformers import GPTaLMHeadModel




@require_torch
@require_sentencepiece
@require_tokenizers
class 	UpperCamelCase__			(unittest.TestCase  ):

    '''simple docstring'''



    @slow
    @unittest.skip("Model is not available."  )
    def 					_lowercase  (					self  )  ->   List[Any]:
           lowerCamelCase     :   List[Any]    				=      "nvidia/megatron-gpt2-345m/"
           if "MYDIR" in os.environ:
                  lowerCamelCase     :   Union[str, Any]    				=      os.path.join(os.environ["MYDIR"]  ,						UpperCamelCase__  )
           lowerCamelCase     :   Any    				=      GPTaLMHeadModel.from_pretrained(UpperCamelCase__  )
           model.to(UpperCamelCase__  )
           model.half()

           lowerCamelCase     :   Dict    				=      torch.tensor(
               [[101, 7110, 1005, 1056, 2023, 1_1333, 1_7413, 1029, 102]]  ,						device=UpperCamelCase__  ,						dtype=torch.long  ,						)

           with torch.no_grad():
                  lowerCamelCase     :   Any    				=      model(UpperCamelCase__  ).logits

           lowerCamelCase     :   Union[str, Any]    				=      torch.Size((1, 9, 5_0257)  )
           self.assertEqual(output.shape  ,						UpperCamelCase__  )

           lowerCamelCase     :   List[str]    				=      torch.tensor(
               [
                   4.9414,
                   -0.2920,
                   -1.2148,
                   -4.0273,
                   -0.5161,
                   -5.2109,
                   -1.2412,
                   -1.8301,
                   -1.7734,
                   -4.7148,
                   -0.2317,
                   -1.0811,
                   -2.1777,
                   0.4141,
                   -3.7969,
                   -4.0586,
                   -2.5332,
                   -3.3809,
                   4.3867,
               ]  ,						device=UpperCamelCase__  ,						dtype=torch.half  ,						)

           for i in range(19  ):
                  lowerCamelCase				,   lowerCamelCase     :   Optional[int]    				=      8 * i // 17, 2792 * i  # along the diagonal
                  lowerCamelCase				,   lowerCamelCase     :   Tuple    				=      output[0, r, c], expected_diag[i]
                  lowerCamelCase     :   List[str]    				=      F'''row={r} col={c} computed={computed} expected={expected}'''
                  self.assertAlmostEqual(UpperCamelCase__  ,						UpperCamelCase__  ,						delta=1e-4  ,						msg=UpperCamelCase__  )






