





import unittest

from parameterized import parameterized

from transformers import AutoTokenizer, GPTNeoXConfig, is_torch_available, set_seed
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers import (
          GPTNeoXForCausalLM,
          GPTNeoXForQuestionAnswering,
          GPTNeoXForSequenceClassification,
          GPTNeoXForTokenClassification,
          GPTNeoXModel,
      )





class UpperCAmelCase_				:


  def __init__(							self ,__snake_case ,__snake_case=1_3 ,__snake_case=7 ,__snake_case=True ,__snake_case=True ,__snake_case=True ,__snake_case=True ,__snake_case=9_9 ,__snake_case=6_4 ,__snake_case=5 ,__snake_case=4 ,__snake_case=3_7 ,__snake_case="gelu" ,__snake_case=0.1 ,__snake_case=0.1 ,__snake_case=5_1_2 ,__snake_case=1_6 ,__snake_case=2 ,__snake_case=0.02 ,__snake_case=3 ,__snake_case=4 ,__snake_case=None ,):



       """simple docstring"""





       A_          =       parent
       A_          =       batch_size
       A_          =       seq_length
       A_          =       is_training
       A_          =       use_input_mask
       A_          =       use_token_type_ids
       A_          =       use_labels
       A_          =       vocab_size
       A_          =       hidden_size
       A_          =       num_hidden_layers
       A_          =       num_attention_heads
       A_          =       intermediate_size
       A_          =       hidden_act
       A_          =       hidden_dropout_prob
       A_          =       attention_probs_dropout_prob
       A_          =       max_position_embeddings
       A_          =       type_vocab_size
       A_          =       type_sequence_label_size
       A_          =       initializer_range
       A_          =       num_labels
       A_          =       num_choices
       A_          =       scope
       A_          =       vocab_size - 1


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       ids_tensor([self.batch_size, self.seq_length] ,self.vocab_size )

       A_          =       None
       if self.use_input_mask:
            A_          =       random_attention_mask([self.batch_size, self.seq_length] )

       A_          =       None
       if self.use_labels:
            A_          =       ids_tensor([self.batch_size, self.seq_length] ,self.num_labels )

       A_          =       self.get_config()

       return config, input_ids, input_mask, token_labels


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       return GPTNeoXConfig(
           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 ,is_decoder=__snake_case ,initializer_range=self.initializer_range ,pad_token_id=self.pad_token_id ,)


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_   ,    A_   ,    A_   ,    A_          =       self.prepare_config_and_inputs()

       A_          =       True

       return config, input_ids, input_mask, token_labels


  def 						__UpperCAmelCase     (							self ,__snake_case ,__snake_case ,__snake_case ):



       """simple docstring"""





       A_          =       GPTNeoXModel(config=__snake_case )
       model.to(__snake_case )
       model.eval()
       A_          =       model(__snake_case ,attention_mask=__snake_case )
       A_          =       model(__snake_case )
       self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) )


  def 						__UpperCAmelCase     (							self ,__snake_case ,__snake_case ,__snake_case ):



       """simple docstring"""





       A_          =       True
       A_          =       GPTNeoXModel(__snake_case )
       model.to(__snake_case )
       model.eval()
       A_          =       model(__snake_case ,attention_mask=__snake_case )
       self.parent.assertEqual(result.last_hidden_state.shape ,(self.batch_size, self.seq_length, self.hidden_size) )


  def 						__UpperCAmelCase     (							self ,__snake_case ,__snake_case ,__snake_case ,__snake_case ):



       """simple docstring"""





       A_          =       GPTNeoXForCausalLM(config=__snake_case )
       model.to(__snake_case )
       model.eval()
       A_          =       model(__snake_case ,attention_mask=__snake_case ,labels=__snake_case )
       self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.vocab_size) )


  def 						__UpperCAmelCase     (							self ,__snake_case ,__snake_case ,__snake_case ,__snake_case ):



       """simple docstring"""





       A_          =       self.num_labels
       A_          =       GPTNeoXForQuestionAnswering(__snake_case )
       model.to(__snake_case )
       model.eval()
       A_          =       model(__snake_case ,attention_mask=__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 						__UpperCAmelCase     (							self ,__snake_case ,__snake_case ,__snake_case ,__snake_case ):



       """simple docstring"""





       A_          =       self.num_labels
       A_          =       GPTNeoXForSequenceClassification(__snake_case )
       model.to(__snake_case )
       model.eval()
       A_          =       ids_tensor([self.batch_size] ,self.type_sequence_label_size )
       A_          =       model(__snake_case ,attention_mask=__snake_case ,labels=__snake_case )
       self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.num_labels) )


  def 						__UpperCAmelCase     (							self ,__snake_case ,__snake_case ,__snake_case ,__snake_case ):



       """simple docstring"""





       A_          =       self.num_labels
       A_          =       GPTNeoXForTokenClassification(__snake_case )
       model.to(__snake_case )
       model.eval()
       A_          =       model(__snake_case ,attention_mask=__snake_case ,labels=__snake_case )
       self.parent.assertEqual(result.logits.shape ,(self.batch_size, self.seq_length, self.num_labels) )


  def 						__UpperCAmelCase     (							self ,__snake_case ,__snake_case ,__snake_case ):



       """simple docstring"""





       A_          =       True
       A_          =       GPTNeoXForCausalLM(config=__snake_case )
       model.to(__snake_case )
       model.eval()

       # first forward pass
       A_          =       model(__snake_case ,attention_mask=__snake_case ,use_cache=__snake_case )
       A_          =       outputs.past_key_values

       # create hypothetical multiple next token and extent to next_input_ids
       A_          =       ids_tensor((self.batch_size, 3) ,config.vocab_size )
       A_          =       ids_tensor((self.batch_size, 3) ,vocab_size=2 )

       # append to next input_ids and
       A_          =       torch.cat([input_ids, next_tokens] ,dim=-1 )
       A_          =       torch.cat([input_mask, next_mask] ,dim=-1 )

       A_          =       model(__snake_case ,attention_mask=__snake_case ,output_hidden_states=__snake_case )
       A_          =       output_from_no_past['''hidden_states'''][0]
       A_          =       model(
           __snake_case ,attention_mask=__snake_case ,past_key_values=__snake_case ,output_hidden_states=__snake_case ,)['''hidden_states'''][0]

       # select random slice
       A_          =       ids_tensor((1,) ,output_from_past.shape[-1] ).item()
       A_          =       output_from_no_past[:, -3:, random_slice_idx].detach()
       A_          =       output_from_past[:, :, random_slice_idx].detach()

       self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1] )

       # test that outputs are equal for slice
       self.parent.assertTrue(torch.allclose(__snake_case ,__snake_case ,atol=1E-3 ) )




  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.prepare_config_and_inputs()
       A_   ,    A_   ,    A_   ,    A_          =       config_and_inputs
       A_          =       {'''input_ids''': input_ids, '''attention_mask''': input_mask}
       return config, inputs_dict





@require_torch
class UpperCAmelCase_				( _SCREAMING_SNAKE_CASE     ,		_SCREAMING_SNAKE_CASE     ,		_SCREAMING_SNAKE_CASE     ,		unittest.TestCase ):
  __lowerCAmelCase   :						List[str]							=   (
      (
          GPTNeoXModel,
          GPTNeoXForCausalLM,
          GPTNeoXForQuestionAnswering,
          GPTNeoXForSequenceClassification,
          GPTNeoXForTokenClassification,
      )
      if is_torch_available()
      else ()
  )
  __lowerCAmelCase   :						List[Any]							=   (GPTNeoXForCausalLM,) if is_torch_available() else ()
  __lowerCAmelCase   :						Optional[int]							=   (
      {
          """feature-extraction""": GPTNeoXModel,
          """question-answering""": GPTNeoXForQuestionAnswering,
          """text-classification""": GPTNeoXForSequenceClassification,
          """text-generation""": GPTNeoXForCausalLM,
          """token-classification""": GPTNeoXForTokenClassification,
          """zero-shot""": GPTNeoXForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  __lowerCAmelCase   :						Union[str, Any]							=   False
  __lowerCAmelCase   :						Union[str, Any]							=   False
  __lowerCAmelCase   :						List[str]							=   False
  __lowerCAmelCase   :						Union[str, Any]							=   False


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       GPTNeoXModelTester(self )
       A_          =       ConfigTester(self ,config_class=__snake_case ,hidden_size=6_4 ,num_attention_heads=8 )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       self.config_tester.run_common_tests()


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_   ,    A_   ,    A_   ,    A_          =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(__snake_case ,__snake_case ,__snake_case )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_   ,    A_   ,    A_   ,    A_          =       self.model_tester.prepare_config_and_inputs_for_decoder()
       self.model_tester.create_and_check_model_as_decoder(__snake_case ,__snake_case ,__snake_case )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_   ,    A_   ,    A_   ,    A_          =       self.model_tester.prepare_config_and_inputs_for_decoder()

       A_          =       None

       self.model_tester.create_and_check_model_as_decoder(__snake_case ,__snake_case ,__snake_case )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_   ,    A_   ,    A_   ,    A_          =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_decoder_model_past_large_inputs(__snake_case ,__snake_case ,__snake_case )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_causal_lm(*__snake_case )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*__snake_case )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*__snake_case )


  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*__snake_case )


  @unittest.skip(reason='''Feed forward chunking is not implemented''' )
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       pass




  @parameterized.expand([('''linear''',), ('''dynamic''',)] )
  def 						__UpperCAmelCase     (							self ,__snake_case ):



       """simple docstring"""





       A_   ,    A_          =       self.model_tester.prepare_config_and_inputs_for_common()
       A_          =       ids_tensor([1, 1_0] ,config.vocab_size )
       A_          =       ids_tensor([1, int(config.max_position_embeddings * 1.5 )] ,config.vocab_size )

       set_seed(4_2 )  # Fixed seed at init time so the two models get the same random weights
       A_          =       GPTNeoXModel(__snake_case )
       original_model.to(__snake_case )
       original_model.eval()
       A_          =       original_model(__snake_case ).last_hidden_state
       A_          =       original_model(__snake_case ).last_hidden_state

       set_seed(4_2 )  # Fixed seed at init time so the two models get the same random weights
       A_          =       {'''type''': scaling_type, '''factor''': 10.0}
       A_          =       GPTNeoXModel(__snake_case )
       scaled_model.to(__snake_case )
       scaled_model.eval()
       A_          =       scaled_model(__snake_case ).last_hidden_state
       A_          =       scaled_model(__snake_case ).last_hidden_state

       # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original
       # maximum sequence length, so the outputs for the short input should match.
       if scaling_type == "dynamic":
            self.assertTrue(torch.allclose(__snake_case ,__snake_case ,atol=1E-5 ) )
       else:
            self.assertFalse(torch.allclose(__snake_case ,__snake_case ,atol=1E-5 ) )

       # The output should be different for long inputs
       self.assertFalse(torch.allclose(__snake_case ,__snake_case ,atol=1E-5 ) )





@require_torch
class UpperCAmelCase_				( unittest.TestCase ):


  @slow
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       AutoTokenizer.from_pretrained('''EleutherAI/pythia-410m-deduped''' )
       for checkpointing in [True, False]:
            A_          =       GPTNeoXForCausalLM.from_pretrained('''EleutherAI/pythia-410m-deduped''' )

            if checkpointing:
                 model.gradient_checkpointing_enable()
            else:
                 model.gradient_checkpointing_disable()
            model.to(__snake_case )

            A_          =       tokenizer('''My favorite food is''' ,return_tensors='''pt''' ).to(__snake_case )
            # The hub repo. is updated on 2023-04-04, resulting in poor outputs.
            # See: https://github.com/huggingface/transformers/pull/24193
            A_          =       '''My favorite food is a good old-fashioned, old-fashioned, old-fashioned.\n\nI\'m not sure'''

            A_          =       model.generate(**__snake_case ,do_sample=__snake_case ,max_new_tokens=2_0 )
            A_          =       tokenizer.batch_decode(__snake_case )[0]

            self.assertEqual(__snake_case ,__snake_case )










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)

a__      :     Any         =		logging.getLogger()

def      UpperCAmelCase_    (    )	->				Any:
     '''simple docstring'''


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

def      UpperCAmelCase_    (    _UpperCAmelCase							:Union[str, Any]				)	->				str:
     '''simple docstring'''


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

def      UpperCAmelCase_    (    )	->				Union[str, Any]:
     '''simple docstring'''


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


a__      :     List[Any]         =		logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)





class UpperCAmelCase_				( _SCREAMING_SNAKE_CASE ):


  @classmethod
  def 						__UpperCAmelCase     (							cls ):



       """simple docstring"""





       A_          =       tempfile.mkdtemp()
       A_          =       os.path.join(cls.tmpdir ,'''default_config.yml''' )
       write_basic_config(save_location=cls.configPath )
       A_          =       ['''accelerate''', '''launch''', '''--config_file''', cls.configPath]


  @classmethod
  def 						__UpperCAmelCase     (							cls ):



       """simple docstring"""





       shutil.rmtree(cls.tmpdir )


  @mock.patch.dict(os.environ ,{'''WANDB_MODE''': '''offline'''} )
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.get_auto_remove_tmp_dir()
       A_          =       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 )
       A_          =       get_results(__snake_case )
       self.assertGreaterEqual(result['''eval_accuracy'''] ,0.75 )
       self.assertTrue(os.path.exists(os.path.join(__snake_case ,'''epoch_0''' ) ) )
       self.assertTrue(os.path.exists(os.path.join(__snake_case ,'''glue_no_trainer''' ) ) )


  @mock.patch.dict(os.environ ,{'''WANDB_MODE''': '''offline'''} )
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.get_auto_remove_tmp_dir()
       A_          =       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 )
       A_          =       get_results(__snake_case )
       self.assertLess(result['''perplexity'''] ,1_0_0 )
       self.assertTrue(os.path.exists(os.path.join(__snake_case ,'''epoch_0''' ) ) )
       self.assertTrue(os.path.exists(os.path.join(__snake_case ,'''clm_no_trainer''' ) ) )


  @mock.patch.dict(os.environ ,{'''WANDB_MODE''': '''offline'''} )
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.get_auto_remove_tmp_dir()
       A_          =       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 )
       A_          =       get_results(__snake_case )
       self.assertLess(result['''perplexity'''] ,4_2 )
       self.assertTrue(os.path.exists(os.path.join(__snake_case ,'''epoch_0''' ) ) )
       self.assertTrue(os.path.exists(os.path.join(__snake_case ,'''mlm_no_trainer''' ) ) )


  @mock.patch.dict(os.environ ,{'''WANDB_MODE''': '''offline'''} )
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       7 if get_gpu_count() > 1 else 2

       A_          =       self.get_auto_remove_tmp_dir()
       A_          =       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 )
       A_          =       get_results(__snake_case )
       self.assertGreaterEqual(result['''eval_accuracy'''] ,0.75 )
       self.assertLess(result['''train_loss'''] ,0.5 )
       self.assertTrue(os.path.exists(os.path.join(__snake_case ,'''epoch_0''' ) ) )
       self.assertTrue(os.path.exists(os.path.join(__snake_case ,'''ner_no_trainer''' ) ) )


  @unittest.skip(reason='''Fix me @muellerzr''' )
  @mock.patch.dict(os.environ ,{'''WANDB_MODE''': '''offline'''} )
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.get_auto_remove_tmp_dir()
       A_          =       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 )
       A_          =       get_results(__snake_case )
       # 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(__snake_case ,'''epoch_0''' ) ) )
       self.assertTrue(os.path.exists(os.path.join(__snake_case ,'''qa_no_trainer''' ) ) )


  @mock.patch.dict(os.environ ,{'''WANDB_MODE''': '''offline'''} )
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.get_auto_remove_tmp_dir()
       A_          =       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 )
       A_          =       get_results(__snake_case )
       self.assertGreaterEqual(result['''eval_accuracy'''] ,0.8 )
       self.assertTrue(os.path.exists(os.path.join(__snake_case ,'''swag_no_trainer''' ) ) )


  @slow
  @mock.patch.dict(os.environ ,{'''WANDB_MODE''': '''offline'''} )
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.get_auto_remove_tmp_dir()
       A_          =       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 )
       A_          =       get_results(__snake_case )
       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(__snake_case ,'''epoch_0''' ) ) )
       self.assertTrue(os.path.exists(os.path.join(__snake_case ,'''summarization_no_trainer''' ) ) )


  @slow
  @mock.patch.dict(os.environ ,{'''WANDB_MODE''': '''offline'''} )
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.get_auto_remove_tmp_dir()
       A_          =       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 )
       A_          =       get_results(__snake_case )
       self.assertGreaterEqual(result['''eval_bleu'''] ,3_0 )
       self.assertTrue(os.path.exists(os.path.join(__snake_case ,'''epoch_0''' ) ) )
       self.assertTrue(os.path.exists(os.path.join(__snake_case ,'''translation_no_trainer''' ) ) )


  @slow
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





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

       A_          =       self.get_auto_remove_tmp_dir()
       A_          =       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 )
       A_          =       get_results(__snake_case )
       self.assertGreaterEqual(result['''eval_overall_accuracy'''] ,0.10 )




  @mock.patch.dict(os.environ ,{'''WANDB_MODE''': '''offline'''} )
  def 						__UpperCAmelCase     (							self ):



       """simple docstring"""





       A_          =       self.get_auto_remove_tmp_dir()
       A_          =       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 )
       A_          =       get_results(__snake_case )
       # The base model scores a 25%
       self.assertGreaterEqual(result['''eval_accuracy'''] ,0.6 )
       self.assertTrue(os.path.exists(os.path.join(__snake_case ,'''step_1''' ) ) )
       self.assertTrue(os.path.exists(os.path.join(__snake_case ,'''image_classification_no_trainer''' ) ) )



