



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        lowerCamelCase__			:




		'''simple docstring'''



		def __init__(self		,__lowerCamelCase		,__lowerCamelCase=3		,__lowerCamelCase=32		,__lowerCamelCase=3		,__lowerCamelCase=10		,__lowerCamelCase=[8, 16, 32, 64]		,__lowerCamelCase=[1, 1, 2, 1]		,__lowerCamelCase=True		,__lowerCamelCase=True		,__lowerCamelCase="relu"		,__lowerCamelCase=3		,__lowerCamelCase=None		,__lowerCamelCase=["stage2", "stage3", "stage4"]		,__lowerCamelCase=[2, 3, 4]		,__lowerCamelCase=1		,)     ->			Union[str, Any]:

						"""simple docstring"""






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



		def 		lowerCAmelCase__      (self )     ->			Any:

						"""simple docstring"""






						lowerCAmelCase__     :				Optional[Any]												=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

						lowerCAmelCase__     :				Tuple												=     None
						if self.use_labels:
										lowerCAmelCase__     :				int												=     ids_tensor([self.batch_size]		,self.num_labels )

						lowerCAmelCase__     :				List[Any]												=     self.get_config()

						return config, pixel_values, labels



		def 		lowerCAmelCase__      (self )     ->			Any:

						"""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 		lowerCAmelCase__      (self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase )     ->			int:

						"""simple docstring"""






						lowerCAmelCase__     :				Tuple												=     BitModel(config=lowerCAmelCase_ )
						model.to(lowerCAmelCase_ )
						model.eval()
						lowerCAmelCase__     :				Tuple												=     model(lowerCAmelCase_ )
						self.parent.assertEqual(
						    result.last_hidden_state.shape		,(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)		,)



		def 		lowerCAmelCase__      (self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase )     ->			Any:

						"""simple docstring"""






						lowerCAmelCase__     :				Tuple												=     self.num_labels
						lowerCAmelCase__     :				Optional[Any]												=     BitForImageClassification(lowerCAmelCase_ )
						model.to(lowerCAmelCase_ )
						model.eval()
						lowerCAmelCase__     :				str												=     model(lowerCAmelCase_		,labels=lowerCAmelCase_ )
						self.parent.assertEqual(result.logits.shape		,(self.batch_size, self.num_labels) )



		def 		lowerCAmelCase__      (self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase )     ->			Dict:

						"""simple docstring"""






						lowerCAmelCase__     :				Any												=     BitBackbone(config=lowerCAmelCase_ )
						model.to(lowerCAmelCase_ )
						model.eval()
						lowerCAmelCase__     :				Tuple												=     model(lowerCAmelCase_ )

						# 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[int]												=     None
						lowerCAmelCase__     :				Dict												=     BitBackbone(config=lowerCAmelCase_ )
						model.to(lowerCAmelCase_ )
						model.eval()
						lowerCAmelCase__     :				Tuple												=     model(lowerCAmelCase_ )

						# 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 		lowerCAmelCase__      (self )     ->			str:

						"""simple docstring"""






						lowerCAmelCase__     :				str												=     self.prepare_config_and_inputs()
						lowerCAmelCase__						,		lowerCAmelCase__						,		lowerCAmelCase__     :				Dict												=     config_and_inputs
						lowerCAmelCase__     :				List[Any]												=     {'''pixel_values''': pixel_values}
						return config, inputs_dict



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




		'''simple docstring'''



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

		snake_case_	=False
		snake_case_	=False
		snake_case_	=False
		snake_case_	=False
		snake_case_	=False



		def 		lowerCAmelCase__      (self )     ->			Optional[int]:

						"""simple docstring"""






						lowerCAmelCase__     :				Dict												=     BitModelTester(self )
						lowerCAmelCase__     :				List[str]												=     ConfigTester(self		,config_class=lowerCAmelCase_		,has_text_modality=lowerCAmelCase_ )



		def 		lowerCAmelCase__      (self )     ->			Dict:

						"""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 		lowerCAmelCase__      (self )     ->			Optional[int]:

						"""simple docstring"""






						return



		@unittest.skip(reason='''Bit does not output attentions''' )
		def 		lowerCAmelCase__      (self )     ->			Optional[int]:

						"""simple docstring"""






						pass



		@unittest.skip(reason='''Bit does not use inputs_embeds''' )
		def 		lowerCAmelCase__      (self )     ->			Optional[int]:

						"""simple docstring"""






						pass



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

						"""simple docstring"""






						pass



		def 		lowerCAmelCase__      (self )     ->			Any:

						"""simple docstring"""






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

						for model_class in self.all_model_classes:
										lowerCAmelCase__     :				Optional[int]												=     model_class(lowerCAmelCase_ )
										lowerCAmelCase__     :				Union[str, Any]												=     inspect.signature(model.forward )
										# signature.parameters is an OrderedDict => so arg_names order is deterministic
										lowerCAmelCase__     :				Optional[Any]												=     [*signature.parameters.keys()]

										lowerCAmelCase__     :				Optional[Any]												=     ['''pixel_values''']
										self.assertListEqual(arg_names[:1]		,lowerCAmelCase_ )



		def 		lowerCAmelCase__      (self )     ->			Any:

						"""simple docstring"""






						lowerCAmelCase__     :				Union[str, Any]												=     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowerCAmelCase_ )



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

						"""simple docstring"""






						lowerCAmelCase__     :				Dict												=     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_backbone(*lowerCAmelCase_ )



		def 		lowerCAmelCase__      (self )     ->			Tuple:

						"""simple docstring"""






						lowerCAmelCase__						,		lowerCAmelCase__     :				List[str]												=     self.model_tester.prepare_config_and_inputs_for_common()

						for model_class in self.all_model_classes:
										lowerCAmelCase__     :				str												=     model_class(config=lowerCAmelCase_ )
										for name, module in model.named_modules():
														if isinstance(lowerCAmelCase_		,(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 		lowerCAmelCase__      (self )     ->			int:

						"""simple docstring"""






						def check_hidden_states_output(__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase ):
										lowerCAmelCase__     :				List[Any]												=     model_class(lowerCAmelCase_ )
										model.to(lowerCAmelCase_ )
										model.eval()

										with torch.no_grad():
														lowerCAmelCase__     :				str												=     model(**self._prepare_for_class(lowerCAmelCase_		,lowerCAmelCase_ ) )

										lowerCAmelCase__     :				Optional[Any]												=     outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

										lowerCAmelCase__     :				str												=     self.model_tester.num_stages
										self.assertEqual(len(lowerCAmelCase_ )		,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__						,		lowerCAmelCase__     :				str												=     self.model_tester.prepare_config_and_inputs_for_common()
						lowerCAmelCase__     :				int												=     ['''preactivation''', '''bottleneck''']
						for model_class in self.all_model_classes:
										for layer_type in layers_type:
														lowerCAmelCase__     :				Dict												=     layer_type
														lowerCAmelCase__     :				Optional[Any]												=     True
														check_hidden_states_output(lowerCAmelCase_		,lowerCAmelCase_		,lowerCAmelCase_ )

														# check that output_hidden_states also work using config
														del inputs_dict["output_hidden_states"]
														lowerCAmelCase__     :				Dict												=     True

														check_hidden_states_output(lowerCAmelCase_		,lowerCAmelCase_		,lowerCAmelCase_ )



		@unittest.skip(reason='''Bit does not use feedforward chunking''' )
		def 		lowerCAmelCase__      (self )     ->			Dict:

						"""simple docstring"""






						pass



		def 		lowerCAmelCase__      (self )     ->			List[str]:

						"""simple docstring"""






						lowerCAmelCase__     :				Optional[Any]												=     self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase_ )



		@slow
		def 		lowerCAmelCase__      (self )     ->			Dict:

						"""simple docstring"""






						for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCAmelCase__     :				Union[str, Any]												=     BitModel.from_pretrained(lowerCAmelCase_ )
										self.assertIsNotNone(lowerCAmelCase_ )






def 	lowerCAmelCase__		(    ):





				'''simple docstring'''

				lowerCAmelCase__     :				Tuple												=     Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''')
				return image



@require_torch
@require_vision
class        lowerCamelCase__			(							unittest.TestCase):




		'''simple docstring'''



		@cached_property
		def 		lowerCAmelCase__      (self )     ->			Optional[Any]:

						"""simple docstring"""






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



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

						"""simple docstring"""






						lowerCAmelCase__     :				Any												=     BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(lowerCAmelCase_ )

						lowerCAmelCase__     :				int												=     self.default_image_processor
						lowerCAmelCase__     :				Optional[int]												=     prepare_img()
						lowerCAmelCase__     :				int												=     image_processor(images=lowerCAmelCase_		,return_tensors='''pt''' ).to(lowerCAmelCase_ )

						# forward pass
						with torch.no_grad():
										lowerCAmelCase__     :				Tuple												=     model(**lowerCAmelCase_ )

						# verify the logits
						lowerCAmelCase__     :				Tuple												=     torch.Size((1, 10_00) )
						self.assertEqual(outputs.logits.shape		,lowerCAmelCase_ )

						lowerCAmelCase__     :				Optional[Any]												=     torch.tensor([[-0.6526, -0.5263, -1.4398]] ).to(lowerCAmelCase_ )

						self.assertTrue(torch.allclose(outputs.logits[0, :3]		,lowerCAmelCase_		,atol=1e-4 ) )



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




		'''simple docstring'''



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

		snake_case_	=False



		def 		lowerCAmelCase__      (self )     ->			Optional[int]:

						"""simple docstring"""






						lowerCAmelCase__     :				Any												=     BitModelTester(self )








from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


_snake_case  :    Union[str, Any]						    =  {
    'configuration_layoutlmv3': [
        'LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'LayoutLMv3Config',
        'LayoutLMv3OnnxConfig',
    ],
    'processing_layoutlmv3': ['LayoutLMv3Processor'],
    'tokenization_layoutlmv3': ['LayoutLMv3Tokenizer'],
}

try:
     if not is_tokenizers_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _snake_case  :    Union[str, Any]						    =  ['LayoutLMv3TokenizerFast']

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _snake_case  :    List[str]						    =  [
         'LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST',
         'LayoutLMv3ForQuestionAnswering',
         'LayoutLMv3ForSequenceClassification',
         'LayoutLMv3ForTokenClassification',
         'LayoutLMv3Model',
         'LayoutLMv3PreTrainedModel',
     ]

try:
     if not is_tf_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _snake_case  :    Optional[Any]						    =  [
         'TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST',
         'TFLayoutLMv3ForQuestionAnswering',
         'TFLayoutLMv3ForSequenceClassification',
         'TFLayoutLMv3ForTokenClassification',
         'TFLayoutLMv3Model',
         'TFLayoutLMv3PreTrainedModel',
     ]

try:
     if not is_vision_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     _snake_case  :    Tuple						    =  ['LayoutLMv3FeatureExtractor']
     _snake_case  :    str						    =  ['LayoutLMv3ImageProcessor']


if TYPE_CHECKING:
     from .configuration_layoutlmva import (
         LAYOUTLMV3_PRETRAINED_CONFIG_ARCHIVE_MAP,
         LayoutLMvaConfig,
         LayoutLMvaOnnxConfig,
     )
     from .processing_layoutlmva import LayoutLMvaProcessor
     from .tokenization_layoutlmva import LayoutLMvaTokenizer

     try:
          if not is_tokenizers_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_layoutlmva import (
              LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
              LayoutLMvaForQuestionAnswering,
              LayoutLMvaForSequenceClassification,
              LayoutLMvaForTokenClassification,
              LayoutLMvaModel,
              LayoutLMvaPreTrainedModel,
          )

     try:
          if not is_tf_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_tf_layoutlmva import (
              TF_LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST,
              TFLayoutLMvaForQuestionAnswering,
              TFLayoutLMvaForSequenceClassification,
              TFLayoutLMvaForTokenClassification,
              TFLayoutLMvaModel,
              TFLayoutLMvaPreTrainedModel,
          )

     try:
          if not is_vision_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor
          from .image_processing_layoutlmva import LayoutLMvaImageProcessor

else:
     import sys

     _snake_case  :    Any						    =  _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)

