


"""simple docstring"""







import json
import os
import re
import shutil
import tempfile
import unittest
from typing import Tuple

from transformers import AddedToken, BatchEncoding, PerceiverTokenizer
from transformers.utils import cached_property, is_tf_available, is_torch_available

from ...test_tokenization_common import TokenizerTesterMixin


if is_torch_available():
     lowerCAmelCase__				    =       '''pt'''
elif is_tf_available():
     lowerCAmelCase__				    =       '''tf'''
else:
     lowerCAmelCase__				    =       '''jax'''


class 				SCREAMING_SNAKE_CASE__    (		lowercase       ,   unittest.TestCase    ):
       """simple docstring"""


       a  :    Optional[Any]       =PerceiverTokenizer
       a  :    str       =False

       def 	lowercase__							(						self						):






         """simple docstring"""





         super().setUp()
         lowerCAmelCase					:					str								=							PerceiverTokenizer()
         tokenizer.save_pretrained(self.tmpdirname						)

       @cached_property
       def 	lowercase__							(						self						):






         """simple docstring"""





         return PerceiverTokenizer.from_pretrained("deepmind/language-perceiver"						)

       def 	lowercase__							(						self		,					**snake_case__						):






         """simple docstring"""





         return self.tokenizer_class.from_pretrained(self.tmpdirname		,					**snake_case__						)

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__=False		,					snake_case__=20		,					snake_case__=5						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[int]								=							[]
         for i in range(len(snake_case__						)						):
           try:
             lowerCAmelCase					:					List[Any]								=							tokenizer.decode([i]		,					clean_up_tokenization_spaces=snake_case__						)
           except UnicodeDecodeError:
             pass
           toks.append((i, tok)						)

         lowerCAmelCase					:					Optional[Any]								=							list(filter(lambda snake_case__						: re.match(r"^[ a-zA-Z]+$"		,					t[1]						)		,					snake_case__						)						)
         lowerCAmelCase					:					List[Any]								=							list(filter(lambda snake_case__						: [t[0]] == tokenizer.encode(t[1]		,					add_special_tokens=snake_case__						)		,					snake_case__						)						)
         if max_length is not None and len(snake_case__						) > max_length:
           lowerCAmelCase					:					Optional[int]								=							toks[:max_length]
         if min_length is not None and len(snake_case__						) < min_length and len(snake_case__						) > 0:
           while len(snake_case__						) < min_length:
             lowerCAmelCase					:					Optional[int]								=							toks + toks
        # toks_str = [t[1] for t in toks]
         lowerCAmelCase					:					Tuple								=							[t[0] for t in toks]

         # Ensure consistency
         lowerCAmelCase					:					Union[str, Any]								=							tokenizer.decode(snake_case__		,					clean_up_tokenization_spaces=snake_case__						)
         if " " not in output_txt and len(snake_case__						) > 1:
           lowerCAmelCase					:					Any								=							(
               tokenizer.decode([toks_ids[0]]		,					clean_up_tokenization_spaces=snake_case__						)
               + " "
               + tokenizer.decode(toks_ids[1:]		,					clean_up_tokenization_spaces=snake_case__						)
           )
         if with_prefix_space:
           lowerCAmelCase					:					int								=							" " + output_txt
         lowerCAmelCase					:					str								=							tokenizer.encode(snake_case__		,					add_special_tokens=snake_case__						)
         return output_txt, output_ids

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[int]								=							self.perceiver_tokenizer
         lowerCAmelCase					:					Optional[Any]								=							"Unicode €."
         lowerCAmelCase					:					List[Any]								=							tokenizer(snake_case__						)
         lowerCAmelCase					:					Optional[Any]								=							[4, 91, 116, 111, 105, 117, 106, 107, 38, 232, 136, 178, 52, 5]
         self.assertEqual(encoded["input_ids"]		,					snake_case__						)

         # decoding
         lowerCAmelCase					:					Tuple								=							tokenizer.decode(snake_case__						)
         self.assertEqual(snake_case__		,					"[CLS]Unicode €.[SEP]"						)

         lowerCAmelCase					:					Optional[int]								=							tokenizer("e è é ê ë"						)
         lowerCAmelCase					:					Any								=							[4, 107, 38, 201, 174, 38, 201, 175, 38, 201, 176, 38, 201, 177, 5]
         self.assertEqual(encoded["input_ids"]		,					snake_case__						)
         # decoding
         lowerCAmelCase					:					Optional[int]								=							tokenizer.decode(snake_case__						)
         self.assertEqual(snake_case__		,					"[CLS]e è é ê ë[SEP]"						)

         # encode/decode, but with `encode` instead of `__call__`
         self.assertEqual(tokenizer.decode(tokenizer.encode("e è é ê ë"						)						)		,					"[CLS]e è é ê ë[SEP]"						)

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Union[str, Any]								=							self.perceiver_tokenizer
         lowerCAmelCase					:					Union[str, Any]								=							["A long paragraph for summarization.", "Another paragraph for summarization."]
         # fmt: off
         lowerCAmelCase					:					int								=							[4, 71, 38, 114, 117, 116, 109, 38, 118, 103, 120, 103, 109, 120, 103, 118, 110, 38, 108, 117, 120, 38, 121, 123, 115, 115, 103, 120, 111, 128, 103, 122, 111, 117, 116, 52, 5, 0]
         # fmt: on
         lowerCAmelCase					:					int								=							tokenizer(snake_case__		,					padding=snake_case__		,					return_tensors=snake_case__						)
         self.assertIsInstance(snake_case__		,					snake_case__						)

         if FRAMEWORK != "jax":
           lowerCAmelCase					:					Tuple								=							list(batch.input_ids.numpy()[0]						)
         else:
           lowerCAmelCase					:					Optional[Any]								=							list(batch.input_ids.tolist()[0]						)

         self.assertListEqual(snake_case__		,					snake_case__						)

         self.assertEqual((2, 38)		,					batch.input_ids.shape						)
         self.assertEqual((2, 38)		,					batch.attention_mask.shape						)

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					List[Any]								=							self.perceiver_tokenizer
         lowerCAmelCase					:					Dict								=							["A long paragraph for summarization.", "Another paragraph for summarization."]
         lowerCAmelCase					:					Optional[Any]								=							tokenizer(snake_case__		,					padding=snake_case__		,					return_tensors=snake_case__						)
         # check if input_ids are returned and no decoder_input_ids
         self.assertIn("input_ids"		,					snake_case__						)
         self.assertIn("attention_mask"		,					snake_case__						)
         self.assertNotIn("decoder_input_ids"		,					snake_case__						)
         self.assertNotIn("decoder_attention_mask"		,					snake_case__						)

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Dict								=							self.perceiver_tokenizer
         lowerCAmelCase					:					Optional[int]								=							[
             "Summary of the text.",
             "Another summary.",
         ]
         lowerCAmelCase					:					Tuple								=							tokenizer(
             text_target=snake_case__		,					max_length=32		,					padding="max_length"		,					truncation=snake_case__		,					return_tensors=snake_case__						)
         self.assertEqual(32		,					targets["input_ids"].shape[1]						)

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					str								=							self.get_tokenizers()
         for tokenizer in tokenizers:
           with self.subTest(f"""{tokenizer.__class__.__name__}"""						):
             self.assertNotEqual(tokenizer.model_max_length		,					42						)

        # Now let's start the test
         lowerCAmelCase					:					Optional[int]								=							self.get_tokenizers()
         for tokenizer in tokenizers:
           with self.subTest(f"""{tokenizer.__class__.__name__}"""						):
             # Isolate this from the other tests because we save additional tokens/etc
             lowerCAmelCase					:					Optional[int]								=							tempfile.mkdtemp()

             lowerCAmelCase					:					Union[str, Any]								=							" He is very happy, UNwant\u00E9d,running"
             lowerCAmelCase					:					str								=							tokenizer.encode(snake_case__		,					add_special_tokens=snake_case__						)
             tokenizer.save_pretrained(snake_case__						)

             lowerCAmelCase					:					List[Any]								=							tokenizer.__class__.from_pretrained(snake_case__						)
             lowerCAmelCase					:					Optional[Any]								=							after_tokenizer.encode(snake_case__		,					add_special_tokens=snake_case__						)
             self.assertListEqual(snake_case__		,					snake_case__						)

             shutil.rmtree(snake_case__						)

         lowerCAmelCase					:					Any								=							self.get_tokenizers(model_max_length=42						)
         for tokenizer in tokenizers:
           with self.subTest(f"""{tokenizer.__class__.__name__}"""						):
             # Isolate this from the other tests because we save additional tokens/etc
             lowerCAmelCase					:					Any								=							tempfile.mkdtemp()

             lowerCAmelCase					:					Optional[int]								=							" He is very happy, UNwant\u00E9d,running"
             tokenizer.add_tokens(["bim", "bambam"]						)
             lowerCAmelCase					:					int								=							tokenizer.additional_special_tokens
             additional_special_tokens.append("new_additional_special_token"						)
             tokenizer.add_special_tokens({"additional_special_tokens": additional_special_tokens}						)
             lowerCAmelCase					:					List[Any]								=							tokenizer.encode(snake_case__		,					add_special_tokens=snake_case__						)
             tokenizer.save_pretrained(snake_case__						)

             lowerCAmelCase					:					Optional[Any]								=							tokenizer.__class__.from_pretrained(snake_case__						)
             lowerCAmelCase					:					Optional[Any]								=							after_tokenizer.encode(snake_case__		,					add_special_tokens=snake_case__						)
             self.assertListEqual(snake_case__		,					snake_case__						)
             self.assertIn("new_additional_special_token"		,					after_tokenizer.additional_special_tokens						)
             self.assertEqual(after_tokenizer.model_max_length		,					42						)

             lowerCAmelCase					:					Tuple								=							tokenizer.__class__.from_pretrained(snake_case__		,					model_max_length=43						)
             self.assertEqual(tokenizer.model_max_length		,					43						)

             shutil.rmtree(snake_case__						)

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[Any]								=							[]
         if self.test_slow_tokenizer:
           tokenizer_list.append((self.tokenizer_class, self.get_tokenizer())						)

         if self.test_rust_tokenizer:
           tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer())						)

         for tokenizer_class, tokenizer_utils in tokenizer_list:
           with tempfile.TemporaryDirectory() as tmp_dir:
             tokenizer_utils.save_pretrained(snake_case__						)

             with open(os.path.join(snake_case__		,					"special_tokens_map.json"						)		,					encoding="utf-8"						) as json_file:
               lowerCAmelCase					:					Dict								=							json.load(snake_case__						)

             with open(os.path.join(snake_case__		,					"tokenizer_config.json"						)		,					encoding="utf-8"						) as json_file:
               lowerCAmelCase					:					Tuple								=							json.load(snake_case__						)

             lowerCAmelCase					:					Tuple								=							[f"""<extra_id_{i}>""" for i in range(125						)]

             lowerCAmelCase					:					Any								=							added_tokens_extra_ids + [
                 "an_additional_special_token"
             ]
             lowerCAmelCase					:					List[str]								=							added_tokens_extra_ids + [
                 "an_additional_special_token"
             ]

             with open(os.path.join(snake_case__		,					"special_tokens_map.json"						)		,					"w"		,					encoding="utf-8"						) as outfile:
               json.dump(snake_case__		,					snake_case__						)
             with open(os.path.join(snake_case__		,					"tokenizer_config.json"						)		,					"w"		,					encoding="utf-8"						) as outfile:
               json.dump(snake_case__		,					snake_case__						)

             # the following checks allow us to verify that our test works as expected, i.e. that the tokenizer takes
             # into account the new value of additional_special_tokens given in the "tokenizer_config.json" and
             # "special_tokens_map.json" files
             lowerCAmelCase					:					Optional[Any]								=							tokenizer_class.from_pretrained(
                 snake_case__		,					)
             self.assertIn(
                 "an_additional_special_token"		,					tokenizer_without_change_in_init.additional_special_tokens						)
             self.assertEqual(
                 ["an_additional_special_token"]		,					tokenizer_without_change_in_init.convert_ids_to_tokens(
                     tokenizer_without_change_in_init.convert_tokens_to_ids(["an_additional_special_token"]						)						)		,					)

             # Now we test that we can change the value of additional_special_tokens in the from_pretrained
             lowerCAmelCase					:					int								=							added_tokens_extra_ids + [AddedToken("a_new_additional_special_token"		,					lstrip=snake_case__						)]
             lowerCAmelCase					:					List[str]								=							tokenizer_class.from_pretrained(
                 snake_case__		,					additional_special_tokens=snake_case__		,					)

             self.assertIn("a_new_additional_special_token"		,					tokenizer.additional_special_tokens						)
             self.assertEqual(
                 ["a_new_additional_special_token"]		,					tokenizer.convert_ids_to_tokens(
                     tokenizer.convert_tokens_to_ids(["a_new_additional_special_token"]						)						)		,					)

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					List[str]								=							self.perceiver_tokenizer
         self.assertEqual(tokenizer.decode([178]						)		,					"�"						)

       def 	lowercase__							(						self						):






         """simple docstring"""





         pass

       def 	lowercase__							(						self						):






         """simple docstring"""





         pass

       def 	lowercase__							(						self						):






         """simple docstring"""





         pass

       def 	lowercase__							(						self						):






         """simple docstring"""





         pass







       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					List[str]								=							self.get_tokenizers(fast=snake_case__		,					do_lower_case=snake_case__						)
         for tokenizer in tokenizers:
           with self.subTest(f"""{tokenizer.__class__.__name__}"""						):
             lowerCAmelCase					:					Tuple								=							["[CLS]", "t", "h", "i", "s", " ", "i", "s", " ", "a", " ", "t", "e", "s", "t", "[SEP]"]
             lowerCAmelCase					:					Union[str, Any]								=							tokenizer.convert_tokens_to_string(snake_case__						)

             self.assertIsInstance(snake_case__		,					snake_case__						)









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







  lowerCAmelCase					:					Tuple								=							argparse.ArgumentParser()
  parser.add_argument("-f"    )
  lowerCAmelCase					:					Tuple								=							parser.parse_args()
  return args.f
def  a__      (    SCREAMING_SNAKE_CASE   :   int    ):


  '''simple docstring'''







  lowerCAmelCase					:					Union[str, Any]								=							{}
  lowerCAmelCase					:					List[str]								=							os.path.join(SCREAMING_SNAKE_CASE    ,				"all_results.json"    )
  if os.path.exists(SCREAMING_SNAKE_CASE    ):
    with open(SCREAMING_SNAKE_CASE    ,				"r"    ) as f:
      lowerCAmelCase					:					str								=							json.load(SCREAMING_SNAKE_CASE    )
  else:
    raise ValueError(f"""can't find {path}"""    )
  return results
def  a__      (    ):


  '''simple docstring'''







  lowerCAmelCase					:					Tuple								=							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 				SCREAMING_SNAKE_CASE__    (		lowercase    ):
       """simple docstring"""


       @classmethod
       def 	lowercase__							(						cls						):






         """simple docstring"""





         lowerCAmelCase					:					Any								=							tempfile.mkdtemp()
         lowerCAmelCase					:					List[Any]								=							os.path.join(cls.tmpdir		,					"default_config.yml"						)
         write_basic_config(save_location=cls.configPath						)
         lowerCAmelCase					:					Optional[int]								=							["accelerate", "launch", "--config_file", cls.configPath]

       @classmethod
       def 	lowercase__							(						cls						):






         """simple docstring"""





         shutil.rmtree(cls.tmpdir						)

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






         """simple docstring"""





         lowerCAmelCase					:					Optional[int]								=							self.get_auto_remove_tmp_dir()
         lowerCAmelCase					:					int								=							f"""
            {self.examples_dir}/pytorch/text-classification/run_glue_no_trainer.py
            --model_name_or_path distilbert-base-uncased
            --output_dir {tmp_dir}
            --train_file ./tests/fixtures/tests_samples/MRPC/train.csv
            --validation_file ./tests/fixtures/tests_samples/MRPC/dev.csv
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --learning_rate=1e-4
            --seed=42
            --checkpointing_steps epoch
            --with_tracking
        """.split()

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

         run_command(self._launch_args + testargs						)
         lowerCAmelCase					:					Dict								=							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 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					List[Any]								=							self.get_auto_remove_tmp_dir()
         lowerCAmelCase					:					Tuple								=							f"""
            {self.examples_dir}/pytorch/language-modeling/run_clm_no_trainer.py
            --model_name_or_path distilgpt2
            --train_file ./tests/fixtures/sample_text.txt
            --validation_file ./tests/fixtures/sample_text.txt
            --block_size 128
            --per_device_train_batch_size 5
            --per_device_eval_batch_size 5
            --num_train_epochs 2
            --output_dir {tmp_dir}
            --checkpointing_steps epoch
            --with_tracking
        """.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						)
         lowerCAmelCase					:					List[str]								=							get_results(snake_case__						)
         self.assertLess(result["perplexity"]		,					100						)
         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 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Union[str, Any]								=							self.get_auto_remove_tmp_dir()
         lowerCAmelCase					:					Tuple								=							f"""
            {self.examples_dir}/pytorch/language-modeling/run_mlm_no_trainer.py
            --model_name_or_path distilroberta-base
            --train_file ./tests/fixtures/sample_text.txt
            --validation_file ./tests/fixtures/sample_text.txt
            --output_dir {tmp_dir}
            --num_train_epochs=1
            --checkpointing_steps epoch
            --with_tracking
        """.split()

         run_command(self._launch_args + testargs						)
         lowerCAmelCase					:					Any								=							get_results(snake_case__						)
         self.assertLess(result["perplexity"]		,					42						)
         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 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[int]								=							7 if get_gpu_count() > 1 else 2

         lowerCAmelCase					:					Optional[Any]								=							self.get_auto_remove_tmp_dir()
         lowerCAmelCase					:					int								=							f"""
            {self.examples_dir}/pytorch/token-classification/run_ner_no_trainer.py
            --model_name_or_path bert-base-uncased
            --train_file tests/fixtures/tests_samples/conll/sample.json
            --validation_file tests/fixtures/tests_samples/conll/sample.json
            --output_dir {tmp_dir}
            --learning_rate=2e-4
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=2
            --num_train_epochs={epochs}
            --seed 7
            --checkpointing_steps epoch
            --with_tracking
        """.split()

         run_command(self._launch_args + testargs						)
         lowerCAmelCase					:					str								=							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 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[int]								=							self.get_auto_remove_tmp_dir()
         lowerCAmelCase					:					List[str]								=							f"""
            {self.examples_dir}/pytorch/question-answering/run_qa_no_trainer.py
            --model_name_or_path bert-base-uncased
            --version_2_with_negative
            --train_file tests/fixtures/tests_samples/SQUAD/sample.json
            --validation_file tests/fixtures/tests_samples/SQUAD/sample.json
            --output_dir {tmp_dir}
            --seed=42
            --max_train_steps=10
            --num_warmup_steps=2
            --learning_rate=2e-4
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --checkpointing_steps epoch
            --with_tracking
        """.split()

         run_command(self._launch_args + testargs						)
         lowerCAmelCase					:					Optional[Any]								=							get_results(snake_case__						)
         # Because we use --version_2_with_negative the testing script uses SQuAD v2 metrics.
         self.assertGreaterEqual(result["eval_f1"]		,					28						)
         self.assertGreaterEqual(result["eval_exact"]		,					28						)
         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 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					List[Any]								=							self.get_auto_remove_tmp_dir()
         lowerCAmelCase					:					List[Any]								=							f"""
            {self.examples_dir}/pytorch/multiple-choice/run_swag_no_trainer.py
            --model_name_or_path bert-base-uncased
            --train_file tests/fixtures/tests_samples/swag/sample.json
            --validation_file tests/fixtures/tests_samples/swag/sample.json
            --output_dir {tmp_dir}
            --max_train_steps=20
            --num_warmup_steps=2
            --learning_rate=2e-4
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --with_tracking
        """.split()

         run_command(self._launch_args + testargs						)
         lowerCAmelCase					:					Tuple								=							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 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					str								=							self.get_auto_remove_tmp_dir()
         lowerCAmelCase					:					Tuple								=							f"""
            {self.examples_dir}/pytorch/summarization/run_summarization_no_trainer.py
            --model_name_or_path t5-small
            --train_file tests/fixtures/tests_samples/xsum/sample.json
            --validation_file tests/fixtures/tests_samples/xsum/sample.json
            --output_dir {tmp_dir}
            --max_train_steps=50
            --num_warmup_steps=8
            --learning_rate=2e-4
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --checkpointing_steps epoch
            --with_tracking
        """.split()

         run_command(self._launch_args + testargs						)
         lowerCAmelCase					:					str								=							get_results(snake_case__						)
         self.assertGreaterEqual(result["eval_rouge1"]		,					10						)
         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 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Tuple								=							self.get_auto_remove_tmp_dir()
         lowerCAmelCase					:					Tuple								=							f"""
            {self.examples_dir}/pytorch/translation/run_translation_no_trainer.py
            --model_name_or_path sshleifer/student_marian_en_ro_6_1
            --source_lang en
            --target_lang ro
            --train_file tests/fixtures/tests_samples/wmt16/sample.json
            --validation_file tests/fixtures/tests_samples/wmt16/sample.json
            --output_dir {tmp_dir}
            --max_train_steps=50
            --num_warmup_steps=8
            --num_beams=6
            --learning_rate=3e-3
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --source_lang en_XX
            --target_lang ro_RO
            --checkpointing_steps epoch
            --with_tracking
        """.split()

         run_command(self._launch_args + testargs						)
         lowerCAmelCase					:					int								=							get_results(snake_case__						)
         self.assertGreaterEqual(result["eval_bleu"]		,					30						)
         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 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[int]								=							logging.StreamHandler(sys.stdout						)
         logger.addHandler(snake_case__						)

         lowerCAmelCase					:					Optional[Any]								=							self.get_auto_remove_tmp_dir()
         lowerCAmelCase					:					Any								=							f"""
            {self.examples_dir}/pytorch/semantic-segmentation/run_semantic_segmentation_no_trainer.py
            --dataset_name huggingface/semantic-segmentation-test-sample
            --output_dir {tmp_dir}
            --max_train_steps=10
            --num_warmup_steps=2
            --learning_rate=2e-4
            --per_device_train_batch_size=2
            --per_device_eval_batch_size=1
            --checkpointing_steps epoch
        """.split()

         run_command(self._launch_args + testargs						)
         lowerCAmelCase					:					int								=							get_results(snake_case__						)
         self.assertGreaterEqual(result["eval_overall_accuracy"]		,					0.10						)







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






         """simple docstring"""





         lowerCAmelCase					:					str								=							self.get_auto_remove_tmp_dir()
         lowerCAmelCase					:					Union[str, Any]								=							f"""
            {self.examples_dir}/pytorch/image-classification/run_image_classification_no_trainer.py
            --model_name_or_path google/vit-base-patch16-224-in21k
            --dataset_name hf-internal-testing/cats_vs_dogs_sample
            --learning_rate 1e-4
            --per_device_train_batch_size 2
            --per_device_eval_batch_size 1
            --max_train_steps 2
            --train_val_split 0.1
            --seed 42
            --output_dir {tmp_dir}
            --with_tracking
            --checkpointing_steps 1
        """.split()

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

         run_command(self._launch_args + testargs						)
         lowerCAmelCase					:					Dict								=							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"						)						)						)





