import json
import os
import unittest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_ftfy, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin






@require_tokenizers
class 		_lowerCamelCase		(   UpperCamelCase_    ,     unittest.TestCase    ):
    __a  =      CLIPTokenizer
    __a  =      CLIPTokenizerFast
    __a  =      True
    __a  =      {}
    __a  =      False
    def 						UpperCamelCase_				(  self   )       ->					List[Any]:
     super().setUp()

     # fmt: off
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      ['''l''', '''o''', '''w''', '''e''', '''r''', '''s''', '''t''', '''i''', '''d''', '''n''', '''lo''', '''l</w>''', '''w</w>''', '''r</w>''', '''t</w>''', '''low</w>''', '''er</w>''', '''lowest</w>''', '''newer</w>''', '''wider''', '''<unk>''', '''<|startoftext|>''', '''<|endoftext|>''']
     # fmt: on
     SCREAMING_SNAKE_CASE__:   Union[str, Any]=      dict(zip(lowerCAmelCase , range(len(lowerCAmelCase   )   )   )   )
     SCREAMING_SNAKE_CASE__:   Tuple=      ['''#version: 0.2''', '''l o''', '''lo w</w>''', '''e r</w>''']
     SCREAMING_SNAKE_CASE__:   List[Any]=      {'''unk_token''': '''<unk>'''}

     SCREAMING_SNAKE_CASE__:   str=      os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''vocab_file''']   )
     SCREAMING_SNAKE_CASE__:   List[str]=      os.path.join(self.tmpdirname , VOCAB_FILES_NAMES['''merges_file''']   )
     with open(self.vocab_file , '''w''' , encoding='''utf-8'''   ) as fp:
      fp.write(json.dumps(lowerCAmelCase   ) + '''\n'''   )
     with open(self.merges_file , '''w''' , encoding='''utf-8'''   ) as fp:
      fp.write('''\n'''.join(lowerCAmelCase   )   )
    def 						UpperCamelCase_				(  self , **lowerCAmelCase   )       ->					Any:
     kwargs.update(self.special_tokens_map   )
     return CLIPTokenizer.from_pretrained(self.tmpdirname , **lowerCAmelCase   )
    def 						UpperCamelCase_				(  self , **lowerCAmelCase   )       ->					Any:
     kwargs.update(self.special_tokens_map   )
     return CLIPTokenizerFast.from_pretrained(self.tmpdirname , **lowerCAmelCase   )
    def 						UpperCamelCase_				(  self , lowerCAmelCase   )       ->					Optional[int]:
     SCREAMING_SNAKE_CASE__:   Dict=      '''lower newer'''
     SCREAMING_SNAKE_CASE__:   List[str]=      '''lower newer'''
     return input_text, output_text
    def 						UpperCamelCase_				(  self   )       ->					List[str]:
     SCREAMING_SNAKE_CASE__:   Dict=      CLIPTokenizer(self.vocab_file , self.merges_file , **self.special_tokens_map   )
     SCREAMING_SNAKE_CASE__:   Tuple=      '''lower newer'''
     SCREAMING_SNAKE_CASE__:   Optional[int]=      ['''lo''', '''w''', '''er</w>''', '''n''', '''e''', '''w''', '''er</w>''']
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      tokenizer.tokenize(lowerCAmelCase   )
     self.assertListEqual(lowerCAmelCase , lowerCAmelCase   )

     SCREAMING_SNAKE_CASE__:   List[Any]=      tokens + [tokenizer.unk_token]
     SCREAMING_SNAKE_CASE__:   Any=      [10, 2, 16, 9, 3, 2, 16, 20]
     self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase   ) , lowerCAmelCase   )
    @require_ftfy
    def 						UpperCamelCase_				(  self   )       ->					List[str]:
     for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
      with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})'   ):
       SCREAMING_SNAKE_CASE__:   List[str]=      self.tokenizer_class.from_pretrained(lowerCAmelCase , **lowerCAmelCase   )
       SCREAMING_SNAKE_CASE__:   Optional[Any]=      self.rust_tokenizer_class.from_pretrained(lowerCAmelCase , **lowerCAmelCase   )

       SCREAMING_SNAKE_CASE__:   Any=      '''A\n\'ll 11p223RF☆ho!!to?\'d\'d\'\'d of a cat to-$\'\'d.'''
       SCREAMING_SNAKE_CASE__:   Optional[Any]=      tokenizer_s.tokenize(lowerCAmelCase   )
       SCREAMING_SNAKE_CASE__:   Any=      tokenizer_r.tokenize(lowerCAmelCase   )

       self.assertListEqual(lowerCAmelCase , lowerCAmelCase   )

       # Test that the tokenization is identical on an example containing a character (Latin Small Letter A
       # with Tilde) encoded in 2 different ways
       SCREAMING_SNAKE_CASE__:   Union[str, Any]=      '''xa\u0303y''' + ''' ''' + '''x\xe3y'''
       SCREAMING_SNAKE_CASE__:   List[Any]=      tokenizer_s.tokenize(lowerCAmelCase   )
       SCREAMING_SNAKE_CASE__:   Dict=      tokenizer_r.tokenize(lowerCAmelCase   )

       self.assertListEqual(lowerCAmelCase , lowerCAmelCase   )

       # Test that the tokenization is identical on unicode of space type
       SCREAMING_SNAKE_CASE__:   Union[str, Any]=      [
           '''\u0009''',  # (horizontal tab, '\t')
           '''\u000B''',  # (vertical tab)
           '''\u000C''',  # (form feed)
           '''\u0020''',  # (space, ' ')
           '''\u200E''',  # (left-to-right mark):w
           '''\u200F''',  # (right-to-left mark)
       ]
       for unicode_seq in spaces_unicodes:
        SCREAMING_SNAKE_CASE__:   int=      tokenizer_s.tokenize(lowerCAmelCase   )
        SCREAMING_SNAKE_CASE__:   Optional[Any]=      tokenizer_r.tokenize(lowerCAmelCase   )

        self.assertListEqual(lowerCAmelCase , lowerCAmelCase   )

       # Test that the tokenization is identical on unicode of line break type
       SCREAMING_SNAKE_CASE__:   Any=      [
           '''\u000A''',  # (line feed, '\n')
           '''\r\n''',  # (carriage return and line feed, '\r\n')
           '''\u000D''',  # (carriage return, '\r')
           '''\r''',  # (carriage return, '\r')
           '''\u000D''',  # (carriage return, '\r')
           '''\u2028''',  # (line separator)
           '''\u2029''',  # (paragraph separator)
           # "\u0085", # (next line)
       ]

       # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms
       # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a
       # space (and thus into an empty list).

       for unicode_seq in line_break_unicodes:
        SCREAMING_SNAKE_CASE__:   Tuple=      tokenizer_s.tokenize(lowerCAmelCase   )
        SCREAMING_SNAKE_CASE__:   Optional[Any]=      tokenizer_r.tokenize(lowerCAmelCase   )

        self.assertListEqual(lowerCAmelCase , lowerCAmelCase   )
    def 						UpperCamelCase_				(  self   )       ->					Optional[int]:
     # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space`
     for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
      with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})'   ):
       SCREAMING_SNAKE_CASE__:   Optional[int]=      '''hello'''  # `hello` is a token in the vocabulary of `pretrained_name`
       SCREAMING_SNAKE_CASE__:   str=      f'{text_of_1_token} {text_of_1_token}'

       SCREAMING_SNAKE_CASE__:   Dict=      self.rust_tokenizer_class.from_pretrained(
           lowerCAmelCase , use_fast=lowerCAmelCase , )
       SCREAMING_SNAKE_CASE__:   List[Any]=      tokenizer_r(lowerCAmelCase , return_offsets_mapping=lowerCAmelCase , add_special_tokens=lowerCAmelCase   )
       self.assertEqual(encoding.offset_mapping[0] , (0, len(lowerCAmelCase   ))   )
       self.assertEqual(
           encoding.offset_mapping[1] , (len(lowerCAmelCase   ) + 1, len(lowerCAmelCase   ) + 1 + len(lowerCAmelCase   )) , )

       SCREAMING_SNAKE_CASE__:   int=      f' {text}'

       SCREAMING_SNAKE_CASE__:   List[str]=      self.rust_tokenizer_class.from_pretrained(
           lowerCAmelCase , use_fast=lowerCAmelCase , )
       SCREAMING_SNAKE_CASE__:   Optional[Any]=      tokenizer_r(lowerCAmelCase , return_offsets_mapping=lowerCAmelCase , add_special_tokens=lowerCAmelCase   )
       self.assertEqual(encoding.offset_mapping[0] , (1, 1 + len(lowerCAmelCase   ))   )
       self.assertEqual(
           encoding.offset_mapping[1] , (1 + len(lowerCAmelCase   ) + 1, 1 + len(lowerCAmelCase   ) + 1 + len(lowerCAmelCase   )) , )
    def 						UpperCamelCase_				(  self   )       ->					str:
     # Test related to the breaking change introduced in transformers v4.17.0
     # We need to check that an error in raised when the user try to load a previous version of the tokenizer.
     with self.assertRaises(lowerCAmelCase   ) as context:
      self.rust_tokenizer_class.from_pretrained('''robot-test/old-clip-tokenizer'''   )

     self.assertTrue(
         context.exception.args[0].startswith(
             '''The `backend_tokenizer` provided does not match the expected format.'''   )   )
    @require_ftfy
    def 						UpperCamelCase_				(  self   )       ->					Tuple:
     super().test_tokenization_python_rust_equals()



    def 						UpperCamelCase_				(  self   )       ->					Union[str, Any]:
     # CLIP always lower cases letters
     pass


import ast
import os
import re
import shutil
import tempfile
import unittest
from unittest import mock

import torch

from accelerate.test_utils.examples import compare_against_test
from accelerate.test_utils.testing import TempDirTestCase, require_trackers, run_command, slow
from accelerate.utils import write_basic_config


# DataLoaders built from `test_samples/MRPC` for quick testing
# Should mock `{script_name}.get_dataloaders` via:
# @mock.patch("{script_name}.get_dataloaders", mocked_dataloaders)

lowercase_     :   Optional[Any] 				=				[
    'cross_validation.py',
    'gradient_accumulation.py',
    'local_sgd.py',
    'multi_process_metrics.py',
    'memory.py',
    'automatic_gradient_accumulation.py',
    'fsdp_with_peak_mem_tracking.py',
    'deepspeed_with_config_support.py',
    'megatron_lm_gpt_pretraining.py',
]






class 		_lowerCamelCase		(   unittest.TestCase    ):
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase = None , lowerCAmelCase = None   )       ->					int:
     SCREAMING_SNAKE_CASE__:   Optional[int]=      None
     SCREAMING_SNAKE_CASE__:   Dict=      os.path.abspath(os.path.join('''examples''' , '''by_feature'''   )   )
     SCREAMING_SNAKE_CASE__:   List[Any]=      os.path.abspath('''examples'''   )
     for item in os.listdir(lowerCAmelCase   ):
      if item not in EXCLUDE_EXAMPLES:
       SCREAMING_SNAKE_CASE__:   Tuple=      os.path.join(lowerCAmelCase , lowerCAmelCase   )
       if os.path.isfile(lowerCAmelCase   ) and ".py" in item_path:
        with self.subTest(
            tested_script=lowerCAmelCase , feature_script=lowerCAmelCase , tested_section='''main()''' if parser_only else '''training_function()''' , ):
         SCREAMING_SNAKE_CASE__:   List[Any]=      compare_against_test(
             os.path.join(lowerCAmelCase , lowerCAmelCase   ) , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )
         SCREAMING_SNAKE_CASE__:   str=      '''\n'''.join(lowerCAmelCase   )
         if special_strings is not None:
          for string in special_strings:
           SCREAMING_SNAKE_CASE__:   Union[str, Any]=      diff.replace(lowerCAmelCase , ''''''   )
         self.assertEqual(lowerCAmelCase , ''''''   )
    def 						UpperCamelCase_				(  self   )       ->					Optional[Any]:
     self.one_complete_example('''complete_nlp_example.py''' , lowerCAmelCase   )
     self.one_complete_example('''complete_nlp_example.py''' , lowerCAmelCase   )



    def 						UpperCamelCase_				(  self   )       ->					Tuple:
     SCREAMING_SNAKE_CASE__:   List[Any]=      os.path.abspath(os.path.join('''examples''' , '''cv_example.py'''   )   )
     SCREAMING_SNAKE_CASE__:   Dict=      [
         ''' ''' * 16 + '''{\n\n''',
         ''' ''' * 20 + '''"accuracy": eval_metric["accuracy"],\n\n''',
         ''' ''' * 20 + '''"f1": eval_metric["f1"],\n\n''',
         ''' ''' * 20 + '''"train_loss": total_loss.item() / len(train_dataloader),\n\n''',
         ''' ''' * 20 + '''"epoch": epoch,\n\n''',
         ''' ''' * 16 + '''},\n\n''',
         ''' ''' * 16 + '''step=epoch,\n''',
         ''' ''' * 12,
         ''' ''' * 8 + '''for step, batch in enumerate(active_dataloader):\n''',
     ]
     self.one_complete_example('''complete_cv_example.py''' , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )
     self.one_complete_example('''complete_cv_example.py''' , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )






@mock.patch.dict(os.environ    ,     {"TESTING_MOCKED_DATALOADERS": "1"}    )
class 		_lowerCamelCase		(   UpperCamelCase_    ):
    __a  =      False
    @classmethod
    def 						UpperCamelCase_				(  cls   )       ->					List[Any]:
     super().setUpClass()
     SCREAMING_SNAKE_CASE__:   List[Any]=      tempfile.mkdtemp()
     SCREAMING_SNAKE_CASE__:   Tuple=      os.path.join(cls._tmpdir , '''default_config.yml'''   )

     write_basic_config(save_location=cls.configPath   )
     SCREAMING_SNAKE_CASE__:   Dict=      ['''accelerate''', '''launch''', '''--config_file''', cls.configPath]
    @classmethod
    def 						UpperCamelCase_				(  cls   )       ->					List[Any]:
     super().tearDownClass()
     shutil.rmtree(cls._tmpdir   )
    def 						UpperCamelCase_				(  self   )       ->					str:
     SCREAMING_SNAKE_CASE__:   Dict=      f'\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps epoch\n        --output_dir {self.tmpdir}\n        '.split()
     run_command(self._launch_args + testargs   )
     self.assertTrue(os.path.exists(os.path.join(self.tmpdir , '''epoch_0'''   )   )   )
    def 						UpperCamelCase_				(  self   )       ->					Dict:
     SCREAMING_SNAKE_CASE__:   List[Any]=      f'\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps 1\n        --output_dir {self.tmpdir}\n        '.split()
     SCREAMING_SNAKE_CASE__:   List[Any]=      run_command(self._launch_args + testargs   )
     self.assertTrue(os.path.exists(os.path.join(self.tmpdir , '''step_2'''   )   )   )
    def 						UpperCamelCase_				(  self   )       ->					Dict:
     SCREAMING_SNAKE_CASE__:   Tuple=      f'\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir , "epoch_0"   )}\n        '.split()
     SCREAMING_SNAKE_CASE__:   Dict=      run_command(self._launch_args + testargs , return_stdout=lowerCAmelCase   )
     self.assertNotIn('''epoch 0:''' , lowerCAmelCase   )
     self.assertIn('''epoch 1:''' , lowerCAmelCase   )
    def 						UpperCamelCase_				(  self   )       ->					List[Any]:
     SCREAMING_SNAKE_CASE__:   Dict=      f'\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir , "step_2"   )}\n        '.split()
     SCREAMING_SNAKE_CASE__:   Dict=      run_command(self._launch_args + testargs , return_stdout=lowerCAmelCase   )
     if torch.cuda.is_available():
      SCREAMING_SNAKE_CASE__:   List[str]=      torch.cuda.device_count()
     else:
      SCREAMING_SNAKE_CASE__:   List[Any]=      1
     if num_processes > 1:
      self.assertNotIn('''epoch 0:''' , lowerCAmelCase   )
      self.assertIn('''epoch 1:''' , lowerCAmelCase   )
     else:
      self.assertIn('''epoch 0:''' , lowerCAmelCase   )
      self.assertIn('''epoch 1:''' , lowerCAmelCase   )
    @slow
    def 						UpperCamelCase_				(  self   )       ->					str:
     SCREAMING_SNAKE_CASE__:   List[str]=      '''
        examples/by_feature/cross_validation.py
        --num_folds 2
        '''.split()
     with mock.patch.dict(os.environ , {'''TESTING_MOCKED_DATALOADERS''': '''0'''}   ):
      SCREAMING_SNAKE_CASE__:   Any=      run_command(self._launch_args + testargs , return_stdout=lowerCAmelCase   )
      SCREAMING_SNAKE_CASE__:   Dict=      re.findall('''({.+})''' , lowerCAmelCase   )
      SCREAMING_SNAKE_CASE__:   int=      [r for r in results if '''accuracy''' in r][-1]
      SCREAMING_SNAKE_CASE__:   List[str]=      ast.literal_eval(lowerCAmelCase   )
      self.assertGreaterEqual(results['''accuracy'''] , 0.75   )
    def 						UpperCamelCase_				(  self   )       ->					List[Any]:
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      ['''examples/by_feature/multi_process_metrics.py''']
     run_command(self._launch_args + testargs   )
    @require_trackers
    @mock.patch.dict(os.environ , {'''WANDB_MODE''': '''offline'''}   )
    def 						UpperCamelCase_				(  self   )       ->					int:
     with tempfile.TemporaryDirectory() as tmpdir:
      SCREAMING_SNAKE_CASE__:   Optional[int]=      f'\n            examples/by_feature/tracking.py\n            --with_tracking\n            --project_dir {tmpdir}\n            '.split()
      run_command(self._launch_args + testargs   )
      self.assertTrue(os.path.exists(os.path.join(lowerCAmelCase , '''tracking'''   )   )   )
    def 						UpperCamelCase_				(  self   )       ->					int:
     SCREAMING_SNAKE_CASE__:   int=      ['''examples/by_feature/gradient_accumulation.py''']
     run_command(self._launch_args + testargs   )



    def 						UpperCamelCase_				(  self   )       ->					List[Any]:
     SCREAMING_SNAKE_CASE__:   List[str]=      ['''examples/by_feature/local_sgd.py''']
     run_command(self._launch_args + testargs   )

