

'''simple docstring'''

import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
    from PIL import Image

    from transformers import AlignProcessor, EfficientNetImageProcessor





@require_vision
class 				_snake_case			(							unittest.TestCase    ):
   def      __UpperCamelCase    (			self      :   Optional[Any]				):
     SCREAMING_SNAKE_CASE:Optional[int]       =       tempfile.mkdtemp()

     SCREAMING_SNAKE_CASE:Any       =       [
         """[UNK]""",
         """[CLS]""",
         """[SEP]""",
         """[PAD]""",
         """[MASK]""",
         """want""",
         """##want""",
         """##ed""",
         """wa""",
         """un""",
         """runn""",
         """##ing""",
         """,""",
         """low""",
         """lowest""",
     ]
     SCREAMING_SNAKE_CASE:Any       =       os.path.join(self.tmpdirname  ,VOCAB_FILES_NAMES["vocab_file"]				)
     with open(self.vocab_file  ,"w"  ,encoding="utf-8"				) as vocab_writer:
       vocab_writer.write("".join([x + "\n" for x in vocab_tokens]				)				)

     SCREAMING_SNAKE_CASE:Any       =       {
         """do_resize""": True,
         """size""": 20,
         """do_center_crop""": True,
         """crop_size""": 18,
         """do_normalize""": True,
         """image_mean""": [0.48_145_466, 0.4_578_275, 0.40_821_073],
         """image_std""": [0.26_862_954, 0.26_130_258, 0.27_577_711],
     }
     SCREAMING_SNAKE_CASE:List[str]       =       os.path.join(self.tmpdirname  ,snake_case_				)
     with open(self.image_processor_file  ,"w"  ,encoding="utf-8"				) as fp:
       json.dump(snake_case_  ,snake_case_				)
   def      __UpperCamelCase    (			self      :   Dict  ,**SCREAMING_SNAKE_CASE__      :   List[str]				):
     return BertTokenizer.from_pretrained(self.tmpdirname  ,**snake_case_				)
   def      __UpperCamelCase    (			self      :   int  ,**SCREAMING_SNAKE_CASE__      :   Tuple				):
     return BertTokenizerFast.from_pretrained(self.tmpdirname  ,**snake_case_				)
   def      __UpperCamelCase    (			self      :   Any  ,**SCREAMING_SNAKE_CASE__      :   Tuple				):
     return EfficientNetImageProcessor.from_pretrained(self.tmpdirname  ,**snake_case_				)
   def      __UpperCamelCase    (			self      :   List[Any]				):
     shutil.rmtree(self.tmpdirname				)
   def      __UpperCamelCase    (			self      :   Any				):
     SCREAMING_SNAKE_CASE:str       =       [np.random.randint(255  ,size=(3, 30, 400)  ,dtype=np.uinta				)]
     SCREAMING_SNAKE_CASE:int       =       [Image.fromarray(np.moveaxis(snake_case_  ,0  ,-1				)				) for x in image_inputs]
     return image_inputs
   def      __UpperCamelCase    (			self      :   Tuple				):
     SCREAMING_SNAKE_CASE:List[str]       =       self.get_tokenizer()
     SCREAMING_SNAKE_CASE:Dict       =       self.get_rust_tokenizer()
     SCREAMING_SNAKE_CASE:List[Any]       =       self.get_image_processor()

     SCREAMING_SNAKE_CASE:str       =       AlignProcessor(tokenizer=snake_case_  ,image_processor=snake_case_				)
     processor_slow.save_pretrained(self.tmpdirname				)
     SCREAMING_SNAKE_CASE:Tuple       =       AlignProcessor.from_pretrained(self.tmpdirname  ,use_fast=snake_case_				)

     SCREAMING_SNAKE_CASE:Any       =       AlignProcessor(tokenizer=snake_case_  ,image_processor=snake_case_				)
     processor_fast.save_pretrained(self.tmpdirname				)
     SCREAMING_SNAKE_CASE:Optional[int]       =       AlignProcessor.from_pretrained(self.tmpdirname				)

     self.assertEqual(processor_slow.tokenizer.get_vocab()  ,tokenizer_slow.get_vocab()				)
     self.assertEqual(processor_fast.tokenizer.get_vocab()  ,tokenizer_fast.get_vocab()				)
     self.assertEqual(tokenizer_slow.get_vocab()  ,tokenizer_fast.get_vocab()				)
     self.assertIsInstance(processor_slow.tokenizer  ,snake_case_				)
     self.assertIsInstance(processor_fast.tokenizer  ,snake_case_				)

     self.assertEqual(processor_slow.image_processor.to_json_string()  ,image_processor.to_json_string()				)
     self.assertEqual(processor_fast.image_processor.to_json_string()  ,image_processor.to_json_string()				)
     self.assertIsInstance(processor_slow.image_processor  ,snake_case_				)
     self.assertIsInstance(processor_fast.image_processor  ,snake_case_				)
   def      __UpperCamelCase    (			self      :   Optional[int]				):
     SCREAMING_SNAKE_CASE:Dict       =       AlignProcessor(tokenizer=self.get_tokenizer()  ,image_processor=self.get_image_processor()				)
     processor.save_pretrained(self.tmpdirname				)

     SCREAMING_SNAKE_CASE:Optional[Any]       =       self.get_tokenizer(bos_token="(BOS)"  ,eos_token="(EOS)"				)
     SCREAMING_SNAKE_CASE:Dict       =       self.get_image_processor(do_normalize=snake_case_  ,padding_value=1.0				)

     SCREAMING_SNAKE_CASE:Optional[int]       =       AlignProcessor.from_pretrained(
         self.tmpdirname  ,bos_token="(BOS)"  ,eos_token="(EOS)"  ,do_normalize=snake_case_  ,padding_value=1.0				)

     self.assertEqual(processor.tokenizer.get_vocab()  ,tokenizer_add_kwargs.get_vocab()				)
     self.assertIsInstance(processor.tokenizer  ,snake_case_				)

     self.assertEqual(processor.image_processor.to_json_string()  ,image_processor_add_kwargs.to_json_string()				)
     self.assertIsInstance(processor.image_processor  ,snake_case_				)
   def      __UpperCamelCase    (			self      :   Optional[Any]				):
     SCREAMING_SNAKE_CASE:int       =       self.get_image_processor()
     SCREAMING_SNAKE_CASE:List[str]       =       self.get_tokenizer()

     SCREAMING_SNAKE_CASE:str       =       AlignProcessor(tokenizer=snake_case_  ,image_processor=snake_case_				)

     SCREAMING_SNAKE_CASE:Tuple       =       self.prepare_image_inputs()

     SCREAMING_SNAKE_CASE:int       =       image_processor(snake_case_  ,return_tensors="np"				)
     SCREAMING_SNAKE_CASE:str       =       processor(images=snake_case_  ,return_tensors="np"				)

     for key in input_image_proc.keys():
       self.assertAlmostEqual(input_image_proc[key].sum()  ,input_processor[key].sum()  ,delta=1e-2				)
   def      __UpperCamelCase    (			self      :   Optional[int]				):
     SCREAMING_SNAKE_CASE:Union[str, Any]       =       self.get_image_processor()
     SCREAMING_SNAKE_CASE:Tuple       =       self.get_tokenizer()

     SCREAMING_SNAKE_CASE:List[Any]       =       AlignProcessor(tokenizer=snake_case_  ,image_processor=snake_case_				)

     SCREAMING_SNAKE_CASE:Optional[int]       =       """lower newer"""

     SCREAMING_SNAKE_CASE:int       =       processor(text=snake_case_				)

     SCREAMING_SNAKE_CASE:List[str]       =       tokenizer(snake_case_  ,padding="max_length"  ,max_length=64				)

     for key in encoded_tok.keys():
       self.assertListEqual(encoded_tok[key]  ,encoded_processor[key]				)
   def      __UpperCamelCase    (			self      :   Optional[int]				):
     SCREAMING_SNAKE_CASE:str       =       self.get_image_processor()
     SCREAMING_SNAKE_CASE:List[Any]       =       self.get_tokenizer()

     SCREAMING_SNAKE_CASE:Dict       =       AlignProcessor(tokenizer=snake_case_  ,image_processor=snake_case_				)

     SCREAMING_SNAKE_CASE:Tuple       =       """lower newer"""
     SCREAMING_SNAKE_CASE:Dict       =       self.prepare_image_inputs()

     SCREAMING_SNAKE_CASE:List[str]       =       processor(text=snake_case_  ,images=snake_case_				)

     self.assertListEqual(list(inputs.keys()				)  ,["input_ids", "token_type_ids", "attention_mask", "pixel_values"]				)

     # test if it raises when no input is passed
     with pytest.raises(snake_case_				):
       processor()
   def      __UpperCamelCase    (			self      :   Optional[int]				):
     SCREAMING_SNAKE_CASE:List[str]       =       self.get_image_processor()
     SCREAMING_SNAKE_CASE:List[str]       =       self.get_tokenizer()

     SCREAMING_SNAKE_CASE:List[Any]       =       AlignProcessor(tokenizer=snake_case_  ,image_processor=snake_case_				)

     SCREAMING_SNAKE_CASE:Dict       =       [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

     SCREAMING_SNAKE_CASE:Optional[int]       =       processor.batch_decode(snake_case_				)
     SCREAMING_SNAKE_CASE:List[Any]       =       tokenizer.batch_decode(snake_case_				)

     self.assertListEqual(snake_case_  ,snake_case_				)







   def      __UpperCamelCase    (			self      :   Optional[int]				):
     SCREAMING_SNAKE_CASE:Tuple       =       self.get_image_processor()
     SCREAMING_SNAKE_CASE:Tuple       =       self.get_tokenizer()

     SCREAMING_SNAKE_CASE:Optional[Any]       =       AlignProcessor(tokenizer=snake_case_  ,image_processor=snake_case_				)

     SCREAMING_SNAKE_CASE:Tuple       =       """lower newer"""
     SCREAMING_SNAKE_CASE:Optional[int]       =       self.prepare_image_inputs()

     SCREAMING_SNAKE_CASE:List[Any]       =       processor(text=snake_case_  ,images=snake_case_				)

     self.assertListEqual(list(inputs.keys()				)  ,processor.model_input_names				)








import os
import sys
import tempfile

import torch

from .state import AcceleratorState
from .utils import PrecisionType, PrepareForLaunch, is_mps_available, patch_environment







def 							A__       (    lowerCamelCase  ,     lowerCamelCase=()  ,     lowerCamelCase=None  ,     lowerCamelCase="no"  ,     lowerCamelCase="29500"    )	->						List[Any]:
				UpperCamelCase_:				Any						=    False
				UpperCamelCase_:				int						=    False
				if any(key.startswith("""KAGGLE"""    ) for key in os.environ.keys()    ):
								UpperCamelCase_:				str						=    True
				elif "IPython" in sys.modules:
								UpperCamelCase_:				Tuple						=    """google.colab""" in str(sys.modules["""IPython"""].get_ipython()    )

				try:
								UpperCamelCase_:				Union[str, Any]						=    PrecisionType(mixed_precision.lower()    )
				except ValueError:
								raise ValueError(
								    F'''Unknown mixed_precision mode: {args.mixed_precision.lower()}. Choose between {PrecisionType.list()}.'''    )

				if (in_colab or in_kaggle) and (os.environ.get("""TPU_NAME"""  ,     lowerCamelCase    ) is not None):
								# TPU launch
								import torch_xla.distributed.xla_multiprocessing as xmp

								if len(AcceleratorState._shared_state    ) > 0:
												raise ValueError(
												    """To train on TPU in Colab or Kaggle Kernel, the `Accelerator` should only be initialized inside """
												    """your training function. Restart your notebook and make sure no cells initializes an """
												    """`Accelerator`."""    )
								if num_processes is None:
												UpperCamelCase_:				str						=    8

								UpperCamelCase_:				int						=    PrepareForLaunch(lowerCamelCase  ,     distributed_type="""TPU"""    )
								print(F'''Launching a training on {num_processes} TPU cores.'''    )
								xmp.spawn(lowerCamelCase  ,     args=lowerCamelCase  ,     nprocs=lowerCamelCase  ,     start_method="""fork"""    )
				elif in_colab:
								# No need for a distributed launch otherwise as it's either CPU or one GPU.
								if torch.cuda.is_available():
												print("""Launching training on one GPU."""    )
								else:
												print("""Launching training on one CPU."""    )
								function(*lowerCamelCase    )
				else:
								if num_processes is None:
												raise ValueError(
												    """You have to specify the number of GPUs you would like to use, add `num_processes=...` to your call."""    )

								if num_processes > 1:
												# Multi-GPU launch
												from torch.multiprocessing import start_processes
												from torch.multiprocessing.spawn import ProcessRaisedException

												if len(AcceleratorState._shared_state    ) > 0:
																raise ValueError(
																    """To launch a multi-GPU training from your notebook, the `Accelerator` should only be initialized """
																    """inside your training function. Restart your notebook and make sure no cells initializes an """
																    """`Accelerator`."""    )

												if torch.cuda.is_initialized():
																raise ValueError(
																    """To launch a multi-GPU training from your notebook, you need to avoid running any instruction """
																    """using `torch.cuda` in any cell. Restart your notebook and make sure no cells use any CUDA """
																    """function."""    )

												# torch.distributed will expect a few environment variable to be here. We set the ones common to each
												# process here (the other ones will be set be the launcher).
												with patch_environment(
												    world_size=lowerCamelCase  ,     master_addr="""127.0.01"""  ,     master_port=lowerCamelCase  ,     mixed_precision=lowerCamelCase    ):
																UpperCamelCase_:				Optional[int]						=    PrepareForLaunch(lowerCamelCase  ,     distributed_type="""MULTI_GPU"""    )
																print(F'''Launching training on {num_processes} GPUs.'''    )
																try:
																				start_processes(lowerCamelCase  ,     args=lowerCamelCase  ,     nprocs=lowerCamelCase  ,     start_method="""fork"""    )
																except ProcessRaisedException as e:
																				if "Cannot re-initialize CUDA in forked subprocess" in e.args[0]:
																								raise RuntimeError(
																								    """CUDA has been initialized before the `notebook_launcher` could create a forked subprocess. """
																								    """This likely stems from an outside import causing issues once the `notebook_launcher()` is called. """
																								    """Please review your imports and test them when running the `notebook_launcher()` to identify """
																								    """which one is problematic."""    ) from e

								else:
												# No need for a distributed launch otherwise as it's either CPU, GPU or MPS.
												if is_mps_available():
																UpperCamelCase_:				List[Any]						=    """1"""
																print("""Launching training on MPS."""    )
												elif torch.cuda.is_available():
																print("""Launching training on one GPU."""    )
												else:
																print("""Launching training on CPU."""    )
												function(*lowerCamelCase    )







def 							A__       (    lowerCamelCase  ,     lowerCamelCase=()  ,     lowerCamelCase=2    )	->						List[str]:
				from torch.multiprocessing import start_processes

				with tempfile.NamedTemporaryFile() as tmp_file:
								# torch.distributed will expect a few environment variable to be here. We set the ones common to each
								# process here (the other ones will be set be the launcher).
								with patch_environment(
								    world_size=lowerCamelCase  ,     master_addr="""127.0.01"""  ,     master_port="""29500"""  ,     accelerate_mixed_precision="""no"""  ,     accelerate_debug_rdv_file=tmp_file.name  ,     accelerate_use_cpu="""yes"""  ,     ):
												UpperCamelCase_:				str						=    PrepareForLaunch(lowerCamelCase  ,     debug=lowerCamelCase    )
												start_processes(lowerCamelCase  ,     args=lowerCamelCase  ,     nprocs=lowerCamelCase  ,     start_method="""fork"""    )






