



import inspect
import unittest

from transformers import SegformerConfig, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

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 transformers import (
				    MODEL_MAPPING,
				    SegformerForImageClassification,
				    SegformerForSemanticSegmentation,
				    SegformerModel,
				)
				from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
				from PIL import Image

				from transformers import SegformerImageProcessor

class 				snake_case__(_UpperCamelCase      ):

					"""simple docstring"""






					def 			snake_case					(							self :     Any					):
									lowercase__					:   List[str]													=					self.config_class(**self.inputs_dict					)
									self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE				,       "hidden_sizes"					)					)
									self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE				,       "num_attention_heads"					)					)
									self.parent.assertTrue(hasattr(SCREAMING_SNAKE_CASE				,       "num_encoder_blocks"					)					)






class 				snake_case__:

					"""simple docstring"""






					def __init__(							self :     str				,       SCREAMING_SNAKE_CASE :     Any				,       SCREAMING_SNAKE_CASE :     Optional[int]=13				,       SCREAMING_SNAKE_CASE :     Union[str, Any]=64				,       SCREAMING_SNAKE_CASE :     Optional[Any]=3				,       SCREAMING_SNAKE_CASE :     Tuple=4				,       SCREAMING_SNAKE_CASE :     List[str]=[2, 2, 2, 2]				,       SCREAMING_SNAKE_CASE :     List[str]=[8, 4, 2, 1]				,       SCREAMING_SNAKE_CASE :     List[str]=[16, 32, 64, 128]				,       SCREAMING_SNAKE_CASE :     Optional[Any]=[1, 4, 8, 16]				,       SCREAMING_SNAKE_CASE :     Union[str, Any]=[1, 2, 4, 8]				,       SCREAMING_SNAKE_CASE :     Any=True				,       SCREAMING_SNAKE_CASE :     str=True				,       SCREAMING_SNAKE_CASE :     Tuple="gelu"				,       SCREAMING_SNAKE_CASE :     str=0.1				,       SCREAMING_SNAKE_CASE :     str=0.1				,       SCREAMING_SNAKE_CASE :     Optional[int]=0.02				,       SCREAMING_SNAKE_CASE :     Optional[Any]=3				,       SCREAMING_SNAKE_CASE :     Tuple=None				,       ):
									lowercase__					:   Dict													=					parent
									lowercase__					:   Union[str, Any]													=					batch_size
									lowercase__					:   Dict													=					image_size
									lowercase__					:   Any													=					num_channels
									lowercase__					:   List[str]													=					num_encoder_blocks
									lowercase__					:   str													=					sr_ratios
									lowercase__					:   List[Any]													=					depths
									lowercase__					:   List[str]													=					hidden_sizes
									lowercase__					:   Any													=					downsampling_rates
									lowercase__					:   List[Any]													=					num_attention_heads
									lowercase__					:   Union[str, Any]													=					is_training
									lowercase__					:   int													=					use_labels
									lowercase__					:   Any													=					hidden_act
									lowercase__					:   Any													=					hidden_dropout_prob
									lowercase__					:   Optional[int]													=					attention_probs_dropout_prob
									lowercase__					:   Dict													=					initializer_range
									lowercase__					:   Optional[int]													=					num_labels
									lowercase__					:   Dict													=					scope







					def 			snake_case					(							self :     Union[str, Any]					):
									lowercase__					:   Dict													=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

									lowercase__					:   Optional[Any]													=					None
									if self.use_labels:
													lowercase__					:   str													=					ids_tensor([self.batch_size, self.image_size, self.image_size]				,       self.num_labels					)

									lowercase__					:   Optional[int]													=					self.get_config()
									return config, pixel_values, labels







					def 			snake_case					(							self :     Optional[int]					):
									return SegformerConfig(
									    image_size=self.image_size				,       num_channels=self.num_channels				,       num_encoder_blocks=self.num_encoder_blocks				,       depths=self.depths				,       hidden_sizes=self.hidden_sizes				,       num_attention_heads=self.num_attention_heads				,       hidden_act=self.hidden_act				,       hidden_dropout_prob=self.hidden_dropout_prob				,       attention_probs_dropout_prob=self.attention_probs_dropout_prob				,       initializer_range=self.initializer_range				,       )







					def 			snake_case					(							self :     List[Any]				,       SCREAMING_SNAKE_CASE :     str				,       SCREAMING_SNAKE_CASE :     Any				,       SCREAMING_SNAKE_CASE :     int					):
									lowercase__					:   Union[str, Any]													=					SegformerModel(config=SCREAMING_SNAKE_CASE					)
									model.to(SCREAMING_SNAKE_CASE					)
									model.eval()
									lowercase__					:   List[Any]													=					model(SCREAMING_SNAKE_CASE					)
									lowercase__					:   str													=					self.image_size // (self.downsampling_rates[-1] * 2)
									self.parent.assertEqual(
									    result.last_hidden_state.shape				,       (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)					)







					def 			snake_case					(							self :     Tuple				,       SCREAMING_SNAKE_CASE :     str				,       SCREAMING_SNAKE_CASE :     Optional[Any]				,       SCREAMING_SNAKE_CASE :     str					):
									lowercase__					:   Tuple													=					self.num_labels
									lowercase__					:   int													=					SegformerForSemanticSegmentation(SCREAMING_SNAKE_CASE					)
									model.to(SCREAMING_SNAKE_CASE					)
									model.eval()
									lowercase__					:   List[str]													=					model(SCREAMING_SNAKE_CASE					)
									self.parent.assertEqual(
									    result.logits.shape				,       (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)					)
									lowercase__					:   List[str]													=					model(SCREAMING_SNAKE_CASE				,       labels=SCREAMING_SNAKE_CASE					)
									self.parent.assertEqual(
									    result.logits.shape				,       (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)					)
									self.parent.assertGreater(result.loss				,       0.0					)







					def 			snake_case					(							self :     Union[str, Any]				,       SCREAMING_SNAKE_CASE :     Dict				,       SCREAMING_SNAKE_CASE :     List[str]				,       SCREAMING_SNAKE_CASE :     Dict					):
									lowercase__					:   str													=					1
									lowercase__					:   List[str]													=					SegformerForSemanticSegmentation(config=SCREAMING_SNAKE_CASE					)
									model.to(SCREAMING_SNAKE_CASE					)
									model.eval()
									lowercase__					:   Dict													=					torch.randint(0				,       1				,       (self.batch_size, self.image_size, self.image_size)					).to(SCREAMING_SNAKE_CASE					)
									lowercase__					:   Dict													=					model(SCREAMING_SNAKE_CASE				,       labels=SCREAMING_SNAKE_CASE					)
									self.parent.assertGreater(result.loss				,       0.0					)







					def 			snake_case					(							self :     Dict					):
									lowercase__					:   List[Any]													=					self.prepare_config_and_inputs()
									lowercase__							,							lowercase__							,							lowercase__					:   Tuple													=					config_and_inputs
									lowercase__					:   Union[str, Any]													=					{"pixel_values": pixel_values}
									return config, inputs_dict






@require_torch
class 				snake_case__(_UpperCamelCase    ,      _UpperCamelCase    ,      unittest.TestCase      ):

					"""simple docstring"""






					lowercase_								=		(
					    (
					        SegformerModel,
					        SegformerForSemanticSegmentation,
					        SegformerForImageClassification,
					    )
					    if is_torch_available()
					    else ()
					)
					lowercase_								=		(
					    {
					        """feature-extraction""": SegformerModel,
					        """image-classification""": SegformerForImageClassification,
					        """image-segmentation""": SegformerForSemanticSegmentation,
					    }
					    if is_torch_available()
					    else {}
					)

					lowercase_								=		True
					lowercase_								=		False
					lowercase_								=		False
					lowercase_								=		False







					def 			snake_case					(							self :     str					):
									lowercase__					:   Optional[Any]													=					SegformerModelTester(self					)
									lowercase__					:   Any													=					SegformerConfigTester(self				,       config_class=SCREAMING_SNAKE_CASE					)







					def 			snake_case					(							self :     Dict					):
									self.config_tester.run_common_tests()







					def 			snake_case					(							self :     Optional[int]					):
									lowercase__					:   int													=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE					)







					def 			snake_case					(							self :     Union[str, Any]					):
									lowercase__					:   Dict													=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_binary_image_segmentation(*SCREAMING_SNAKE_CASE					)







					def 			snake_case					(							self :     List[Any]					):
									lowercase__					:   int													=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_segmentation(*SCREAMING_SNAKE_CASE					)







					@unittest.skip("SegFormer does not use inputs_embeds"					)
					def 			snake_case					(							self :     Dict					):
									pass







					@unittest.skip("SegFormer does not have get_input_embeddings method and get_output_embeddings methods"					)
					def 			snake_case					(							self :     Optional[Any]					):
									pass







					def 			snake_case					(							self :     str					):
									lowercase__							,							lowercase__					:   Union[str, Any]													=					self.model_tester.prepare_config_and_inputs_for_common()

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

													lowercase__					:   List[Any]													=					["pixel_values"]
													self.assertListEqual(arg_names[:1]				,       SCREAMING_SNAKE_CASE					)







					def 			snake_case					(							self :     List[Any]					):
									lowercase__							,							lowercase__					:   Optional[int]													=					self.model_tester.prepare_config_and_inputs_for_common()
									lowercase__					:   List[Any]													=					True

									for model_class in self.all_model_classes:
													lowercase__					:   List[str]													=					True
													lowercase__					:   List[str]													=					False
													lowercase__					:   Optional[Any]													=					True
													lowercase__					:   Tuple													=					model_class(SCREAMING_SNAKE_CASE					)
													model.to(SCREAMING_SNAKE_CASE					)
													model.eval()
													with torch.no_grad():
																	lowercase__					:   List[str]													=					model(**self._prepare_for_class(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					)					)
													lowercase__					:   str													=					outputs.attentions

													lowercase__					:   Tuple													=					sum(self.model_tester.depths					)
													self.assertEqual(len(SCREAMING_SNAKE_CASE					)				,       SCREAMING_SNAKE_CASE					)

													# check that output_attentions also work using config
													del inputs_dict["output_attentions"]
													lowercase__					:   Dict													=					True
													lowercase__					:   Union[str, Any]													=					model_class(SCREAMING_SNAKE_CASE					)
													model.to(SCREAMING_SNAKE_CASE					)
													model.eval()
													with torch.no_grad():
																	lowercase__					:   str													=					model(**self._prepare_for_class(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					)					)
													lowercase__					:   List[str]													=					outputs.attentions

													self.assertEqual(len(SCREAMING_SNAKE_CASE					)				,       SCREAMING_SNAKE_CASE					)

													# verify the first attentions (first block, first layer)
													lowercase__					:   List[str]													=					(self.model_tester.image_size // 4) ** 2
													lowercase__					:   str													=					(self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
													self.assertListEqual(
													    list(attentions[0].shape[-3:]					)				,       [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]				,       )

													# verify the last attentions (last block, last layer)
													lowercase__					:   Any													=					(self.model_tester.image_size // 32) ** 2
													lowercase__					:   Optional[Any]													=					(self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2
													self.assertListEqual(
													    list(attentions[-1].shape[-3:]					)				,       [self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len]				,       )
													lowercase__					:   int													=					len(SCREAMING_SNAKE_CASE					)

													# Check attention is always last and order is fine
													lowercase__					:   List[Any]													=					True
													lowercase__					:   Any													=					True
													lowercase__					:   List[Any]													=					model_class(SCREAMING_SNAKE_CASE					)
													model.to(SCREAMING_SNAKE_CASE					)
													model.eval()
													with torch.no_grad():
																	lowercase__					:   str													=					model(**self._prepare_for_class(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					)					)

													self.assertEqual(out_len + 1				,       len(SCREAMING_SNAKE_CASE					)					)

													lowercase__					:   Dict													=					outputs.attentions

													self.assertEqual(len(SCREAMING_SNAKE_CASE					)				,       SCREAMING_SNAKE_CASE					)
													# verify the first attentions (first block, first layer)
													lowercase__					:   Tuple													=					(self.model_tester.image_size // 4) ** 2
													lowercase__					:   str													=					(self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
													self.assertListEqual(
													    list(self_attentions[0].shape[-3:]					)				,       [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]				,       )







					def 			snake_case					(							self :     Optional[int]					):
									def check_hidden_states_output(SCREAMING_SNAKE_CASE :     Optional[Any]				,       SCREAMING_SNAKE_CASE :     List[Any]				,       SCREAMING_SNAKE_CASE :     Tuple					):
													lowercase__					:   List[Any]													=					model_class(SCREAMING_SNAKE_CASE					)
													model.to(SCREAMING_SNAKE_CASE					)
													model.eval()

													with torch.no_grad():
																	lowercase__					:   Any													=					model(**self._prepare_for_class(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					)					)

													lowercase__					:   Dict													=					outputs.hidden_states

													lowercase__					:   List[Any]													=					self.model_tester.num_encoder_blocks
													self.assertEqual(len(SCREAMING_SNAKE_CASE					)				,       SCREAMING_SNAKE_CASE					)

													# verify the first hidden states (first block)
													self.assertListEqual(
													    list(hidden_states[0].shape[-3:]					)				,       [
													        self.model_tester.hidden_sizes[0],
													        self.model_tester.image_size // 4,
													        self.model_tester.image_size // 4,
													    ]				,       )

									lowercase__							,							lowercase__					:   Tuple													=					self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
													lowercase__					:   Union[str, Any]													=					True
													check_hidden_states_output(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					)

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

													check_hidden_states_output(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE					)







					def 			snake_case					(							self :     Union[str, Any]					):
									if not self.model_tester.is_training:
													return

									lowercase__							,							lowercase__					:   Any													=					self.model_tester.prepare_config_and_inputs_for_common()
									lowercase__					:   List[str]													=					True

									for model_class in self.all_model_classes:
													if model_class in get_values(SCREAMING_SNAKE_CASE					):
																	continue

													lowercase__					:   Dict													=					model_class(SCREAMING_SNAKE_CASE					)
													model.to(SCREAMING_SNAKE_CASE					)
													model.train()
													lowercase__					:   Dict													=					self._prepare_for_class(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE				,       return_labels=SCREAMING_SNAKE_CASE					)
													lowercase__					:   int													=					model(**SCREAMING_SNAKE_CASE					).loss
													loss.backward()







					@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."					)
					def 			snake_case					(							self :     Optional[Any]					):
									pass







					@slow
					def 			snake_case					(							self :     Optional[int]					):
									for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													lowercase__					:   List[Any]													=					SegformerModel.from_pretrained(SCREAMING_SNAKE_CASE					)
													self.assertIsNotNone(SCREAMING_SNAKE_CASE					)






def  __lowerCamelCase (       ):

				"""simple docstring"""


				lowercase__					:   int													=					Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
				return image

@require_torch
class 				snake_case__(unittest.TestCase      ):

					"""simple docstring"""






					@slow
					def 			snake_case					(							self :     Union[str, Any]					):
									# only resize + normalize
									lowercase__					:   int													=					SegformerImageProcessor(
									    image_scale=(512, 512)				,       keep_ratio=SCREAMING_SNAKE_CASE				,       align=SCREAMING_SNAKE_CASE				,       do_random_crop=SCREAMING_SNAKE_CASE					)
									lowercase__					:   int													=					SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512"					).to(
									    SCREAMING_SNAKE_CASE					)

									lowercase__					:   Tuple													=					prepare_img()
									lowercase__					:   List[Any]													=					image_processor(images=SCREAMING_SNAKE_CASE				,       return_tensors="pt"					)
									lowercase__					:   str													=					encoded_inputs.pixel_values.to(SCREAMING_SNAKE_CASE					)

									with torch.no_grad():
													lowercase__					:   Optional[Any]													=					model(SCREAMING_SNAKE_CASE					)

									lowercase__					:   List[str]													=					torch.Size((1, model.config.num_labels, 128, 128)					)
									self.assertEqual(outputs.logits.shape				,       SCREAMING_SNAKE_CASE					)

									lowercase__					:   Optional[Any]													=					torch.tensor(
									    [
									        [[-4.6_310, -5.5_232, -6.2_356], [-5.1_921, -6.1_444, -6.5_996], [-5.4_424, -6.2_790, -6.7_574]],
									        [[-12.1_391, -13.3_122, -13.9_554], [-12.8_732, -13.9_352, -14.3_563], [-12.9_438, -13.8_226, -14.2_513]],
									        [[-12.5_134, -13.4_686, -14.4_915], [-12.8_669, -14.4_343, -14.7_758], [-13.2_523, -14.5_819, -15.0_694]],
									    ]					).to(SCREAMING_SNAKE_CASE					)
									self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]				,       SCREAMING_SNAKE_CASE				,       atol=1E-4					)					)







					@slow
					def 			snake_case					(							self :     int					):
									# only resize + normalize
									lowercase__					:   Optional[int]													=					SegformerImageProcessor(
									    image_scale=(512, 512)				,       keep_ratio=SCREAMING_SNAKE_CASE				,       align=SCREAMING_SNAKE_CASE				,       do_random_crop=SCREAMING_SNAKE_CASE					)
									lowercase__					:   Optional[int]													=					SegformerForSemanticSegmentation.from_pretrained(
									    "nvidia/segformer-b1-finetuned-cityscapes-1024-1024"					).to(SCREAMING_SNAKE_CASE					)

									lowercase__					:   List[Any]													=					prepare_img()
									lowercase__					:   Optional[Any]													=					image_processor(images=SCREAMING_SNAKE_CASE				,       return_tensors="pt"					)
									lowercase__					:   Any													=					encoded_inputs.pixel_values.to(SCREAMING_SNAKE_CASE					)

									with torch.no_grad():
													lowercase__					:   Any													=					model(SCREAMING_SNAKE_CASE					)

									lowercase__					:   Dict													=					torch.Size((1, model.config.num_labels, 128, 128)					)
									self.assertEqual(outputs.logits.shape				,       SCREAMING_SNAKE_CASE					)

									lowercase__					:   Any													=					torch.tensor(
									    [
									        [[-13.5_748, -13.9_111, -12.6_500], [-14.3_500, -15.3_683, -14.2_328], [-14.7_532, -16.0_424, -15.6_087]],
									        [[-17.1_651, -15.8_725, -12.9_653], [-17.2_580, -17.3_718, -14.8_223], [-16.6_058, -16.8_783, -16.7_452]],
									        [[-3.6_456, -3.0_209, -1.4_203], [-3.0_797, -3.1_959, -2.0_000], [-1.8_757, -1.9_217, -1.6_997]],
									    ]					).to(SCREAMING_SNAKE_CASE					)
									self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]				,       SCREAMING_SNAKE_CASE				,       atol=1E-1					)					)







					@slow
					def 			snake_case					(							self :     List[Any]					):
									# only resize + normalize
									lowercase__					:   Tuple													=					SegformerImageProcessor(
									    image_scale=(512, 512)				,       keep_ratio=SCREAMING_SNAKE_CASE				,       align=SCREAMING_SNAKE_CASE				,       do_random_crop=SCREAMING_SNAKE_CASE					)
									lowercase__					:   List[str]													=					SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512"					).to(
									    SCREAMING_SNAKE_CASE					)

									lowercase__					:   Any													=					prepare_img()
									lowercase__					:   List[str]													=					image_processor(images=SCREAMING_SNAKE_CASE				,       return_tensors="pt"					)
									lowercase__					:   Tuple													=					encoded_inputs.pixel_values.to(SCREAMING_SNAKE_CASE					)

									with torch.no_grad():
													lowercase__					:   List[Any]													=					model(SCREAMING_SNAKE_CASE					)

									lowercase__					:   List[str]													=					outputs.logits.detach().cpu()

									lowercase__					:   Union[str, Any]													=					image_processor.post_process_semantic_segmentation(outputs=SCREAMING_SNAKE_CASE				,       target_sizes=[(500, 300)]					)
									lowercase__					:   Optional[Any]													=					torch.Size((500, 300)					)
									self.assertEqual(segmentation[0].shape				,       SCREAMING_SNAKE_CASE					)

									lowercase__					:   List[Any]													=					image_processor.post_process_semantic_segmentation(outputs=SCREAMING_SNAKE_CASE					)
									lowercase__					:   int													=					torch.Size((128, 128)					)
									self.assertEqual(segmentation[0].shape				,       SCREAMING_SNAKE_CASE					)









import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase__					  =  logging.get_logger(__name__)

lowerCAmelCase__					  =  {
    '''BridgeTower/bridgetower-base''': '''https://huggingface.co/BridgeTower/bridgetower-base/blob/main/config.json''',
    '''BridgeTower/bridgetower-base-itm-mlm''': (
        '''https://huggingface.co/BridgeTower/bridgetower-base-itm-mlm/blob/main/config.json'''
    ),
}

class 				snake_case__(_UpperCamelCase      ):

					"""simple docstring"""






					lowercase_								=		"""bridgetower_vision_model"""







					def __init__(							self :     str				,       SCREAMING_SNAKE_CASE :     Dict=768				,       SCREAMING_SNAKE_CASE :     Union[str, Any]=12				,       SCREAMING_SNAKE_CASE :     Dict=3				,       SCREAMING_SNAKE_CASE :     Dict=16				,       SCREAMING_SNAKE_CASE :     List[Any]=288				,       SCREAMING_SNAKE_CASE :     List[Any]=1				,       SCREAMING_SNAKE_CASE :     List[str]=1E-0_5				,       SCREAMING_SNAKE_CASE :     str=False				,       SCREAMING_SNAKE_CASE :     Any=True				,       SCREAMING_SNAKE_CASE :     Tuple=False				,       **SCREAMING_SNAKE_CASE :     List[str]				,       ):
									super().__init__(**SCREAMING_SNAKE_CASE					)
									lowercase__					:   Union[str, Any]													=					hidden_size
									lowercase__					:   str													=					num_hidden_layers
									lowercase__					:   str													=					num_channels
									lowercase__					:   Optional[int]													=					patch_size
									lowercase__					:   Dict													=					image_size
									lowercase__					:   List[Any]													=					initializer_factor
									lowercase__					:   int													=					layer_norm_eps
									lowercase__					:   List[str]													=					stop_gradient
									lowercase__					:   Optional[int]													=					share_layernorm
									lowercase__					:   Optional[int]													=					remove_last_layer







					@classmethod
					def 			snake_case					(							cls :     Tuple				,       SCREAMING_SNAKE_CASE :     Union[str, os.PathLike]				,       **SCREAMING_SNAKE_CASE :     str					):
									lowercase__							,							lowercase__					:   Optional[Any]													=					cls.get_config_dict(SCREAMING_SNAKE_CASE				,       **SCREAMING_SNAKE_CASE					)

									if config_dict.get("model_type"					) == "bridgetower":
													lowercase__					:   Optional[int]													=					config_dict["text_config"]

									if "model_type" in config_dict and hasattr(cls				,       "model_type"					) and config_dict["model_type"] != cls.model_type:
													logger.warning(
													    f"""You are using a model of type {config_dict["model_type"]} to instantiate a model of type """
													    f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors."""					)

									return cls.from_dict(SCREAMING_SNAKE_CASE				,       **SCREAMING_SNAKE_CASE					)






class 				snake_case__(_UpperCamelCase      ):

					"""simple docstring"""






					lowercase_								=		"""bridgetower_text_model"""







					def __init__(							self :     Tuple				,       SCREAMING_SNAKE_CASE :     Union[str, Any]=50_265				,       SCREAMING_SNAKE_CASE :     Optional[int]=768				,       SCREAMING_SNAKE_CASE :     List[str]=12				,       SCREAMING_SNAKE_CASE :     Union[str, Any]=12				,       SCREAMING_SNAKE_CASE :     List[Any]=1				,       SCREAMING_SNAKE_CASE :     Any=3_072				,       SCREAMING_SNAKE_CASE :     Union[str, Any]="gelu"				,       SCREAMING_SNAKE_CASE :     Optional[int]=0.1				,       SCREAMING_SNAKE_CASE :     List[str]=0.1				,       SCREAMING_SNAKE_CASE :     Tuple=514				,       SCREAMING_SNAKE_CASE :     List[str]=1				,       SCREAMING_SNAKE_CASE :     Dict=1E-0_5				,       SCREAMING_SNAKE_CASE :     List[str]=1				,       SCREAMING_SNAKE_CASE :     str=0				,       SCREAMING_SNAKE_CASE :     List[Any]=2				,       SCREAMING_SNAKE_CASE :     Optional[Any]="absolute"				,       SCREAMING_SNAKE_CASE :     int=True				,       **SCREAMING_SNAKE_CASE :     int				,       ):
									super().__init__(**SCREAMING_SNAKE_CASE					)

									lowercase__					:   Any													=					vocab_size
									lowercase__					:   Optional[Any]													=					hidden_size
									lowercase__					:   Optional[int]													=					num_hidden_layers
									lowercase__					:   Optional[int]													=					num_attention_heads
									lowercase__					:   int													=					hidden_act
									lowercase__					:   Optional[Any]													=					initializer_factor
									lowercase__					:   Dict													=					intermediate_size
									lowercase__					:   Tuple													=					hidden_dropout_prob
									lowercase__					:   List[Any]													=					attention_probs_dropout_prob
									lowercase__					:   Optional[int]													=					max_position_embeddings
									lowercase__					:   str													=					type_vocab_size
									lowercase__					:   str													=					layer_norm_eps
									lowercase__					:   Dict													=					position_embedding_type
									lowercase__					:   Optional[Any]													=					use_cache
									lowercase__					:   List[str]													=					pad_token_id
									lowercase__					:   Optional[Any]													=					bos_token_id
									lowercase__					:   Optional[Any]													=					eos_token_id







					@classmethod
					def 			snake_case					(							cls :     Optional[int]				,       SCREAMING_SNAKE_CASE :     Union[str, os.PathLike]				,       **SCREAMING_SNAKE_CASE :     str					):
									lowercase__							,							lowercase__					:   Optional[Any]													=					cls.get_config_dict(SCREAMING_SNAKE_CASE				,       **SCREAMING_SNAKE_CASE					)

									if config_dict.get("model_type"					) == "bridgetower":
													lowercase__					:   List[Any]													=					config_dict["text_config"]

									if "model_type" in config_dict and hasattr(cls				,       "model_type"					) and config_dict["model_type"] != cls.model_type:
													logger.warning(
													    f"""You are using a model of type {config_dict["model_type"]} to instantiate a model of type """
													    f"""{cls.model_type}. This is not supported for all configurations of models and can yield errors."""					)

									return cls.from_dict(SCREAMING_SNAKE_CASE				,       **SCREAMING_SNAKE_CASE					)






class 				snake_case__(_UpperCamelCase      ):

					"""simple docstring"""






					lowercase_								=		"""bridgetower"""







					def __init__(							self :     Tuple				,       SCREAMING_SNAKE_CASE :     Optional[int]=True				,       SCREAMING_SNAKE_CASE :     Dict="gelu"				,       SCREAMING_SNAKE_CASE :     Any=768				,       SCREAMING_SNAKE_CASE :     Dict=1				,       SCREAMING_SNAKE_CASE :     Union[str, Any]=1E-0_5				,       SCREAMING_SNAKE_CASE :     Tuple=False				,       SCREAMING_SNAKE_CASE :     List[Any]="add"				,       SCREAMING_SNAKE_CASE :     Any=12				,       SCREAMING_SNAKE_CASE :     Any=6				,       SCREAMING_SNAKE_CASE :     Any=False				,       SCREAMING_SNAKE_CASE :     List[Any]=False				,       SCREAMING_SNAKE_CASE :     str=None				,       SCREAMING_SNAKE_CASE :     Dict=None				,       **SCREAMING_SNAKE_CASE :     List[Any]				,       ):
									# TODO: remove this once the Hub files are updated.
									lowercase__					:   int													=					kwargs.pop("text_config_dict"				,       SCREAMING_SNAKE_CASE					)
									lowercase__					:   int													=					kwargs.pop("vision_config_dict"				,       SCREAMING_SNAKE_CASE					)

									super().__init__(**SCREAMING_SNAKE_CASE					)
									lowercase__					:   Dict													=					share_cross_modal_transformer_layers
									lowercase__					:   int													=					hidden_act
									lowercase__					:   int													=					hidden_size
									lowercase__					:   Optional[int]													=					initializer_factor
									lowercase__					:   Optional[int]													=					layer_norm_eps
									lowercase__					:   str													=					share_link_tower_layers
									lowercase__					:   Optional[int]													=					link_tower_type
									lowercase__					:   List[Any]													=					num_attention_heads
									lowercase__					:   Dict													=					num_hidden_layers
									lowercase__					:   Dict													=					tie_word_embeddings
									lowercase__					:   Optional[Any]													=					init_layernorm_from_vision_encoder

									if text_config is None:
													lowercase__					:   Optional[Any]													=					{}
													logger.info("`text_config` is `None`. Initializing the `BridgeTowerTextConfig` with default values."					)

									if vision_config is None:
													lowercase__					:   List[Any]													=					{}
													logger.info("`vision_config` is `None`. Initializing the `BridgeTowerVisionConfig` with default values."					)

									lowercase__					:   Any													=					BridgeTowerTextConfig(**SCREAMING_SNAKE_CASE					)
									lowercase__					:   Dict													=					BridgeTowerVisionConfig(**SCREAMING_SNAKE_CASE					)







					@classmethod
					def 			snake_case					(							cls :     Optional[int]				,       SCREAMING_SNAKE_CASE :     BridgeTowerTextConfig				,       SCREAMING_SNAKE_CASE :     BridgeTowerVisionConfig				,       **SCREAMING_SNAKE_CASE :     List[str]					):
									return cls(text_config=text_config.to_dict()				,       vision_config=vision_config.to_dict()				,       **SCREAMING_SNAKE_CASE					)







					def 			snake_case					(							self :     Optional[int]					):
									lowercase__					:   List[str]													=					copy.deepcopy(self.__dict__					)
									lowercase__					:   Dict													=					self.text_config.to_dict()
									lowercase__					:   Tuple													=					self.vision_config.to_dict()
									lowercase__					:   Optional[Any]													=					self.__class__.model_type
									return output




