


'''simple docstring'''







import inspect
import unittest

from transformers import BitConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
       import torch
       from torch import nn

       from transformers import BitBackbone, BitForImageClassification, BitImageProcessor, BitModel
       from transformers.models.bit.modeling_bit import BIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       from PIL import Image
class 						UpperCamelCase__       :



     """simple docstring"""






     def __init__(			self   ,						snake_case__   ,						snake_case__=3   ,						snake_case__=32   ,						snake_case__=3   ,						snake_case__=10   ,						snake_case__=[8, 16, 32, 64]   ,						snake_case__=[1, 1, 2, 1]   ,						snake_case__=True   ,						snake_case__=True   ,						snake_case__="relu"   ,						snake_case__=3   ,						snake_case__=None   ,						snake_case__=["stage2", "stage3", "stage4"]   ,						snake_case__=[2, 3, 4]   ,						snake_case__=1   ,						):

            '''simple docstring'''

            _lowerCAmelCase     :				Optional[int]       =							parent
            _lowerCAmelCase     :				Union[str, Any]       =							batch_size
            _lowerCAmelCase     :				Union[str, Any]       =							image_size
            _lowerCAmelCase     :				List[str]       =							num_channels
            _lowerCAmelCase     :				Optional[int]       =							embeddings_size
            _lowerCAmelCase     :				int       =							hidden_sizes
            _lowerCAmelCase     :				List[Any]       =							depths
            _lowerCAmelCase     :				int       =							is_training
            _lowerCAmelCase     :				Union[str, Any]       =							use_labels
            _lowerCAmelCase     :				List[str]       =							hidden_act
            _lowerCAmelCase     :				int       =							num_labels
            _lowerCAmelCase     :				Union[str, Any]       =							scope
            _lowerCAmelCase     :				int       =							len(__UpperCAmelCase )
            _lowerCAmelCase     :				List[Any]       =							out_features
            _lowerCAmelCase     :				Union[str, Any]       =							out_indices
            _lowerCAmelCase     :				Optional[Any]       =							num_groups







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Tuple       =							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

            _lowerCAmelCase     :				Union[str, Any]       =							None
            if self.use_labels:
                   _lowerCAmelCase     :				str       =							ids_tensor([self.batch_size]   ,						self.num_labels )

            _lowerCAmelCase     :				Dict       =							self.get_config()

            return config, pixel_values, labels







     def a						(			self ):

            '''simple docstring'''

            return BitConfig(
                num_channels=self.num_channels   ,						embeddings_size=self.embeddings_size   ,						hidden_sizes=self.hidden_sizes   ,						depths=self.depths   ,						hidden_act=self.hidden_act   ,						num_labels=self.num_labels   ,						out_features=self.out_features   ,						out_indices=self.out_indices   ,						num_groups=self.num_groups   ,						)







     def a						(			self   ,						snake_case__   ,						snake_case__   ,						snake_case__ ):

            '''simple docstring'''

            _lowerCAmelCase     :				Any       =							BitModel(config=__UpperCAmelCase )
            model.to(__UpperCAmelCase )
            model.eval()
            _lowerCAmelCase     :				Union[str, Any]       =							model(__UpperCAmelCase )
            self.parent.assertEqual(
                result.last_hidden_state.shape   ,						(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)   ,						)







     def a						(			self   ,						snake_case__   ,						snake_case__   ,						snake_case__ ):

            '''simple docstring'''

            _lowerCAmelCase     :				Union[str, Any]       =							self.num_labels
            _lowerCAmelCase     :				Optional[int]       =							BitForImageClassification(__UpperCAmelCase )
            model.to(__UpperCAmelCase )
            model.eval()
            _lowerCAmelCase     :				List[str]       =							model(__UpperCAmelCase   ,						labels=__UpperCAmelCase )
            self.parent.assertEqual(result.logits.shape   ,						(self.batch_size, self.num_labels) )







     def a						(			self   ,						snake_case__   ,						snake_case__   ,						snake_case__ ):

            '''simple docstring'''

            _lowerCAmelCase     :				List[str]       =							BitBackbone(config=__UpperCAmelCase )
            model.to(__UpperCAmelCase )
            model.eval()
            _lowerCAmelCase     :				Optional[int]       =							model(__UpperCAmelCase )

            # verify feature maps
            self.parent.assertEqual(len(result.feature_maps )   ,						len(config.out_features ) )
            self.parent.assertListEqual(list(result.feature_maps[0].shape )   ,						[self.batch_size, self.hidden_sizes[1], 4, 4] )

            # verify channels
            self.parent.assertEqual(len(model.channels )   ,						len(config.out_features ) )
            self.parent.assertListEqual(model.channels   ,						config.hidden_sizes[1:] )

            # verify backbone works with out_features=None
            _lowerCAmelCase     :				Optional[Any]       =							None
            _lowerCAmelCase     :				str       =							BitBackbone(config=__UpperCAmelCase )
            model.to(__UpperCAmelCase )
            model.eval()
            _lowerCAmelCase     :				Any       =							model(__UpperCAmelCase )

            # verify feature maps
            self.parent.assertEqual(len(result.feature_maps )   ,						1 )
            self.parent.assertListEqual(list(result.feature_maps[0].shape )   ,						[self.batch_size, self.hidden_sizes[-1], 1, 1] )

            # verify channels
            self.parent.assertEqual(len(model.channels )   ,						1 )
            self.parent.assertListEqual(model.channels   ,						[config.hidden_sizes[-1]] )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				str       =							self.prepare_config_and_inputs()
            _lowerCAmelCase     :				str       =							config_and_inputs
            _lowerCAmelCase     :				Union[str, Any]       =							{"pixel_values": pixel_values}
            return config, inputs_dict
@require_torch
class 						UpperCamelCase__       (			SCREAMING_SNAKE_CASE_     ,       SCREAMING_SNAKE_CASE_     ,       unittest.TestCase						):



     """simple docstring"""






     __magic_name__           =     (BitModel, BitForImageClassification, BitBackbone) if is_torch_available() else ()
     __magic_name__           =     (
         {"feature-extraction": BitModel, "image-classification": BitForImageClassification}
         if is_torch_available()
         else {}
     )

     __magic_name__           =     False
     __magic_name__           =     False
     __magic_name__           =     False
     __magic_name__           =     False
     __magic_name__           =     False







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Optional[Any]       =							BitModelTester(self )
            _lowerCAmelCase     :				Union[str, Any]       =							ConfigTester(self   ,						config_class=__UpperCAmelCase   ,						has_text_modality=__UpperCAmelCase )







     def a						(			self ):

            '''simple docstring'''

            self.create_and_test_config_common_properties()
            self.config_tester.create_and_test_config_to_json_string()
            self.config_tester.create_and_test_config_to_json_file()
            self.config_tester.create_and_test_config_from_and_save_pretrained()
            self.config_tester.create_and_test_config_with_num_labels()
            self.config_tester.check_config_can_be_init_without_params()
            self.config_tester.check_config_arguments_init()







     def a						(			self ):

            '''simple docstring'''

            return







     @unittest.skip(reason='Bit does not output attentions' )
     def a						(			self ):

            '''simple docstring'''

            pass







     @unittest.skip(reason='Bit does not use inputs_embeds' )
     def a						(			self ):

            '''simple docstring'''

            pass







     @unittest.skip(reason='Bit does not support input and output embeddings' )
     def a						(			self ):

            '''simple docstring'''

            pass







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Optional[Any]       =							self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                   _lowerCAmelCase     :				List[Any]       =							model_class(__UpperCAmelCase )
                   _lowerCAmelCase     :				str       =							inspect.signature(model.forward )
                   # signature.parameters is an OrderedDict => so arg_names order is deterministic
                   _lowerCAmelCase     :				int       =							[*signature.parameters.keys()]

                   _lowerCAmelCase     :				Union[str, Any]       =							["pixel_values"]
                   self.assertListEqual(arg_names[:1]   ,						__UpperCAmelCase )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Optional[Any]       =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*__UpperCAmelCase )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				List[str]       =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_backbone(*__UpperCAmelCase )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				str       =							self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                   _lowerCAmelCase     :				List[str]       =							model_class(config=__UpperCAmelCase )
                   for name, module in model.named_modules():
                          if isinstance(__UpperCAmelCase   ,						(nn.BatchNormad, nn.GroupNorm) ):
                                 self.assertTrue(
                                     torch.all(module.weight == 1 )   ,						msg=F'Parameter {name} of model {model_class} seems not properly initialized'   ,						)
                                 self.assertTrue(
                                     torch.all(module.bias == 0 )   ,						msg=F'Parameter {name} of model {model_class} seems not properly initialized'   ,						)







     def a						(			self ):

            '''simple docstring'''

            def check_hidden_states_output(snake_case__   ,						snake_case__   ,						snake_case__ ):
                   _lowerCAmelCase     :				Optional[int]       =							model_class(__UpperCAmelCase )
                   model.to(__UpperCAmelCase )
                   model.eval()

                   with torch.no_grad():
                          _lowerCAmelCase     :				Union[str, Any]       =							model(**self._prepare_for_class(__UpperCAmelCase   ,						__UpperCAmelCase ) )

                   _lowerCAmelCase     :				str       =							outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                   _lowerCAmelCase     :				Optional[Any]       =							self.model_tester.num_stages
                   self.assertEqual(len(__UpperCAmelCase )   ,						expected_num_stages + 1 )

                   # Bit's feature maps are of shape (batch_size, num_channels, height, width)
                   self.assertListEqual(
                       list(hidden_states[0].shape[-2:] )   ,						[self.model_tester.image_size // 4, self.model_tester.image_size // 4]   ,						)

            _lowerCAmelCase     :				Union[str, Any]       =							self.model_tester.prepare_config_and_inputs_for_common()
            _lowerCAmelCase     :				Optional[Any]       =							["preactivation", "bottleneck"]
            for model_class in self.all_model_classes:
                   for layer_type in layers_type:
                          _lowerCAmelCase     :				List[Any]       =							layer_type
                          _lowerCAmelCase     :				str       =							True
                          check_hidden_states_output(__UpperCAmelCase   ,						__UpperCAmelCase   ,						__UpperCAmelCase )

                          # check that output_hidden_states also work using config
                          del inputs_dict["output_hidden_states"]
                          _lowerCAmelCase     :				Optional[int]       =							True

                          check_hidden_states_output(__UpperCAmelCase   ,						__UpperCAmelCase   ,						__UpperCAmelCase )







     @unittest.skip(reason='Bit does not use feedforward chunking' )
     def a						(			self ):

            '''simple docstring'''

            pass







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Optional[Any]       =							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase )







     @slow
     def a						(			self ):

            '''simple docstring'''

            for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   _lowerCAmelCase     :				List[Any]       =							BitModel.from_pretrained(__UpperCAmelCase )
                   self.assertIsNotNone(__UpperCAmelCase )






def       lowercase					():







       """simple docstring"""







       _lowerCAmelCase     :				str       =							Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'       )
       return image
@require_torch
@require_vision
class 						UpperCamelCase__       (			unittest.TestCase						):



     """simple docstring"""






     @cached_property
     def a						(			self ):

            '''simple docstring'''

            return (
                BitImageProcessor.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None
            )







     @slow
     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Optional[Any]       =							BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(__UpperCAmelCase )

            _lowerCAmelCase     :				Tuple       =							self.default_image_processor
            _lowerCAmelCase     :				List[Any]       =							prepare_img()
            _lowerCAmelCase     :				Dict       =							image_processor(images=__UpperCAmelCase   ,						return_tensors='pt' ).to(__UpperCAmelCase )

            # forward pass
            with torch.no_grad():
                   _lowerCAmelCase     :				Optional[int]       =							model(**__UpperCAmelCase )

            # verify the logits
            _lowerCAmelCase     :				Any       =							torch.Size((1, 1000) )
            self.assertEqual(outputs.logits.shape   ,						__UpperCAmelCase )

            _lowerCAmelCase     :				str       =							torch.tensor([[-0.6526, -0.5263, -1.4398]] ).to(__UpperCAmelCase )

            self.assertTrue(torch.allclose(outputs.logits[0, :3]   ,						__UpperCAmelCase   ,						atol=1E-4 ) )
@require_torch
class 						UpperCamelCase__       (			SCREAMING_SNAKE_CASE_     ,       unittest.TestCase						):



     """simple docstring"""






     __magic_name__           =     (BitBackbone,) if is_torch_available() else ()
     __magic_name__           =     BitConfig

     __magic_name__           =     False







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				str       =							BitModelTester(self )






import argparse
import os

import transformers

from .convert_slow_tokenizer import SLOW_TO_FAST_CONVERTERS
from .utils import logging


logging.set_verbosity_info()

lowerCamelCase		       =      logging.get_logger(__name__)


lowerCamelCase		       =      {name: getattr(transformers, name + """Fast""") for name in SLOW_TO_FAST_CONVERTERS}






def     SCREAMING_SNAKE_CASE(      __UpperCamelCase	,				__UpperCamelCase	,				__UpperCamelCase	,				__UpperCamelCase )	->						Dict:
				if tokenizer_name is not None and tokenizer_name not in TOKENIZER_CLASSES:
								raise ValueError(F'Unrecognized tokenizer name, should be one of {list(TOKENIZER_CLASSES.keys() )}.' )

				if tokenizer_name is None:
								a__	:							str      						= TOKENIZER_CLASSES
				else:
								a__	:							int      						= {tokenizer_name: getattr(__UpperCamelCase	,				tokenizer_name + "Fast" )}

				logger.info(F'Loading tokenizer classes: {tokenizer_names}' )

				for tokenizer_name in tokenizer_names:
								a__	:							Any      						= TOKENIZER_CLASSES[tokenizer_name]

								a__	:							Dict      						= True
								if checkpoint_name is None:
												a__	:							Tuple      						= list(tokenizer_class.max_model_input_sizes.keys() )
								else:
												a__	:							List[str]      						= [checkpoint_name]

								logger.info(F'For tokenizer {tokenizer_class.__class__.__name__} loading checkpoints: {checkpoint_names}' )

								for checkpoint in checkpoint_names:
												logger.info(F'Loading {tokenizer_class.__class__.__name__} {checkpoint}' )

												# Load tokenizer
												a__	:							List[str]      						= tokenizer_class.from_pretrained(__UpperCamelCase	,				force_download=__UpperCamelCase )

												# Save fast tokenizer
												logger.info(F'Save fast tokenizer to {dump_path} with prefix {checkpoint} add_prefix {add_prefix}' )

												# For organization names we create sub-directories
												if "/" in checkpoint:
																a__						,   a__	:							Dict      						= checkpoint.split("/" )
																a__	:							Any      						= os.path.join(__UpperCamelCase	,				__UpperCamelCase )
												elif add_prefix:
																a__	:							int      						= checkpoint
																a__	:							Optional[Any]      						= dump_path
												else:
																a__	:							Dict      						= None
																a__	:							Optional[Any]      						= dump_path

												logger.info(F'=> {dump_path_full} with prefix {checkpoint_prefix_name}, add_prefix {add_prefix}' )

												if checkpoint in list(tokenizer.pretrained_vocab_files_map.values() )[0]:
																a__	:							Dict      						= list(tokenizer.pretrained_vocab_files_map.values() )[0][checkpoint]
																a__	:							Optional[Any]      						= file_path.split(__UpperCamelCase )[-1][0]
																if next_char == "/":
																				a__	:							Union[str, Any]      						= os.path.join(__UpperCamelCase	,				__UpperCamelCase )
																				a__	:							Any      						= None

																logger.info(F'=> {dump_path_full} with prefix {checkpoint_prefix_name}, add_prefix {add_prefix}' )

												a__	:							Any      						= tokenizer.save_pretrained(
												    __UpperCamelCase	,				legacy_format=__UpperCamelCase	,				filename_prefix=__UpperCamelCase )
												logger.info(F'=> File names {file_names}' )

												for file_name in file_names:
																if not file_name.endswith("tokenizer.json" ):
																				os.remove(__UpperCamelCase )
																				logger.info(F'=> removing {file_name}' )


if __name__ == "__main__":
						lowerCamelCase		       =      argparse.ArgumentParser()
						# Required parameters
						parser.add_argument(
						    """--dump_path""", default=None, type=str, required=True, help="""Path to output generated fast tokenizer files."""
						)
						parser.add_argument(
						    """--tokenizer_name""",
						    default=None,
						    type=str,
						    help=(
						        F'Optional tokenizer type selected in the list of {list(TOKENIZER_CLASSES.keys())}. If not given, will '
						        """download and convert all the checkpoints from AWS."""
						    ),
						)
						parser.add_argument(
						    """--checkpoint_name""",
						    default=None,
						    type=str,
						    help="""Optional checkpoint name. If not given, will download and convert the canonical checkpoints from AWS.""",
						)
						parser.add_argument(
						    """--force_download""",
						    action="""store_true""",
						    help="""Re-download checkpoints.""",
						)
						lowerCamelCase		       =      parser.parse_args()

						convert_slow_checkpoint_to_fast(args.tokenizer_name, args.checkpoint_name, args.dump_path, args.force_download)


