





# This test is meant to be run in on an instance with TPUs like this:
#
#   python examples/pytorch/xla_spawn.py --num_cores=8 tests/test_trainer_tpu.py
#
# Replace 8 with the number of TPU cores you have.
#

import sys
from typing import Dict

from transformers import EvalPrediction, HfArgumentParser, TrainingArguments, is_torch_available
from transformers.utils import logging


A_        =  logging.get_logger(__name__)


if is_torch_available():
						import torch
						from torch import nn
						from torch.utils.data import Dataset

						from transformers import Trainer

						class SCREAMING_SNAKE_CASE_			(       lowercase_ ):





											"""simple docstring"""


											def __init__(			self						,	_lowerCAmelCase = 101						):
																lowerCamelCase__      =							length





											def __len__(			self						):
																return self.length






											def __getitem__(			self						,	_lowerCAmelCase						):
																return i

						class SCREAMING_SNAKE_CASE_			:





											"""simple docstring"""


											def __call__(			self						,	_lowerCAmelCase						):
																return {"input_ids": torch.tensor(_lowerCAmelCase						), "labels": torch.tensor(_lowerCAmelCase						)}

						class SCREAMING_SNAKE_CASE_			(       nn.Module ):





											"""simple docstring"""


											def __init__(			self						):
																super().__init__()
																# Add some (unused) params otherwise DDP will complain.
																lowerCamelCase__      =							nn.Linear(120						,	80						)






											def        __magic_name__			(			self						,	_lowerCAmelCase						,	_lowerCAmelCase=None						):
																if labels is not None:
																					return torch.tensor(0.0						,	device=input_ids.device						), input_ids
																else:
																					return input_ids





def  __UpperCamelCase   (     )			->str:
					lowerCamelCase__      =							HfArgumentParser((TrainingArguments,))
					sys.argv += ["--output_dir", "./examples"]
					lowerCamelCase__      =							parser.parse_args_into_dataclasses()[0]

					logger.warning(
					    f"Process rank: {training_args.local_rank}, device: {training_args.device}, "
					    f"tpu_num_cores: {training_args.tpu_num_cores}",  )

					# Essentially, what we want to verify in the distributed case is
					# that we get all samples back, in the right order.
					# (this is crucial for prediction for instance)
					for dataset_length in [1001, 256, 15]:
										lowerCamelCase__      =							DummyDataset(a)

										def compute_metrics(a) -> Dict:
															lowerCamelCase__      =							list(range(len(a)))
															lowerCamelCase__      =							p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential
															return {"success": success}

										lowerCamelCase__      =							Trainer(
										    model=DummyModel(),  args=a,  data_collator=DummyDataCollator(),  eval_dataset=a,  compute_metrics=a,  )
										lowerCamelCase__      =							trainer.evaluate()
										logger.info(a)
										if metrics["eval_success"] is not True:
															logger.error(a)
															exit(1)

										lowerCamelCase__      =							trainer.predict(a)
										logger.info(p.metrics)
										if p.metrics["test_success"] is not True:
															logger.error(p.metrics)
															exit(1)

										lowerCamelCase__      =							2

										lowerCamelCase__      =							trainer.evaluate()
										logger.info(a)
										if metrics["eval_success"] is not True:
															logger.error(a)
															exit(1)

										lowerCamelCase__      =							trainer.predict(a)
										logger.info(p.metrics)
										if p.metrics["test_success"] is not True:
															logger.error(p.metrics)
															exit(1)

										lowerCamelCase__      =							None

					logger.info("🔥 All distributed tests successful")





def  __UpperCamelCase   (     a)			->Dict:
					# For xla_spawn (TPUs)
					main()


if __name__ == "__main__":
						main()








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 SCREAMING_SNAKE_CASE_			(       unittest.TestCase ):





					"""simple docstring"""


					@property
					def        __magic_name__			(			self						):
										torch.manual_seed(0						)
										lowerCamelCase__      =							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        __magic_name__			(			self						):
										lowerCamelCase__      =							"cpu"
										lowerCamelCase__      =							self.dummy_uncond_unet
										lowerCamelCase__      =							DDPMScheduler()

										lowerCamelCase__      =							DDPMPipeline(unet=_lowerCAmelCase						,	scheduler=_lowerCAmelCase						)
										ddpm.to(_lowerCAmelCase						)
										ddpm.set_progress_bar_config(disable=_lowerCAmelCase						)

										lowerCamelCase__      =							torch.Generator(device=_lowerCAmelCase						).manual_seed(0						)
										lowerCamelCase__      =							ddpm(generator=_lowerCAmelCase						,	num_inference_steps=2						,	output_type="numpy"						).images

										lowerCamelCase__      =							torch.Generator(device=_lowerCAmelCase						).manual_seed(0						)
										lowerCamelCase__      =							ddpm(generator=_lowerCAmelCase						,	num_inference_steps=2						,	output_type="numpy"						,	return_dict=_lowerCAmelCase						)[0]

										lowerCamelCase__      =							image[0, -3:, -3:, -1]
										lowerCamelCase__      =							image_from_tuple[0, -3:, -3:, -1]

										assert image.shape == (1, 32, 32, 3)
										lowerCamelCase__      =							np.array(
										    [9.9_56E-01, 5.7_85E-01, 4.6_75E-01, 9.9_30E-01, 0.0, 1.000, 1.1_99E-03, 2.6_48E-04, 5.1_01E-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        __magic_name__			(			self						):
										lowerCamelCase__      =							self.dummy_uncond_unet
										lowerCamelCase__      =							DDPMScheduler(prediction_type="sample"						)

										lowerCamelCase__      =							DDPMPipeline(unet=_lowerCAmelCase						,	scheduler=_lowerCAmelCase						)
										ddpm.to(_lowerCAmelCase						)
										ddpm.set_progress_bar_config(disable=_lowerCAmelCase						)

										lowerCamelCase__      =							torch.manual_seed(0						)
										lowerCamelCase__      =							ddpm(generator=_lowerCAmelCase						,	num_inference_steps=2						,	output_type="numpy"						).images

										lowerCamelCase__      =							torch.manual_seed(0						)
										lowerCamelCase__      =							ddpm(generator=_lowerCAmelCase						,	num_inference_steps=2						,	output_type="numpy"						)[0]

										lowerCamelCase__      =							image[0, -3:, -3:, -1]
										lowerCamelCase__      =							image_eps[0, -3:, -3:, -1]

										assert image.shape == (1, 32, 32, 3)
										lowerCamelCase__      =							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 SCREAMING_SNAKE_CASE_			(       unittest.TestCase ):





					"""simple docstring"""


					def        __magic_name__			(			self						):
										lowerCamelCase__      =							"google/ddpm-cifar10-32"

										lowerCamelCase__      =							UNetaDModel.from_pretrained(_lowerCAmelCase						)
										lowerCamelCase__      =							DDPMScheduler.from_pretrained(_lowerCAmelCase						)

										lowerCamelCase__      =							DDPMPipeline(unet=_lowerCAmelCase						,	scheduler=_lowerCAmelCase						)
										ddpm.to(_lowerCAmelCase						)
										ddpm.set_progress_bar_config(disable=_lowerCAmelCase						)

										lowerCamelCase__      =							torch.manual_seed(0						)
										lowerCamelCase__      =							ddpm(generator=_lowerCAmelCase						,	output_type="numpy"						).images

										lowerCamelCase__      =							image[0, -3:, -3:, -1]

										assert image.shape == (1, 32, 32, 3)
										lowerCamelCase__      =							np.array([0.4_200, 0.3_588, 0.1_939, 0.3_847, 0.3_382, 0.2_647, 0.4_155, 0.3_582, 0.3_385]						)
										assert np.abs(image_slice.flatten() - expected_slice						).max() < 1E-2

