




"""simple docstring"""


import json
import logging
import os
import sys
from time import time
from unittest.mock import patch

from transformers.testing_utils import TestCasePlus, require_torch_tpu


logging.basicConfig(level=logging.DEBUG)

A_      =    logging.getLogger()


def  UpperCAmelCase__	(snake_case__							: Optional[Any]   ):
  """simple docstring"""

  _snake_case							:		Optional[int]								=  {}
  _snake_case							:		Any								=  os.path.join(_A		,       """all_results.json"""   )
  if os.path.exists(_A   ):
    with open(_A		,       """r"""   ) as f:
      _snake_case							:		List[str]								=  json.load(_A   )
  else:
    raise ValueError(F"can't find {path}"   )
  return results


A_      =    logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)






@require_torch_tpu
class  lowercase(					lowerCamelCase__			):







 '''simple docstring'''





 def 			UpperCamelCase_       (    self:		List[str]		):




   '''simple docstring'''



   import xla_spawn

   _snake_case							:		Dict								=  self.get_auto_remove_tmp_dir()
   _snake_case							:		int								=  f"\n            ./examples/pytorch/text-classification/run_glue.py\n            --num_cores=8\n            ./examples/pytorch/text-classification/run_glue.py\n            --model_name_or_path distilbert-base-uncased\n            --output_dir {tmp_dir}\n            --overwrite_output_dir\n            --train_file ./tests/fixtures/tests_samples/MRPC/train.csv\n            --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv\n            --do_train\n            --do_eval\n            --debug tpu_metrics_debug\n            --per_device_train_batch_size=2\n            --per_device_eval_batch_size=1\n            --learning_rate=1e-4\n            --max_steps=10\n            --warmup_steps=2\n            --seed=42\n            --max_seq_length=128\n            ".split()

   with patch.object(__snake_case,				"""argv""",				__snake_case		):
     _snake_case							:		str								=  time()
     xla_spawn.main()
     _snake_case							:		str								=  time()

     _snake_case							:		List[str]								=  get_results(__snake_case		)
     self.assertGreaterEqual(result["""eval_accuracy"""],				0.75		)

     # Assert that the script takes less than 500 seconds to make sure it doesn't hang.
     self.assertLess(end - start,				500		)


 def 			UpperCamelCase_       (    self:		Tuple		):




   '''simple docstring'''



   import xla_spawn

   _snake_case							:		Optional[Any]								=  '\n            ./tests/test_trainer_tpu.py\n            --num_cores=8\n            ./tests/test_trainer_tpu.py\n            '.split()
   with patch.object(__snake_case,				"""argv""",				__snake_case		):
     xla_spawn.main()









"""simple docstring"""


import unicodedata
from dataclasses import dataclass
from typing import Optional, Union

import numpy as np

from transformers.data.data_collator import DataCollatorMixin
from transformers.file_utils import PaddingStrategy
from transformers.tokenization_utils_base import PreTrainedTokenizerBase


def  UpperCAmelCase__	(snake_case__							: Optional[int]		,       snake_case__							: Any		,       snake_case__							: List[str]		,       snake_case__							: int   ):
  """simple docstring"""

  if isinstance(snake_case__		,       snake_case__   ):
    _snake_case							:		List[Any]								=  np.full((len(snake_case__   ), sequence_length, 2)		,       snake_case__   )
  else:
    _snake_case							:		Any								=  np.full((len(snake_case__   ), sequence_length)		,       snake_case__   )

  for i, tensor in enumerate(snake_case__   ):
    if padding_side == "right":
      if isinstance(snake_case__		,       snake_case__   ):
        _snake_case							:		Dict								=  tensor[:sequence_length]
      else:
        _snake_case							:		List[Any]								=  tensor[:sequence_length]
    else:
      if isinstance(snake_case__		,       snake_case__   ):
        _snake_case							:		str								=  tensor[:sequence_length]
      else:
        _snake_case							:		Tuple								=  tensor[:sequence_length]

  return out_tensor.tolist()


def  UpperCAmelCase__	(snake_case__							: Optional[int]   ):
  """simple docstring"""

  _snake_case							:		str								=  ord(snake_case__   )
  if (cp >= 33 and cp <= 47) or (cp >= 58 and cp <= 64) or (cp >= 91 and cp <= 96) or (cp >= 1_23 and cp <= 1_26):
    return True
  _snake_case							:		Union[str, Any]								=  unicodedata.category(snake_case__   )
  if cat.startswith("""P"""   ):
    return True
  return False






@dataclass
class  lowercase(					__a			):







 '''simple docstring'''





 lowercase__			 = 42
 lowercase__			 = True
 lowercase__			 = None
 lowercase__			 = None
 lowercase__			 = -1_00
 lowercase__			 = "pt"


 def 			UpperCamelCase_       (    self:		Any,				a_:		Union[str, Any]		):




   '''simple docstring'''



   import torch

   _snake_case							:		Optional[Any]								=  """label""" if """label""" in features[0].keys() else """labels"""
   _snake_case							:		str								=  [feature[label_name] for feature in features] if label_name in features[0].keys() else None
   _snake_case							:		Any								=  self.tokenizer.pad(
       a_,				padding=self.padding,				max_length=self.max_length,				pad_to_multiple_of=self.pad_to_multiple_of,				return_tensors="""pt""" if labels is None else None,				)

   if labels is None:
     return batch

   _snake_case							:		Optional[int]								=  torch.tensor(batch["""entity_ids"""]		).shape[1]
   _snake_case							:		Any								=  self.tokenizer.padding_side
   if padding_side == "right":
     _snake_case							:		Optional[int]								=  [
         list(a_		) + [self.label_pad_token_id] * (sequence_length - len(a_		)) for label in labels
     ]
   else:
     _snake_case							:		Union[str, Any]								=  [
         [self.label_pad_token_id] * (sequence_length - len(a_		)) + list(a_		) for label in labels
     ]

   _snake_case							:		List[Any]								=  [feature["""ner_tags"""] for feature in features]
   _snake_case							:		str								=  padding_tensor(a_,				-1,				a_,				a_		)
   _snake_case							:		Any								=  [feature["""original_entity_spans"""] for feature in features]
   _snake_case							:		int								=  padding_tensor(a_,				(-1, -1),				a_,				a_		)
   _snake_case							:		str								=  {k: torch.tensor(a_,				dtype=torch.intaa		) for k, v in batch.items()}

   return batch


