




'''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


SCREAMING_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       lowercase_		(SCREAMING_SNAKE_CASE__   ):



												def __init__( self  :      Optional[Any]				,       snake_case__  :      int = 1_01     ):

														"""simple docstring"""

														SCREAMING_SNAKE_CASE_							=       length



												def __len__( self  :      int     ):

														"""simple docstring"""

														return self.length






												def __getitem__( self  :      Dict				,       snake_case__  :      int     ):

														"""simple docstring"""

														return i



						class       lowercase_		:



												def __call__( self  :      List[Any]				,       snake_case__  :      Any     ):

														"""simple docstring"""

														return {"input_ids": torch.tensor(snake_case__     ), "labels": torch.tensor(snake_case__     )}



						class       lowercase_		(nn.Module   ):



												def __init__( self  :      Any     ):

														"""simple docstring"""

														super().__init__()
														# Add some (unused) params otherwise DDP will complain.
														SCREAMING_SNAKE_CASE_							=       nn.Linear(1_20				,       80     )






												def   __a				( self  :      Dict				,       snake_case__  :      Union[str, Any]				,       snake_case__  :      List[str]=None     ):

														"""simple docstring"""

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


def 				_a       (				)->  Optional[Any]:
		SCREAMING_SNAKE_CASE_							=       HfArgumentParser((TrainingArguments,)   )
		sys.argv += ["--output_dir", "./examples"]
		SCREAMING_SNAKE_CASE_							=       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]:
				SCREAMING_SNAKE_CASE_							=       DummyDataset(lowerCAmelCase   )

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

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

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

				SCREAMING_SNAKE_CASE_							=       2

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

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

				SCREAMING_SNAKE_CASE_							=       None

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


def 				_a       (				lowerCAmelCase   )->  Any:
		# For xla_spawn (TPUs)
		main()


if __name__ == "__main__":
						main()





from __future__ import annotations


def 				_a       (				lowerCAmelCase						,	lowerCAmelCase   )->  int:
		SCREAMING_SNAKE_CASE_			,							SCREAMING_SNAKE_CASE_							=       0, len(lowerCAmelCase   ) - 1
		while start_ind <= end_ind:
				if array[start_ind] == search_item:
						return start_ind
				elif array[end_ind] == search_item:
						return end_ind
				else:
						start_ind += 1
						end_ind -= 1
    # returns -1 if search_item is not found in array
		return -1


if __name__ == "__main__":
						print(double_linear_search(list(range(1_0_0)), 4_0))