

import operator as op


_a		       =   '''scaler.pt'''
_a		       =   '''pytorch_model'''
_a		       =   '''random_states'''
_a		       =   '''optimizer'''
_a		       =   '''scheduler'''
_a		       =   '''pytorch_model.bin'''
_a		       =   '''pytorch_model.bin.index.json'''
_a		       =   '''model.safetensors'''
_a		       =   '''model.safetensors.index.json'''
_a		       =   '''1.10.2'''
_a		       =   '''py38'''
_a		       =   '''4.17.0'''
_a		       =   ['''ml.p3.16xlarge''', '''ml.p3dn.24xlarge''', '''ml.p4dn.24xlarge''']
_a		       =   ['''FULL_SHARD''', '''SHARD_GRAD_OP''', '''NO_SHARD''', '''HYBRID_SHARD''', '''HYBRID_SHARD_ZERO2''']
_a		       =   ['''TRANSFORMER_BASED_WRAP''', '''SIZE_BASED_WRAP''', '''NO_WRAP''']
_a		       =   ['''BACKWARD_PRE''', '''BACKWARD_POST''', '''NO_PREFETCH''']
_a		       =   ['''FULL_STATE_DICT''', '''LOCAL_STATE_DICT''', '''SHARDED_STATE_DICT''']
_a		       =   '''2.0.1'''
_a		       =   ['''pdsh''', '''standard''', '''openmpi''', '''mvapich''']
_a		       =   ['''default''', '''reduce-overhead''', '''max-autotune''']

_a		       =   {'''>''': op.gt, '''>=''': op.ge, '''==''': op.eq, '''!=''': op.ne, '''<=''': op.le, '''<''': op.lt}

# These are the args for `torch.distributed.launch` for pytorch < 1.9
_a		       =   [
    '''nnodes''',
    '''nproc_per_node''',
    '''rdzv_backend''',
    '''rdzv_endpoint''',
    '''rdzv_id''',
    '''rdzv_conf''',
    '''standalone''',
    '''max_restarts''',
    '''monitor_interval''',
    '''start_method''',
    '''role''',
    '''module''',
    '''m''',
    '''no_python''',
    '''run_path''',
    '''log_dir''',
    '''r''',
    '''redirects''',
    '''t''',
    '''tee''',
    '''node_rank''',
    '''master_addr''',
    '''master_port''',
]

_a		       =   ['''DEEPSPEED''', '''MULTI_GPU''', '''FSDP''', '''MEGATRON_LM''']
_a		       =   ['''DEEPSPEED''', '''MULTI_XPU''', '''FSDP''']













'''simple docstring'''







import json
import os
import shutil
import tempfile
import unittest

import numpy as np

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


if is_vision_available():
			from PIL import Image

			from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor

@require_tokenizers
@require_vision
class _lowercase	(				unittest.TestCase		):




							def 		lowerCamelCase_				(							self:				Dict     ):
														lowerCamelCase__					:	int         =       tempfile.mkdtemp()

														# fmt: off
														lowerCamelCase__					:	int         =       ["""[UNK]""", """[CLS]""", """[SEP]""", """[PAD]""", """[MASK]""", """want""", """##want""", """##ed""", """wa""", """un""", """runn""", """##ing""", """,""", """low""", """lowest"""]
														# fmt: on
														lowerCamelCase__					:	Union[str, 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]     )     )

														lowerCamelCase__					:	Tuple         =       {
														    """do_resize""": True,
														    """size""": {"""height""": 18, """width""": 18},
														    """do_normalize""": True,
														    """image_mean""": [0.5, 0.5, 0.5],
														    """image_std""": [0.5, 0.5, 0.5],
														}
														lowerCamelCase__					:	Tuple         =       os.path.join(self.tmpdirname			,							UpperCamelCase__     )
														with open(self.image_processor_file			,							"""w"""			,							encoding="""utf-8"""     ) as fp:
																					json.dump(UpperCamelCase__			,							UpperCamelCase__     )




							def 		lowerCamelCase_				(							self:				str			,							**UpperCamelCase__:				List[str]     ):
														return BertTokenizer.from_pretrained(self.tmpdirname			,							**UpperCamelCase__     )




							def 		lowerCamelCase_				(							self:				int			,							**UpperCamelCase__:				Tuple     ):
														return ViTImageProcessor.from_pretrained(self.tmpdirname			,							**UpperCamelCase__     )




							def 		lowerCamelCase_				(							self:				Optional[Any]     ):
														shutil.rmtree(self.tmpdirname     )




							def 		lowerCamelCase_				(							self:				Any     ):
														lowerCamelCase__					:	List[Any]         =       [np.random.randint(255			,							size=(3, 30, 400)			,							dtype=np.uinta     )]

														lowerCamelCase__					:	Tuple         =       [Image.fromarray(np.moveaxis(UpperCamelCase__			,							0			,							-1     )     ) for x in image_inputs]

														return image_inputs




							def 		lowerCamelCase_				(							self:				int     ):
														lowerCamelCase__					:	Optional[Any]         =       self.get_tokenizer()
														lowerCamelCase__					:	Dict         =       self.get_image_processor()

														lowerCamelCase__					:	Optional[Any]         =       VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase__			,							image_processor=UpperCamelCase__     )

														processor.save_pretrained(self.tmpdirname     )
														lowerCamelCase__					:	int         =       VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname     )

														self.assertEqual(processor.tokenizer.get_vocab()			,							tokenizer.get_vocab()     )
														self.assertIsInstance(processor.tokenizer			,							(BertTokenizer, BertTokenizerFast)     )

														self.assertEqual(processor.image_processor.to_json_string()			,							image_processor.to_json_string()     )
														self.assertIsInstance(processor.image_processor			,							UpperCamelCase__     )




							def 		lowerCamelCase_				(							self:				Tuple     ):
														lowerCamelCase__					:	Dict         =       VisionTextDualEncoderProcessor(
														    tokenizer=self.get_tokenizer()			,							image_processor=self.get_image_processor()     )
														processor.save_pretrained(self.tmpdirname     )

														lowerCamelCase__					:	int         =       self.get_tokenizer(bos_token="""(BOS)"""			,							eos_token="""(EOS)"""     )
														lowerCamelCase__					:	List[Any]         =       self.get_image_processor(do_normalize=UpperCamelCase__			,							padding_value=1.0     )

														lowerCamelCase__					:	Tuple         =       VisionTextDualEncoderProcessor.from_pretrained(
														    self.tmpdirname			,							bos_token="""(BOS)"""			,							eos_token="""(EOS)"""			,							do_normalize=UpperCamelCase__			,							padding_value=1.0     )

														self.assertEqual(processor.tokenizer.get_vocab()			,							tokenizer_add_kwargs.get_vocab()     )
														self.assertIsInstance(processor.tokenizer			,							(BertTokenizer, BertTokenizerFast)     )

														self.assertEqual(processor.image_processor.to_json_string()			,							image_processor_add_kwargs.to_json_string()     )
														self.assertIsInstance(processor.image_processor			,							UpperCamelCase__     )




							def 		lowerCamelCase_				(							self:				Union[str, Any]     ):
														lowerCamelCase__					:	Optional[Any]         =       self.get_image_processor()
														lowerCamelCase__					:	Union[str, Any]         =       self.get_tokenizer()

														lowerCamelCase__					:	Any         =       VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase__			,							image_processor=UpperCamelCase__     )

														lowerCamelCase__					:	List[Any]         =       self.prepare_image_inputs()

														lowerCamelCase__					:	List[str]         =       image_processor(UpperCamelCase__			,							return_tensors="""np"""     )
														lowerCamelCase__					:	Optional[Any]         =       processor(images=UpperCamelCase__			,							return_tensors="""np"""     )

														for key in input_feat_extract.keys():
																					self.assertAlmostEqual(input_feat_extract[key].sum()			,							input_processor[key].sum()			,							delta=1e-2     )




							def 		lowerCamelCase_				(							self:				Optional[int]     ):
														lowerCamelCase__					:	Any         =       self.get_image_processor()
														lowerCamelCase__					:	List[str]         =       self.get_tokenizer()

														lowerCamelCase__					:	List[Any]         =       VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase__			,							image_processor=UpperCamelCase__     )

														lowerCamelCase__					:	List[Any]         =       """lower newer"""

														lowerCamelCase__					:	Union[str, Any]         =       processor(text=UpperCamelCase__     )

														lowerCamelCase__					:	Any         =       tokenizer(UpperCamelCase__     )

														for key in encoded_tok.keys():
																					self.assertListEqual(encoded_tok[key]			,							encoded_processor[key]     )




							def 		lowerCamelCase_				(							self:				Dict     ):
														lowerCamelCase__					:	Optional[Any]         =       self.get_image_processor()
														lowerCamelCase__					:	List[Any]         =       self.get_tokenizer()

														lowerCamelCase__					:	List[Any]         =       VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase__			,							image_processor=UpperCamelCase__     )

														lowerCamelCase__					:	Any         =       """lower newer"""
														lowerCamelCase__					:	Dict         =       self.prepare_image_inputs()

														lowerCamelCase__					:	Tuple         =       processor(text=UpperCamelCase__			,							images=UpperCamelCase__     )

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

														# test if it raises when no input is passed
														with self.assertRaises(UpperCamelCase__     ):
																					processor()




							def 		lowerCamelCase_				(							self:				int     ):
														lowerCamelCase__					:	List[str]         =       self.get_image_processor()
														lowerCamelCase__					:	List[str]         =       self.get_tokenizer()

														lowerCamelCase__					:	int         =       VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase__			,							image_processor=UpperCamelCase__     )

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

														lowerCamelCase__					:	Union[str, Any]         =       processor.batch_decode(UpperCamelCase__     )
														lowerCamelCase__					:	Dict         =       tokenizer.batch_decode(UpperCamelCase__     )

														self.assertListEqual(UpperCamelCase__			,							UpperCamelCase__     )




							def 		lowerCamelCase_				(							self:				List[str]     ):
														lowerCamelCase__					:	Any         =       self.get_image_processor()
														lowerCamelCase__					:	Union[str, Any]         =       self.get_tokenizer()

														lowerCamelCase__					:	int         =       VisionTextDualEncoderProcessor(tokenizer=UpperCamelCase__			,							image_processor=UpperCamelCase__     )

														lowerCamelCase__					:	Optional[Any]         =       """lower newer"""
														lowerCamelCase__					:	str         =       self.prepare_image_inputs()

														lowerCamelCase__					:	int         =       processor(text=UpperCamelCase__			,							images=UpperCamelCase__     )

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

