






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


__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 a_     (     UpperCamelCase_				):







         def __init__(self       , __a = 1_0_1)      ->   Optional[Any]:
            """simple docstring"""






            __snake_case					:   Tuple  				=			length







         def __len__(self)      ->   List[Any]:
            """simple docstring"""






            return self.length







         def __getitem__(self       , __a)      ->   int:
            """simple docstring"""






            return i




      class a_     :







         def __call__(self       , __a)      ->   int:
            """simple docstring"""






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




      class a_     (     nn.Module				):







         def __init__(self)      ->   Tuple:
            """simple docstring"""






            super().__init__()
            # Add some (unused) params otherwise DDP will complain.
            __snake_case					:   Optional[int]  				=			nn.Linear(1_2_0       , 8_0)







         def      SCREAMING_SNAKE_CASE__       (self       , __a       , __a=None)      ->   Optional[Any]:
            """simple docstring"""






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




def 							_SCREAMING_SNAKE_CASE	(					)     ->			int:







   """simple docstring"""
   __snake_case					:   Tuple  				=			HfArgumentParser((TrainingArguments,)	)
   sys.argv += ["--output_dir", "./examples"]
   __snake_case					:   Optional[Any]  				=			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 [10_01, 2_56, 15]:
      __snake_case					:   List[str]  				=			DummyDataset(A	)

      def compute_metrics(A	:							EvalPrediction	) -> Dict:
         __snake_case					:   List[str]  				=			list(range(len(A	)	)	)
         __snake_case					:   int  				=			p.predictions.tolist() == sequential and p.label_ids.tolist() == sequential
         return {"success": success}

      __snake_case					:   Union[str, Any]  				=			Trainer(
          model=DummyModel()	,		args=A	,		data_collator=DummyDataCollator()	,		eval_dataset=A	,		compute_metrics=A	,		)
      __snake_case					:   Optional[Any]  				=			trainer.evaluate()
      logger.info(A	)
      if metrics["eval_success"] is not True:
         logger.error(A	)
         exit(1	)

      __snake_case					:   Union[str, Any]  				=			trainer.predict(A	)
      logger.info(p.metrics	)
      if p.metrics["test_success"] is not True:
         logger.error(p.metrics	)
         exit(1	)

      __snake_case					:   Dict  				=			2

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

      __snake_case					:   Union[str, Any]  				=			trainer.predict(A	)
      logger.info(p.metrics	)
      if p.metrics["test_success"] is not True:
         logger.error(p.metrics	)
         exit(1	)

      __snake_case					:   Tuple  				=			None

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




def 							_SCREAMING_SNAKE_CASE	(					A	:							Union[str, Any]	)     ->			Optional[Any]:







   """simple docstring"""
   # For xla_spawn (TPUs)
   main()


if __name__ == "__main__":
      main()







'''simple docstring'''






import bsa
import requests




def 							_SCREAMING_SNAKE_CASE	(					A	:							bsa.element.ResultSet	)     ->			dict[str, str]:







   """simple docstring"""
   return {
       "name": soup.ha.a.text,
       "genre": soup.find('span'	,		class_='genre'	).text.strip(),
       "rating": soup.strong.text,
       "page_link": F"""https://www.imdb.com{soup.a.get("href"	)}""",
   }




def 							_SCREAMING_SNAKE_CASE	(					A	:							int = 5	)     ->			tuple:







   """simple docstring"""
   __snake_case					:   Optional[int]  				=			int(float(A	)	)
   if num_movies < 1:
      return ()
   __snake_case					:   Union[str, Any]  				=			(
       'https://www.imdb.com/search/title?title_type='
       F"""feature&sort=num_votes,desc&count={num_movies}"""
   )
   __snake_case					:   List[Any]  				=			bsa.BeautifulSoup(requests.get(A	).content	,		'html.parser'	)
   return tuple(
       get_movie_data_from_soup(A	)
       for movie in source.find_all('div'	,		class_='lister-item mode-advanced'	)	)


if __name__ == "__main__":
      import json

      __A     						= int(input('''How many movies would you like to see? '''))
      print(
          ''', '''.join(
              json.dumps(movie, indent=4) for movie in get_imdb_top_movies(num_movies)
          )
      )