code: 
  
  
  
  
  
  '''simple docstring'''
  
  
  # 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
  
  
  _lowerCamelCase				:	Optional[int]				    =       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 							__UpperCAmelCase  (       A__						):
  
  
  
  
  
  
  
  						'''simple docstring'''
  
  
  
  
  
  
  
  						def __init__(self						:      Optional[Any]      ,      _lowerCAmelCase						:      int = 101      ):
  							A							  =      length
  
  
  
  
  
  
  
  						def __len__(self						:      Union[str, Any]      ):
  							return self.length
  
  
  
  
  
  
  
  						def __getitem__(self						:      Tuple      ,      _lowerCAmelCase						:      str      ):
  							return i
  
  
  
  
  
  
  				class 							__UpperCAmelCase  :
  
  
  
  
  
  
  
  						'''simple docstring'''
  
  
  
  
  
  
  
  						def __call__(self						:      List[Any]      ,      _lowerCAmelCase						:      Tuple      ):
  							return {"input_ids": torch.tensor(_lowerCAmelCase      ), "labels": torch.tensor(_lowerCAmelCase      )}
  
  
  
  
  
  
  				class 							__UpperCAmelCase  (       nn.Module						):
  
  
  
  
  
  
  
  						'''simple docstring'''
  
  
  
  
  
  
  
  						def __init__(self						:      Optional[int]      ):
  							super().__init__()
  							# Add some (unused) params otherwise DDP will complain.
  							A							  =      nn.Linear(120      ,      80      )
  
  
  
  
  
  
  
  						def        A     (self						:      int      ,      _lowerCAmelCase						:      Tuple      ,      _lowerCAmelCase						:      Union[str, Any]=None      ):
  							if labels is not None:
  								return torch.tensor(0.0      ,      device=input_ids.device      ), input_ids
  							else:
  								return input_ids
  
  
  
  
  def    __a (     )      ->int:
  
  
  
  	"""simple docstring"""
  
  
  
  
  
  
  
  	A							  =      HfArgumentParser((TrainingArguments,)    )
  	sys.argv += ["--output_dir", "./examples"]
  	A							  =      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]:
  		A							  =      DummyDataset(UpperCAmelCase    )
  
  		def compute_metrics(UpperCAmelCase    ) -> Dict:
  			A							  =      list(range(len(UpperCAmelCase    )    )    )
  			A							  =      p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential
  			return {"success": success}
  
  		A							  =      Trainer(
  		    model=DummyModel()       ,			args=UpperCAmelCase       ,			data_collator=DummyDataCollator()       ,			eval_dataset=UpperCAmelCase       ,			compute_metrics=UpperCAmelCase       ,			)
  		A							  =      trainer.evaluate()
  		logger.info(UpperCAmelCase    )
  		if metrics["eval_success"] is not True:
  			logger.error(UpperCAmelCase    )
  			exit(1    )
  
  		A							  =      trainer.predict(UpperCAmelCase    )
  		logger.info(p.metrics    )
  		if p.metrics["test_success"] is not True:
  			logger.error(p.metrics    )
  			exit(1    )
  
  		A							  =      2
  
  		A							  =      trainer.evaluate()
  		logger.info(UpperCAmelCase    )
  		if metrics["eval_success"] is not True:
  			logger.error(UpperCAmelCase    )
  			exit(1    )
  
  		A							  =      trainer.predict(UpperCAmelCase    )
  		logger.info(p.metrics    )
  		if p.metrics["test_success"] is not True:
  			logger.error(p.metrics    )
  			exit(1    )
  
  		A							  =      None
  
  	logger.info("""🔥 All distributed tests successful"""    )
  
  
  
  
  def    __a (     UpperCAmelCase    )      ->Optional[Any]:
  
  
  
  	"""simple docstring"""
  
  
  
  
  
  
  
  	main()
  
  
  if __name__ == "__main__":
  				main()
  
  
style_context: 
  
  
  
  
  
  '''simple docstring'''
  
  
  import tempfile
  import unittest
  
  import numpy as np
  import torch
  
  from diffusers import DiffusionPipeline
  from diffusers.models.attention_processor import Attention, AttnAddedKVProcessor
  
  
  
  class 							__UpperCAmelCase  (       unittest.TestCase						):
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[int]      ,      _lowerCAmelCase						:      bool = False      ):
  			A							  =      10
  
  			if only_cross_attention:
  				A							  =      12
  			else:
  				# when only cross attention is not set, the cross attention dim must be the same as the query dim
  				A							  =      query_dim
  
  			return {
  			    "query_dim": query_dim,
  			    "cross_attention_dim": cross_attention_dim,
  			    "heads": 2,
  			    "dim_head": 4,
  			    "added_kv_proj_dim": 6,
  			    "norm_num_groups": 1,
  			    "only_cross_attention": only_cross_attention,
  			    "processor": AttnAddedKVProcessor(),
  			}
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[int]      ,      _lowerCAmelCase						:      Dict      ,      _lowerCAmelCase						:      Any      ):
  			A							  =      2
  
  			A							  =      torch.rand(_lowerCAmelCase      ,      _lowerCAmelCase      ,      3      ,      2      )
  			A							  =      torch.rand(_lowerCAmelCase      ,      4      ,      _lowerCAmelCase      )
  			A							  =      None
  
  			return {
  			    "hidden_states": hidden_states,
  			    "encoder_hidden_states": encoder_hidden_states,
  			    "attention_mask": attention_mask,
  			}
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[Any]      ):
  			# self and cross attention
  
  			torch.manual_seed(0      )
  
  			A							  =      self.get_constructor_arguments(only_cross_attention=_lowerCAmelCase      )
  			A							  =      Attention(**_lowerCAmelCase      )
  
  			self.assertTrue(attn.to_k is not None      )
  			self.assertTrue(attn.to_v is not None      )
  
  			A							  =      self.get_forward_arguments(
  			    query_dim=constructor_args["""query_dim"""]      ,      added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )
  
  			A							  =      attn(**_lowerCAmelCase      )
  
  			# only self attention
  
  			torch.manual_seed(0      )
  
  			A							  =      self.get_constructor_arguments(only_cross_attention=_lowerCAmelCase      )
  			A							  =      Attention(**_lowerCAmelCase      )
  
  			self.assertTrue(attn.to_k is None      )
  			self.assertTrue(attn.to_v is None      )
  
  			A							  =      self.get_forward_arguments(
  			    query_dim=constructor_args["""query_dim"""]      ,      added_kv_proj_dim=constructor_args["""added_kv_proj_dim"""]      )
  
  			A							  =      attn(**_lowerCAmelCase      )
  
  			self.assertTrue((only_cross_attn_out != self_and_cross_attn_out).all()      )
  
  
  
  
  
  
  class 							__UpperCAmelCase  (       unittest.TestCase						):
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		def        A     (self						:      Any      ):
  			A							  =      DiffusionPipeline.from_pretrained("""hf-internal-testing/tiny-stable-diffusion-pipe"""      ,      safety_checker=_lowerCAmelCase      )
  
  			A							  =      pipe(
  			    """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""      ).manual_seed(0      )      ,      output_type="""np"""      ,      ).images
  
  			# the initial conversion succeeds
  			A							  =      DiffusionPipeline.from_pretrained(
  			    """hf-internal-testing/tiny-stable-diffusion-pipe"""      ,      device_map="""sequential"""      ,      safety_checker=_lowerCAmelCase      )
  
  			A							  =      pipe(
  			    """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""      ).manual_seed(0      )      ,      output_type="""np"""      ,      ).images
  
  			with tempfile.TemporaryDirectory() as tmpdir:
  				# save the converted model
  				pipe.save_pretrained(_lowerCAmelCase      )
  
  				# can also load the converted weights
  				A							  =      DiffusionPipeline.from_pretrained(_lowerCAmelCase      ,      device_map="""sequential"""      ,      safety_checker=_lowerCAmelCase      )
  
  			A							  =      pipe(
  			    """foo"""      ,      num_inference_steps=2      ,      generator=torch.Generator("""cpu"""      ).manual_seed(0      )      ,      output_type="""np"""      ,      ).images
  
  			self.assertTrue(np.allclose(_lowerCAmelCase      ,      _lowerCAmelCase      ,      atol=1e-5      )      )
  			self.assertTrue(np.allclose(_lowerCAmelCase      ,      _lowerCAmelCase      ,      atol=1e-5      )      )
  
  