






import platform
from argparse import ArgumentParser

import huggingface_hub
import pandas
import pyarrow

from datasets import __version__ as version
from datasets.commands import BaseDatasetsCLICommand

def    lowerCAmelCase__		(			_a				:	int			):
		return EnvironmentCommand()




class 						UpperCAmelCase_  (		SCREAMING_SNAKE_CASE__    ):


						'''simple docstring'''




						@staticmethod
						def 					_lowerCAmelCase				(		_SCREAMING_SNAKE_CASE )       -> int:
								snake_case_ :							Tuple         =				parser.add_parser("env"						, help="Print relevant system environment info." )
								download_parser.set_defaults(func=_SCREAMING_SNAKE_CASE )






						def 					_lowerCAmelCase				(		self )       -> str:
								snake_case_ :							List[Any]         =				{
								    "`datasets` version": version,
								    "Platform": platform.platform(),
								    "Python version": platform.python_version(),
								    "Huggingface_hub version": huggingface_hub.__version__,
								    "PyArrow version": pyarrow.__version__,
								    "Pandas version": pandas.__version__,
								}

								print("\nCopy-and-paste the text below in your GitHub issue.\n" )
								print(self.format_dict(_SCREAMING_SNAKE_CASE ) )

								return info






						@staticmethod
						def 					_lowerCAmelCase				(		_SCREAMING_SNAKE_CASE )       -> int:
								return "\n".join([f'''- {prop}: {val}''' for prop, val in d.items()] ) + "\n"








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


						'''simple docstring'''




						def __init__(		self						, _SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE=13						, _SCREAMING_SNAKE_CASE=32						, _SCREAMING_SNAKE_CASE=2						, _SCREAMING_SNAKE_CASE=3						, _SCREAMING_SNAKE_CASE=16						, _SCREAMING_SNAKE_CASE=[1, 2, 1]						, _SCREAMING_SNAKE_CASE=[2, 2, 4]						, _SCREAMING_SNAKE_CASE=2						, _SCREAMING_SNAKE_CASE=2.0						, _SCREAMING_SNAKE_CASE=True						, _SCREAMING_SNAKE_CASE=0.0						, _SCREAMING_SNAKE_CASE=0.0						, _SCREAMING_SNAKE_CASE=0.1						, _SCREAMING_SNAKE_CASE="gelu"						, _SCREAMING_SNAKE_CASE=False						, _SCREAMING_SNAKE_CASE=True						, _SCREAMING_SNAKE_CASE=0.02						, _SCREAMING_SNAKE_CASE=1e-5						, _SCREAMING_SNAKE_CASE=True						, _SCREAMING_SNAKE_CASE=None						, _SCREAMING_SNAKE_CASE=True						, _SCREAMING_SNAKE_CASE=10						, _SCREAMING_SNAKE_CASE=8						, )       -> None:
								snake_case_ :							List[str]         =				parent
								snake_case_ :							Dict         =				batch_size
								snake_case_ :							str         =				image_size
								snake_case_ :							Dict         =				patch_size
								snake_case_ :							Dict         =				num_channels
								snake_case_ :							Any         =				embed_dim
								snake_case_ :							int         =				depths
								snake_case_ :							Tuple         =				num_heads
								snake_case_ :							List[str]         =				window_size
								snake_case_ :							List[Any]         =				mlp_ratio
								snake_case_ :							Any         =				qkv_bias
								snake_case_ :							int         =				hidden_dropout_prob
								snake_case_ :							Tuple         =				attention_probs_dropout_prob
								snake_case_ :							str         =				drop_path_rate
								snake_case_ :							Any         =				hidden_act
								snake_case_ :							Tuple         =				use_absolute_embeddings
								snake_case_ :							Tuple         =				patch_norm
								snake_case_ :							Dict         =				layer_norm_eps
								snake_case_ :							Any         =				initializer_range
								snake_case_ :							str         =				is_training
								snake_case_ :							str         =				scope
								snake_case_ :							List[Any]         =				use_labels
								snake_case_ :							Dict         =				type_sequence_label_size
								snake_case_ :							List[Any]         =				encoder_stride






						def 					_lowerCAmelCase				(		self )       -> Optional[Any]:
								snake_case_ :							List[str]         =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

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

								snake_case_ :							List[Any]         =				self.get_config()

								return config, pixel_values, labels






						def 					_lowerCAmelCase				(		self )       -> Optional[int]:
								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 					_lowerCAmelCase				(		self						, _SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE )       -> Union[str, Any]:
								snake_case_ :							List[Any]         =				TFSwinModel(config=_SCREAMING_SNAKE_CASE )
								snake_case_ :							str         =				model(_SCREAMING_SNAKE_CASE )

								snake_case_ :							Tuple         =				((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths ) - 1))
								snake_case_ :							Optional[Any]         =				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 					_lowerCAmelCase				(		self						, _SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE )       -> List[str]:
								snake_case_ :							List[Any]         =				TFSwinForMaskedImageModeling(config=_SCREAMING_SNAKE_CASE )
								snake_case_ :							List[str]         =				model(_SCREAMING_SNAKE_CASE )
								self.parent.assertEqual(
								    result.logits.shape						, (self.batch_size, self.num_channels, self.image_size, self.image_size) )

								# test greyscale images
								snake_case_ :							Dict         =				1
								snake_case_ :							str         =				TFSwinForMaskedImageModeling(_SCREAMING_SNAKE_CASE )

								snake_case_ :							Optional[Any]         =				floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
								snake_case_ :							Tuple         =				model(_SCREAMING_SNAKE_CASE )
								self.parent.assertEqual(result.logits.shape						, (self.batch_size, 1, self.image_size, self.image_size) )






						def 					_lowerCAmelCase				(		self						, _SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE )       -> Optional[Any]:
								snake_case_ :							str         =				self.type_sequence_label_size
								snake_case_ :							Dict         =				TFSwinForImageClassification(_SCREAMING_SNAKE_CASE )
								snake_case_ :							Optional[int]         =				model(_SCREAMING_SNAKE_CASE						, labels=_SCREAMING_SNAKE_CASE )
								self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.type_sequence_label_size) )

								# test greyscale images
								snake_case_ :							List[str]         =				1
								snake_case_ :							Optional[int]         =				TFSwinForImageClassification(_SCREAMING_SNAKE_CASE )
								snake_case_ :							Optional[int]         =				floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
								snake_case_ :							Tuple         =				model(_SCREAMING_SNAKE_CASE )
								self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.type_sequence_label_size) )






						def 					_lowerCAmelCase				(		self )       -> str:
								snake_case_ :							List[str]         =				self.prepare_config_and_inputs()
								snake_case_       ,				snake_case_       ,				snake_case_ :							Union[str, Any]         =				config_and_inputs
								snake_case_ :							str         =				{"pixel_values": pixel_values}
								return config, inputs_dict




@require_tf
class 						UpperCAmelCase_  (		SCREAMING_SNAKE_CASE__    ,     SCREAMING_SNAKE_CASE__    ,     unittest.TestCase    ):


						'''simple docstring'''




						A   :      Optional[int]						 =    (
						    (
						        TFSwinModel,
						        TFSwinForImageClassification,
						        TFSwinForMaskedImageModeling,
						    )
						    if is_tf_available()
						    else ()
						)
						A   :      Dict						 =    (
						    {'feature-extraction': TFSwinModel, 'image-classification': TFSwinForImageClassification}
						    if is_tf_available()
						    else {}
						)

						A   :      int						 =    False
						A   :      Tuple						 =    False
						A   :      Union[str, Any]						 =    False
						A   :      List[str]						 =    False






						def 					_lowerCAmelCase				(		self )       -> Optional[Any]:
								snake_case_ :							List[str]         =				TFSwinModelTester(self )
								snake_case_ :							Dict         =				ConfigTester(self						, config_class=_SCREAMING_SNAKE_CASE						, embed_dim=37 )






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






						def 					_lowerCAmelCase				(		self )       -> Union[str, Any]:
								snake_case_ :							Dict         =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE )






						def 					_lowerCAmelCase				(		self )       -> List[Any]:
								snake_case_ :							Any         =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_image_modeling(*_SCREAMING_SNAKE_CASE )






						def 					_lowerCAmelCase				(		self )       -> int:
								snake_case_ :							str         =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE )






						@unittest.skip(reason="Swin does not use inputs_embeds" )
						def 					_lowerCAmelCase				(		self )       -> Any:
								pass






						def 					_lowerCAmelCase				(		self )       -> List[Any]:
								snake_case_       ,				snake_case_ :							Optional[int]         =				self.model_tester.prepare_config_and_inputs_for_common()

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






						def 					_lowerCAmelCase				(		self )       -> int:
								snake_case_       ,				snake_case_ :							List[Any]         =				self.model_tester.prepare_config_and_inputs_for_common()

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

										snake_case_ :							Optional[Any]         =				["pixel_values"]
										self.assertListEqual(arg_names[:1]						, _SCREAMING_SNAKE_CASE )






						def 					_lowerCAmelCase				(		self )       -> List[Any]:
								snake_case_       ,				snake_case_ :							Optional[int]         =				self.model_tester.prepare_config_and_inputs_for_common()
								snake_case_ :							Union[str, Any]         =				True

								for model_class in self.all_model_classes:
										snake_case_ :							str         =				True
										snake_case_ :							Tuple         =				False
										snake_case_ :							Any         =				True
										snake_case_ :							Union[str, Any]         =				model_class(_SCREAMING_SNAKE_CASE )
										snake_case_ :							str         =				model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE ) )
										snake_case_ :							Any         =				outputs.attentions
										snake_case_ :							str         =				len(self.model_tester.depths )
										self.assertEqual(len(_SCREAMING_SNAKE_CASE )						, _SCREAMING_SNAKE_CASE )

										# check that output_attentions also work using config
										del inputs_dict["output_attentions"]
										snake_case_ :							int         =				True
										snake_case_ :							List[Any]         =				config.window_size**2
										snake_case_ :							Optional[Any]         =				model_class(_SCREAMING_SNAKE_CASE )
										snake_case_ :							Dict         =				model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE ) )
										snake_case_ :							Optional[Any]         =				outputs.attentions
										self.assertEqual(len(_SCREAMING_SNAKE_CASE )						, _SCREAMING_SNAKE_CASE )

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

										# Check attention is always last and order is fine
										snake_case_ :							str         =				True
										snake_case_ :							Optional[Any]         =				True
										snake_case_ :							Dict         =				model_class(_SCREAMING_SNAKE_CASE )
										snake_case_ :							str         =				model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE ) )

										if hasattr(self.model_tester						, "num_hidden_states_types" ):
												snake_case_ :							Optional[Any]         =				self.model_tester.num_hidden_states_types
										else:
												# also another +1 for reshaped_hidden_states
												snake_case_ :							Dict         =				2
										self.assertEqual(out_len + added_hidden_states						, len(_SCREAMING_SNAKE_CASE ) )

										snake_case_ :							List[str]         =				outputs.attentions

										self.assertEqual(len(_SCREAMING_SNAKE_CASE )						, _SCREAMING_SNAKE_CASE )

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






						def 					_lowerCAmelCase				(		self						, _SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE )       -> List[Any]:
								snake_case_ :							Optional[int]         =				model_class(_SCREAMING_SNAKE_CASE )
								snake_case_ :							Optional[Any]         =				model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE ) )
								snake_case_ :							Optional[int]         =				outputs.hidden_states

								snake_case_ :							List[str]         =				getattr(
								    self.model_tester						, "expected_num_hidden_layers"						, len(self.model_tester.depths ) + 1 )
								self.assertEqual(len(_SCREAMING_SNAKE_CASE )						, _SCREAMING_SNAKE_CASE )

								# Swin has a different seq_length
								snake_case_ :							int         =				to_atuple(config.patch_size )

								snake_case_ :							Dict         =				(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]						, )

								snake_case_ :							Union[str, Any]         =				outputs.reshaped_hidden_states
								self.assertEqual(len(_SCREAMING_SNAKE_CASE )						, _SCREAMING_SNAKE_CASE )

								snake_case_       ,				snake_case_       ,				snake_case_       ,				snake_case_ :							Union[str, Any]         =				reshaped_hidden_states[0].shape

								snake_case_ :							Any         =				tf.reshape(reshaped_hidden_states[0]						, (batch_size, num_channels, height * width) )
								snake_case_ :							Tuple         =				tf.transpose(_SCREAMING_SNAKE_CASE						, (0, 2, 1) )

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






						def 					_lowerCAmelCase				(		self )       -> Optional[Any]:
								snake_case_       ,				snake_case_ :							List[Any]         =				self.model_tester.prepare_config_and_inputs_for_common()

								snake_case_ :							Union[str, Any]         =				to_atuple(self.model_tester.image_size )

								for model_class in self.all_model_classes:
										snake_case_ :							Optional[Any]         =				True
										self.check_hidden_states_output(_SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE )

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

										self.check_hidden_states_output(_SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE )






						def 					_lowerCAmelCase				(		self )       -> Optional[int]:
								snake_case_       ,				snake_case_ :							int         =				self.model_tester.prepare_config_and_inputs_for_common()
								snake_case_ :							Tuple         =				3

								snake_case_ :							Any         =				to_atuple(self.model_tester.image_size )
								snake_case_ :							Dict         =				to_atuple(config.patch_size )

								snake_case_ :							Union[str, Any]         =				image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
								snake_case_ :							int         =				image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

								for model_class in self.all_model_classes:
										snake_case_ :							List[str]         =				True
										self.check_hidden_states_output(_SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE						, (padded_height, padded_width) )

										# check that output_hidden_states also work using config
										del inputs_dict["output_hidden_states"]
										snake_case_ :							Tuple         =				True
										self.check_hidden_states_output(_SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE						, _SCREAMING_SNAKE_CASE						, (padded_height, padded_width) )






						@slow
						def 					_lowerCAmelCase				(		self )       -> int:
								for model_name in TF_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										snake_case_ :							str         =				TFSwinModel.from_pretrained(_SCREAMING_SNAKE_CASE )
										self.assertIsNotNone(_SCREAMING_SNAKE_CASE )




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


						'''simple docstring'''




						@cached_property
						def 					_lowerCAmelCase				(		self )       -> Optional[Any]:
								return (
								    AutoImageProcessor.from_pretrained("microsoft/swin-tiny-patch4-window7-224" )
								    if is_vision_available()
								    else None
								)






						@slow
						def 					_lowerCAmelCase				(		self )       -> Tuple:
								snake_case_ :							Optional[Any]         =				TFSwinForImageClassification.from_pretrained("microsoft/swin-tiny-patch4-window7-224" )
								snake_case_ :							List[Any]         =				self.default_image_processor

								snake_case_ :							Union[str, Any]         =				Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
								snake_case_ :							List[Any]         =				image_processor(images=_SCREAMING_SNAKE_CASE						, return_tensors="tf" )

								# forward pass
								snake_case_ :							Any         =				model(_SCREAMING_SNAKE_CASE )

								# verify the logits
								snake_case_ :							Any         =				tf.TensorShape((1, 1000) )
								self.assertEqual(outputs.logits.shape						, _SCREAMING_SNAKE_CASE )
								snake_case_ :							Union[str, Any]         =				tf.constant([-0.0948, -0.6454, -0.0921] )
								self.assertTrue(np.allclose(outputs.logits[0, :3]						, _SCREAMING_SNAKE_CASE						, atol=1e-4 ) )
