
import logging
import os
import sys
import threading
from logging import (
    CRITICAL,  # NOQA
    DEBUG,  # NOQA
    ERROR,  # NOQA
    FATAL,  # NOQA
    INFO,  # NOQA
    NOTSET,  # NOQA
    WARN,  # NOQA
    WARNING,  # NOQA
)
from typing import Optional

from tqdm import auto as tqdm_lib


_snake_case			:       Optional[int]      =						threading.Lock()
_snake_case			:       Optional[logging.Handler]      =						None

_snake_case			:       Tuple      =						{
    "debug": logging.DEBUG,
    "info": logging.INFO,
    "warning": logging.WARNING,
    "error": logging.ERROR,
    "critical": logging.CRITICAL,
}

_snake_case			:       Optional[Any]      =						logging.WARNING

_snake_case			:       str      =						True

def    __snake_case       (  ):







				'''simple docstring'''


				lowercase						    =			os.getenv("DIFFUSERS_VERBOSITY"		,						__magic_name__	)
				if env_level_str:
								if env_level_str in log_levels:
												return log_levels[env_level_str]
								else:
												logging.getLogger().warning(
												    F'''Unknown option DIFFUSERS_VERBOSITY={env_level_str}, '''
												    F'''has to be one of: { ", ".join(log_levels.keys()	) }'''	)
				return _default_log_level

def    __snake_case       (  ):







				'''simple docstring'''


				return __name__.split("."	)[0]

def    __snake_case       (  ):







				'''simple docstring'''


				return logging.getLogger(_get_library_name()	)

def    __snake_case       (  ):







				'''simple docstring'''


				global _default_handler

				with _lock:
								if _default_handler:
												# This library has already configured the library root logger.
												return
								lowercase						    =			logging.StreamHandler()  # Set sys.stderr as stream.
								lowercase						    =			sys.stderr.flush

								# Apply our default configuration to the library root logger.
								lowercase						    =			_get_library_root_logger()
								library_root_logger.addHandler(_default_handler	)
								library_root_logger.setLevel(_get_default_logging_level()	)
								lowercase						    =			False

def    __snake_case       (  ):







				'''simple docstring'''


				global _default_handler

				with _lock:
								if not _default_handler:
												return

								lowercase						    =			_get_library_root_logger()
								library_root_logger.removeHandler(_default_handler	)
								library_root_logger.setLevel(logging.NOTSET	)
								lowercase						    =			None

def    __snake_case       (  ):







				'''simple docstring'''


				return log_levels

def    __snake_case       (  __magic_name__ = None	):







				'''simple docstring'''


				if name is None:
								lowercase						    =			_get_library_name()

				_configure_library_root_logger()
				return logging.getLogger(__magic_name__	)

def    __snake_case       (  ):







				'''simple docstring'''


				_configure_library_root_logger()
				return _get_library_root_logger().getEffectiveLevel()

def    __snake_case       (  __magic_name__	):







				'''simple docstring'''


				_configure_library_root_logger()
				_get_library_root_logger().setLevel(__magic_name__	)

def    __snake_case       (  ):







				'''simple docstring'''


				return set_verbosity(__magic_name__	)

def    __snake_case       (  ):







				'''simple docstring'''


				return set_verbosity(__magic_name__	)

def    __snake_case       (  ):







				'''simple docstring'''


				return set_verbosity(__magic_name__	)

def    __snake_case       (  ):







				'''simple docstring'''


				return set_verbosity(__magic_name__	)

def    __snake_case       (  ):







				'''simple docstring'''


				_configure_library_root_logger()

				assert _default_handler is not None
				_get_library_root_logger().removeHandler(_default_handler	)

def    __snake_case       (  ):







				'''simple docstring'''


				_configure_library_root_logger()

				assert _default_handler is not None
				_get_library_root_logger().addHandler(_default_handler	)

def    __snake_case       (  __magic_name__	):







				'''simple docstring'''


				_configure_library_root_logger()

				assert handler is not None
				_get_library_root_logger().addHandler(__magic_name__	)

def    __snake_case       (  __magic_name__	):







				'''simple docstring'''


				_configure_library_root_logger()

				assert handler is not None and handler not in _get_library_root_logger().handlers
				_get_library_root_logger().removeHandler(__magic_name__	)

def    __snake_case       (  ):







				'''simple docstring'''


				_configure_library_root_logger()
				lowercase						    =			False

def    __snake_case       (  ):







				'''simple docstring'''


				_configure_library_root_logger()
				lowercase						    =			True

def    __snake_case       (  ):







				'''simple docstring'''


				lowercase						    =			_get_library_root_logger().handlers

				for handler in handlers:
								lowercase						    =			logging.Formatter("[%(levelname)s|%(filename)s:%(lineno)s] %(asctime)s >> %(message)s"	)
								handler.setFormatter(__magic_name__	)

def    __snake_case       (  ):







				'''simple docstring'''


				lowercase						    =			_get_library_root_logger().handlers

				for handler in handlers:
								handler.setFormatter(__magic_name__	)

def    __snake_case       (  self		,						*__magic_name__		,						**__magic_name__	):







				'''simple docstring'''


				lowercase						    =			os.getenv("DIFFUSERS_NO_ADVISORY_WARNINGS"		,						__magic_name__	)
				if no_advisory_warnings:
								return
				self.warning(*__magic_name__		,						**__magic_name__	)


_snake_case			:       Tuple      =						warning_advice





class      UpperCamelCase_			:







						'''simple docstring'''



						def __init__( self       :int		,						*lowerCAmelCase__       :Optional[Any]		,						**lowerCAmelCase__       :int    )     ->Any:  # pylint: disable=unused-argument
										lowercase						    =			args[0] if args else None





						def __iter__( self       :List[Any]    )     ->Tuple:
										return iter(self._iterator    )





						def __getattr__( self       :List[str]		,						lowerCAmelCase__       :Optional[Any]    )     ->List[Any]:

										def empty_fn(*lowerCAmelCase__       :Optional[int]		,						**lowerCAmelCase__       :Union[str, Any]    ):  # pylint: disable=unused-argument
														return

										return empty_fn





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





						def __exit__( self       :int		,						lowerCAmelCase__       :Any		,						lowerCAmelCase__       :Dict		,						lowerCAmelCase__       :str    )     ->List[str]:
										return





class      UpperCamelCase_			:







						'''simple docstring'''



						def __call__( self       :Optional[int]		,						*lowerCAmelCase__       :List[Any]		,						**lowerCAmelCase__       :int    )     ->Optional[int]:
										if _tqdm_active:
														return tqdm_lib.tqdm(*lowerCAmelCase__		,						**lowerCAmelCase__    )
										else:
														return EmptyTqdm(*lowerCAmelCase__		,						**lowerCAmelCase__    )





						def 					SCREAMING_SNAKE_CASE( self       :Optional[int]		,						*lowerCAmelCase__       :str		,						**lowerCAmelCase__       :List[str]    )     ->List[Any]:
										lowercase						    =			None
										if _tqdm_active:
														return tqdm_lib.tqdm.set_lock(*lowerCAmelCase__		,						**lowerCAmelCase__    )





						def 					SCREAMING_SNAKE_CASE( self       :Union[str, Any]    )     ->Optional[int]:
										if _tqdm_active:
														return tqdm_lib.tqdm.get_lock()




_snake_case			:       List[str]      =						_tqdm_cls()

def    __snake_case       (  ):







				'''simple docstring'''


				global _tqdm_active
				return bool(_tqdm_active	)

def    __snake_case       (  ):







				'''simple docstring'''


				global _tqdm_active
				lowercase						    =			True




def    __snake_case       (  ):







				'''simple docstring'''


				global _tqdm_active
				lowercase						    =			False


# 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


_snake_case			:       Optional[Any]      =						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[int]		,						lowerCAmelCase__       :int = 101    )     ->Any:
																lowercase						    =			length





												def __len__( self       :Tuple    )     ->Any:
																return self.length





												def __getitem__( self       :Any		,						lowerCAmelCase__       :Tuple    )     ->int:
																return i





						class      UpperCamelCase_			:







												'''simple docstring'''



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





						class      UpperCamelCase_			(			nn.Module		):







												'''simple docstring'''



												def __init__( self       :Any    )     ->str:
																super().__init__()
																# Add some (unused) params otherwise DDP will complain.
																lowercase						    =			nn.Linear(120		,						80    )





												def 					SCREAMING_SNAKE_CASE( self       :int		,						lowerCAmelCase__       :Optional[int]		,						lowerCAmelCase__       :Union[str, Any]=None    )     ->List[Any]:
																if labels is not None:
																				return torch.tensor(0.0		,						device=input_ids.device    ), input_ids
																else:
																				return input_ids

def    __snake_case       (  ):







				'''simple docstring'''


				lowercase						    =			HfArgumentParser((TrainingArguments,)	)
				sys.argv += ["--output_dir", "./examples"]
				lowercase						    =			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]:
								lowercase						    =			DummyDataset(__magic_name__	)

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

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

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

								lowercase						    =			2

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

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

								lowercase						    =			None

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

def    __snake_case       (  __magic_name__	):







				'''simple docstring'''


				main()


if __name__ == "__main__":
						main()
