



import inspect
import tempfile
import unittest

from huggingface_hub import hf_hub_download

from transformers import is_torch_available
from transformers.testing_utils import is_flaky, 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


__SCREAMING_SNAKE_CASE           =  1e-4

if is_torch_available():
							import torch

							from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel
							from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder

@require_torch
class 	lowerCamelCase_    :






						'''simple docstring'''







						def __init__(	self      :			Optional[Any]	,	__lowerCamelCase      :			int	,	__lowerCamelCase      :			Dict=16	,	__lowerCamelCase      :			Union[str, Any]=13	,	__lowerCamelCase      :			Tuple=7	,	__lowerCamelCase      :			List[str]=14	,	__lowerCamelCase      :			str=10	,	__lowerCamelCase      :			Dict=19	,	__lowerCamelCase      :			int=5	,	__lowerCamelCase      :			Any=4	,	__lowerCamelCase      :			int=True	,	__lowerCamelCase      :			str=16	,	__lowerCamelCase      :			Any=2	,	__lowerCamelCase      :			int=4	,	__lowerCamelCase      :			Union[str, Any]=4	,	__lowerCamelCase      :			Tuple="gelu"	,	__lowerCamelCase      :			List[str]=0.1	,	__lowerCamelCase      :			int=0.1	,	__lowerCamelCase      :			Tuple=[1, 2, 3, 4, 5]	,	__lowerCamelCase      :			Tuple=25	,	__lowerCamelCase      :			Optional[Any]=5	,	)      ->				List[Any]:
									A							:					Optional[Any]              =						d_model
									A							:					int              =						parent
									A							:					str              =						batch_size
									A							:					Union[str, Any]              =						prediction_length
									A							:					Union[str, Any]              =						context_length
									A							:					List[Any]              =						cardinality
									A							:					int              =						num_time_features
									A							:					Tuple              =						lags_sequence
									A							:					Union[str, Any]              =						embedding_dimension
									A							:					Optional[Any]              =						is_training
									A							:					Optional[Any]              =						hidden_size
									A							:					Tuple              =						num_hidden_layers
									A							:					Any              =						num_attention_heads
									A							:					Union[str, Any]              =						intermediate_size
									A							:					int              =						hidden_act
									A							:					int              =						hidden_dropout_prob
									A							:					Dict              =						attention_probs_dropout_prob

									A							:					str              =						context_length
									A							:					Any              =						prediction_length + label_length
									A							:					Any              =						label_length

									A							:					Tuple              =						moving_average
									A							:					Tuple              =						autocorrelation_factor







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[Any] )      ->				str:
									return AutoformerConfig(
									    d_model=self.d_model	,	encoder_layers=self.num_hidden_layers	,	decoder_layers=self.num_hidden_layers	,	encoder_attention_heads=self.num_attention_heads	,	decoder_attention_heads=self.num_attention_heads	,	encoder_ffn_dim=self.intermediate_size	,	decoder_ffn_dim=self.intermediate_size	,	dropout=self.hidden_dropout_prob	,	attention_dropout=self.attention_probs_dropout_prob	,	prediction_length=self.prediction_length	,	context_length=self.context_length	,	label_length=self.label_length	,	lags_sequence=self.lags_sequence	,	num_time_features=self.num_time_features	,	num_static_categorical_features=1	,	cardinality=[self.cardinality]	,	embedding_dimension=[self.embedding_dimension]	,	moving_average=self.moving_average	,	)







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[int]	,	__lowerCamelCase      :			Any )      ->				List[str]:
									A							:					List[Any]              =						config.context_length + max(config.lags_sequence )

									A							:					Optional[int]              =						ids_tensor([self.batch_size, 1]	,	config.cardinality[0] )
									A							:					int              =						floats_tensor([self.batch_size, _past_length, config.num_time_features] )
									A							:					Tuple              =						floats_tensor([self.batch_size, _past_length] )
									A							:					str              =						floats_tensor([self.batch_size, _past_length] ) > 0.5

									# decoder inputs
									A							:					int              =						floats_tensor([self.batch_size, config.prediction_length, config.num_time_features] )
									A							:					List[Any]              =						floats_tensor([self.batch_size, config.prediction_length] )

									A							:					Optional[Any]              =						{
									    'past_values': past_values,
									    'static_categorical_features': static_categorical_features,
									    'past_time_features': past_time_features,
									    'past_observed_mask': past_observed_mask,
									    'future_time_features': future_time_features,
									    'future_values': future_values,
									}
									return inputs_dict







						def 			SCREAMING_SNAKE_CASE__		(	self      :			int )      ->				int:
									A							:					Optional[int]              =						self.get_config()
									A							:					Dict              =						self.prepare_autoformer_inputs_dict(_A )
									return config, inputs_dict







						def 			SCREAMING_SNAKE_CASE__		(	self      :			int )      ->				Union[str, Any]:
									A							:					Optional[int]              =						self.prepare_config_and_inputs()
									return config, inputs_dict







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[Any]	,	__lowerCamelCase      :			int	,	__lowerCamelCase      :			Union[str, Any] )      ->				str:
									A							:					Optional[int]              =						AutoformerModel(config=_A ).to(_A ).eval()
									A							:					Tuple              =						model(**_A )

									A							:					Any              =						outputs.encoder_last_hidden_state
									A							:					List[Any]              =						outputs.last_hidden_state

									with tempfile.TemporaryDirectory() as tmpdirname:
												A							:					Union[str, Any]              =						model.get_encoder()
												encoder.save_pretrained(_A )
												A							:					List[Any]              =						AutoformerEncoder.from_pretrained(_A ).to(_A )

									A							:					Dict              =						model.create_network_inputs(**_A )
									A							:					List[Any]              =						model.decomposition_layer(transformer_inputs[:, : config.context_length, ...] )

									A							:					Tuple              =						torch.cat(
									    (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...])	,	dim=-1	,	)
									A							:					List[str]              =						encoder(inputs_embeds=_A )[0]
									self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1e-3 )

									A							:					str              =						(
									    torch.mean(transformer_inputs[:, : config.context_length, ...]	,	dim=1 )
									    .unsqueeze(1 )
									    .repeat(1	,	config.prediction_length	,	1 )
									)
									A							:					str              =						torch.zeros(
									    [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]]	,	device=enc_input.device	,	)

									A							:					List[str]              =						torch.cat(
									    (
									        torch.cat((seasonal_input[:, -config.label_length :, ...], zeros)	,	dim=1 ),
									        feature[:, config.context_length - config.label_length :, ...],
									    )	,	dim=-1	,	)
									A							:					str              =						torch.cat(
									    (
									        torch.cat((trend_input[:, -config.label_length :, ...], mean)	,	dim=1 ),
									        feature[:, config.context_length - config.label_length :, ...],
									    )	,	dim=-1	,	)

									with tempfile.TemporaryDirectory() as tmpdirname:
												A							:					Dict              =						model.get_decoder()
												decoder.save_pretrained(_A )
												A							:					Dict              =						AutoformerDecoder.from_pretrained(_A ).to(_A )

									A							:					Tuple              =						decoder(
									    trend=_A	,	inputs_embeds=_A	,	encoder_hidden_states=_A	,	)[0]

									self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1e-3 )







@require_torch
class 	lowerCamelCase_    ( a__						,a__						,unittest.TestCase						):






						'''simple docstring'''
						a__   =  (AutoformerModel, AutoformerForPrediction) if is_torch_available() else ()
						a__   =  (AutoformerForPrediction,) if is_torch_available() else ()
						a__   =  {"feature-extraction": AutoformerModel} if is_torch_available() else {}
						a__   =  False
						a__   =  False
						a__   =  False
						a__   =  False
						a__   =  False
						a__   =  False







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[int] )      ->				List[str]:
									A							:					Optional[Any]              =						AutoformerModelTester(self )
									A							:					List[Any]              =						ConfigTester(self	,	config_class=_A	,	has_text_modality=_A )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any] )      ->				Optional[int]:
									self.config_tester.run_common_tests()







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Tuple )      ->				int:
									A							:					Tuple              =						self.model_tester.prepare_config_and_inputs()
									for model_class in self.all_model_classes:
												A							:					Dict              =						model_class(_A )

												with tempfile.TemporaryDirectory() as tmpdirname:
															model.save_pretrained(_A )
															A							:					Optional[Any]              =						model_class.from_pretrained(_A	,	output_loading_info=_A )
												self.assertEqual(info["missing_keys"]	,	[] )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[Any] )      ->				Dict:
									A							:					List[Any]              =						self.model_tester.prepare_config_and_inputs_for_common()
									self.model_tester.check_encoder_decoder_model_standalone(*_A )







						@unittest.skip(reason="Model has no tokens embeddings" )
						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[str] )      ->				Any:
									pass







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Dict )      ->				Union[str, Any]:
									A							:					str              =						inspect.signature(getattr(_A	,	"forward" ) )
									# The main input is the name of the argument after `self`
									A							:					str              =						list(model_signature.parameters.keys() )[1]
									self.assertEqual(AutoformerModel.main_input_name	,	_A )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			str )      ->				Union[str, Any]:
									A							:					Union[str, Any]              =						self.model_tester.prepare_config_and_inputs_for_common()

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

												A							:					Dict              =						[
												    'past_values',
												    'past_time_features',
												    'past_observed_mask',
												    'static_categorical_features',
												    'static_real_features',
												    'future_values',
												    'future_time_features',
												]

												if model.__class__.__name__ in ["AutoformerForPrediction"]:
															expected_arg_names.append("future_observed_mask" )

												expected_arg_names.extend(
												    [
												        "decoder_attention_mask",
												        "head_mask",
												        "decoder_head_mask",
												        "cross_attn_head_mask",
												        "encoder_outputs",
												        "past_key_values",
												        "output_hidden_states",
												        "output_attentions",
												        "use_cache",
												        "return_dict",
												    ] )

												self.assertListEqual(arg_names[: len(_A )]	,	_A )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[str] )      ->				Union[str, Any]:
									A							:					Dict              =						self.model_tester.prepare_config_and_inputs_for_common()
									A							:					Any              =						True

									A							:					str              =						getattr(self.model_tester	,	"seq_length"	,	_A )
									A							:					List[str]              =						getattr(self.model_tester	,	"decoder_seq_length"	,	_A )
									A							:					int              =						getattr(self.model_tester	,	"encoder_seq_length"	,	_A )
									A							:					List[Any]              =						getattr(self.model_tester	,	"d_model"	,	_A )
									A							:					str              =						getattr(self.model_tester	,	"num_attention_heads"	,	_A )
									A							:					Optional[Any]              =						d_model // num_attention_heads

									for model_class in self.all_model_classes:
												A							:					Any              =						True
												A							:					Any              =						False
												A							:					int              =						True
												A							:					int              =						model_class(_A )
												model.to(_A )
												model.eval()
												with torch.no_grad():
															A							:					Union[str, Any]              =						model(**self._prepare_for_class(_A	,	_A ) )
												A							:					Any              =						outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
												self.assertEqual(len(_A )	,	self.model_tester.num_hidden_layers )

												# check that output_attentions also work using config
												del inputs_dict["output_attentions"]
												A							:					int              =						True
												A							:					List[str]              =						model_class(_A )
												model.to(_A )
												model.eval()
												with torch.no_grad():
															A							:					Optional[Any]              =						model(**self._prepare_for_class(_A	,	_A ) )
												A							:					List[Any]              =						outputs.encoder_attentions
												self.assertEqual(len(_A )	,	self.model_tester.num_hidden_layers )

												self.assertListEqual(
												    list(attentions[0].shape[-3:] )	,	[self.model_tester.num_attention_heads, encoder_seq_length, dim]	,	)
												A							:					Union[str, Any]              =						len(_A )

												A							:					Union[str, Any]              =						7

												if "last_hidden_state" in outputs:
															correct_outlen += 1

												if "trend" in outputs:
															correct_outlen += 1

												if "past_key_values" in outputs:
															correct_outlen += 1  # past_key_values have been returned

												if "loss" in outputs:
															correct_outlen += 1

												if "params" in outputs:
															correct_outlen += 1

												self.assertEqual(_A	,	_A )

												# decoder attentions
												A							:					Dict              =						outputs.decoder_attentions
												self.assertIsInstance(_A	,	(list, tuple) )
												self.assertEqual(len(_A )	,	self.model_tester.num_hidden_layers )
												self.assertListEqual(
												    list(decoder_attentions[0].shape[-3:] )	,	[self.model_tester.num_attention_heads, decoder_seq_length, dim]	,	)

												# cross attentions
												A							:					Any              =						outputs.cross_attentions
												self.assertIsInstance(_A	,	(list, tuple) )
												self.assertEqual(len(_A )	,	self.model_tester.num_hidden_layers )
												self.assertListEqual(
												    list(cross_attentions[0].shape[-3:] )	,	[self.model_tester.num_attention_heads, decoder_seq_length, dim]	,	)

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

									self.assertEqual(out_len + 2	,	len(_A ) )

									A							:					str              =						outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions

									self.assertEqual(len(_A )	,	self.model_tester.num_hidden_layers )
									self.assertListEqual(
									    list(self_attentions[0].shape[-3:] )	,	[self.model_tester.num_attention_heads, encoder_seq_length, dim]	,	)







						@is_flaky()
						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[str] )      ->				str:
									super().test_retain_grad_hidden_states_attentions()







def 	UpperCAmelCase   (						_lowerCamelCase="train-batch.pt"    ):
			A							:					int              =						hf_hub_download(repo_id="hf-internal-testing/tourism-monthly-batch" ,    filename=_lowerCamelCase ,    repo_type="dataset"    )
			A							:					Optional[Any]              =						torch.load(_lowerCamelCase ,    map_location=_lowerCamelCase    )
			return batch






@require_torch
@slow
class 	lowerCamelCase_    ( unittest.TestCase						):






						'''simple docstring'''







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[int] )      ->				List[Any]:
									A							:					Optional[Any]              =						AutoformerModel.from_pretrained("huggingface/autoformer-tourism-monthly" ).to(_A )
									A							:					Dict              =						prepare_batch()

									with torch.no_grad():
												A							:					Union[str, Any]              =						model(
												    past_values=batch["past_values"]	,	past_time_features=batch["past_time_features"]	,	past_observed_mask=batch["past_observed_mask"]	,	static_categorical_features=batch["static_categorical_features"]	,	future_values=batch["future_values"]	,	future_time_features=batch["future_time_features"]	,	)[0]

									A							:					Any              =						torch.Size(
									    (64, model.config.prediction_length + model.config.label_length, model.config.feature_size) )
									self.assertEqual(output.shape	,	_A )

									A							:					Dict              =						torch.tensor(
									    [[0.3593, -1.3398, 0.6330], [0.2279, 1.5396, -0.1792], [0.0450, 1.3225, -0.2335]]	,	device=_A )
									self.assertTrue(torch.allclose(output[0, :3, :3]	,	_A	,	atol=_A ) )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			str )      ->				Tuple:
									A							:					Any              =						AutoformerForPrediction.from_pretrained("huggingface/autoformer-tourism-monthly" ).to(_A )
									A							:					Any              =						prepare_batch("val-batch.pt" )
									with torch.no_grad():
												A							:					List[str]              =						model(
												    past_values=batch["past_values"]	,	past_time_features=batch["past_time_features"]	,	past_observed_mask=batch["past_observed_mask"]	,	static_categorical_features=batch["static_categorical_features"]	,	).encoder_last_hidden_state
									A							:					Dict              =						torch.Size((64, model.config.context_length, model.config.d_model) )
									self.assertEqual(output.shape	,	_A )

									A							:					Tuple              =						torch.tensor(
									    [[-0.0734, -0.9036, 0.8358], [4.7186, 2.4113, 1.9581], [1.7953, 2.3558, 1.2970]]	,	device=_A )
									self.assertTrue(torch.allclose(output[0, :3, :3]	,	_A	,	atol=_A ) )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[int] )      ->				Union[str, Any]:
									A							:					Union[str, Any]              =						AutoformerForPrediction.from_pretrained("huggingface/autoformer-tourism-monthly" ).to(_A )
									A							:					int              =						prepare_batch("val-batch.pt" )
									with torch.no_grad():
												A							:					Any              =						model.generate(
												    static_categorical_features=batch["static_categorical_features"]	,	past_time_features=batch["past_time_features"]	,	past_values=batch["past_values"]	,	future_time_features=batch["future_time_features"]	,	past_observed_mask=batch["past_observed_mask"]	,	)
									A							:					List[str]              =						torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length) )
									self.assertEqual(outputs.sequences.shape	,	_A )

									A							:					Dict              =						torch.tensor([31_30.67_63, 40_56.52_93, 70_53.07_86]	,	device=_A )
									A							:					Optional[int]              =						outputs.sequences.mean(dim=1 )
									self.assertTrue(torch.allclose(mean_prediction[0, -3:]	,	_A	,	rtol=1e-1 ) )




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      :			int	,	__lowerCamelCase      :			Any	,	__lowerCamelCase      :			Dict=3	,	__lowerCamelCase      :			Dict=32	,	__lowerCamelCase      :			Any=3	,	__lowerCamelCase      :			Optional[Any]=10	,	__lowerCamelCase      :			str=[8, 16, 32, 64]	,	__lowerCamelCase      :			Dict=[1, 1, 2, 1]	,	__lowerCamelCase      :			Optional[int]=True	,	__lowerCamelCase      :			Optional[int]=True	,	__lowerCamelCase      :			List[str]="relu"	,	__lowerCamelCase      :			str=3	,	__lowerCamelCase      :			Optional[Any]=None	,	__lowerCamelCase      :			Optional[Any]=["stage2", "stage3", "stage4"]	,	__lowerCamelCase      :			Tuple=[2, 3, 4]	,	__lowerCamelCase      :			Any=1	,	)      ->				int:
									A							:					Optional[int]              =						parent
									A							:					List[str]              =						batch_size
									A							:					Tuple              =						image_size
									A							:					List[str]              =						num_channels
									A							:					List[str]              =						embeddings_size
									A							:					List[str]              =						hidden_sizes
									A							:					str              =						depths
									A							:					Optional[Any]              =						is_training
									A							:					int              =						use_labels
									A							:					Optional[int]              =						hidden_act
									A							:					List[Any]              =						num_labels
									A							:					List[str]              =						scope
									A							:					str              =						len(__lowerCamelCase )
									A							:					Optional[int]              =						out_features
									A							:					str              =						out_indices
									A							:					Optional[int]              =						num_groups







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Any )      ->				Optional[Any]:
									A							:					Optional[Any]              =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

									A							:					Tuple              =						self.get_config()

									return config, pixel_values, labels







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







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any]	,	__lowerCamelCase      :			Any	,	__lowerCamelCase      :			Optional[Any]	,	__lowerCamelCase      :			str )      ->				List[Any]:
									A							:					Dict              =						BitBackbone(config=__lowerCamelCase )
									model.to(__lowerCamelCase )
									model.eval()
									A							:					Optional[Any]              =						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
									A							:					Optional[Any]              =						None
									A							:					Optional[int]              =						BitBackbone(config=__lowerCamelCase )
									model.to(__lowerCamelCase )
									model.eval()
									A							:					Any              =						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 			SCREAMING_SNAKE_CASE__		(	self      :			Tuple )      ->				Dict:
									A							:					List[str]              =						self.prepare_config_and_inputs()
									A       , A       , A							:					Tuple              =						config_and_inputs
									A							:					Tuple              =						{"pixel_values": pixel_values}
									return config, inputs_dict







@require_torch
class 	lowerCamelCase_    ( _A						,_A						,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 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any] )      ->				Dict:
									A							:					Any              =						BitModelTester(self )
									A							:					Optional[int]              =						ConfigTester(self	,	config_class=__lowerCamelCase	,	has_text_modality=__lowerCamelCase )







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







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







						@unittest.skip(reason="Bit does not use inputs_embeds" )
						def 			SCREAMING_SNAKE_CASE__		(	self      :			Dict )      ->				Optional[Any]:
									pass







						@unittest.skip(reason="Bit does not support input and output embeddings" )
						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any] )      ->				List[str]:
									pass







						def 			SCREAMING_SNAKE_CASE__		(	self      :			int )      ->				str:
									A       , A							:					int              =						self.model_tester.prepare_config_and_inputs_for_common()

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

												A							:					List[str]              =						["pixel_values"]
												self.assertListEqual(arg_names[:1]	,	__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[str] )      ->				Union[str, Any]:
									A							:					List[Any]              =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Any )      ->				List[str]:
									A							:					int              =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_backbone(*__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[Any] )      ->				Optional[int]:
									A       , A							:					List[str]              =						self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
												A							:					Optional[int]              =						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 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any] )      ->				Optional[Any]:
									def check_hidden_states_output(__lowerCamelCase      :			str	,	__lowerCamelCase      :			Tuple	,	__lowerCamelCase      :			Union[str, Any] ):
												A							:					Dict              =						model_class(__lowerCamelCase )
												model.to(__lowerCamelCase )
												model.eval()

												with torch.no_grad():
															A							:					List[Any]              =						model(**self._prepare_for_class(__lowerCamelCase	,	__lowerCamelCase ) )

												A							:					List[str]              =						outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

												A							:					List[Any]              =						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]	,	)

									A       , A							:					Optional[int]              =						self.model_tester.prepare_config_and_inputs_for_common()
									A							:					Dict              =						["preactivation", "bottleneck"]
									for model_class in self.all_model_classes:
												for layer_type in layers_type:
															A							:					Dict              =						layer_type
															A							:					Union[str, Any]              =						True
															check_hidden_states_output(__lowerCamelCase	,	__lowerCamelCase	,	__lowerCamelCase )

															# check that output_hidden_states also work using config
															del inputs_dict["output_hidden_states"]
															A							:					Union[str, Any]              =						True

															check_hidden_states_output(__lowerCamelCase	,	__lowerCamelCase	,	__lowerCamelCase )







						@unittest.skip(reason="Bit does not use feedforward chunking" )
						def 			SCREAMING_SNAKE_CASE__		(	self      :			Any )      ->				Optional[Any]:
									pass







						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[str] )      ->				Optional[int]:
									A							:					Union[str, Any]              =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase )







						@slow
						def 			SCREAMING_SNAKE_CASE__		(	self      :			Any )      ->				List[Any]:
									for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												A							:					Optional[Any]              =						BitModel.from_pretrained(__lowerCamelCase )
												self.assertIsNotNone(__lowerCamelCase )







def 	UpperCAmelCase   (						):
			A							:					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 			SCREAMING_SNAKE_CASE__		(	self      :			int )      ->				Union[str, Any]:
									return (
									    BitImageProcessor.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ) if is_vision_available() else None
									)







						@slow
						def 			SCREAMING_SNAKE_CASE__		(	self      :			Any )      ->				Dict:
									A							:					Union[str, Any]              =						BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0] ).to(__lowerCamelCase )

									A							:					List[Any]              =						self.default_image_processor
									A							:					List[Any]              =						prepare_img()
									A							:					Tuple              =						image_processor(images=__lowerCamelCase	,	return_tensors="pt" ).to(__lowerCamelCase )

									# forward pass
									with torch.no_grad():
												A							:					Union[str, Any]              =						model(**__lowerCamelCase )

									# verify the logits
									A							:					str              =						torch.Size((1, 10_00) )
									self.assertEqual(outputs.logits.shape	,	__lowerCamelCase )

									A							:					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_    ( _A						,unittest.TestCase						):






						'''simple docstring'''
						a__   =  (BitBackbone,) if is_torch_available() else ()
						a__   =  BitConfig

						a__   =  False







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[int] )      ->				Optional[int]:
									A							:					Union[str, Any]              =						BitModelTester(self )