




"""simple docstring"""

import collections
import inspect
import unittest

from transformers import SwinvaConfig
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_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 SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel
		from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
		from PIL import Image

		from transformers import AutoImageProcessor

class        lowercase      :







				def __init__(							self   :					Tuple      ,     _lowerCamelCase   :					List[str]      ,     _lowerCamelCase   :					Union[str, Any]=13      ,     _lowerCamelCase   :					List[str]=32      ,     _lowerCamelCase   :					Any=2      ,     _lowerCamelCase   :					List[str]=3      ,     _lowerCamelCase   :					Union[str, Any]=16      ,     _lowerCamelCase   :					Tuple=[1, 2, 1]      ,     _lowerCamelCase   :					Union[str, Any]=[2, 2, 4]      ,     _lowerCamelCase   :					List[str]=2      ,     _lowerCamelCase   :					List[Any]=2.0      ,     _lowerCamelCase   :					str=True      ,     _lowerCamelCase   :					Union[str, Any]=0.0      ,     _lowerCamelCase   :					str=0.0      ,     _lowerCamelCase   :					Any=0.1      ,     _lowerCamelCase   :					str="gelu"      ,     _lowerCamelCase   :					Tuple=False      ,     _lowerCamelCase   :					List[Any]=True      ,     _lowerCamelCase   :					Any=0.02      ,     _lowerCamelCase   :					str=1E-5      ,     _lowerCamelCase   :					Any=True      ,     _lowerCamelCase   :					int=None      ,     _lowerCamelCase   :					Union[str, Any]=True      ,     _lowerCamelCase   :					Union[str, Any]=10      ,     _lowerCamelCase   :					Optional[int]=8      ,     ):



					"""simple docstring"""

					A_  :				Optional[int]	   =		parent
					A_  :				Dict	   =		batch_size
					A_  :				Dict	   =		image_size
					A_  :				Any	   =		patch_size
					A_  :				Optional[Any]	   =		num_channels
					A_  :				str	   =		embed_dim
					A_  :				Optional[int]	   =		depths
					A_  :				Any	   =		num_heads
					A_  :				Any	   =		window_size
					A_  :				List[str]	   =		mlp_ratio
					A_  :				Optional[int]	   =		qkv_bias
					A_  :				Tuple	   =		hidden_dropout_prob
					A_  :				int	   =		attention_probs_dropout_prob
					A_  :				str	   =		drop_path_rate
					A_  :				Optional[int]	   =		hidden_act
					A_  :				Any	   =		use_absolute_embeddings
					A_  :				str	   =		patch_norm
					A_  :				Optional[Any]	   =		layer_norm_eps
					A_  :				Dict	   =		initializer_range
					A_  :				Optional[Any]	   =		is_training
					A_  :				int	   =		scope
					A_  :				List[str]	   =		use_labels
					A_  :				str	   =		type_sequence_label_size
					A_  :				List[str]	   =		encoder_stride







				def 					a_  (							self   :					Optional[Any]       ):



					"""simple docstring"""

					A_  :				int	   =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

					A_  :				Optional[Any]	   =		None
					if self.use_labels:
						A_  :				str	   =		ids_tensor([self.batch_size]      ,     self.type_sequence_label_size       )

					A_  :				Union[str, Any]	   =		self.get_config()

					return config, pixel_values, labels







				def 					a_  (							self   :					str       ):



					"""simple docstring"""

					return SwinvaConfig(
					    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   :					int      ,     _lowerCamelCase   :					List[str]      ,     _lowerCamelCase   :					List[Any]      ,     _lowerCamelCase   :					int       ):



					"""simple docstring"""

					A_  :				Optional[Any]	   =		SwinvaModel(config=_lowerCamelCase       )
					model.to(_lowerCamelCase       )
					model.eval()
					A_  :				Optional[int]	   =		model(_lowerCamelCase       )

					A_  :				Tuple	   =		((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths       ) - 1))
					A_  :				List[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 					a_  (							self   :					Optional[int]      ,     _lowerCamelCase   :					Any      ,     _lowerCamelCase   :					Tuple      ,     _lowerCamelCase   :					List[str]       ):



					"""simple docstring"""

					A_  :				Tuple	   =		SwinvaForMaskedImageModeling(config=_lowerCamelCase       )
					model.to(_lowerCamelCase       )
					model.eval()
					A_  :				List[Any]	   =		model(_lowerCamelCase       )
					self.parent.assertEqual(
					    result.logits.shape      ,     (self.batch_size, self.num_channels, self.image_size, self.image_size)       )

					# test greyscale images
					A_  :				List[str]	   =		1
					A_  :				Any	   =		SwinvaForMaskedImageModeling(_lowerCamelCase       )
					model.to(_lowerCamelCase       )
					model.eval()

					A_  :				List[Any]	   =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
					A_  :				Tuple	   =		model(_lowerCamelCase       )
					self.parent.assertEqual(result.logits.shape      ,     (self.batch_size, 1, self.image_size, self.image_size)       )







				def 					a_  (							self   :					List[str]      ,     _lowerCamelCase   :					Union[str, Any]      ,     _lowerCamelCase   :					List[Any]      ,     _lowerCamelCase   :					int       ):



					"""simple docstring"""

					A_  :				List[str]	   =		self.type_sequence_label_size
					A_  :				List[Any]	   =		SwinvaForImageClassification(_lowerCamelCase       )
					model.to(_lowerCamelCase       )
					model.eval()
					A_  :				Any	   =		model(_lowerCamelCase      ,     labels=_lowerCamelCase       )
					self.parent.assertEqual(result.logits.shape      ,     (self.batch_size, self.type_sequence_label_size)       )







				def 					a_  (							self   :					Optional[int]       ):



					"""simple docstring"""

					A_  :				List[str]	   =		self.prepare_config_and_inputs()
					A_  :				List[str]	   =		config_and_inputs
					A_  :				Dict	   =		{'''pixel_values''': pixel_values}
					return config, inputs_dict

@require_torch
class        lowercase      (    __UpperCAmelCase     ,	__UpperCAmelCase     ,	unittest.TestCase):
				__lowerCAmelCase    :    int         =   (
				    (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
				)
				__lowerCAmelCase    :    str         =   (
				    {"""feature-extraction""": SwinvaModel, """image-classification""": SwinvaForImageClassification}
				    if is_torch_available()
				    else {}
				)

				__lowerCAmelCase    :    Any         =   False
				__lowerCAmelCase    :    List[Any]         =   False
				__lowerCAmelCase    :    int         =   False
				__lowerCAmelCase    :    str         =   False







				def 					a_  (							self   :					List[str]       ):



					"""simple docstring"""

					A_  :				str	   =		SwinvaModelTester(self       )
					A_  :				Optional[Any]	   =		ConfigTester(self      ,     config_class=_lowerCamelCase      ,     embed_dim=37       )







				def 					a_  (							self   :					str       ):



					"""simple docstring"""

					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   :					Optional[Any]       ):



					"""simple docstring"""

					A_  :				Optional[Any]	   =		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*_lowerCamelCase       )







				@unittest.skip(reason='''Got `CUDA error: misaligned address` with PyTorch 2.0.0.'''       )
				def 					a_  (							self   :					int       ):



					"""simple docstring"""

					pass







				@unittest.skip(reason='''Swinv2 does not use inputs_embeds'''       )
				def 					a_  (							self   :					Optional[Any]       ):



					"""simple docstring"""

					pass







				def 					a_  (							self   :					Dict       ):



					"""simple docstring"""

					A_  :				int	   =		self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						A_  :				str	   =		model_class(_lowerCamelCase       )
						self.assertIsInstance(model.get_input_embeddings()      ,     (nn.Module)       )
						A_  :				Optional[Any]	   =		model.get_output_embeddings()
						self.assertTrue(x is None or isinstance(_lowerCamelCase      ,     nn.Linear       )       )







				def 					a_  (							self   :					Dict       ):



					"""simple docstring"""

					A_  :				int	   =		self.model_tester.prepare_config_and_inputs_for_common()

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

						A_  :				Optional[int]	   =		['''pixel_values''']
						self.assertListEqual(arg_names[:1]      ,     _lowerCamelCase       )







				def 					a_  (							self   :					Optional[Any]       ):



					"""simple docstring"""

					A_  :				Dict	   =		self.model_tester.prepare_config_and_inputs_for_common()
					A_  :				List[Any]	   =		True

					for model_class in self.all_model_classes:
						A_  :				Union[str, Any]	   =		True
						A_  :				int	   =		False
						A_  :				Union[str, Any]	   =		True
						A_  :				List[str]	   =		model_class(_lowerCamelCase       )
						model.to(_lowerCamelCase       )
						model.eval()
						with torch.no_grad():
							A_  :				Dict	   =		model(**self._prepare_for_class(_lowerCamelCase      ,     _lowerCamelCase       )       )
						A_  :				List[str]	   =		outputs.attentions
						A_  :				str	   =		len(self.model_tester.depths       )
						self.assertEqual(len(_lowerCamelCase       )      ,     _lowerCamelCase       )

						# check that output_attentions also work using config
						del inputs_dict["output_attentions"]
						A_  :				Union[str, Any]	   =		True
						A_  :				str	   =		config.window_size**2
						A_  :				Optional[Any]	   =		model_class(_lowerCamelCase       )
						model.to(_lowerCamelCase       )
						model.eval()
						with torch.no_grad():
							A_  :				Dict	   =		model(**self._prepare_for_class(_lowerCamelCase      ,     _lowerCamelCase       )       )
						A_  :				int	   =		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]      ,     )
						A_  :				Dict	   =		len(_lowerCamelCase       )

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

						if hasattr(self.model_tester      ,     '''num_hidden_states_types'''       ):
							A_  :				int	   =		self.model_tester.num_hidden_states_types
						else:
							# also another +1 for reshaped_hidden_states
							A_  :				List[str]	   =		2
						self.assertEqual(out_len + added_hidden_states      ,     len(_lowerCamelCase       )       )

						A_  :				Dict	   =		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   :					Dict      ,     _lowerCamelCase   :					int      ,     _lowerCamelCase   :					List[str]      ,     _lowerCamelCase   :					List[str]      ,     _lowerCamelCase   :					str       ):



					"""simple docstring"""

					A_  :				int	   =		model_class(_lowerCamelCase       )
					model.to(_lowerCamelCase       )
					model.eval()

					with torch.no_grad():
						A_  :				int	   =		model(**self._prepare_for_class(_lowerCamelCase      ,     _lowerCamelCase       )       )

					A_  :				Optional[Any]	   =		outputs.hidden_states

					A_  :				str	   =		getattr(
					    self.model_tester      ,     '''expected_num_hidden_layers'''      ,     len(self.model_tester.depths       ) + 1       )
					self.assertEqual(len(_lowerCamelCase       )      ,     _lowerCamelCase       )

					# Swinv2 has a different seq_length
					A_  :				Optional[int]	   =		(
					    config.patch_size
					    if isinstance(config.patch_size      ,     collections.abc.Iterable       )
					    else (config.patch_size, config.patch_size)
					)

					A_  :				List[str]	   =		(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]      ,     )

					A_  :				Tuple	   =		outputs.reshaped_hidden_states
					self.assertEqual(len(_lowerCamelCase       )      ,     _lowerCamelCase       )

					A_  :				Any	   =		reshaped_hidden_states[0].shape
					A_  :				List[str]	   =		(
					    reshaped_hidden_states[0].view(_lowerCamelCase      ,     _lowerCamelCase      ,     height * width       ).permute(0      ,     2      ,     1       )
					)
					self.assertListEqual(
					    list(reshaped_hidden_states.shape[-2:]       )      ,     [num_patches, self.model_tester.embed_dim]      ,     )







				def 					a_  (							self   :					int       ):



					"""simple docstring"""

					A_  :				List[Any]	   =		self.model_tester.prepare_config_and_inputs_for_common()

					A_  :				Any	   =		(
					    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:
						A_  :				Any	   =		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"]
						A_  :				Tuple	   =		True

						self.check_hidden_states_output(_lowerCamelCase      ,     _lowerCamelCase      ,     _lowerCamelCase      ,     _lowerCamelCase       )







				def 					a_  (							self   :					Tuple       ):



					"""simple docstring"""

					A_  :				Union[str, Any]	   =		self.model_tester.prepare_config_and_inputs_for_common()
					A_  :				Optional[Any]	   =		3

					A_  :				Tuple	   =		(
					    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)
					)
					A_  :				Any	   =		(
					    config.patch_size
					    if isinstance(config.patch_size      ,     collections.abc.Iterable       )
					    else (config.patch_size, config.patch_size)
					)

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

					for model_class in self.all_model_classes:
						A_  :				Optional[Any]	   =		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"]
						A_  :				Union[str, Any]	   =		True
						self.check_hidden_states_output(_lowerCamelCase      ,     _lowerCamelCase      ,     _lowerCamelCase      ,     (padded_height, padded_width)       )







				def 					a_  (							self   :					Dict       ):



					"""simple docstring"""

					A_  :				Union[str, Any]	   =		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_image_modeling(*_lowerCamelCase       )







				def 					a_  (							self   :					Union[str, Any]       ):



					"""simple docstring"""

					A_  :				List[str]	   =		self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase       )







				@slow
				def 					a_  (							self   :					Any       ):



					"""simple docstring"""

					for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						A_  :				str	   =		SwinvaModel.from_pretrained(_lowerCamelCase       )
						self.assertIsNotNone(_lowerCamelCase       )







				def 					a_  (							self   :					Optional[Any]       ):



					"""simple docstring"""

					A_  :				Any	   =		self.model_tester.prepare_config_and_inputs_for_common()

					A_  :				List[Any]	   =		_config_zero_init(_lowerCamelCase       )
					for model_class in self.all_model_classes:
						A_  :				int	   =		model_class(config=_lowerCamelCase       )
						for name, param in model.named_parameters():
							if "embeddings" not in name and "logit_scale" 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        lowercase      (    unittest.TestCase):







				@cached_property
				def 					a_  (							self   :					List[Any]       ):



					"""simple docstring"""

					return (
					    AutoImageProcessor.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256'''       )
					    if is_vision_available()
					    else None
					)







				@slow
				def 					a_  (							self   :					List[str]       ):



					"""simple docstring"""

					A_  :				Optional[Any]	   =		SwinvaForImageClassification.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256'''       ).to(
					    _lowerCamelCase       )
					A_  :				Optional[Any]	   =		self.default_image_processor

					A_  :				Tuple	   =		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
					A_  :				List[str]	   =		image_processor(images=_lowerCamelCase      ,     return_tensors='''pt'''       ).to(_lowerCamelCase       )

					# forward pass
					with torch.no_grad():
						A_  :				List[str]	   =		model(**_lowerCamelCase       )

					# verify the logits
					A_  :				Dict	   =		torch.Size((1, 10_00)       )
					self.assertEqual(outputs.logits.shape      ,     _lowerCamelCase       )
					A_  :				Optional[Any]	   =		torch.tensor([-0.3947, -0.4306, 0.0026]       ).to(_lowerCamelCase       )
					self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,     _lowerCamelCase      ,     atol=1E-4       )       )












"""simple docstring"""

import random

from .binary_exp_mod import bin_exp_mod

def 		lowercase_      (		_UpperCAmelCase  ,      _UpperCAmelCase=1000	):




	"""simple docstring"""






	if n < 2:
		return False

	if n % 2 == 0:
		return n == 2

	# this means n is odd
	A_  :				List[Any]	   =		n - 1
	A_  :				Dict	   =		0
	while d % 2 == 0:
		d /= 2
		exp += 1

	# n - 1=d*(2**exp)
	A_  :				Optional[Any]	   =		0
	while count < prec:
		A_  :				Optional[int]	   =		random.randint(2  ,      n - 1	)
		A_  :				Any	   =		bin_exp_mod(_UpperCAmelCase  ,      _UpperCAmelCase  ,      _UpperCAmelCase	)
		if b != 1:
			A_  :				Dict	   =		True
			for _ in range(_UpperCAmelCase	):
				if b == n - 1:
					A_  :				Optional[int]	   =		False
					break
				A_  :				Any	   =		b * b
				b %= n
			if flag:
				return False
			count += 1
	return True


if __name__ == "__main__":
		_lowerCamelCase  :			List[str]        =       abs(int(input('Enter bound : ').strip()))
		print('Here\'s the list of primes:')
		print(', '.join(str(i) for i in range(n + 1) if is_prime_big(i)))





