
from __future__ import annotations

import unittest

from transformers import RoFormerConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers import (
      TFRoFormerForCausalLM,
      TFRoFormerForMaskedLM,
      TFRoFormerForMultipleChoice,
      TFRoFormerForQuestionAnswering,
      TFRoFormerForSequenceClassification,
      TFRoFormerForTokenClassification,
      TFRoFormerModel,
  )
  from transformers.models.roformer.modeling_tf_roformer import (
      TFRoFormerSelfAttention,
      TFRoFormerSinusoidalPositionalEmbedding,
  )

class __snake_case		:
    def __init__(  self		,snake_case		,snake_case=13		,snake_case=7		,snake_case=True		,snake_case=True		,snake_case=True		,snake_case=True		,snake_case=99		,snake_case=32		,snake_case=2		,snake_case=4		,snake_case=37		,snake_case="gelu"		,snake_case=0.1		,snake_case=0.1		,snake_case=512		,snake_case=16		,snake_case=2		,snake_case=0.02		,snake_case=3		,snake_case=4		,snake_case=None		,):



           '''simple docstring'''







           lowercase							:     Optional[Any]       	=      parent
           lowercase							:     List[Any]       	=      13
           lowercase							:     Any       	=      7
           lowercase							:     Union[str, Any]       	=      True
           lowercase							:     Dict       	=      True
           lowercase							:     List[Any]       	=      True
           lowercase							:     List[str]       	=      True
           lowercase							:     Any       	=      99
           lowercase							:     Tuple       	=      32
           lowercase							:     Optional[Any]       	=      2
           lowercase							:     Tuple       	=      4
           lowercase							:     Tuple       	=      37
           lowercase							:     Optional[int]       	=      """gelu"""
           lowercase							:     List[str]       	=      0.1
           lowercase							:     Tuple       	=      0.1
           lowercase							:     Union[str, Any]       	=      512
           lowercase							:     List[str]       	=      16
           lowercase							:     Optional[int]       	=      2
           lowercase							:     str       	=      0.02
           lowercase							:     int       	=      3
           lowercase							:     Optional[Any]       	=      4
           lowercase							:     str       	=      None
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     Optional[Any]       	=      ids_tensor([self.batch_size, self.seq_length]		,self.vocab_size      )

           lowercase							:     Tuple       	=      None
           if self.use_input_mask:
                  lowercase							:     Dict       	=      random_attention_mask([self.batch_size, self.seq_length]      )

           lowercase							:     Any       	=      None
           if self.use_token_type_ids:
                  lowercase							:     Union[str, Any]       	=      ids_tensor([self.batch_size, self.seq_length]		,self.type_vocab_size      )

           lowercase							:     List[str]       	=      None
           lowercase							:     Optional[int]       	=      None
           lowercase							:     Any       	=      None
           if self.use_labels:
                  lowercase							:     Union[str, Any]       	=      ids_tensor([self.batch_size]		,self.type_sequence_label_size      )
                  lowercase							:     Any       	=      ids_tensor([self.batch_size, self.seq_length]		,self.num_labels      )
                  lowercase							:     str       	=      ids_tensor([self.batch_size]		,self.num_choices      )

           lowercase							:     Any       	=      RoFormerConfig(
               vocab_size=self.vocab_size		,hidden_size=self.hidden_size		,num_hidden_layers=self.num_hidden_layers		,num_attention_heads=self.num_attention_heads		,intermediate_size=self.intermediate_size		,hidden_act=self.hidden_act		,hidden_dropout_prob=self.hidden_dropout_prob		,attention_probs_dropout_prob=self.attention_probs_dropout_prob		,max_position_embeddings=self.max_position_embeddings		,type_vocab_size=self.type_vocab_size		,initializer_range=self.initializer_range		,return_dict=snake_case		,)

           return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case      ):



           '''simple docstring'''







           lowercase							:     Union[str, Any]       	=      TFRoFormerModel(config=snake_case      )
           lowercase							:     List[str]       	=      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

           lowercase							:     Tuple       	=      [input_ids, input_mask]
           lowercase							:     Any       	=      model(snake_case      )

           lowercase							:     Any       	=      model(snake_case      )

           self.parent.assertEqual(result.last_hidden_state.shape		,(self.batch_size, self.seq_length, self.hidden_size)      )
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case      ):



           '''simple docstring'''







           lowercase							:     str       	=      True
           lowercase							:     Dict       	=      TFRoFormerForCausalLM(config=snake_case      )
           lowercase							:     Dict       	=      {
               """input_ids""": input_ids,
               """attention_mask""": input_mask,
               """token_type_ids""": token_type_ids,
           }
           lowercase							:     Optional[int]       	=      model(snake_case      )["""logits"""]
           self.parent.assertListEqual(
               list(prediction_scores.numpy().shape      )		,[self.batch_size, self.seq_length, self.vocab_size]      )
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case      ):



           '''simple docstring'''







           lowercase							:     Dict       	=      TFRoFormerForMaskedLM(config=snake_case      )
           lowercase							:     List[Any]       	=      {
               """input_ids""": input_ids,
               """attention_mask""": input_mask,
               """token_type_ids""": token_type_ids,
           }
           lowercase							:     Dict       	=      model(snake_case      )
           self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.seq_length, self.vocab_size)      )
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case      ):



           '''simple docstring'''







           lowercase							:     int       	=      self.num_labels
           lowercase							:     Any       	=      TFRoFormerForSequenceClassification(config=snake_case      )
           lowercase							:     int       	=      {
               """input_ids""": input_ids,
               """attention_mask""": input_mask,
               """token_type_ids""": token_type_ids,
           }

           lowercase							:     Tuple       	=      model(snake_case      )
           self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.num_labels)      )
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case      ):



           '''simple docstring'''







           lowercase							:     int       	=      self.num_choices
           lowercase							:     Tuple       	=      TFRoFormerForMultipleChoice(config=snake_case      )
           lowercase							:     Any       	=      tf.tile(tf.expand_dims(snake_case		,1      )		,(1, self.num_choices, 1)      )
           lowercase							:     Union[str, Any]       	=      tf.tile(tf.expand_dims(snake_case		,1      )		,(1, self.num_choices, 1)      )
           lowercase							:     Optional[Any]       	=      tf.tile(tf.expand_dims(snake_case		,1      )		,(1, self.num_choices, 1)      )
           lowercase							:     Optional[Any]       	=      {
               """input_ids""": multiple_choice_inputs_ids,
               """attention_mask""": multiple_choice_input_mask,
               """token_type_ids""": multiple_choice_token_type_ids,
           }
           lowercase							:     Dict       	=      model(snake_case      )
           self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.num_choices)      )
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case      ):



           '''simple docstring'''







           lowercase							:     List[Any]       	=      self.num_labels
           lowercase							:     List[str]       	=      TFRoFormerForTokenClassification(config=snake_case      )
           lowercase							:     int       	=      {
               """input_ids""": input_ids,
               """attention_mask""": input_mask,
               """token_type_ids""": token_type_ids,
           }
           lowercase							:     List[str]       	=      model(snake_case      )
           self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.seq_length, self.num_labels)      )
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case      ):



           '''simple docstring'''







           lowercase							:     Any       	=      TFRoFormerForQuestionAnswering(config=snake_case      )
           lowercase							:     Optional[Any]       	=      {
               """input_ids""": input_ids,
               """attention_mask""": input_mask,
               """token_type_ids""": token_type_ids,
           }

           lowercase							:     Dict       	=      model(snake_case      )
           self.parent.assertEqual(result.start_logits.shape		,(self.batch_size, self.seq_length)      )
           self.parent.assertEqual(result.end_logits.shape		,(self.batch_size, self.seq_length)      )
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     Union[str, Any]       	=      self.prepare_config_and_inputs()
           (
               (
               lowercase
           )	,			(
               lowercase
           )	,			(
               lowercase
           )	,			(
               lowercase
           )	,			(
               lowercase
           )	,			(
               lowercase
           )	,			(
               lowercase
           )	,			
           )							:     List[str]       	=      config_and_inputs
           lowercase							:     List[str]       	=      {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
           return config, inputs_dict






@require_tf
class __snake_case		(      lowerCAmelCase					,	lowerCAmelCase					,	unittest.TestCase     ):
    _a		:						Tuple=				(
        (
            TFRoFormerModel,
            TFRoFormerForCausalLM,
            TFRoFormerForMaskedLM,
            TFRoFormerForQuestionAnswering,
            TFRoFormerForSequenceClassification,
            TFRoFormerForTokenClassification,
            TFRoFormerForMultipleChoice,
        )
        if is_tf_available()
        else ()
    )
    _a		:						Dict=				(
        {
            "feature-extraction": TFRoFormerModel,
            "fill-mask": TFRoFormerForMaskedLM,
            "question-answering": TFRoFormerForQuestionAnswering,
            "text-classification": TFRoFormerForSequenceClassification,
            "text-generation": TFRoFormerForCausalLM,
            "token-classification": TFRoFormerForTokenClassification,
            "zero-shot": TFRoFormerForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )

    _a		:						List[Any]=				False
    _a		:						Optional[Any]=				False
    def       _SCREAMING_SNAKE_CASE       (  self		,snake_case		,snake_case		,snake_case		,snake_case		,snake_case      ):



           '''simple docstring'''







           if pipeline_test_casse_name == "TextGenerationPipelineTests":
                  return True

           return False
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     Optional[Any]       	=      TFRoFormerModelTester(self      )
           lowercase							:     Optional[Any]       	=      ConfigTester(self		,config_class=snake_case		,hidden_size=37      )
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           self.config_tester.run_common_tests()
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     Dict       	=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*snake_case      )
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     Any       	=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*snake_case      )
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     str       	=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_lm_head(*snake_case      )
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     Any       	=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_multiple_choice(*snake_case      )
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     Optional[Any]       	=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*snake_case      )
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     str       	=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*snake_case      )
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     int       	=      self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*snake_case      )
    @slow
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     Any       	=      TFRoFormerModel.from_pretrained("""junnyu/roformer_chinese_base"""      )
           self.assertIsNotNone(snake_case      )






@require_tf
class __snake_case		(      unittest.TestCase     ):
    @slow
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     Any       	=      TFRoFormerForMaskedLM.from_pretrained("""junnyu/roformer_chinese_base"""      )
           lowercase							:     Optional[int]       	=      tf.constant([[0, 1, 2, 3, 4, 5]]      )
           lowercase							:     Tuple       	=      model(snake_case      )[0]

           # TODO Replace vocab size
           lowercase							:     int       	=      50000

           lowercase							:     Optional[Any]       	=      [1, 6, vocab_size]
           self.assertEqual(output.shape		,snake_case      )

           print(output[:, :3, :3]      )

           # TODO Replace values below with what was printed above.
           lowercase							:     Any       	=      tf.constant(
               [
                   [
                       [-0.12_053_341, -1.0_264_901, 0.29_221_946],
                       [-1.5_133_783, 0.197_433, 0.15_190_607],
                       [-5.0_135_403, -3.900_256, -0.84_038_764],
                   ]
               ]      )
           tf.debugging.assert_near(output[:, :3, :3]		,snake_case		,atol=1e-4      )






@require_tf
class __snake_case		(      unittest.TestCase     ):
    _a		:						Dict=				1E-4
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     Dict       	=      tf.constant([[4, 10]]      )
           lowercase							:     int       	=      TFRoFormerSinusoidalPositionalEmbedding(num_positions=6		,embedding_dim=6      )

           lowercase							:     Optional[Any]       	=      emba(input_ids.shape      )
           lowercase							:     Optional[int]       	=      tf.constant(
               [[0.0_000, 0.0_000, 0.0_000, 1.0_000, 1.0_000, 1.0_000], [0.8_415, 0.0_464, 0.0_022, 0.5_403, 0.9_989, 1.0_000]]      )

           tf.debugging.assert_near(snake_case		,snake_case		,atol=self.tolerance      )
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     Optional[Any]       	=      tf.constant(
               [
                   [0.0_000, 0.0_000, 0.0_000, 0.0_000, 0.0_000],
                   [0.8_415, 0.8_219, 0.8_020, 0.7_819, 0.7_617],
                   [0.9_093, 0.9_364, 0.9_581, 0.9_749, 0.9_870],
               ]      )
           lowercase							:     str       	=      TFRoFormerSinusoidalPositionalEmbedding(num_positions=512		,embedding_dim=512      )
           emba([2, 16, 512]      )
           lowercase							:     Union[str, Any]       	=      emba.weight[:3, :5]

           tf.debugging.assert_near(snake_case		,snake_case		,atol=self.tolerance      )






@require_tf
class __snake_case		(      unittest.TestCase     ):
    _a		:						int=				1E-4
    def       _SCREAMING_SNAKE_CASE       (  self      ):



           '''simple docstring'''







           lowercase							:     Optional[int]       	=      tf.reshape(tf.range(2 * 12 * 16 * 64		,dtype=tf.floataa      )		,shape=(2, 12, 16, 64)      ) / 100

           lowercase							:     Tuple       	=      -tf.reshape(tf.range(2 * 12 * 16 * 64		,dtype=tf.floataa      )		,shape=(2, 12, 16, 64)      ) / 100

           lowercase							:     Tuple       	=      TFRoFormerSinusoidalPositionalEmbedding(num_positions=32		,embedding_dim=64      )
           lowercase							:     Union[str, Any]       	=      embed_positions([2, 16, 768]      )[None, None, :, :]

           lowercase	,			lowercase							:     Union[str, Any]       	=      TFRoFormerSelfAttention.apply_rotary_position_embeddings(
               snake_case		,snake_case		,snake_case      )

           lowercase							:     Optional[Any]       	=      tf.constant(
               [
                   [0.0_000, 0.0_100, 0.0_200, 0.0_300, 0.0_400, 0.0_500, 0.0_600, 0.0_700],
                   [-0.2_012, 0.8_897, 0.0_263, 0.9_401, 0.2_074, 0.9_463, 0.3_481, 0.9_343],
                   [-1.7_057, 0.6_271, -1.2_145, 1.3_897, -0.6_303, 1.7_647, -0.1_173, 1.8_985],
                   [-2.1_731, -1.6_397, -2.7_358, 0.2_854, -2.1_840, 1.7_183, -1.3_018, 2.4_871],
                   [0.2_717, -3.6_173, -2.9_206, -2.1_988, -3.6_638, 0.3_858, -2.9_155, 2.2_980],
                   [3.9_859, -2.1_580, -0.7_984, -4.4_904, -4.1_181, -2.0_252, -4.4_782, 1.1_253],
               ]      )
           lowercase							:     Optional[Any]       	=      tf.constant(
               [
                   [0.0_000, -0.0_100, -0.0_200, -0.0_300, -0.0_400, -0.0_500, -0.0_600, -0.0_700],
                   [0.2_012, -0.8_897, -0.0_263, -0.9_401, -0.2_074, -0.9_463, -0.3_481, -0.9_343],
                   [1.7_057, -0.6_271, 1.2_145, -1.3_897, 0.6_303, -1.7_647, 0.1_173, -1.8_985],
                   [2.1_731, 1.6_397, 2.7_358, -0.2_854, 2.1_840, -1.7_183, 1.3_018, -2.4_871],
                   [-0.2_717, 3.6_173, 2.9_206, 2.1_988, 3.6_638, -0.3_858, 2.9_155, -2.2_980],
                   [-3.9_859, 2.1_580, 0.7_984, 4.4_904, 4.1_181, 2.0_252, 4.4_782, -1.1_253],
               ]      )

           tf.debugging.assert_near(query_layer[0, 0, :6, :8]		,snake_case		,atol=self.tolerance      )
           tf.debugging.assert_near(key_layer[0, 0, :6, :8]		,snake_case		,atol=self.tolerance      )



'''simple docstring'''






import argparse
import json
import logging
import os
import shutil
import sys
import tempfile
import unittest
from unittest import mock

import torch
from accelerate.utils import write_basic_config

from transformers.testing_utils import TestCasePlus, get_gpu_count, run_command, slow, torch_device
from transformers.utils import is_apex_available


logging.basicConfig(level=logging.DEBUG)

lowerCAmelCase__											=	logging.getLogger()


def 		_A     (    ):






 """simple docstring"""



 __lowercase							  = argparse.ArgumentParser()
 parser.add_argument('''-f'''      )
 __lowercase							  = parser.parse_args()
 return args.f


def 		_A     (    A__      ):






 """simple docstring"""



 __lowercase							  = {}
 __lowercase							  = os.path.join(A__     ,				'''all_results.json'''      )
 if os.path.exists(A__      ):
  with open(A__     ,				'''r'''      ) as f:
   __lowercase							  = json.load(A__      )
 else:
  raise ValueError(F"can't find {path}"      )
 return results


def 		_A     (    ):






 """simple docstring"""



 __lowercase							  = torch.cuda.is_available() and torch_device == '''cuda'''
 return is_using_cuda and is_apex_available()


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






class 			lowercase_	(lowerCamelCase__      ):




  """simple docstring"""




  @classmethod
  def 					SCREAMING_SNAKE_CASE      (      cls   :     List[str]			):
   # Write Accelerate config, will pick up on CPU, GPU, and multi-GPU
   __lowercase							  = tempfile.mkdtemp()
   __lowercase							  = os.path.join(cls.tmpdir					,'''default_config.yml'''			)
   write_basic_config(save_location=cls.configPath			)
   __lowercase							  = ['''accelerate''', '''launch''', '''--config_file''', cls.configPath]






  @classmethod
  def 					SCREAMING_SNAKE_CASE      (      cls   :     str			):
   shutil.rmtree(cls.tmpdir			)






  @mock.patch.dict(os.environ					,{'''WANDB_MODE''': '''offline'''}			)
  def 					SCREAMING_SNAKE_CASE      (      self   :     Any			):
   __lowercase							  = self.get_auto_remove_tmp_dir()
   __lowercase							  = F"\n            {self.examples_dir}/pytorch/text-classification/run_glue_no_trainer.py\n            --model_name_or_path distilbert-base-uncased\n            --output_dir {tmp_dir}\n            --train_file ./tests/fixtures/tests_samples/MRPC/train.csv\n            --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv\n            --per_device_train_batch_size=2\n            --per_device_eval_batch_size=1\n            --learning_rate=1e-4\n            --seed=42\n            --checkpointing_steps epoch\n            --with_tracking\n        ".split()

   if is_cuda_and_apex_available():
    testargs.append('''--fp16'''			)

   run_command(self._launch_args + testargs			)
   __lowercase							  = get_results(lowercase__			)
   self.assertGreaterEqual(result['''eval_accuracy''']					,0.7_5			)
   self.assertTrue(os.path.exists(os.path.join(lowercase__					,'''epoch_0'''			)			)			)
   self.assertTrue(os.path.exists(os.path.join(lowercase__					,'''glue_no_trainer'''			)			)			)






  @mock.patch.dict(os.environ					,{'''WANDB_MODE''': '''offline'''}			)
  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[int]			):
   __lowercase							  = self.get_auto_remove_tmp_dir()
   __lowercase							  = F"\n            {self.examples_dir}/pytorch/language-modeling/run_clm_no_trainer.py\n            --model_name_or_path distilgpt2\n            --train_file ./tests/fixtures/sample_text.txt\n            --validation_file ./tests/fixtures/sample_text.txt\n            --block_size 128\n            --per_device_train_batch_size 5\n            --per_device_eval_batch_size 5\n            --num_train_epochs 2\n            --output_dir {tmp_dir}\n            --checkpointing_steps epoch\n            --with_tracking\n        ".split()

   if torch.cuda.device_count() > 1:
    # Skipping because there are not enough batches to train the model + would need a drop_last to work.
    return

   run_command(self._launch_args + testargs			)
   __lowercase							  = get_results(lowercase__			)
   self.assertLess(result['''perplexity''']					,1_0_0			)
   self.assertTrue(os.path.exists(os.path.join(lowercase__					,'''epoch_0'''			)			)			)
   self.assertTrue(os.path.exists(os.path.join(lowercase__					,'''clm_no_trainer'''			)			)			)






  @mock.patch.dict(os.environ					,{'''WANDB_MODE''': '''offline'''}			)
  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[int]			):
   __lowercase							  = self.get_auto_remove_tmp_dir()
   __lowercase							  = F"\n            {self.examples_dir}/pytorch/language-modeling/run_mlm_no_trainer.py\n            --model_name_or_path distilroberta-base\n            --train_file ./tests/fixtures/sample_text.txt\n            --validation_file ./tests/fixtures/sample_text.txt\n            --output_dir {tmp_dir}\n            --num_train_epochs=1\n            --checkpointing_steps epoch\n            --with_tracking\n        ".split()

   run_command(self._launch_args + testargs			)
   __lowercase							  = get_results(lowercase__			)
   self.assertLess(result['''perplexity''']					,4_2			)
   self.assertTrue(os.path.exists(os.path.join(lowercase__					,'''epoch_0'''			)			)			)
   self.assertTrue(os.path.exists(os.path.join(lowercase__					,'''mlm_no_trainer'''			)			)			)






  @mock.patch.dict(os.environ					,{'''WANDB_MODE''': '''offline'''}			)
  def 					SCREAMING_SNAKE_CASE      (      self   :     Tuple			):
   # with so little data distributed training needs more epochs to get the score on par with 0/1 gpu
   __lowercase							  = 7 if get_gpu_count() > 1 else 2

   __lowercase							  = self.get_auto_remove_tmp_dir()
   __lowercase							  = F"\n            {self.examples_dir}/pytorch/token-classification/run_ner_no_trainer.py\n            --model_name_or_path bert-base-uncased\n            --train_file tests/fixtures/tests_samples/conll/sample.json\n            --validation_file tests/fixtures/tests_samples/conll/sample.json\n            --output_dir {tmp_dir}\n            --learning_rate=2e-4\n            --per_device_train_batch_size=2\n            --per_device_eval_batch_size=2\n            --num_train_epochs={epochs}\n            --seed 7\n            --checkpointing_steps epoch\n            --with_tracking\n        ".split()

   run_command(self._launch_args + testargs			)
   __lowercase							  = get_results(lowercase__			)
   self.assertGreaterEqual(result['''eval_accuracy''']					,0.7_5			)
   self.assertLess(result['''train_loss''']					,0.5			)
   self.assertTrue(os.path.exists(os.path.join(lowercase__					,'''epoch_0'''			)			)			)
   self.assertTrue(os.path.exists(os.path.join(lowercase__					,'''ner_no_trainer'''			)			)			)






  @unittest.skip(reason='''Fix me @muellerzr'''			)
  @mock.patch.dict(os.environ					,{'''WANDB_MODE''': '''offline'''}			)
  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[Any]			):
   __lowercase							  = self.get_auto_remove_tmp_dir()
   __lowercase							  = F"\n            {self.examples_dir}/pytorch/question-answering/run_qa_no_trainer.py\n            --model_name_or_path bert-base-uncased\n            --version_2_with_negative\n            --train_file tests/fixtures/tests_samples/SQUAD/sample.json\n            --validation_file tests/fixtures/tests_samples/SQUAD/sample.json\n            --output_dir {tmp_dir}\n            --seed=42\n            --max_train_steps=10\n            --num_warmup_steps=2\n            --learning_rate=2e-4\n            --per_device_train_batch_size=2\n            --per_device_eval_batch_size=1\n            --checkpointing_steps epoch\n            --with_tracking\n        ".split()

   run_command(self._launch_args + testargs			)
   __lowercase							  = get_results(lowercase__			)
   # Because we use --version_2_with_negative the testing script uses SQuAD v2 metrics.
   self.assertGreaterEqual(result['''eval_f1''']					,2_8			)
   self.assertGreaterEqual(result['''eval_exact''']					,2_8			)
   self.assertTrue(os.path.exists(os.path.join(lowercase__					,'''epoch_0'''			)			)			)
   self.assertTrue(os.path.exists(os.path.join(lowercase__					,'''qa_no_trainer'''			)			)			)






  @mock.patch.dict(os.environ					,{'''WANDB_MODE''': '''offline'''}			)
  def 					SCREAMING_SNAKE_CASE      (      self   :     Dict			):
   __lowercase							  = self.get_auto_remove_tmp_dir()
   __lowercase							  = F"\n            {self.examples_dir}/pytorch/multiple-choice/run_swag_no_trainer.py\n            --model_name_or_path bert-base-uncased\n            --train_file tests/fixtures/tests_samples/swag/sample.json\n            --validation_file tests/fixtures/tests_samples/swag/sample.json\n            --output_dir {tmp_dir}\n            --max_train_steps=20\n            --num_warmup_steps=2\n            --learning_rate=2e-4\n            --per_device_train_batch_size=2\n            --per_device_eval_batch_size=1\n            --with_tracking\n        ".split()

   run_command(self._launch_args + testargs			)
   __lowercase							  = get_results(lowercase__			)
   self.assertGreaterEqual(result['''eval_accuracy''']					,0.8			)
   self.assertTrue(os.path.exists(os.path.join(lowercase__					,'''swag_no_trainer'''			)			)			)






  @slow
  @mock.patch.dict(os.environ					,{'''WANDB_MODE''': '''offline'''}			)
  def 					SCREAMING_SNAKE_CASE      (      self   :     List[str]			):
   __lowercase							  = self.get_auto_remove_tmp_dir()
   __lowercase							  = F"\n            {self.examples_dir}/pytorch/summarization/run_summarization_no_trainer.py\n            --model_name_or_path t5-small\n            --train_file tests/fixtures/tests_samples/xsum/sample.json\n            --validation_file tests/fixtures/tests_samples/xsum/sample.json\n            --output_dir {tmp_dir}\n            --max_train_steps=50\n            --num_warmup_steps=8\n            --learning_rate=2e-4\n            --per_device_train_batch_size=2\n            --per_device_eval_batch_size=1\n            --checkpointing_steps epoch\n            --with_tracking\n        ".split()

   run_command(self._launch_args + testargs			)
   __lowercase							  = get_results(lowercase__			)
   self.assertGreaterEqual(result['''eval_rouge1''']					,1_0			)
   self.assertGreaterEqual(result['''eval_rouge2''']					,2			)
   self.assertGreaterEqual(result['''eval_rougeL''']					,7			)
   self.assertGreaterEqual(result['''eval_rougeLsum''']					,7			)
   self.assertTrue(os.path.exists(os.path.join(lowercase__					,'''epoch_0'''			)			)			)
   self.assertTrue(os.path.exists(os.path.join(lowercase__					,'''summarization_no_trainer'''			)			)			)






  @slow
  @mock.patch.dict(os.environ					,{'''WANDB_MODE''': '''offline'''}			)
  def 					SCREAMING_SNAKE_CASE      (      self   :     List[Any]			):
   __lowercase							  = self.get_auto_remove_tmp_dir()
   __lowercase							  = F"\n            {self.examples_dir}/pytorch/translation/run_translation_no_trainer.py\n            --model_name_or_path sshleifer/student_marian_en_ro_6_1\n            --source_lang en\n            --target_lang ro\n            --train_file tests/fixtures/tests_samples/wmt16/sample.json\n            --validation_file tests/fixtures/tests_samples/wmt16/sample.json\n            --output_dir {tmp_dir}\n            --max_train_steps=50\n            --num_warmup_steps=8\n            --num_beams=6\n            --learning_rate=3e-3\n            --per_device_train_batch_size=2\n            --per_device_eval_batch_size=1\n            --source_lang en_XX\n            --target_lang ro_RO\n            --checkpointing_steps epoch\n            --with_tracking\n        ".split()

   run_command(self._launch_args + testargs			)
   __lowercase							  = get_results(lowercase__			)
   self.assertGreaterEqual(result['''eval_bleu''']					,3_0			)
   self.assertTrue(os.path.exists(os.path.join(lowercase__					,'''epoch_0'''			)			)			)
   self.assertTrue(os.path.exists(os.path.join(lowercase__					,'''translation_no_trainer'''			)			)			)






  @slow
  def 					SCREAMING_SNAKE_CASE      (      self   :     str			):
   __lowercase							  = logging.StreamHandler(sys.stdout			)
   logger.addHandler(lowercase__			)

   __lowercase							  = self.get_auto_remove_tmp_dir()
   __lowercase							  = F"\n            {self.examples_dir}/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py\n            --dataset_name huggingface/semantic-segmentation-test-sample\n            --output_dir {tmp_dir}\n            --max_train_steps=10\n            --num_warmup_steps=2\n            --learning_rate=2e-4\n            --per_device_train_batch_size=2\n            --per_device_eval_batch_size=1\n            --checkpointing_steps epoch\n        ".split()

   run_command(self._launch_args + testargs			)
   __lowercase							  = get_results(lowercase__			)
   self.assertGreaterEqual(result['''eval_overall_accuracy''']					,0.1_0			)






  @mock.patch.dict(os.environ					,{'''WANDB_MODE''': '''offline'''}			)
  def 					SCREAMING_SNAKE_CASE      (      self   :     Tuple			):
   __lowercase							  = self.get_auto_remove_tmp_dir()
   __lowercase							  = F"\n            {self.examples_dir}/pytorch/image-classification/run_image_classification_no_trainer.py\n            --model_name_or_path google/vit-base-patch16-224-in21k\n            --dataset_name hf-internal-testing/cats_vs_dogs_sample\n            --learning_rate 1e-4\n            --per_device_train_batch_size 2\n            --per_device_eval_batch_size 1\n            --max_train_steps 2\n            --train_val_split 0.1\n            --seed 42\n            --output_dir {tmp_dir}\n            --with_tracking\n            --checkpointing_steps 1\n        ".split()

   if is_cuda_and_apex_available():
    testargs.append('''--fp16'''			)

   run_command(self._launch_args + testargs			)
   __lowercase							  = get_results(lowercase__			)
   # The base model scores a 25%
   self.assertGreaterEqual(result['''eval_accuracy''']					,0.6			)
   self.assertTrue(os.path.exists(os.path.join(lowercase__					,'''step_1'''			)			)			)
   self.assertTrue(os.path.exists(os.path.join(lowercase__					,'''image_classification_no_trainer'''			)			)			)

