import collections
import inspect
import unittest

from transformers import FocalNetConfig
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, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch
				from torch import nn

				from transformers import (
				    FocalNetBackbone,
				    FocalNetForImageClassification,
				    FocalNetForMaskedImageModeling,
				    FocalNetModel,
				)
				from transformers.models.focalnet.modeling_focalnet import FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
				from PIL import Image

				from transformers import AutoImageProcessor



class        lowerCamelCase__				:





			def __init__(       self	:	str  ,  lowercase__	:	Union[str, Any]  ,  lowercase__	:	str=13  ,  lowercase__	:	List[str]=32  ,  lowercase__	:	int=2  ,  lowercase__	:	List[Any]=3  ,  lowercase__	:	Union[str, Any]=16  ,  lowercase__	:	List[Any]=[32, 64, 1_28]  ,  lowercase__	:	List[Any]=[1, 2, 1]  ,  lowercase__	:	int=[2, 2, 4]  ,  lowercase__	:	str=2  ,  lowercase__	:	Union[str, Any]=2.0  ,  lowercase__	:	Any=True  ,  lowercase__	:	List[Any]=0.0  ,  lowercase__	:	List[str]=0.0  ,  lowercase__	:	Union[str, Any]=0.1  ,  lowercase__	:	Any="gelu"  ,  lowercase__	:	Dict=False  ,  lowercase__	:	Any=True  ,  lowercase__	:	List[str]=0.0_2  ,  lowercase__	:	str=1e-5  ,  lowercase__	:	List[Any]=True  ,  lowercase__	:	Tuple=None  ,  lowercase__	:	int=True  ,  lowercase__	:	Any=10  ,  lowercase__	:	Optional[int]=8  ,  lowercase__	:	int=["stage1", "stage2"]  ,  lowercase__	:	int=[1, 2]  ,  ):
										_lowerCAmelCase							=							parent
										_lowerCAmelCase							=							batch_size
										_lowerCAmelCase							=							image_size
										_lowerCAmelCase							=							patch_size
										_lowerCAmelCase							=							num_channels
										_lowerCAmelCase							=							embed_dim
										_lowerCAmelCase							=							hidden_sizes
										_lowerCAmelCase							=							depths
										_lowerCAmelCase							=							num_heads
										_lowerCAmelCase							=							window_size
										_lowerCAmelCase							=							mlp_ratio
										_lowerCAmelCase							=							qkv_bias
										_lowerCAmelCase							=							hidden_dropout_prob
										_lowerCAmelCase							=							attention_probs_dropout_prob
										_lowerCAmelCase							=							drop_path_rate
										_lowerCAmelCase							=							hidden_act
										_lowerCAmelCase							=							use_absolute_embeddings
										_lowerCAmelCase							=							patch_norm
										_lowerCAmelCase							=							layer_norm_eps
										_lowerCAmelCase							=							initializer_range
										_lowerCAmelCase							=							is_training
										_lowerCAmelCase							=							scope
										_lowerCAmelCase							=							use_labels
										_lowerCAmelCase							=							type_sequence_label_size
										_lowerCAmelCase							=							encoder_stride
										_lowerCAmelCase							=							out_features
										_lowerCAmelCase							=							out_indices





			def   SCREAMING_SNAKE_CASE__     (       self	:	int						):
										_lowerCAmelCase							=							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

										_lowerCAmelCase							=							None
										if self.use_labels:
																	_lowerCAmelCase							=							ids_tensor([self.batch_size]  ,  self.type_sequence_label_size						)

										_lowerCAmelCase							=							self.get_config()

										return config, pixel_values, labels





			def   SCREAMING_SNAKE_CASE__     (       self	:	List[str]						):
										return FocalNetConfig(
										    image_size=self.image_size  ,  patch_size=self.patch_size  ,  num_channels=self.num_channels  ,  embed_dim=self.embed_dim  ,  hidden_sizes=self.hidden_sizes  ,  depths=self.depths  ,  num_heads=self.num_heads  ,  window_size=self.window_size  ,  mlp_ratio=self.mlp_ratio  ,  qkv_bias=self.qkv_bias  ,  hidden_dropout_prob=self.hidden_dropout_prob  ,  attention_probs_dropout_prob=self.attention_probs_dropout_prob  ,  drop_path_rate=self.drop_path_rate  ,  hidden_act=self.hidden_act  ,  use_absolute_embeddings=self.use_absolute_embeddings  ,  path_norm=self.patch_norm  ,  layer_norm_eps=self.layer_norm_eps  ,  initializer_range=self.initializer_range  ,  encoder_stride=self.encoder_stride  ,  out_features=self.out_features  ,  out_indices=self.out_indices  ,  )





			def   SCREAMING_SNAKE_CASE__     (       self	:	str  ,  lowercase__	:	str  ,  lowercase__	:	Any  ,  lowercase__	:	Optional[Any]						):
										_lowerCAmelCase							=							FocalNetModel(config=_UpperCamelCase						)
										model.to(_UpperCamelCase						)
										model.eval()
										_lowerCAmelCase							=							model(_UpperCamelCase						)

										_lowerCAmelCase							=							((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths						) - 1))
										_lowerCAmelCase							=							int(config.embed_dim * 2 ** (len(config.depths						) - 1)						)

										self.parent.assertEqual(result.last_hidden_state.shape  ,  (self.batch_size, expected_seq_len, expected_dim)						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[Any]  ,  lowercase__	:	str  ,  lowercase__	:	str  ,  lowercase__	:	List[Any]						):
										_lowerCAmelCase							=							FocalNetBackbone(config=_UpperCamelCase						)
										model.to(_UpperCamelCase						)
										model.eval()
										_lowerCAmelCase							=							model(_UpperCamelCase						)

										# 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.image_size, 8, 8]						)

										# 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							=							None
										_lowerCAmelCase							=							FocalNetBackbone(config=_UpperCamelCase						)
										model.to(_UpperCamelCase						)
										model.eval()
										_lowerCAmelCase							=							model(_UpperCamelCase						)

										# verify feature maps
										self.parent.assertEqual(len(result.feature_maps						)  ,  1						)
										self.parent.assertListEqual(list(result.feature_maps[0].shape						)  ,  [self.batch_size, self.image_size * 2, 4, 4]						)

										# verify channels
										self.parent.assertEqual(len(model.channels						)  ,  1						)
										self.parent.assertListEqual(model.channels  ,  [config.hidden_sizes[-1]]						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[int]  ,  lowercase__	:	Any  ,  lowercase__	:	Any  ,  lowercase__	:	str						):
										_lowerCAmelCase							=							FocalNetForMaskedImageModeling(config=_UpperCamelCase						)
										model.to(_UpperCamelCase						)
										model.eval()
										_lowerCAmelCase							=							model(_UpperCamelCase						)
										self.parent.assertEqual(
										    result.reconstruction.shape  ,  (self.batch_size, self.num_channels, self.image_size, self.image_size)						)

										# test greyscale images
										_lowerCAmelCase							=							1
										_lowerCAmelCase							=							FocalNetForMaskedImageModeling(_UpperCamelCase						)
										model.to(_UpperCamelCase						)
										model.eval()

										_lowerCAmelCase							=							floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
										_lowerCAmelCase							=							model(_UpperCamelCase						)
										self.parent.assertEqual(result.reconstruction.shape  ,  (self.batch_size, 1, self.image_size, self.image_size)						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Tuple  ,  lowercase__	:	Dict  ,  lowercase__	:	List[str]  ,  lowercase__	:	Any						):
										_lowerCAmelCase							=							self.type_sequence_label_size
										_lowerCAmelCase							=							FocalNetForImageClassification(_UpperCamelCase						)
										model.to(_UpperCamelCase						)
										model.eval()
										_lowerCAmelCase							=							model(_UpperCamelCase  ,  labels=_UpperCamelCase						)
										self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.type_sequence_label_size)						)

										# test greyscale images
										_lowerCAmelCase							=							1
										_lowerCAmelCase							=							FocalNetForImageClassification(_UpperCamelCase						)
										model.to(_UpperCamelCase						)
										model.eval()

										_lowerCAmelCase							=							floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
										_lowerCAmelCase							=							model(_UpperCamelCase						)
										self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.type_sequence_label_size)						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Union[str, Any]						):
										_lowerCAmelCase							=							self.prepare_config_and_inputs()

										_lowerCAmelCase			,		_lowerCAmelCase			,		_lowerCAmelCase							=							config_and_inputs
										_lowerCAmelCase							=							{'pixel_values': pixel_values}
										return config, inputs_dict







@require_torch
class        lowerCamelCase__				(   A__       ,A__       ,unittest.TestCase				):
			UpperCamelCase__    							=(
			    (
			        FocalNetModel,
			        FocalNetForImageClassification,
			        FocalNetForMaskedImageModeling,
			        FocalNetBackbone,
			    )
			    if is_torch_available()
			    else ()
			)
			UpperCamelCase__    							=(
			    {"""feature-extraction""": FocalNetModel, """image-classification""": FocalNetForImageClassification}
			    if is_torch_available()
			    else {}
			)
			UpperCamelCase__    							=False

			UpperCamelCase__    							=False
			UpperCamelCase__    							=False
			UpperCamelCase__    							=False
			UpperCamelCase__    							=False





			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[int]						):
										_lowerCAmelCase							=							FocalNetModelTester(self						)
										_lowerCAmelCase							=							ConfigTester(self  ,  config_class=_UpperCamelCase  ,  embed_dim=37  ,  has_text_modality=_UpperCamelCase						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Tuple						):
										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	:	Optional[int]						):
										return





			def   SCREAMING_SNAKE_CASE__     (       self	:	str						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_UpperCamelCase						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	List[str]						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_backbone(*_UpperCamelCase						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Optional[Any]						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_image_modeling(*_UpperCamelCase						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Tuple						):
										_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*_UpperCamelCase						)





			@unittest.skip(reason='FocalNet does not use inputs_embeds'						)
			def   SCREAMING_SNAKE_CASE__     (       self	:	Any						):
										pass





			@unittest.skip(reason='FocalNet does not use feedforward chunking'						)
			def   SCREAMING_SNAKE_CASE__     (       self	:	Union[str, Any]						):
										pass





			def   SCREAMING_SNAKE_CASE__     (       self	:	List[Any]						):
										_lowerCAmelCase			,		_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes[:-1]:
																	_lowerCAmelCase							=							model_class(_UpperCamelCase						)
																	self.assertIsInstance(model.get_input_embeddings()  ,  (nn.Module)						)
																	_lowerCAmelCase							=							model.get_output_embeddings()
																	self.assertTrue(x is None or isinstance(_UpperCamelCase  ,  nn.Linear						)						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	List[Any]						):
										_lowerCAmelCase			,		_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes[:-1]:
																	_lowerCAmelCase							=							model_class(_UpperCamelCase						)
																	_lowerCAmelCase							=							inspect.signature(model.forward						)
																	# signature.parameters is an OrderedDict => so arg_names order is deterministic
																	_lowerCAmelCase							=							[*signature.parameters.keys()]

																	_lowerCAmelCase							=							['pixel_values']
																	self.assertListEqual(arg_names[:1]  ,  _UpperCamelCase						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Tuple  ,  lowercase__	:	Optional[Any]  ,  lowercase__	:	Tuple  ,  lowercase__	:	List[str]  ,  lowercase__	:	Tuple						):
										_lowerCAmelCase							=							model_class(_UpperCamelCase						)
										model.to(_UpperCamelCase						)
										model.eval()

										with torch.no_grad():
																	_lowerCAmelCase							=							model(**self._prepare_for_class(_UpperCamelCase  ,  _UpperCamelCase						)						)

										_lowerCAmelCase							=							outputs.hidden_states

										_lowerCAmelCase							=							getattr(
										    self.model_tester  ,  'expected_num_hidden_layers'  ,  len(self.model_tester.depths						) + 1						)
										self.assertEqual(len(_UpperCamelCase						)  ,  _UpperCamelCase						)

										# FocalNet has a different seq_length
										_lowerCAmelCase							=							(
										    config.patch_size
										    if isinstance(config.patch_size  ,  collections.abc.Iterable						)
										    else (config.patch_size, config.patch_size)
										)

										_lowerCAmelCase							=							(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

										self.assertListEqual(
										    list(hidden_states[0].shape[-2:]						)  ,  [num_patches, self.model_tester.embed_dim]  ,  )

										_lowerCAmelCase							=							outputs.reshaped_hidden_states
										self.assertEqual(len(_UpperCamelCase						)  ,  _UpperCamelCase						)

										_lowerCAmelCase			,		_lowerCAmelCase			,		_lowerCAmelCase			,		_lowerCAmelCase							=							reshaped_hidden_states[0].shape
										_lowerCAmelCase							=							(
										    reshaped_hidden_states[0].view(_UpperCamelCase  ,  _UpperCamelCase  ,  height * width						).permute(0  ,  2  ,  1						)
										)
										self.assertListEqual(
										    list(reshaped_hidden_states.shape[-2:]						)  ,  [num_patches, self.model_tester.embed_dim]  ,  )





			def   SCREAMING_SNAKE_CASE__     (       self	:	int						):
										_lowerCAmelCase			,		_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs_for_common()

										_lowerCAmelCase							=							(
										    self.model_tester.image_size
										    if isinstance(self.model_tester.image_size  ,  collections.abc.Iterable						)
										    else (self.model_tester.image_size, self.model_tester.image_size)
										)

										for model_class in self.all_model_classes[:-1]:
																	_lowerCAmelCase							=							True
																	self.check_hidden_states_output(_UpperCamelCase  ,  _UpperCamelCase  ,  _UpperCamelCase  ,  _UpperCamelCase						)

																	# check that output_hidden_states also work using config
																	del inputs_dict["output_hidden_states"]
																	_lowerCAmelCase							=							True

																	self.check_hidden_states_output(_UpperCamelCase  ,  _UpperCamelCase  ,  _UpperCamelCase  ,  _UpperCamelCase						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Dict						):
										_lowerCAmelCase			,		_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs_for_common()
										_lowerCAmelCase							=							3

										_lowerCAmelCase							=							(
										    self.model_tester.image_size
										    if isinstance(self.model_tester.image_size  ,  collections.abc.Iterable						)
										    else (self.model_tester.image_size, self.model_tester.image_size)
										)
										_lowerCAmelCase							=							(
										    config.patch_size
										    if isinstance(config.patch_size  ,  collections.abc.Iterable						)
										    else (config.patch_size, config.patch_size)
										)

										_lowerCAmelCase							=							image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
										_lowerCAmelCase							=							image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

										for model_class in self.all_model_classes[:-1]:
																	_lowerCAmelCase							=							True
																	self.check_hidden_states_output(_UpperCamelCase  ,  _UpperCamelCase  ,  _UpperCamelCase  ,  (padded_height, padded_width)						)

																	# check that output_hidden_states also work using config
																	del inputs_dict["output_hidden_states"]
																	_lowerCAmelCase							=							True
																	self.check_hidden_states_output(_UpperCamelCase  ,  _UpperCamelCase  ,  _UpperCamelCase  ,  (padded_height, padded_width)						)





			@slow
			def   SCREAMING_SNAKE_CASE__     (       self	:	Any						):
										for model_name in FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	_lowerCAmelCase							=							FocalNetModel.from_pretrained(_UpperCamelCase						)
																	self.assertIsNotNone(_UpperCamelCase						)





			def   SCREAMING_SNAKE_CASE__     (       self	:	Tuple						):
										_lowerCAmelCase			,		_lowerCAmelCase							=							self.model_tester.prepare_config_and_inputs_for_common()

										_lowerCAmelCase							=							_config_zero_init(_UpperCamelCase						)
										for model_class in self.all_model_classes:
																	_lowerCAmelCase							=							model_class(config=_UpperCamelCase						)
																	for name, param in model.named_parameters():
																								if "embeddings" not in name and param.requires_grad:
																															self.assertIn(
																															    ((param.data.mean() * 1e9).round() / 1e9).item()  ,  [0.0, 1.0]  ,  msg=f'Parameter {name} of model {model_class} seems not properly initialized'  ,  )







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





			@cached_property
			def   SCREAMING_SNAKE_CASE__     (       self	:	str						):
										# TODO update organization
										return AutoImageProcessor.from_pretrained('microsoft/focalnet-tiny'						) if is_vision_available() else None





			@slow
			def   SCREAMING_SNAKE_CASE__     (       self	:	Tuple						):
										_lowerCAmelCase							=							FocalNetForImageClassification.from_pretrained('microsoft/focalnet-tiny'						).to(_UpperCamelCase						)
										_lowerCAmelCase							=							self.default_image_processor

										_lowerCAmelCase							=							Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
										_lowerCAmelCase							=							image_processor(images=_UpperCamelCase  ,  return_tensors='pt'						).to(_UpperCamelCase						)

										# forward pass
										with torch.no_grad():
																	_lowerCAmelCase							=							model(**_UpperCamelCase						)

										# verify the logits
										_lowerCAmelCase							=							torch.Size((1, 10_00)						)
										self.assertEqual(outputs.logits.shape  ,  _UpperCamelCase						)
										_lowerCAmelCase							=							torch.tensor([0.2_1_6_6, -0.4_3_6_8, 0.2_1_9_1]						).to(_UpperCamelCase						)
										self.assertTrue(torch.allclose(outputs.logits[0, :3]  ,  _UpperCamelCase  ,  atol=1e-4						)						)
										self.assertTrue(outputs.logits.argmax(dim=-1						).item()  ,  2_81						)







@require_torch
class        lowerCamelCase__				(   A__       ,unittest.TestCase				):
			UpperCamelCase__    							=(FocalNetBackbone,) if is_torch_available() else ()
			UpperCamelCase__    							=FocalNetConfig

			UpperCamelCase__    							=False





			def   SCREAMING_SNAKE_CASE__     (       self	:	Tuple						):
										_lowerCAmelCase							=							FocalNetModelTester(self						)













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


UpperCAmelCase_           =	logging.get_logger(__name__)

UpperCAmelCase_           =	{
    "tiiuae/falcon-40b": "https://huggingface.co/tiiuae/falcon-40b/resolve/main/config.json",
    "tiiuae/falcon-7b": "https://huggingface.co/tiiuae/falcon-7b/resolve/main/config.json",
}




class 			__UpperCamelCase    (							A__    ):
     __A	:  Dict							  =   """falcon"""
     __A	:  Any							  =   ["""past_key_values"""]





     def __init__(     self				,							_UpperCamelCase=65024				,							_UpperCamelCase=4544				,							_UpperCamelCase=32				,							_UpperCamelCase=71				,							_UpperCamelCase=1e-5				,							_UpperCamelCase=0.02				,							_UpperCamelCase=True				,							_UpperCamelCase=0.0				,							_UpperCamelCase=0.0				,							_UpperCamelCase=None				,							_UpperCamelCase=False				,							_UpperCamelCase=False				,							_UpperCamelCase=True				,							_UpperCamelCase=True				,							_UpperCamelCase=False				,							_UpperCamelCase=11				,							_UpperCamelCase=11				,							**_UpperCamelCase				,							):
        _UpperCAmelCase           =   vocab_size
        # Backward compatibility with n_embed kwarg
        _UpperCAmelCase           =   kwargs.pop('''n_embed'''				,							_UpperCamelCase					)
        _UpperCAmelCase           =   hidden_size if n_embed is None else n_embed
        _UpperCAmelCase           =   num_hidden_layers
        _UpperCAmelCase           =   num_attention_heads
        _UpperCAmelCase           =   layer_norm_epsilon
        _UpperCAmelCase           =   initializer_range
        _UpperCAmelCase           =   use_cache
        _UpperCAmelCase           =   hidden_dropout
        _UpperCAmelCase           =   attention_dropout

        _UpperCAmelCase           =   bos_token_id
        _UpperCAmelCase           =   eos_token_id
        _UpperCAmelCase           =   num_attention_heads if num_kv_heads is None else num_kv_heads
        _UpperCAmelCase           =   alibi
        _UpperCAmelCase           =   new_decoder_architecture
        _UpperCAmelCase           =   multi_query  # Ignored when new_decoder_architecture is True
        _UpperCAmelCase           =   parallel_attn
        _UpperCAmelCase           =   bias

        super().__init__(bos_token_id=_UpperCamelCase				,							eos_token_id=_UpperCamelCase				,							**_UpperCamelCase					)





     @property
     def 		UpperCamelCase(     self					):
        return self.hidden_size // self.num_attention_heads





     @property
     def 		UpperCamelCase(     self					):
        return not self.alibi