

from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import SwinConfig
from transformers.testing_utils import require_tf, require_vision, slow, to_atuple
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
		import tensorflow as tf

		from transformers.models.swin.modeling_tf_swin import (
		    TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TFSwinForImageClassification,
		    TFSwinForMaskedImageModeling,
		    TFSwinModel,
		)


if is_vision_available():
		from PIL import Image

		from transformers import AutoImageProcessor





class      snake_case_      :


							"""simple docstring"""





							def __init__(			self       ,    _A       ,    _A=1_3       ,    _A=3_2       ,    _A=2       ,    _A=3       ,    _A=1_6       ,    _A=[1, 2, 1]       ,    _A=[2, 2, 4]       ,    _A=2       ,    _A=2.0       ,    _A=True       ,    _A=0.0       ,    _A=0.0       ,    _A=0.1       ,    _A="gelu"       ,    _A=False       ,    _A=True       ,    _A=0.02       ,    _A=1e-5       ,    _A=True       ,    _A=None       ,    _A=True       ,    _A=1_0       ,    _A=8       ,    ):
												__lowerCAmelCase       =				parent
												__lowerCAmelCase       =				batch_size
												__lowerCAmelCase       =				image_size
												__lowerCAmelCase       =				patch_size
												__lowerCAmelCase       =				num_channels
												__lowerCAmelCase       =				embed_dim
												__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




							def 							A__	(			self				):
												__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 							A__	(			self				):
												return SwinConfig(
												    image_size=self.image_size       ,    patch_size=self.patch_size       ,    num_channels=self.num_channels       ,    embed_dim=self.embed_dim       ,    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       ,    )




							def 							A__	(			self       ,    _A       ,    _A       ,    _A				):
												__lowerCAmelCase       =				TFSwinModel(config=_lowerCAmelCase				)
												__lowerCAmelCase       =				model(_lowerCAmelCase				)

												__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 							A__	(			self       ,    _A       ,    _A       ,    _A				):
												__lowerCAmelCase       =				TFSwinForMaskedImageModeling(config=_lowerCAmelCase				)
												__lowerCAmelCase       =				model(_lowerCAmelCase				)
												self.parent.assertEqual(
												    result.logits.shape       ,    (self.batch_size, self.num_channels, self.image_size, self.image_size)				)

												# test greyscale images
												__lowerCAmelCase       =				1
												__lowerCAmelCase       =				TFSwinForMaskedImageModeling(_lowerCAmelCase				)

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




							def 							A__	(			self       ,    _A       ,    _A       ,    _A				):
												__lowerCAmelCase       =				self.type_sequence_label_size
												__lowerCAmelCase       =				TFSwinForImageClassification(_lowerCAmelCase				)
												__lowerCAmelCase       =				model(_lowerCAmelCase       ,    labels=_lowerCAmelCase				)
												self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.type_sequence_label_size)				)

												# test greyscale images
												__lowerCAmelCase       =				1
												__lowerCAmelCase       =				TFSwinForImageClassification(_lowerCAmelCase				)
												__lowerCAmelCase       =				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
												__lowerCAmelCase       =				model(_lowerCAmelCase				)
												self.parent.assertEqual(result.logits.shape       ,    (self.batch_size, self.type_sequence_label_size)				)




							def 							A__	(			self				):
												__lowerCAmelCase       =				self.prepare_config_and_inputs()
												__lowerCAmelCase       =				config_and_inputs
												__lowerCAmelCase       =				{"""pixel_values""": pixel_values}
												return config, inputs_dict





@require_tf
class      snake_case_      (					__UpperCamelCase      ,			__UpperCamelCase      ,			unittest.TestCase				):


							"""simple docstring"""





							__UpperCAmelCase      =(
							    (
							        TFSwinModel,
							        TFSwinForImageClassification,
							        TFSwinForMaskedImageModeling,
							    )
							    if is_tf_available()
							    else ()
							)
							__UpperCAmelCase      =(
							    {"feature-extraction": TFSwinModel, "image-classification": TFSwinForImageClassification}
							    if is_tf_available()
							    else {}
							)

							__UpperCAmelCase      =False
							__UpperCAmelCase      =False
							__UpperCAmelCase      =False
							__UpperCAmelCase      =False




							def 							A__	(			self				):
												__lowerCAmelCase       =				TFSwinModelTester(self				)
												__lowerCAmelCase       =				ConfigTester(self       ,    config_class=_lowerCAmelCase       ,    embed_dim=3_7				)




							def 							A__	(			self				):
												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 							A__	(			self				):
												return




							def 							A__	(			self				):
												__lowerCAmelCase       =				self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*_lowerCAmelCase				)




							def 							A__	(			self				):
												__lowerCAmelCase       =				self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_masked_image_modeling(*_lowerCAmelCase				)




							def 							A__	(			self				):
												__lowerCAmelCase       =				self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase				)




							@unittest.skip(reason='Swin does not use inputs_embeds'				)
							def 							A__	(			self				):
												pass




							def 							A__	(			self				):
												__lowerCAmelCase       =				self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																	__lowerCAmelCase       =				model_class(_lowerCAmelCase				)
																	self.assertIsInstance(model.get_input_embeddings()       ,    tf.keras.layers.Layer				)
																	__lowerCAmelCase       =				model.get_output_embeddings()
																	self.assertTrue(x is None or isinstance(_lowerCAmelCase       ,    tf.keras.layers.Dense				)				)




							def 							A__	(			self				):
												__lowerCAmelCase       =				self.model_tester.prepare_config_and_inputs_for_common()

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

																	__lowerCAmelCase       =				["""pixel_values"""]
																	self.assertListEqual(arg_names[:1]       ,    _lowerCAmelCase				)




							def 							A__	(			self				):
												__lowerCAmelCase       =				self.model_tester.prepare_config_and_inputs_for_common()
												__lowerCAmelCase       =				True

												for model_class in self.all_model_classes:
																	__lowerCAmelCase       =				True
																	__lowerCAmelCase       =				False
																	__lowerCAmelCase       =				True
																	__lowerCAmelCase       =				model_class(_lowerCAmelCase				)
																	__lowerCAmelCase       =				model(**self._prepare_for_class(_lowerCAmelCase       ,    _lowerCAmelCase				)				)
																	__lowerCAmelCase       =				outputs.attentions
																	__lowerCAmelCase       =				len(self.model_tester.depths				)
																	self.assertEqual(len(_lowerCAmelCase				)       ,    _lowerCAmelCase				)

																	# check that output_attentions also work using config
																	del inputs_dict["output_attentions"]
																	__lowerCAmelCase       =				True
																	__lowerCAmelCase       =				config.window_size**2
																	__lowerCAmelCase       =				model_class(_lowerCAmelCase				)
																	__lowerCAmelCase       =				model(**self._prepare_for_class(_lowerCAmelCase       ,    _lowerCAmelCase				)				)
																	__lowerCAmelCase       =				outputs.attentions
																	self.assertEqual(len(_lowerCAmelCase				)       ,    _lowerCAmelCase				)

																	self.assertListEqual(
																	    list(attentions[0].shape[-3:]				)       ,    [self.model_tester.num_heads[0], window_size_squared, window_size_squared]       ,    )
																	__lowerCAmelCase       =				len(_lowerCAmelCase				)

																	# Check attention is always last and order is fine
																	__lowerCAmelCase       =				True
																	__lowerCAmelCase       =				True
																	__lowerCAmelCase       =				model_class(_lowerCAmelCase				)
																	__lowerCAmelCase       =				model(**self._prepare_for_class(_lowerCAmelCase       ,    _lowerCAmelCase				)				)

																	if hasattr(self.model_tester       ,    'num_hidden_states_types'				):
																						__lowerCAmelCase       =				self.model_tester.num_hidden_states_types
																	else:
																						# also another +1 for reshaped_hidden_states
																						__lowerCAmelCase       =				2
																	self.assertEqual(out_len + added_hidden_states       ,    len(_lowerCAmelCase				)				)

																	__lowerCAmelCase       =				outputs.attentions

																	self.assertEqual(len(_lowerCAmelCase				)       ,    _lowerCAmelCase				)

																	self.assertListEqual(
																	    list(self_attentions[0].shape[-3:]				)       ,    [self.model_tester.num_heads[0], window_size_squared, window_size_squared]       ,    )




							def 							A__	(			self       ,    _A       ,    _A       ,    _A       ,    _A				):
												__lowerCAmelCase       =				model_class(_lowerCAmelCase				)
												__lowerCAmelCase       =				model(**self._prepare_for_class(_lowerCAmelCase       ,    _lowerCAmelCase				)				)
												__lowerCAmelCase       =				outputs.hidden_states

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

												# Swin has a different seq_length
												__lowerCAmelCase       =				to_atuple(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(_lowerCAmelCase				)       ,    _lowerCAmelCase				)

												__lowerCAmelCase       =				reshaped_hidden_states[0].shape

												__lowerCAmelCase       =				tf.reshape(reshaped_hidden_states[0]       ,    (batch_size, num_channels, height * width)				)
												__lowerCAmelCase       =				tf.transpose(_lowerCAmelCase       ,    (0, 2, 1)				)

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




							def 							A__	(			self				):
												__lowerCAmelCase       =				self.model_tester.prepare_config_and_inputs_for_common()

												__lowerCAmelCase       =				to_atuple(self.model_tester.image_size				)

												for model_class in self.all_model_classes:
																	__lowerCAmelCase       =				True
																	self.check_hidden_states_output(_lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase				)

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

																	self.check_hidden_states_output(_lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase				)




							def 							A__	(			self				):
												__lowerCAmelCase       =				self.model_tester.prepare_config_and_inputs_for_common()
												__lowerCAmelCase       =				3

												__lowerCAmelCase       =				to_atuple(self.model_tester.image_size				)
												__lowerCAmelCase       =				to_atuple(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:
																	__lowerCAmelCase       =				True
																	self.check_hidden_states_output(_lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    (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(_lowerCAmelCase       ,    _lowerCAmelCase       ,    _lowerCAmelCase       ,    (padded_height, padded_width)				)




							@slow
							def 							A__	(			self				):
												for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	__lowerCAmelCase       =				TFSwinModel.from_pretrained(_lowerCAmelCase				)
																	self.assertIsNotNone(_lowerCAmelCase				)





@require_vision
@require_tf
class      snake_case_      (					unittest.TestCase				):


							"""simple docstring"""





							@cached_property
							def 							A__	(			self				):
												return (
												    AutoImageProcessor.from_pretrained('microsoft/swin-tiny-patch4-window7-224'				)
												    if is_vision_available()
												    else None
												)




							@slow
							def 							A__	(			self				):
												__lowerCAmelCase       =				TFSwinForImageClassification.from_pretrained('microsoft/swin-tiny-patch4-window7-224'				)
												__lowerCAmelCase       =				self.default_image_processor

												__lowerCAmelCase       =				Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
												__lowerCAmelCase       =				image_processor(images=_lowerCAmelCase       ,    return_tensors='tf'				)

												# forward pass
												__lowerCAmelCase       =				model(_lowerCAmelCase				)

												# verify the logits
												__lowerCAmelCase       =				tf.TensorShape((1, 1_0_0_0)				)
												self.assertEqual(outputs.logits.shape       ,    _lowerCAmelCase				)
												__lowerCAmelCase       =				tf.constant([-0.0948, -0.6454, -0.0921]				)
												self.assertTrue(np.allclose(outputs.logits[0, :3]       ,    _lowerCAmelCase       ,    atol=1e-4				)				)







import functools
import operator

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


lowercase_  							=			logging.get_logger(__name__)

lowercase_  							=			{
    "facebook/hubert-base-ls960": "https://huggingface.co/facebook/hubert-base-ls960/resolve/main/config.json",
    # See all Hubert models at https://huggingface.co/models?filter=hubert
}







class      SCREAMING_SNAKE_CASE__					(  __UpperCamelCase    ):
       A							:	List[str]          =	"hubert"

       def __init__(							self					:     List[str]			,			_lowerCAmelCase					:     Union[str, Any]=32			,			_lowerCAmelCase					:     Any=7_68			,			_lowerCAmelCase					:     Optional[Any]=12			,			_lowerCAmelCase					:     Optional[int]=12			,			_lowerCAmelCase					:     int=30_72			,			_lowerCAmelCase					:     Optional[Any]="gelu"			,			_lowerCAmelCase					:     List[str]=0.1			,			_lowerCAmelCase					:     Any=0.1			,			_lowerCAmelCase					:     List[str]=0.1			,			_lowerCAmelCase					:     Dict=True			,			_lowerCAmelCase					:     Dict=0.0			,			_lowerCAmelCase					:     List[str]=0.1			,			_lowerCAmelCase					:     Optional[int]=0.1			,			_lowerCAmelCase					:     Dict=0.02			,			_lowerCAmelCase					:     Any=1e-5			,			_lowerCAmelCase					:     str="group"			,			_lowerCAmelCase					:     Any="gelu"			,			_lowerCAmelCase					:     List[str]=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12)			,			_lowerCAmelCase					:     Any=(5, 2, 2, 2, 2, 2, 2)			,			_lowerCAmelCase					:     Dict=(10, 3, 3, 3, 3, 2, 2)			,			_lowerCAmelCase					:     List[str]=False			,			_lowerCAmelCase					:     Optional[Any]=1_28			,			_lowerCAmelCase					:     int=16			,			_lowerCAmelCase					:     Tuple=False			,			_lowerCAmelCase					:     int=True			,			_lowerCAmelCase					:     int=0.05			,			_lowerCAmelCase					:     Any=10			,			_lowerCAmelCase					:     Dict=2			,			_lowerCAmelCase					:     Dict=0.0			,			_lowerCAmelCase					:     Optional[int]=10			,			_lowerCAmelCase					:     Union[str, Any]=0			,			_lowerCAmelCase					:     int="sum"			,			_lowerCAmelCase					:     Optional[int]=False			,			_lowerCAmelCase					:     str=False			,			_lowerCAmelCase					:     Optional[Any]=2_56			,			_lowerCAmelCase					:     Any=0			,			_lowerCAmelCase					:     List[Any]=1			,			_lowerCAmelCase					:     List[str]=2			,			**_lowerCAmelCase					:     Optional[int]			,			):
           super().__init__(**_lowerCAmelCase			,			pad_token_id=_lowerCAmelCase			,			bos_token_id=_lowerCAmelCase			,			eos_token_id=_lowerCAmelCase			)
           __snake_case       :				Optional[int] 			=    hidden_size
           __snake_case       :				Optional[Any] 			=    feat_extract_norm
           __snake_case       :				Optional[int] 			=    feat_extract_activation
           __snake_case       :				List[str] 			=    list(_lowerCAmelCase			)
           __snake_case       :				Any 			=    list(_lowerCAmelCase			)
           __snake_case       :				List[Any] 			=    list(_lowerCAmelCase			)
           __snake_case       :				Any 			=    conv_bias
           __snake_case       :				Tuple 			=    num_conv_pos_embeddings
           __snake_case       :				List[Any] 			=    num_conv_pos_embedding_groups
           __snake_case       :				Union[str, Any] 			=    len(self.conv_dim			)
           __snake_case       :				List[Any] 			=    num_hidden_layers
           __snake_case       :				Tuple 			=    intermediate_size
           __snake_case       :				Optional[int] 			=    hidden_act
           __snake_case       :				Union[str, Any] 			=    num_attention_heads
           __snake_case       :				str 			=    hidden_dropout
           __snake_case       :				Union[str, Any] 			=    attention_dropout
           __snake_case       :				Dict 			=    activation_dropout
           __snake_case       :				List[Any] 			=    feat_proj_layer_norm
           __snake_case       :				List[str] 			=    feat_proj_dropout
           __snake_case       :				List[Any] 			=    final_dropout
           __snake_case       :				str 			=    layerdrop
           __snake_case       :				List[str] 			=    layer_norm_eps
           __snake_case       :				Optional[Any] 			=    initializer_range
           __snake_case       :				Optional[int] 			=    vocab_size
           __snake_case       :				Dict 			=    do_stable_layer_norm
           __snake_case       :				Tuple 			=    use_weighted_layer_sum
           __snake_case       :				List[Any] 			=    classifier_proj_size

           if (
               (len(self.conv_stride			) != self.num_feat_extract_layers)
               or (len(self.conv_kernel			) != self.num_feat_extract_layers)
               or (len(self.conv_dim			) != self.num_feat_extract_layers)
           ):
               raise ValueError(
                   """Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="""
                   """ `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="""
                   f''' {len(self.conv_dim			)}`, `len(config.conv_stride) = {len(self.conv_stride			)}`,'''
                   f''' `len(config.conv_kernel) = {len(self.conv_kernel			)}`.'''			)

           # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
           __snake_case       :				Union[str, Any] 			=    apply_spec_augment
           __snake_case       :				List[str] 			=    mask_time_prob
           __snake_case       :				Dict 			=    mask_time_length
           __snake_case       :				int 			=    mask_time_min_masks
           __snake_case       :				List[str] 			=    mask_feature_prob
           __snake_case       :				Tuple 			=    mask_feature_length
           __snake_case       :				int 			=    mask_feature_min_masks

           # ctc loss
           __snake_case       :				Dict 			=    ctc_loss_reduction
           __snake_case       :				List[str] 			=    ctc_zero_infinity





       @property
       def 			snake_case__    (							self					:     Any			):
           return functools.reduce(operator.mul			,			self.conv_stride			,			1			)



