


'''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__:   List[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 A__	(						__lowerCamelCase				):






        def __init__(			self      :List[Any]					,						SCREAMING_SNAKE_CASE      :int = 1_0_1     )	-> List[Any]:




               '''simple docstring'''
               _a   :			Optional[int]					 =length






        def __len__(			self      :List[str]     )	-> str:




               '''simple docstring'''
               return self.length






        def __getitem__(			self      :str					,						SCREAMING_SNAKE_CASE      :str     )	-> int:




               '''simple docstring'''
               return i







  class A__	:






        def __call__(			self      :List[str]					,						SCREAMING_SNAKE_CASE      :Dict     )	-> Optional[Any]:




               '''simple docstring'''
               return {"input_ids": torch.tensor(_UpperCamelCase     ), "labels": torch.tensor(_UpperCamelCase     )}







  class A__	(						nn.Module				):






        def __init__(			self      :Union[str, Any]     )	-> List[Any]:




               '''simple docstring'''
               super().__init__()
               # Add some (unused) params otherwise DDP will complain.
               _a   :			Tuple					 =nn.Linear(1_2_0					,						8_0     )






        def __UpperCAmelCase	(			self      :List[Any]					,						SCREAMING_SNAKE_CASE      :List[str]					,						SCREAMING_SNAKE_CASE      :Optional[int]=None     )	-> Union[str, 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_ (  )  ->      Dict:
       _a   :			Optional[int]					 =HfArgumentParser((TrainingArguments,)  )
       sys.argv += ["--output_dir", "./examples"]
       _a   :			Union[str, 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 [1001, 256, 15]:
              _a   :			Tuple					 =DummyDataset(__UpperCamelCase  )

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

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

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

              _a   :			Union[str, Any]					 =2

              _a   :			List[str]					 =trainer.evaluate()
              logger.info(__UpperCamelCase  )
              if metrics["eval_success"] is not True:
                     logger.error(__UpperCamelCase  )
                     exit(1  )

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

              _a   :			Dict					 =None

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


def      SCREAMING_SNAKE_CASE_ (  _UpperCAmelCase     :     Dict  )  ->      Tuple:
       # For xla_spawn (TPUs)
       main()


if __name__ == "__main__":
  main()






'''simple docstring'''







from typing import Dict, Optional, Tuple

import torch


def      SCREAMING_SNAKE_CASE_ (  _UpperCAmelCase     :     torch.Tensor  )  ->      torch.Tensor:
       _a   :			Tuple					 =boundaries[1] - boundaries[0]
       _a   :			Dict					 =boundaries + step / 2
       _a   :			List[Any]					 =torch.cat([bin_centers, (bin_centers[-1] + step).unsqueeze(-1  )]   ,dim=0  )
       return bin_centers


def      SCREAMING_SNAKE_CASE_ (  _UpperCAmelCase     :     torch.Tensor   ,_UpperCAmelCase     :     torch.Tensor   ,)  ->      Tuple[torch.Tensor, torch.Tensor]:
       _a   :			Any					 =_calculate_bin_centers(_UpperCAmelCase  )
       return (
           torch.sum(aligned_distance_error_probs * bin_centers   ,dim=-1  ),
           bin_centers[-1],
       )


def      SCREAMING_SNAKE_CASE_ (  _UpperCAmelCase     :     torch.Tensor   ,_UpperCAmelCase     :     int = 31   ,_UpperCAmelCase     :     int = 64   ,**_UpperCAmelCase     :     Dict   ,)  ->      Dict[str, torch.Tensor]:
       _a   :			Dict					 =torch.linspace(0   ,_UpperCAmelCase   ,steps=(no_bins - 1)   ,device=logits.device  )

       _a   :			int					 =torch.nn.functional.softmax(_UpperCAmelCase   ,dim=-1  )
       _a ,   _a   :			List[Any]					 =_calculate_expected_aligned_error(
           alignment_confidence_breaks=_UpperCAmelCase   ,aligned_distance_error_probs=_UpperCAmelCase   ,)

       return {
           "aligned_confidence_probs": aligned_confidence_probs,
           "predicted_aligned_error": predicted_aligned_error,
           "max_predicted_aligned_error": max_predicted_aligned_error,
       }


def      SCREAMING_SNAKE_CASE_ (  _UpperCAmelCase     :     torch.Tensor   ,_UpperCAmelCase     :     Optional[torch.Tensor] = None   ,_UpperCAmelCase     :     int = 31   ,_UpperCAmelCase     :     int = 64   ,_UpperCAmelCase     :     float = 1e-8   ,**_UpperCAmelCase     :     Optional[Any]   ,)  ->      torch.Tensor:
       if residue_weights is None:
              _a   :			Dict					 =logits.new_ones(logits.shape[-2]  )

       _a   :			str					 =torch.linspace(0   ,_UpperCAmelCase   ,steps=(no_bins - 1)   ,device=logits.device  )

       _a   :			Optional[int]					 =_calculate_bin_centers(_UpperCAmelCase  )
       torch.sum(_UpperCAmelCase  )
       _a   :			List[str]					 =logits.shape[-2]
       _a   :			List[Any]					 =max(_UpperCAmelCase   ,19  )

       _a   :			Union[str, Any]					 =1.2_4 * (clipped_n - 15) ** (1.0 / 3) - 1.8

       _a   :			Tuple					 =torch.nn.functional.softmax(_UpperCAmelCase   ,dim=-1  )

       _a   :			Dict					 =1.0 / (1 + (bin_centers**2) / (da**2))
       _a   :			int					 =torch.sum(probs * tm_per_bin   ,dim=-1  )

       _a   :			List[Any]					 =residue_weights / (eps + residue_weights.sum())
       _a   :			List[str]					 =torch.sum(predicted_tm_term * normed_residue_mask   ,dim=-1  )

       _a   :			int					 =per_alignment * residue_weights

       _a   :			List[Any]					 =(weighted == torch.max(_UpperCAmelCase  )).nonzero()[0]
       return per_alignment[tuple(_UpperCAmelCase  )]


