
# 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


SCREAMING_SNAKE_CASE__     :    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      lowerCAmelCase__  (			__lowercase    ):






							def __init__(	self	:							Tuple							,    SCREAMING_SNAKE_CASE__	:							int = 1_01						)  ->							Dict:
											__lowerCamelCase					      =			length






							def __len__(	self	:							List[Any]						)  ->							Union[str, Any]:
											return self.length







							def __getitem__(	self	:							Tuple							,    SCREAMING_SNAKE_CASE__	:							List[Any]						)  ->							int:
											return i





					class      lowerCAmelCase__  :






							def __call__(	self	:							Union[str, Any]							,    SCREAMING_SNAKE_CASE__	:							str						)  ->							Optional[int]:
											return {"input_ids": torch.tensor(SCREAMING_SNAKE_CASE__						), "labels": torch.tensor(SCREAMING_SNAKE_CASE__						)}





					class      lowerCAmelCase__  (			nn.Module    ):






							def __init__(	self	:							Any						)  ->							Union[str, Any]:
											super().__init__()
											# Add some (unused) params otherwise DDP will complain.
											__lowerCamelCase					      =			nn.Linear(1_20							,    80						)







							def 				__A (	self	:							Dict							,    SCREAMING_SNAKE_CASE__	:							Any							,    SCREAMING_SNAKE_CASE__	:							int=None						)  ->							Dict:
											if labels is not None:
															return torch.tensor(0.0							,    device=input_ids.device						), input_ids
											else:
															return input_ids




def 				__magic_name__				(     )    ->     Optional[Any]:
				__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(__lowerCAmelCase							)

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

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

								__lowerCamelCase					      =			trainer.predict(__lowerCAmelCase							)
								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(__lowerCAmelCase							)
								if metrics["eval_success"] is not True:
												logger.error(__lowerCAmelCase							)
												exit(1							)

								__lowerCamelCase					      =			trainer.predict(__lowerCAmelCase							)
								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 				__magic_name__				(     __lowerCAmelCase			:		List[Any]							)    ->     Tuple:
				# For xla_spawn (TPUs)
				main()


if __name__ == "__main__":
					main()





from ..utils import deprecate
from .attention_processor import (  # noqa: F401
    Attention,
    AttentionProcessor,
    AttnAddedKVProcessor,
    AttnProcessor2_0,
    LoRAAttnProcessor,
    LoRALinearLayer,
    LoRAXFormersAttnProcessor,
    SlicedAttnAddedKVProcessor,
    SlicedAttnProcessor,
    XFormersAttnProcessor,
)
from .attention_processor import AttnProcessor as AttnProcessorRename  # noqa: F401


deprecate(
    "cross_attention",
    "0.20.0",
    "Importing from cross_attention is deprecated. Please import from diffusers.models.attention_processor instead.",
    standard_warn=False,
)


SCREAMING_SNAKE_CASE__     :    Optional[int]      							=					AttentionProcessor

class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							Tuple							,    *SCREAMING_SNAKE_CASE__	:							Dict							,    **SCREAMING_SNAKE_CASE__	:							int						)  ->							List[Any]:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							str							,    *SCREAMING_SNAKE_CASE__	:							Optional[Any]							,    **SCREAMING_SNAKE_CASE__	:							Optional[int]						)  ->							Optional[Any]:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							str							,    *SCREAMING_SNAKE_CASE__	:							List[Any]							,    **SCREAMING_SNAKE_CASE__	:							List[str]						)  ->							List[Any]:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							Optional[int]							,    *SCREAMING_SNAKE_CASE__	:							Optional[int]							,    **SCREAMING_SNAKE_CASE__	:							Tuple						)  ->							Optional[Any]:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							Optional[Any]							,    *SCREAMING_SNAKE_CASE__	:							int							,    **SCREAMING_SNAKE_CASE__	:							Optional[Any]						)  ->							List[str]:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							Tuple							,    *SCREAMING_SNAKE_CASE__	:							Tuple							,    **SCREAMING_SNAKE_CASE__	:							Any						)  ->							Union[str, Any]:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							str							,    *SCREAMING_SNAKE_CASE__	:							Union[str, Any]							,    **SCREAMING_SNAKE_CASE__	:							Tuple						)  ->							int:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)





class      lowerCAmelCase__  (			__lowercase    ):






		def __init__(	self	:							Optional[Any]							,    *SCREAMING_SNAKE_CASE__	:							Any							,    **SCREAMING_SNAKE_CASE__	:							str						)  ->							Dict:
						__lowerCamelCase					      =			f'''{self.__class__.__name__} is deprecated and will be removed in `0.20.0`. Please use `from diffusers.models.attention_processor import {''.join(self.__class__.__name__.split('Cross'						)						)} instead.'''
						deprecate('''cross_attention'''							,    '''0.20.0'''							,    SCREAMING_SNAKE_CASE__							,    standard_warn=SCREAMING_SNAKE_CASE__						)
						super().__init__(*SCREAMING_SNAKE_CASE__							,    **SCREAMING_SNAKE_CASE__						)



