
'''simple docstring'''



from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
				import tensorflow as tf

				from transformers import (
				    TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFXLMForMultipleChoice,
				    TFXLMForQuestionAnsweringSimple,
				    TFXLMForSequenceClassification,
				    TFXLMForTokenClassification,
				    TFXLMModel,
				    TFXLMWithLMHeadModel,
				    XLMConfig,
				)





class      A							:



			def __init__(   self    ,       lowerCamelCase__    ,       )     ->						Dict:




									'''simple docstring'''



									lowercase__								=    parent
									lowercase__								=    13
									lowercase__								=    7
									lowercase__								=    True
									lowercase__								=    True
									lowercase__								=    True
									lowercase__								=    True
									lowercase__								=    True
									lowercase__								=    False
									lowercase__								=    False
									lowercase__								=    False
									lowercase__								=    2
									lowercase__								=    99
									lowercase__								=    0
									lowercase__								=    32
									lowercase__								=    2
									lowercase__								=    4
									lowercase__								=    0.1
									lowercase__								=    0.1
									lowercase__								=    512
									lowercase__								=    16
									lowercase__								=    2
									lowercase__								=    0.02
									lowercase__								=    3
									lowercase__								=    4
									lowercase__								=    "last"
									lowercase__								=    True
									lowercase__								=    None
									lowercase__								=    0



			def A__	(   self    )     ->						Optional[Any]:




									'''simple docstring'''



									lowercase__								=    ids_tensor([self.batch_size, self.seq_length]    ,       self.vocab_size    )
									lowercase__								=    random_attention_mask([self.batch_size, self.seq_length]    ,       dtype=tf.floataa    )

									lowercase__								=    None
									if self.use_input_lengths:
															lowercase__								=    (
															    ids_tensor([self.batch_size]    ,       vocab_size=2    ) + self.seq_length - 2
															)  # small variation of seq_length

									lowercase__								=    None
									if self.use_token_type_ids:
															lowercase__								=    ids_tensor([self.batch_size, self.seq_length]    ,       self.n_langs    )

									lowercase__								=    None
									lowercase__								=    None
									lowercase__								=    None
									if self.use_labels:
															lowercase__								=    ids_tensor([self.batch_size]    ,       self.type_sequence_label_size    )
															lowercase__								=    ids_tensor([self.batch_size, self.seq_length]    ,       self.num_labels    )
															lowercase__								=    ids_tensor([self.batch_size]    ,       2    ,       dtype=tf.floataa    )
															lowercase__								=    ids_tensor([self.batch_size]    ,       self.num_choices    )

									lowercase__								=    XLMConfig(
									    vocab_size=self.vocab_size    ,       n_special=self.n_special    ,       emb_dim=self.hidden_size    ,       n_layers=self.num_hidden_layers    ,       n_heads=self.num_attention_heads    ,       dropout=self.hidden_dropout_prob    ,       attention_dropout=self.attention_probs_dropout_prob    ,       gelu_activation=self.gelu_activation    ,       sinusoidal_embeddings=self.sinusoidal_embeddings    ,       asm=self.asm    ,       causal=self.causal    ,       n_langs=self.n_langs    ,       max_position_embeddings=self.max_position_embeddings    ,       initializer_range=self.initializer_range    ,       summary_type=self.summary_type    ,       use_proj=self.use_proj    ,       bos_token_id=self.bos_token_id    ,       )

									return (
									    config,
									    input_ids,
									    token_type_ids,
									    input_lengths,
									    sequence_labels,
									    token_labels,
									    is_impossible_labels,
									    choice_labels,
									    input_mask,
									)



			def A__	(   self    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       )     ->						List[Any]:




									'''simple docstring'''



									lowercase__								=    TFXLMModel(config=__A    )
									lowercase__								=    {"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids}
									lowercase__								=    model(__A    )

									lowercase__								=    [input_ids, input_mask]
									lowercase__								=    model(__A    )
									self.parent.assertEqual(result.last_hidden_state.shape    ,       (self.batch_size, self.seq_length, self.hidden_size)    )



			def A__	(   self    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       )     ->						Optional[Any]:




									'''simple docstring'''



									lowercase__								=    TFXLMWithLMHeadModel(__A    )

									lowercase__								=    {"input_ids": input_ids, "lengths": input_lengths, "langs": token_type_ids}
									lowercase__								=    model(__A    )

									lowercase__								=    outputs

									self.parent.assertEqual(result.logits.shape    ,       (self.batch_size, self.seq_length, self.vocab_size)    )



			def A__	(   self    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       )     ->						Optional[int]:




									'''simple docstring'''



									lowercase__								=    TFXLMForQuestionAnsweringSimple(__A    )

									lowercase__								=    {"input_ids": input_ids, "lengths": input_lengths}

									lowercase__								=    model(__A    )

									self.parent.assertEqual(result.start_logits.shape    ,       (self.batch_size, self.seq_length)    )
									self.parent.assertEqual(result.end_logits.shape    ,       (self.batch_size, self.seq_length)    )



			def A__	(   self    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       )     ->						str:




									'''simple docstring'''



									lowercase__								=    TFXLMForSequenceClassification(__A    )

									lowercase__								=    {"input_ids": input_ids, "lengths": input_lengths}

									lowercase__								=    model(__A    )

									self.parent.assertEqual(result.logits.shape    ,       (self.batch_size, self.type_sequence_label_size)    )



			def A__	(   self    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       )     ->						Optional[Any]:




									'''simple docstring'''



									lowercase__								=    self.num_labels
									lowercase__								=    TFXLMForTokenClassification(config=__A    )
									lowercase__								=    {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
									lowercase__								=    model(__A    )
									self.parent.assertEqual(result.logits.shape    ,       (self.batch_size, self.seq_length, self.num_labels)    )



			def A__	(   self    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       )     ->						Dict:




									'''simple docstring'''



									lowercase__								=    self.num_choices
									lowercase__								=    TFXLMForMultipleChoice(config=__A    )
									lowercase__								=    tf.tile(tf.expand_dims(__A    ,       1    )    ,       (1, self.num_choices, 1)    )
									lowercase__								=    tf.tile(tf.expand_dims(__A    ,       1    )    ,       (1, self.num_choices, 1)    )
									lowercase__								=    tf.tile(tf.expand_dims(__A    ,       1    )    ,       (1, self.num_choices, 1)    )
									lowercase__								=    {
									    "input_ids": multiple_choice_inputs_ids,
									    "attention_mask": multiple_choice_input_mask,
									    "token_type_ids": multiple_choice_token_type_ids,
									}
									lowercase__								=    model(__A    )
									self.parent.assertEqual(result.logits.shape    ,       (self.batch_size, self.num_choices)    )



			def A__	(   self    )     ->						int:




									'''simple docstring'''



									lowercase__								=    self.prepare_config_and_inputs()
									(
									    lowercase__     
									)								=    config_and_inputs
									lowercase__								=    {
									    "input_ids": input_ids,
									    "token_type_ids": token_type_ids,
									    "langs": token_type_ids,
									    "lengths": input_lengths,
									}
									return config, inputs_dict





@require_tf
class      A							(    __UpperCAmelCase						,  __UpperCAmelCase						,  unittest.TestCase			):
			lowerCamelCase							:							List[str]    						=					(
			    (
			        TFXLMModel,
			        TFXLMWithLMHeadModel,
			        TFXLMForSequenceClassification,
			        TFXLMForQuestionAnsweringSimple,
			        TFXLMForTokenClassification,
			        TFXLMForMultipleChoice,
			    )
			    if is_tf_available()
			    else ()
			)
			lowerCamelCase							:							int    						=					(
			    (TFXLMWithLMHeadModel,) if is_tf_available() else ()
			)  # TODO (PVP): Check other models whether language generation is also applicable
			lowerCamelCase							:							List[Any]    						=					(
			    {
			        "feature-extraction": TFXLMModel,
			        "fill-mask": TFXLMWithLMHeadModel,
			        "question-answering": TFXLMForQuestionAnsweringSimple,
			        "text-classification": TFXLMForSequenceClassification,
			        "text-generation": TFXLMWithLMHeadModel,
			        "token-classification": TFXLMForTokenClassification,
			        "zero-shot": TFXLMForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			lowerCamelCase							:							List[str]    						=					False
			lowerCamelCase							:							str    						=					False



			def A__	(   self    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    ,       lowerCamelCase__    )     ->						Union[str, Any]:




									'''simple docstring'''



									if (
									    pipeline_test_casse_name == "QAPipelineTests"
									    and tokenizer_name is not None
									    and not tokenizer_name.endswith("""Fast"""    )
									):
															# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
															# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
															# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
															return True

									return False



			def A__	(   self    )     ->						str:




									'''simple docstring'''



									lowercase__								=    TFXLMModelTester(self    )
									lowercase__								=    ConfigTester(self    ,       config_class=__A    ,       emb_dim=37    )



			def A__	(   self    )     ->						Dict:




									'''simple docstring'''



									self.config_tester.run_common_tests()



			def A__	(   self    )     ->						Any:




									'''simple docstring'''



									lowercase__								=    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_xlm_model(*__A    )



			def A__	(   self    )     ->						Dict:




									'''simple docstring'''



									lowercase__								=    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_xlm_lm_head(*__A    )



			def A__	(   self    )     ->						Tuple:




									'''simple docstring'''



									lowercase__								=    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_xlm_qa(*__A    )



			def A__	(   self    )     ->						Optional[Any]:




									'''simple docstring'''



									lowercase__								=    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_xlm_sequence_classif(*__A    )



			def A__	(   self    )     ->						List[Any]:




									'''simple docstring'''



									lowercase__								=    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_xlm_for_token_classification(*__A    )



			def A__	(   self    )     ->						Union[str, Any]:




									'''simple docstring'''



									lowercase__								=    self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_xlm_for_multiple_choice(*__A    )



			@slow
			def A__	(   self    )     ->						List[Any]:




									'''simple docstring'''



									for model_name in TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															lowercase__								=    TFXLMModel.from_pretrained(__A    )
															self.assertIsNotNone(__A    )





@require_tf
class      A							(    unittest.TestCase			):



			@slow
			def A__	(   self    )     ->						List[Any]:




									'''simple docstring'''



									lowercase__								=    TFXLMWithLMHeadModel.from_pretrained("""xlm-mlm-en-2048"""    )
									lowercase__								=    tf.convert_to_tensor([[14, 447]]    ,       dtype=tf.intaa    )  # the president
									lowercase__								=    [
									    14,
									    447,
									    14,
									    447,
									    14,
									    447,
									    14,
									    447,
									    14,
									    447,
									    14,
									    447,
									    14,
									    447,
									    14,
									    447,
									    14,
									    447,
									    14,
									    447,
									]  # the president the president the president the president the president the president the president the president the president the president
									# TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
									lowercase__								=    model.generate(__A    ,       do_sample=__A    )
									self.assertListEqual(output_ids[0].numpy().tolist()    ,       __A    )








'''simple docstring'''



import copy
from typing import Dict

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import CONFIG_MAPPING
from ..bit import BitConfig


__A       =      logging.get_logger(__name__)

__A       =      {
    "google/vit-hybrid-base-bit-384": "https://huggingface.co/vit-hybrid-base-bit-384/resolve/main/config.json",
    # See all ViT hybrid models at https://huggingface.co/models?filter=vit
}





class      A							(    __UpperCAmelCase			):
			lowerCamelCase							:							List[str]    						=					"""vit-hybrid"""



			def __init__(   self    ,       lowerCamelCase__=None    ,       lowerCamelCase__=768    ,       lowerCamelCase__=12    ,       lowerCamelCase__=12    ,       lowerCamelCase__=3_072    ,       lowerCamelCase__="gelu"    ,       lowerCamelCase__=0.0    ,       lowerCamelCase__=0.0    ,       lowerCamelCase__=0.02    ,       lowerCamelCase__=1e-12    ,       lowerCamelCase__=224    ,       lowerCamelCase__=1    ,       lowerCamelCase__=3    ,       lowerCamelCase__=[1, 1_024, 24, 24]    ,       lowerCamelCase__=True    ,       **lowerCamelCase__    ,       )     ->						Optional[Any]:




									'''simple docstring'''



									super().__init__(**lowerCamelCase__    )

									if backbone_config is None:
															logger.info("""`backbone_config` is `None`. Initializing the config with a `BiT` backbone."""    )
															lowercase__								=    {
															    """global_padding""": """same""",
															    """layer_type""": """bottleneck""",
															    """depths""": [3, 4, 9],
															    """out_features""": ["""stage3"""],
															    """embedding_dynamic_padding""": True,
															}

									if isinstance(lowerCamelCase__    ,       lowerCamelCase__    ):
															if "model_type" in backbone_config:
																					lowercase__								=    CONFIG_MAPPING[backbone_config["""model_type"""]]
															else:
																					logger.info(
																					    """`model_type` is not found in `backbone_config`. Use `Bit` as the backbone configuration class."""    )
																					lowercase__								=    BitConfig
															lowercase__								=    backbone_config_class(**lowerCamelCase__    )

									lowercase__								=    backbone_featmap_shape
									lowercase__								=    backbone_config
									lowercase__								=    hidden_size
									lowercase__								=    num_hidden_layers
									lowercase__								=    num_attention_heads
									lowercase__								=    intermediate_size
									lowercase__								=    hidden_act
									lowercase__								=    hidden_dropout_prob
									lowercase__								=    attention_probs_dropout_prob
									lowercase__								=    initializer_range
									lowercase__								=    layer_norm_eps
									lowercase__								=    image_size
									lowercase__								=    patch_size
									lowercase__								=    num_channels
									lowercase__								=    qkv_bias



			def A__	(   self    )     ->						Dict[str, any]:




									'''simple docstring'''



									lowercase__								=    copy.deepcopy(self.__dict__    )
									lowercase__								=    self.backbone_config.to_dict()
									lowercase__								=    self.__class__.model_type
									return output






