



"""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  __SCREAMING_SNAKE_CASE			:


					'''simple docstring'''





					def __init__( self							:						List[Any],						lowerCamelCase							:						List[Any],						lowerCamelCase							:						Dict=3,						lowerCamelCase							:						Optional[Any]=32,						lowerCamelCase							:						Tuple=3,						lowerCamelCase							:						Optional[Any]=10,						lowerCamelCase							:						Dict=[8, 16, 32, 64],						lowerCamelCase							:						Dict=[1, 1, 2, 1],						lowerCamelCase							:						Tuple=True,						lowerCamelCase							:						Any=True,						lowerCamelCase							:						Any="relu",						lowerCamelCase							:						List[str]=3,						lowerCamelCase							:						Union[str, Any]=None,						lowerCamelCase							:						Optional[int]=["stage2", "stage3", "stage4"],						lowerCamelCase							:						List[str]=[2, 3, 4],						lowerCamelCase							:						Union[str, Any]=1,						)->      Any:
								lowerCamelCase__	:	str				=parent
								lowerCamelCase__	:	Union[str, Any]				=batch_size
								lowerCamelCase__	:	List[Any]				=image_size
								lowerCamelCase__	:	List[str]				=num_channels
								lowerCamelCase__	:	Dict				=embeddings_size
								lowerCamelCase__	:	Optional[int]				=hidden_sizes
								lowerCamelCase__	:	Optional[Any]				=depths
								lowerCamelCase__	:	List[Any]				=is_training
								lowerCamelCase__	:	List[str]				=use_labels
								lowerCamelCase__	:	Tuple				=hidden_act
								lowerCamelCase__	:	Any				=num_labels
								lowerCamelCase__	:	Tuple				=scope
								lowerCamelCase__	:	List[str]				=len(lowerCamelCase  )
								lowerCamelCase__	:	Union[str, Any]				=out_features
								lowerCamelCase__	:	Union[str, Any]				=out_indices
								lowerCamelCase__	:	List[str]				=num_groups



					def 	snake_case ( self							:						str  )->      Tuple:
								lowerCamelCase__	:	str				=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

								lowerCamelCase__	:	Any				=None
								if self.use_labels:
											lowerCamelCase__	:	List[Any]				=ids_tensor([self.batch_size],						self.num_labels  )

								lowerCamelCase__	:	Dict				=self.get_config()

								return config, pixel_values, labels



					def 	snake_case ( self							:						Union[str, Any]  )->      Dict:
								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 	snake_case ( self							:						Optional[Any],						lowerCamelCase							:						str,						lowerCamelCase							:						int,						lowerCamelCase							:						Optional[Any]  )->      List[Any]:
								lowerCamelCase__	:	Dict				=BitModel(config=lowerCamelCase  )
								model.to(lowerCamelCase  )
								model.eval()
								lowerCamelCase__	:	Dict				=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 	snake_case ( self							:						str,						lowerCamelCase							:						Optional[Any],						lowerCamelCase							:						Union[str, Any],						lowerCamelCase							:						List[str]  )->      str:
								lowerCamelCase__	:	Dict				=self.num_labels
								lowerCamelCase__	:	List[str]				=BitForImageClassification(lowerCamelCase  )
								model.to(lowerCamelCase  )
								model.eval()
								lowerCamelCase__	:	Optional[Any]				=model(lowerCamelCase,						labels=lowerCamelCase  )
								self.parent.assertEqual(result.logits.shape,						(self.batch_size, self.num_labels)  )



					def 	snake_case ( self							:						Any,						lowerCamelCase							:						List[Any],						lowerCamelCase							:						Union[str, Any],						lowerCamelCase							:						Optional[int]  )->      Optional[Any]:
								lowerCamelCase__	:	Union[str, Any]				=BitBackbone(config=lowerCamelCase  )
								model.to(lowerCamelCase  )
								model.eval()
								lowerCamelCase__	:	str				=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__	:	Dict				=None
								lowerCamelCase__	:	Optional[int]				=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 	snake_case ( self							:						Any  )->      List[Any]:
								lowerCamelCase__	:	Dict				=self.prepare_config_and_inputs()
								lowerCamelCase__			,     lowerCamelCase__			,     lowerCamelCase__	:	Union[str, Any]				=config_and_inputs
								lowerCamelCase__	:	str				={'''pixel_values''': pixel_values}
								return config, inputs_dict





@require_torch
class  __SCREAMING_SNAKE_CASE			(   lowerCAmelCase_	,     lowerCAmelCase_	,     unittest.TestCase     ):


					'''simple docstring'''





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

					_a         =							False
					_a         =							False
					_a         =							False
					_a         =							False
					_a         =							False



					def 	snake_case ( self							:						Union[str, Any]  )->      Union[str, Any]:
								lowerCamelCase__	:	Dict				=BitModelTester(self  )
								lowerCamelCase__	:	int				=ConfigTester(self,						config_class=lowerCamelCase,						has_text_modality=lowerCamelCase  )



					def 	snake_case ( self							:						Optional[Any]  )->      str:
								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 	snake_case ( self							:						List[Any]  )->      List[Any]:
								return



					@unittest.skip(reason='''Bit does not output attentions'''  )
					def 	snake_case ( self							:						Optional[Any]  )->      List[Any]:
								pass



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



					@unittest.skip(reason='''Bit does not support input and output embeddings'''  )
					def 	snake_case ( self							:						Any  )->      str:
								pass



					def 	snake_case ( self							:						Union[str, Any]  )->      Any:
								lowerCamelCase__			,     lowerCamelCase__	:	List[str]				=self.model_tester.prepare_config_and_inputs_for_common()

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

											lowerCamelCase__	:	Dict				=['''pixel_values''']
											self.assertListEqual(arg_names[:1],						lowerCamelCase  )



					def 	snake_case ( self							:						int  )->      Optional[Any]:
								lowerCamelCase__	:	Any				=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowerCamelCase  )



					def 	snake_case ( self							:						Dict  )->      List[str]:
								lowerCamelCase__	:	Optional[Any]				=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_backbone(*lowerCamelCase  )



					def 	snake_case ( self							:						Optional[int]  )->      Optional[Any]:
								lowerCamelCase__			,     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=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 	snake_case ( self							:						List[str]  )->      List[str]:
								def check_hidden_states_output(lowerCamelCase							:						Optional[int],						lowerCamelCase							:						List[str],						lowerCamelCase							:						Optional[Any]  ):
											lowerCamelCase__	:	Tuple				=model_class(lowerCamelCase  )
											model.to(lowerCamelCase  )
											model.eval()

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

											lowerCamelCase__	:	Optional[int]				=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__	:	Tuple				=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__	:	List[Any]				=layer_type
														lowerCamelCase__	:	Optional[int]				=True
														check_hidden_states_output(lowerCamelCase,						lowerCamelCase,						lowerCamelCase  )

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

														check_hidden_states_output(lowerCamelCase,						lowerCamelCase,						lowerCamelCase  )



					@unittest.skip(reason='''Bit does not use feedforward chunking'''  )
					def 	snake_case ( self							:						List[Any]  )->      int:
								pass



					def 	snake_case ( self							:						Optional[int]  )->      List[str]:
								lowerCamelCase__	:	Any				=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*lowerCamelCase  )




					@slow
					def 	snake_case ( self							:						Any  )->      int:
								for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											lowerCamelCase__	:	str				=BitModel.from_pretrained(lowerCamelCase  )
											self.assertIsNotNone(lowerCamelCase  )






def   snake_case__     (	):
			"""simple docstring"""
			lowerCamelCase__	:	int				=Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)
			return image

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


					'''simple docstring'''





					@cached_property
					def 	snake_case ( self							:						Tuple  )->      Any:
								return (
								    BitImageProcessor.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]  ) if is_vision_available() else None
								)




					@slow
					def 	snake_case ( self							:						Dict  )->      List[str]:
								lowerCamelCase__	:	Optional[Any]				=BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]  ).to(lowerCamelCase  )

								lowerCamelCase__	:	Union[str, Any]				=self.default_image_processor
								lowerCamelCase__	:	Dict				=prepare_img()
								lowerCamelCase__	:	Union[str, Any]				=image_processor(images=lowerCamelCase,						return_tensors='''pt'''  ).to(lowerCamelCase  )

								# forward pass
								with torch.no_grad():
											lowerCamelCase__	:	List[Any]				=model(**lowerCamelCase  )

								# verify the logits
								lowerCamelCase__	:	str				=torch.Size((1, 1000)  )
								self.assertEqual(outputs.logits.shape,						lowerCamelCase  )

								lowerCamelCase__	:	Dict				=torch.tensor([[-0.6_526, -0.5_263, -1.4_398]]  ).to(lowerCamelCase  )

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





@require_torch
class  __SCREAMING_SNAKE_CASE			(   lowerCAmelCase_	,     unittest.TestCase     ):


					'''simple docstring'''





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

					_a         =							False



					def 	snake_case ( self							:						Optional[Any]  )->      List[Any]:
								lowerCamelCase__	:	List[str]				=BitModelTester(self  )









"""simple docstring"""






def   snake_case__     (	__lowerCamelCase       :					int = 10							,	__lowerCamelCase       :					int = 22			):
			"""simple docstring"""
			lowerCamelCase__	:	Optional[Any]				=range(1							,	__lowerCamelCase			)
			lowerCamelCase__	:	str				=range(1							,	__lowerCamelCase			)
			return sum(
			    1 for power in powers for base in bases if len(str(base**power			)			) == power			)


if __name__ == "__main__":
		print(f'{solution(1_0, 2_2) = }')




