

import unittest

from transformers import AutoTokenizer, FalconConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers import (
         FalconForCausalLM,
         FalconForQuestionAnswering,
         FalconForSequenceClassification,
         FalconForTokenClassification,
         FalconModel,
     )





class  __UpperCAmelCase					:



   def __init__(							self							:					str,		__A							:					List[Any],		__A							:					List[str]=3,		__A							:					str=7,		__A							:					List[Any]=True,		__A							:					List[str]=True,		__A							:					Union[str, Any]=False,		__A							:					Dict=True,		__A							:					str=9_9,		__A							:					Tuple=3_2,		__A							:					Optional[Any]=5,		__A							:					int=4,		__A							:					List[Any]=3_7,		__A							:					List[str]="gelu",		__A							:					str=0.1,		__A							:					Optional[int]=0.1,		__A							:					int=5_1_2,		__A							:					str=1_6,		__A							:					List[str]=2,		__A							:					int=0.0_2,		__A							:					List[str]=3,		__A							:					int=4,		__A							:					List[Any]=None,		):
     UpperCAmelCase	:							Any						=			parent
     UpperCAmelCase	:							Union[str, Any]						=			batch_size
     UpperCAmelCase	:							Optional[Any]						=			seq_length
     UpperCAmelCase	:							Dict						=			is_training
     UpperCAmelCase	:							Optional[int]						=			use_input_mask
     UpperCAmelCase	:							List[str]						=			use_token_type_ids
     UpperCAmelCase	:							List[str]						=			use_labels
     UpperCAmelCase	:							Dict						=			vocab_size
     UpperCAmelCase	:							List[Any]						=			hidden_size
     UpperCAmelCase	:							List[Any]						=			num_hidden_layers
     UpperCAmelCase	:							str						=			num_attention_heads
     UpperCAmelCase	:							Optional[int]						=			intermediate_size
     UpperCAmelCase	:							Optional[Any]						=			hidden_act
     UpperCAmelCase	:							Optional[int]						=			hidden_dropout_prob
     UpperCAmelCase	:							List[str]						=			attention_probs_dropout_prob
     UpperCAmelCase	:							int						=			max_position_embeddings
     UpperCAmelCase	:							Tuple						=			type_vocab_size
     UpperCAmelCase	:							int						=			type_sequence_label_size
     UpperCAmelCase	:							Optional[Any]						=			initializer_range
     UpperCAmelCase	:							Optional[Any]						=			num_labels
     UpperCAmelCase	:							Optional[Any]						=			num_choices
     UpperCAmelCase	:							List[Any]						=			scope



   def 	__magic_name__   (							self							:					List[str]						):
     UpperCAmelCase	:							Optional[Any]						=			ids_tensor([self.batch_size, self.seq_length],		self.vocab_size						)

     UpperCAmelCase	:							Union[str, Any]						=			None
     if self.use_input_mask:
       UpperCAmelCase	:							int						=			random_attention_mask([self.batch_size, self.seq_length]						)

     UpperCAmelCase	:							str						=			None

     UpperCAmelCase	:							List[str]						=			None
     UpperCAmelCase	:							Optional[int]						=			None
     UpperCAmelCase	:							Optional[int]						=			None
     if self.use_labels:
       UpperCAmelCase	:							List[Any]						=			ids_tensor([self.batch_size],		self.type_sequence_label_size						)
       UpperCAmelCase	:							Any						=			ids_tensor([self.batch_size, self.seq_length],		self.num_labels						)
       UpperCAmelCase	:							Optional[Any]						=			ids_tensor([self.batch_size],		self.num_choices						)

     UpperCAmelCase	:							Optional[int]						=			self.get_config()

     return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels



   def 	__magic_name__   (							self							:					Dict						):
     return FalconConfig(
         vocab_size=self.vocab_size,		hidden_size=self.hidden_size,		num_hidden_layers=self.num_hidden_layers,		num_attention_heads=self.num_attention_heads,		intermediate_size=self.intermediate_size,		hidden_act=self.hidden_act,		hidden_dropout_prob=self.hidden_dropout_prob,		attention_probs_dropout_prob=self.attention_probs_dropout_prob,		max_position_embeddings=self.max_position_embeddings,		type_vocab_size=self.type_vocab_size,		is_decoder=lowercase_,		initializer_range=self.initializer_range,		pad_token_id=1,		new_decoder_architecture=lowercase_,		)



   def 	__magic_name__   (							self							:					Optional[Any],		__A							:					List[str],		__A							:					Tuple,		__A							:					Optional[int],		__A							:					Tuple,		__A							:					Tuple,		__A							:					str,		__A							:					str						):
     UpperCAmelCase	:							Dict						=			FalconModel(config=lowercase_						)
     model.to(lowercase_						)
     model.eval()
     UpperCAmelCase	:							List[str]						=			model(lowercase_,		attention_mask=lowercase_						)
     UpperCAmelCase	:							Dict						=			model(lowercase_						)
     self.parent.assertEqual(result.last_hidden_state.shape,		(self.batch_size, self.seq_length, self.hidden_size)						)



   def 	__magic_name__   (							self							:					Union[str, Any],		__A							:					List[str],		__A							:					Tuple,		__A							:					Any,		__A							:					Optional[int],		__A							:					int,		__A							:					List[str],		__A							:					int,		__A							:					Tuple,		__A							:					Union[str, Any],		):
     UpperCAmelCase	:							str						=			True
     UpperCAmelCase	:							Any						=			FalconModel(lowercase_						)
     model.to(lowercase_						)
     model.eval()
     UpperCAmelCase	:							Dict						=			model(
         lowercase_,		attention_mask=lowercase_,		encoder_hidden_states=lowercase_,		encoder_attention_mask=lowercase_,		)
     UpperCAmelCase	:							List[str]						=			model(
         lowercase_,		attention_mask=lowercase_,		encoder_hidden_states=lowercase_,		)
     UpperCAmelCase	:							int						=			model(lowercase_,		attention_mask=lowercase_						)
     self.parent.assertEqual(result.last_hidden_state.shape,		(self.batch_size, self.seq_length, self.hidden_size)						)



   def 	__magic_name__   (							self							:					Optional[int],		__A							:					str,		__A							:					Union[str, Any],		__A							:					Optional[Any],		__A							:					Optional[Any],		__A							:					Tuple,		__A							:					List[Any],		__A							:					Optional[Any],		__A							:					List[str],		__A							:					int,		):
     UpperCAmelCase	:							List[Any]						=			FalconForCausalLM(config=lowercase_						)
     model.to(lowercase_						)
     model.eval()
     UpperCAmelCase	:							str						=			model(lowercase_,		attention_mask=lowercase_,		labels=lowercase_						)
     self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.seq_length, self.vocab_size)						)



   def 	__magic_name__   (							self							:					Dict,		__A							:					Optional[int],		__A							:					int,		__A							:					List[str],		__A							:					Dict,		__A							:					Union[str, Any],		__A							:					Optional[Any],		__A							:					Optional[Any],		__A							:					Union[str, Any],		__A							:					List[str],		):
     UpperCAmelCase	:							Optional[int]						=			True
     UpperCAmelCase	:							Optional[Any]						=			True
     UpperCAmelCase	:							Optional[Any]						=			FalconForCausalLM(config=lowercase_						)
     model.to(lowercase_						)
     model.eval()

     # first forward pass
     UpperCAmelCase	:							Tuple						=			model(
         lowercase_,		attention_mask=lowercase_,		encoder_hidden_states=lowercase_,		encoder_attention_mask=lowercase_,		use_cache=lowercase_,		)
     UpperCAmelCase	:							List[Any]						=			outputs.past_key_values

     # create hypothetical multiple next token and extent to next_input_ids
     UpperCAmelCase	:							Optional[Any]						=			ids_tensor((self.batch_size, 3),		config.vocab_size						)
     UpperCAmelCase	:							List[str]						=			ids_tensor((self.batch_size, 3),		vocab_size=2						)

     # append to next input_ids and
     UpperCAmelCase	:							Any						=			torch.cat([input_ids, next_tokens],		dim=-1						)
     UpperCAmelCase	:							Tuple						=			torch.cat([input_mask, next_mask],		dim=-1						)

     UpperCAmelCase	:							Any						=			model(
         lowercase_,		attention_mask=lowercase_,		encoder_hidden_states=lowercase_,		encoder_attention_mask=lowercase_,		output_hidden_states=lowercase_,		)['''hidden_states'''][0]
     UpperCAmelCase	:							List[Any]						=			model(
         lowercase_,		attention_mask=lowercase_,		encoder_hidden_states=lowercase_,		encoder_attention_mask=lowercase_,		past_key_values=lowercase_,		output_hidden_states=lowercase_,		)['''hidden_states'''][0]

     # select random slice
     UpperCAmelCase	:							Any						=			ids_tensor((1,),		output_from_past.shape[-1]						).item()
     UpperCAmelCase	:							Union[str, Any]						=			output_from_no_past[:, -3:, random_slice_idx].detach()
     UpperCAmelCase	:							Union[str, Any]						=			output_from_past[:, :, random_slice_idx].detach()

     self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]						)

     # test that outputs are equal for slice
     self.parent.assertTrue(torch.allclose(lowercase_,		lowercase_,		atol=1E-3						)						)





   def 	__magic_name__   (							self							:					Optional[int]						):
     UpperCAmelCase	:							Optional[int]						=			self.prepare_config_and_inputs()
     (
         (
         UpperCAmelCase
     )			,						(
         UpperCAmelCase
     )			,						(
         UpperCAmelCase
     )			,						(
         UpperCAmelCase
     )			,						(
         UpperCAmelCase
     )			,						(
         UpperCAmelCase
     )			,						(
         UpperCAmelCase
     )			,						
     )	:							Tuple						=			config_and_inputs
     UpperCAmelCase	:							Optional[Any]						=			{'''input_ids''': input_ids, '''attention_mask''': input_mask}
     return config, inputs_dict






@require_torch
class  __UpperCAmelCase					(	_lowerCamelCase      ,  _lowerCamelCase      ,  _lowerCamelCase      ,  unittest.TestCase						):
   UpperCamelCase       					=					(
       (
           FalconModel,
           FalconForCausalLM,
           FalconForSequenceClassification,
           FalconForTokenClassification,
           FalconForQuestionAnswering,
       )
       if is_torch_available()
       else ()
   )
   UpperCamelCase       					=					(FalconForCausalLM,) if is_torch_available() else ()
   UpperCamelCase       					=					(
       {
           """feature-extraction""": FalconModel,
           """text-classification""": FalconForSequenceClassification,
           """text-generation""": FalconForCausalLM,
           """question-answering""": FalconForQuestionAnswering,
           """token-classification""": FalconForTokenClassification,
           """zero-shot""": FalconForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   UpperCamelCase       					=					False
   UpperCamelCase       					=					False



   def 	__magic_name__   (							self							:					Optional[int]						):
     UpperCAmelCase	:							Optional[int]						=			FalconModelTester(self						)
     UpperCAmelCase	:							Optional[int]						=			ConfigTester(self,		config_class=lowercase_,		hidden_size=3_7						)



   def 	__magic_name__   (							self							:					Optional[int]						):
     self.config_tester.run_common_tests()



   def 	__magic_name__   (							self							:					List[str]						):
     UpperCAmelCase	:							List[Any]						=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase_						)



   def 	__magic_name__   (							self							:					Dict						):
     UpperCAmelCase			,						*UpperCAmelCase	:							Any						=			self.model_tester.prepare_config_and_inputs()
     for alibi in [True, False]:
       UpperCAmelCase	:							str						=			alibi
       self.model_tester.create_and_check_model(lowercase_,		*lowercase_						)



   def 	__magic_name__   (							self							:					Optional[int]						):
     UpperCAmelCase			,						UpperCAmelCase	:							List[Any]						=			self.model_tester.prepare_config_and_inputs_for_common()
     UpperCAmelCase	:							Union[str, Any]						=			3
     UpperCAmelCase	:							int						=			input_dict['''input_ids''']
     UpperCAmelCase	:							Optional[Any]						=			input_ids.ne(1						).to(lowercase_						)
     UpperCAmelCase	:							Optional[Any]						=			ids_tensor([self.model_tester.batch_size],		self.model_tester.type_sequence_label_size						)
     UpperCAmelCase	:							List[Any]						=			FalconForSequenceClassification(lowercase_						)
     model.to(lowercase_						)
     model.eval()
     UpperCAmelCase	:							List[str]						=			model(lowercase_,		attention_mask=lowercase_,		labels=lowercase_						)
     self.assertEqual(result.logits.shape,		(self.model_tester.batch_size, self.model_tester.num_labels)						)



   def 	__magic_name__   (							self							:					int						):
     UpperCAmelCase			,						UpperCAmelCase	:							List[Any]						=			self.model_tester.prepare_config_and_inputs_for_common()
     UpperCAmelCase	:							Tuple						=			3
     UpperCAmelCase	:							Any						=			'''single_label_classification'''
     UpperCAmelCase	:							Union[str, Any]						=			input_dict['''input_ids''']
     UpperCAmelCase	:							Optional[Any]						=			input_ids.ne(1						).to(lowercase_						)
     UpperCAmelCase	:							Dict						=			ids_tensor([self.model_tester.batch_size],		self.model_tester.type_sequence_label_size						)
     UpperCAmelCase	:							Dict						=			FalconForSequenceClassification(lowercase_						)
     model.to(lowercase_						)
     model.eval()
     UpperCAmelCase	:							Dict						=			model(lowercase_,		attention_mask=lowercase_,		labels=lowercase_						)
     self.assertEqual(result.logits.shape,		(self.model_tester.batch_size, self.model_tester.num_labels)						)



   def 	__magic_name__   (							self							:					List[str]						):
     UpperCAmelCase			,						UpperCAmelCase	:							Optional[int]						=			self.model_tester.prepare_config_and_inputs_for_common()
     UpperCAmelCase	:							Optional[Any]						=			input_dict['''input_ids''']
     UpperCAmelCase	:							Tuple						=			FalconForCausalLM(lowercase_						)
     model.to(lowercase_						)
     model.eval()
     UpperCAmelCase	:							int						=			model(lowercase_,		use_cache=lowercase_						)
     UpperCAmelCase	:							Union[str, Any]						=			input_ids.shape[0]
     UpperCAmelCase	:							str						=			model._convert_to_rw_cache(result.past_key_values						)
     UpperCAmelCase	:							Any						=			model._convert_cache_to_standard_format(lowercase_,		lowercase_						)
     for layer in range(len(lowercase_						)						):
       for tensor_idx in range(2						):
         self.assertTrue(rw_cache[layer][tensor_idx].ndim == 3						)
         self.assertTrue(result.past_key_values[layer][tensor_idx].ndim == 4						)
         self.assertTrue(
             torch.all(result.past_key_values[layer][tensor_idx] == standard_cache[layer][tensor_idx]						)						)



   def 	__magic_name__   (							self							:					Optional[Any]						):
     UpperCAmelCase			,						UpperCAmelCase	:							List[Any]						=			self.model_tester.prepare_config_and_inputs_for_common()
     UpperCAmelCase	:							List[str]						=			3
     UpperCAmelCase	:							str						=			'''multi_label_classification'''
     UpperCAmelCase	:							Dict						=			input_dict['''input_ids''']
     UpperCAmelCase	:							Dict						=			input_ids.ne(1						).to(lowercase_						)
     UpperCAmelCase	:							str						=			ids_tensor(
         [self.model_tester.batch_size, config.num_labels],		self.model_tester.type_sequence_label_size						).to(torch.float						)
     UpperCAmelCase	:							int						=			FalconForSequenceClassification(lowercase_						)
     model.to(lowercase_						)
     model.eval()
     UpperCAmelCase	:							Dict						=			model(lowercase_,		attention_mask=lowercase_,		labels=lowercase_						)
     self.assertEqual(result.logits.shape,		(self.model_tester.batch_size, self.model_tester.num_labels)						)





   def 	__magic_name__   (							self							:					Tuple						):
     # Falcon can have different numbers of KV-heads than the number of query heads, so we need
     # to override this test to use the right head counts.
     for model_class in self.all_generative_model_classes:
       UpperCAmelCase			,						UpperCAmelCase	:							Optional[int]						=			self.model_tester.prepare_config_and_inputs_for_common()

       # If it doesn't support cache, pass the test
       if not hasattr(lowercase_,		'''use_cache'''						):
         return

       UpperCAmelCase	:							Tuple						=			model_class(lowercase_						).to(lowercase_						)
       if "use_cache" not in inputs:
         UpperCAmelCase	:							Dict						=			True
       UpperCAmelCase	:							Dict						=			model(**lowercase_						)

       # If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format)
       if "past_key_values" not in outputs:
         return

       UpperCAmelCase	:							Optional[int]						=			(
           getattr(lowercase_,		'''decoder_layers''',		lowercase_						)
           or getattr(lowercase_,		'''num_decoder_layers''',		lowercase_						)
           or config.num_hidden_layers
       )
       UpperCAmelCase	:							Tuple						=			getattr(lowercase_,		'''num_kv_heads''',		config.num_attention_heads						)
       UpperCAmelCase	:							Tuple						=			getattr(lowercase_,		'''d_model''',		config.hidden_size						)
       UpperCAmelCase	:							List[Any]						=			embed_dim // num_attention_heads

       UpperCAmelCase	:							Tuple						=			outputs['''past_key_values''']
       self.assertEqual(len(lowercase_						),		lowercase_						)

       UpperCAmelCase			,						UpperCAmelCase	:							int						=			inputs['''input_ids'''].shape
       for i in range(lowercase_						):
         if config.new_decoder_architecture:
           UpperCAmelCase	:							Optional[int]						=			config.num_attention_heads
         elif config.multi_query:
           UpperCAmelCase	:							Tuple						=			1
         self.assertEqual(len(past_kv[0]						),		2						)  # K V for the decoder = 2
         self.assertEqual(
             past_kv[i][0].shape,		(batch_size, num_attention_heads, seq_length, per_head_embed_dim)						)
         self.assertEqual(
             past_kv[i][1].shape,		(batch_size, num_attention_heads, seq_length, per_head_embed_dim)						)






@require_torch
class  __UpperCAmelCase					(	unittest.TestCase						):



   @slow
   def 	__magic_name__   (							self							:					str						):
     UpperCAmelCase	:							Union[str, Any]						=			AutoTokenizer.from_pretrained('''Rocketknight1/falcon-rw-1b'''						)
     UpperCAmelCase	:							str						=			FalconForCausalLM.from_pretrained('''Rocketknight1/falcon-rw-1b'''						)
     model.eval()
     model.to(lowercase_						)
     UpperCAmelCase	:							List[str]						=			tokenizer('''My favorite food is''',		return_tensors='''pt'''						).to(lowercase_						)

     UpperCAmelCase	:							Any						=			(
         '''My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday.'''
     )

     UpperCAmelCase	:							int						=			model.generate(**lowercase_,		do_sample=lowercase_,		max_new_tokens=1_9						)
     UpperCAmelCase	:							Tuple						=			tokenizer.batch_decode(lowercase_						)[0]

     self.assertEqual(lowercase_,		lowercase_						)



   @slow
   def 	__magic_name__   (							self							:					List[str]						):
     # The big models are way too big for the CI, so we use tiny random models that resemble their
     # architectures but with much smaller and fewer layers
     for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
       UpperCAmelCase	:							Tuple						=			AutoTokenizer.from_pretrained(lowercase_						)
       UpperCAmelCase	:							int						=			FalconForCausalLM.from_pretrained(lowercase_						)
       model.eval()
       model.to(lowercase_						)
       UpperCAmelCase	:							int						=			tokenizer('''My favorite food is''',		return_tensors='''pt'''						).to(lowercase_						)

       # We just test that these run without errors - the models are randomly initialized
       # and so the actual text outputs will be garbage
       model.generate(**lowercase_,		do_sample=lowercase_,		max_new_tokens=4						)
       model.generate(**lowercase_,		do_sample=lowercase_,		max_new_tokens=4						)
       model.generate(**lowercase_,		num_beams=2,		max_new_tokens=4						)





   @slow
   def 	__magic_name__   (							self							:					Optional[int]						):
     # The big models are way too big for the CI, so we use tiny random models that resemble their
     # architectures but with much smaller and fewer layers
     with torch.no_grad():
       for repo in [
           "Rocketknight1/falcon-rw-1b",
           "Rocketknight1/tiny-random-falcon-7b",
           "Rocketknight1/tiny-random-falcon-40b",
       ]:
         UpperCAmelCase	:							List[Any]						=			AutoTokenizer.from_pretrained(lowercase_						)
         UpperCAmelCase	:							Optional[int]						=			FalconForCausalLM.from_pretrained(lowercase_						)
         model.eval()
         model.to(device=lowercase_						)
         UpperCAmelCase	:							int						=			tokenizer('''My favorite food is''',		return_tensors='''pt'''						).to(lowercase_						)

         # Test results are the same with and without cache
         UpperCAmelCase	:							str						=			model.generate(**lowercase_,		do_sample=lowercase_,		max_new_tokens=2_0,		use_cache=lowercase_						)
         UpperCAmelCase	:							Tuple						=			model.generate(**lowercase_,		do_sample=lowercase_,		max_new_tokens=2_0,		use_cache=lowercase_						)
         self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0						)







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



   def __init__(							self							:					Any,		__A							:					Optional[Any],		__A							:					Optional[Any]=3,		__A							:					Union[str, Any]=3_2,		__A							:					Optional[int]=3,		__A							:					str=1_0,		__A							:					Union[str, Any]=[8, 1_6, 3_2, 6_4],		__A							:					List[str]=[1, 1, 2, 1],		__A							:					Dict=True,		__A							:					List[Any]=True,		__A							:					int="relu",		__A							:					Optional[Any]=3,		__A							:					Any=None,		__A							:					Any=["stage2", "stage3", "stage4"],		__A							:					Optional[int]=[2, 3, 4],		__A							:					Any=1,		):
     UpperCAmelCase	:							Optional[int]						=			parent
     UpperCAmelCase	:							Union[str, Any]						=			batch_size
     UpperCAmelCase	:							Optional[int]						=			image_size
     UpperCAmelCase	:							List[Any]						=			num_channels
     UpperCAmelCase	:							Optional[Any]						=			embeddings_size
     UpperCAmelCase	:							List[Any]						=			hidden_sizes
     UpperCAmelCase	:							Any						=			depths
     UpperCAmelCase	:							int						=			is_training
     UpperCAmelCase	:							Optional[int]						=			use_labels
     UpperCAmelCase	:							Optional[Any]						=			hidden_act
     UpperCAmelCase	:							Any						=			num_labels
     UpperCAmelCase	:							List[Any]						=			scope
     UpperCAmelCase	:							int						=			len(__A						)
     UpperCAmelCase	:							Union[str, Any]						=			out_features
     UpperCAmelCase	:							List[str]						=			out_indices
     UpperCAmelCase	:							Tuple						=			num_groups



   def 	__magic_name__   (							self							:					Union[str, Any]						):
     UpperCAmelCase	:							Any						=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

     UpperCAmelCase	:							Dict						=			None
     if self.use_labels:
       UpperCAmelCase	:							List[str]						=			ids_tensor([self.batch_size],		self.num_labels						)

     UpperCAmelCase	:							List[str]						=			self.get_config()

     return config, pixel_values, labels



   def 	__magic_name__   (							self							:					Tuple						):
     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 	__magic_name__   (							self							:					int,		__A							:					str,		__A							:					List[Any],		__A							:					Any						):
     UpperCAmelCase	:							Optional[int]						=			BitModel(config=__A						)
     model.to(__A						)
     model.eval()
     UpperCAmelCase	:							Optional[int]						=			model(__A						)
     self.parent.assertEqual(
         result.last_hidden_state.shape,		(self.batch_size, self.hidden_sizes[-1], self.image_size // 3_2, self.image_size // 3_2),		)



   def 	__magic_name__   (							self							:					List[Any],		__A							:					Any,		__A							:					Union[str, Any],		__A							:					Dict						):
     UpperCAmelCase	:							Optional[Any]						=			self.num_labels
     UpperCAmelCase	:							Union[str, Any]						=			BitForImageClassification(__A						)
     model.to(__A						)
     model.eval()
     UpperCAmelCase	:							Optional[int]						=			model(__A,		labels=__A						)
     self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.num_labels)						)



   def 	__magic_name__   (							self							:					Optional[int],		__A							:					Any,		__A							:					List[str],		__A							:					str						):
     UpperCAmelCase	:							Any						=			BitBackbone(config=__A						)
     model.to(__A						)
     model.eval()
     UpperCAmelCase	:							Tuple						=			model(__A						)

     # 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
     UpperCAmelCase	:							List[str]						=			None
     UpperCAmelCase	:							Optional[Any]						=			BitBackbone(config=__A						)
     model.to(__A						)
     model.eval()
     UpperCAmelCase	:							Tuple						=			model(__A						)

     # 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 	__magic_name__   (							self							:					Optional[int]						):
     UpperCAmelCase	:							str						=			self.prepare_config_and_inputs()
     UpperCAmelCase			,						UpperCAmelCase			,						UpperCAmelCase	:							Union[str, Any]						=			config_and_inputs
     UpperCAmelCase	:							Union[str, Any]						=			{'''pixel_values''': pixel_values}
     return config, inputs_dict






@require_torch
class  __UpperCAmelCase					(	lowerCamelCase__      ,  lowerCamelCase__      ,  unittest.TestCase						):
   UpperCamelCase       					=					(BitModel, BitForImageClassification, BitBackbone) if is_torch_available() else ()
   UpperCamelCase       					=					(
       {"""feature-extraction""": BitModel, """image-classification""": BitForImageClassification}
       if is_torch_available()
       else {}
   )

   UpperCamelCase       					=					False
   UpperCamelCase       					=					False
   UpperCamelCase       					=					False
   UpperCamelCase       					=					False
   UpperCamelCase       					=					False



   def 	__magic_name__   (							self							:					Union[str, Any]						):
     UpperCAmelCase	:							Optional[int]						=			BitModelTester(self						)
     UpperCAmelCase	:							Any						=			ConfigTester(self,		config_class=__A,		has_text_modality=__A						)



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



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



   @unittest.skip(reason='''Bit does not use inputs_embeds'''						)
   def 	__magic_name__   (							self							:					Dict						):
     pass



   @unittest.skip(reason='''Bit does not support input and output embeddings'''						)
   def 	__magic_name__   (							self							:					List[Any]						):
     pass



   def 	__magic_name__   (							self							:					List[Any]						):
     UpperCAmelCase			,						UpperCAmelCase	:							Any						=			self.model_tester.prepare_config_and_inputs_for_common()

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

       UpperCAmelCase	:							List[Any]						=			['''pixel_values''']
       self.assertListEqual(arg_names[:1],		__A						)



   def 	__magic_name__   (							self							:					Union[str, Any]						):
     UpperCAmelCase	:							Optional[int]						=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__A						)



   def 	__magic_name__   (							self							:					int						):
     UpperCAmelCase	:							List[str]						=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_backbone(*__A						)



   def 	__magic_name__   (							self							:					int						):
     UpperCAmelCase			,						UpperCAmelCase	:							Optional[int]						=			self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
       UpperCAmelCase	:							Dict						=			model_class(config=__A						)
       for name, module in model.named_modules():
         if isinstance(__A,		(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 	__magic_name__   (							self							:					Dict						):
     def check_hidden_states_output(__A							:					List[Any],		__A							:					Optional[int],		__A							:					int						):
       UpperCAmelCase	:							int						=			model_class(__A						)
       model.to(__A						)
       model.eval()

       with torch.no_grad():
         UpperCAmelCase	:							Any						=			model(**self._prepare_for_class(__A,		__A						)						)

       UpperCAmelCase	:							Union[str, Any]						=			outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

       UpperCAmelCase	:							Any						=			self.model_tester.num_stages
       self.assertEqual(len(__A						),		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],		)

     UpperCAmelCase			,						UpperCAmelCase	:							Dict						=			self.model_tester.prepare_config_and_inputs_for_common()
     UpperCAmelCase	:							Union[str, Any]						=			['''preactivation''', '''bottleneck''']
     for model_class in self.all_model_classes:
       for layer_type in layers_type:
         UpperCAmelCase	:							int						=			layer_type
         UpperCAmelCase	:							List[Any]						=			True
         check_hidden_states_output(__A,		__A,		__A						)

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

         check_hidden_states_output(__A,		__A,		__A						)



   @unittest.skip(reason='''Bit does not use feedforward chunking'''						)
   def 	__magic_name__   (							self							:					List[str]						):
     pass



   def 	__magic_name__   (							self							:					Optional[int]						):
     UpperCAmelCase	:							str						=			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*__A						)





   @slow
   def 	__magic_name__   (							self							:					List[Any]						):
     for model_name in BIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase	:							List[str]						=			BitModel.from_pretrained(__A						)
       self.assertIsNotNone(__A						)






def       a__					(		)  ->						Union[str, Any]:
  UpperCAmelCase	:							Optional[int]						=			Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''      )
  return image





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



   @cached_property
   def 	__magic_name__   (							self							:					Union[str, Any]						):
     return (
         BitImageProcessor.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]						) if is_vision_available() else None
     )





   @slow
   def 	__magic_name__   (							self							:					str						):
     UpperCAmelCase	:							List[Any]						=			BitForImageClassification.from_pretrained(BIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]						).to(__A						)

     UpperCAmelCase	:							str						=			self.default_image_processor
     UpperCAmelCase	:							Dict						=			prepare_img()
     UpperCAmelCase	:							Tuple						=			image_processor(images=__A,		return_tensors='''pt'''						).to(__A						)

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

     # verify the logits
     UpperCAmelCase	:							Dict						=			torch.Size((1, 1_0_0_0)						)
     self.assertEqual(outputs.logits.shape,		__A						)

     UpperCAmelCase	:							int						=			torch.tensor([[-0.6_5_2_6, -0.5_2_6_3, -1.4_3_9_8]]						).to(__A						)

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






@require_torch
class  __UpperCAmelCase					(	lowerCamelCase__      ,  unittest.TestCase						):
   UpperCamelCase       					=					(BitBackbone,) if is_torch_available() else ()
   UpperCamelCase       					=					BitConfig

   UpperCamelCase       					=					False



   def 	__magic_name__   (							self							:					int						):
     UpperCAmelCase	:							int						=			BitModelTester(self						)





