


"""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     _UpperCamelCase    :
							'''simple docstring'''







							def __init__(	self       ,  __a       ,  __a=13       ,  __a=32       ,  __a=2       ,  __a=3       ,  __a=16       ,  __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.0_2       ,  __a=1e-5       ,  __a=True       ,  __a=None       ,  __a=True       ,  __a=10       ,  __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      snake_case							(	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      snake_case							(	self      ):
										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      snake_case							(	self       ,  __a       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     SwinvaModel(config=__a      )
										model.to(__a      )
										model.eval()
										__lowerCAmelCase							       =     model(__a      )

										__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      snake_case							(	self       ,  __a       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     SwinvaForMaskedImageModeling(config=__a      )
										model.to(__a      )
										model.eval()
										__lowerCAmelCase							       =     model(__a      )
										self.parent.assertEqual(
										    result.logits.shape       ,  (self.batch_size, self.num_channels, self.image_size, self.image_size)      )

										# test greyscale images
										__lowerCAmelCase							       =     1
										__lowerCAmelCase							       =     SwinvaForMaskedImageModeling(__a      )
										model.to(__a      )
										model.eval()

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

							def      snake_case							(	self       ,  __a       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     self.type_sequence_label_size
										__lowerCAmelCase							       =     SwinvaForImageClassification(__a      )
										model.to(__a      )
										model.eval()
										__lowerCAmelCase							       =     model(__a       ,  labels=__a      )
										self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.type_sequence_label_size)      )



							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     self.prepare_config_and_inputs()
										__lowerCAmelCase       ,      __lowerCAmelCase       ,      __lowerCAmelCase							       =     config_and_inputs
										__lowerCAmelCase							       =     {"pixel_values": pixel_values}
										return config, inputs_dict
@require_torch
class     _UpperCamelCase    (  lowerCAmelCase__			,lowerCAmelCase__			,unittest.TestCase						):
							'''simple docstring'''







							__UpperCAmelCase		:       Optional[int]							 =(
							    (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
							)
							__UpperCAmelCase		:       str							 =(
							    {"""feature-extraction""": SwinvaModel, """image-classification""": SwinvaForImageClassification}
							    if is_torch_available()
							    else {}
							)

							__UpperCAmelCase		:       str							 =False
							__UpperCAmelCase		:       List[Any]							 =False
							__UpperCAmelCase		:       Any							 =False
							__UpperCAmelCase		:       Tuple							 =False

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     SwinvaModelTester(self      )
										__lowerCAmelCase							       =     ConfigTester(self       ,  config_class=__a       ,  embed_dim=37      )

							def      snake_case							(	self      ):
										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      snake_case							(	self      ):
										__lowerCAmelCase							       =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*__a      )

							@unittest.skip(reason="Got `CUDA error: misaligned address` with PyTorch 2.0.0."      )
							def      snake_case							(	self      ):
										pass

							@unittest.skip(reason="Swinv2 does not use inputs_embeds"      )
							def      snake_case							(	self      ):
										pass

							def      snake_case							(	self      ):
										__lowerCAmelCase       ,      __lowerCAmelCase							       =     self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
													__lowerCAmelCase							       =     model_class(__a      )
													self.assertIsInstance(model.get_input_embeddings()       ,  (nn.Module)      )
													__lowerCAmelCase							       =     model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(__a       ,  nn.Linear      )      )

							def      snake_case							(	self      ):
										__lowerCAmelCase       ,      __lowerCAmelCase							       =     self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
													__lowerCAmelCase							       =     model_class(__a      )
													__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]       ,  __a      )

							def      snake_case							(	self      ):
										__lowerCAmelCase       ,      __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(__a      )
													model.to(__a      )
													model.eval()
													with torch.no_grad():
																__lowerCAmelCase							       =     model(**self._prepare_for_class(__a       ,  __a      )      )
													__lowerCAmelCase							       =     outputs.attentions
													__lowerCAmelCase							       =     len(self.model_tester.depths      )
													self.assertEqual(len(__a      )       ,  __a      )

													# check that output_attentions also work using config
													del inputs_dict["output_attentions"]
													__lowerCAmelCase							       =     True
													__lowerCAmelCase							       =     config.window_size**2
													__lowerCAmelCase							       =     model_class(__a      )
													model.to(__a      )
													model.eval()
													with torch.no_grad():
																__lowerCAmelCase							       =     model(**self._prepare_for_class(__a       ,  __a      )      )
													__lowerCAmelCase							       =     outputs.attentions
													self.assertEqual(len(__a      )       ,  __a      )

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

													# Check attention is always last and order is fine
													__lowerCAmelCase							       =     True
													__lowerCAmelCase							       =     True
													__lowerCAmelCase							       =     model_class(__a      )
													model.to(__a      )
													model.eval()
													with torch.no_grad():
																__lowerCAmelCase							       =     model(**self._prepare_for_class(__a       ,  __a      )      )

													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(__a      )      )

													__lowerCAmelCase							       =     outputs.attentions

													self.assertEqual(len(__a      )       ,  __a      )

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

							def      snake_case							(	self       ,  __a       ,  __a       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     model_class(__a      )
										model.to(__a      )
										model.eval()

										with torch.no_grad():
													__lowerCAmelCase							       =     model(**self._prepare_for_class(__a       ,  __a      )      )

										__lowerCAmelCase							       =     outputs.hidden_states

										__lowerCAmelCase							       =     getattr(
										    self.model_tester       ,  "expected_num_hidden_layers"       ,  len(self.model_tester.depths      ) + 1      )
										self.assertEqual(len(__a      )       ,  __a      )

										# Swinv2 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(__a      )       ,  __a      )

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

							def      snake_case							(	self      ):
										__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:
													__lowerCAmelCase							       =     True
													self.check_hidden_states_output(__a       ,  __a       ,  __a       ,  __a      )

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

													self.check_hidden_states_output(__a       ,  __a       ,  __a       ,  __a      )

							def      snake_case							(	self      ):
										__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:
													__lowerCAmelCase							       =     True
													self.check_hidden_states_output(__a       ,  __a       ,  __a       ,  (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(__a       ,  __a       ,  __a       ,  (padded_height, padded_width)      )

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_image_modeling(*__a      )

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*__a      )

							@slow
							def      snake_case							(	self      ):
										for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__lowerCAmelCase							       =     SwinvaModel.from_pretrained(__a      )
													self.assertIsNotNone(__a      )



							def      snake_case							(	self      ):
										__lowerCAmelCase       ,      __lowerCAmelCase							       =     self.model_tester.prepare_config_and_inputs_for_common()

										__lowerCAmelCase							       =     _config_zero_init(__a      )
										for model_class in self.all_model_classes:
													__lowerCAmelCase							       =     model_class(config=__a      )
													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     _UpperCamelCase    (  unittest.TestCase						):
							'''simple docstring'''







							@cached_property
							def      snake_case							(	self      ):
										return (
										    AutoImageProcessor.from_pretrained("microsoft/swinv2-tiny-patch4-window8-256"      )
										    if is_vision_available()
										    else None
										)



							@slow
							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     SwinvaForImageClassification.from_pretrained("microsoft/swinv2-tiny-patch4-window8-256"      ).to(
										    __a      )
										__lowerCAmelCase							       =     self.default_image_processor

										__lowerCAmelCase							       =     Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
										__lowerCAmelCase							       =     image_processor(images=__a       ,  return_tensors="pt"      ).to(__a      )

										# forward pass
										with torch.no_grad():
													__lowerCAmelCase							       =     model(**__a      )

										# verify the logits
										__lowerCAmelCase							       =     torch.Size((1, 10_00)      )
										self.assertEqual(outputs.logits.shape       ,  __a      )
										__lowerCAmelCase							       =     torch.tensor([-0.3_9_4_7, -0.4_3_0_6, 0.0_0_2_6]      ).to(__a      )
										self.assertTrue(torch.allclose(outputs.logits[0, :3]       ,  __a       ,  atol=1e-4      )      )








"""simple docstring"""


import torch

from diffusers import KDPMaDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest
class     _UpperCamelCase    (  lowerCAmelCase__						):
							'''simple docstring'''







							__UpperCAmelCase		:       Tuple							 =(KDPMaDiscreteScheduler,)
							__UpperCAmelCase		:       Optional[Any]							 =1_0

							def      snake_case							(	self       ,  **__a      ):
										__lowerCAmelCase							       =     {
										    "num_train_timesteps": 11_00,
										    "beta_start": 0.0_0_0_1,
										    "beta_end": 0.0_2,
										    "beta_schedule": "linear",
										}

										config.update(**__a      )
										return config

							def      snake_case							(	self      ):
										for timesteps in [10, 50, 1_00, 10_00]:
													self.check_over_configs(num_train_timesteps=__a      )

							def      snake_case							(	self      ):
										for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1]       ,  [0.0_0_0_2, 0.0_0_2, 0.0_2]      ):
													self.check_over_configs(beta_start=__a       ,  beta_end=__a      )

							def      snake_case							(	self      ):
										for schedule in ["linear", "scaled_linear"]:
													self.check_over_configs(beta_schedule=__a      )

							def      snake_case							(	self      ):
										for prediction_type in ["epsilon", "v_prediction"]:
													self.check_over_configs(prediction_type=__a      )

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     self.scheduler_classes[0]
										__lowerCAmelCase							       =     self.get_scheduler_config(prediction_type="v_prediction"      )
										__lowerCAmelCase							       =     scheduler_class(**__a      )

										scheduler.set_timesteps(self.num_inference_steps      )

										__lowerCAmelCase							       =     self.dummy_model()
										__lowerCAmelCase							       =     self.dummy_sample_deter * scheduler.init_noise_sigma
										__lowerCAmelCase							       =     sample.to(__a      )

										for i, t in enumerate(scheduler.timesteps      ):
													__lowerCAmelCase							       =     scheduler.scale_model_input(__a       ,  __a      )

													__lowerCAmelCase							       =     model(__a       ,  __a      )

													__lowerCAmelCase							       =     scheduler.step(__a       ,  __a       ,  __a      )
													__lowerCAmelCase							       =     output.prev_sample

										__lowerCAmelCase							       =     torch.sum(torch.abs(__a      )      )
										__lowerCAmelCase							       =     torch.mean(torch.abs(__a      )      )

										if torch_device in ["cpu", "mps"]:
													assert abs(result_sum.item() - 4.6_9_3_4e-0_7      ) < 1e-2
													assert abs(result_mean.item() - 6.1_1_1_2e-1_0      ) < 1e-3
										else:
													# CUDA
													assert abs(result_sum.item() - 4.6_9_3_4_2_8_6_5_0_1_7_0_9_7_2e-0_7      ) < 1e-2
													assert abs(result_mean.item() - 0.0_0_0_2      ) < 1e-3

							def      snake_case							(	self      ):
										if torch_device == "mps":
													return
										__lowerCAmelCase							       =     self.scheduler_classes[0]
										__lowerCAmelCase							       =     self.get_scheduler_config()
										__lowerCAmelCase							       =     scheduler_class(**__a      )

										scheduler.set_timesteps(self.num_inference_steps      )

										__lowerCAmelCase							       =     self.dummy_model()
										__lowerCAmelCase							       =     self.dummy_sample_deter * scheduler.init_noise_sigma
										__lowerCAmelCase							       =     sample.to(__a      )

										for i, t in enumerate(scheduler.timesteps      ):
													__lowerCAmelCase							       =     scheduler.scale_model_input(__a       ,  __a      )

													__lowerCAmelCase							       =     model(__a       ,  __a      )

													__lowerCAmelCase							       =     scheduler.step(__a       ,  __a       ,  __a      )
													__lowerCAmelCase							       =     output.prev_sample

										__lowerCAmelCase							       =     torch.sum(torch.abs(__a      )      )
										__lowerCAmelCase							       =     torch.mean(torch.abs(__a      )      )

										if torch_device in ["cpu", "mps"]:
													assert abs(result_sum.item() - 2_0.4_1_2_5      ) < 1e-2
													assert abs(result_mean.item() - 0.0_2_6_6      ) < 1e-3
										else:
													# CUDA
													assert abs(result_sum.item() - 2_0.4_1_2_5      ) < 1e-2
													assert abs(result_mean.item() - 0.0_2_6_6      ) < 1e-3



							def      snake_case							(	self      ):
										if torch_device == "mps":
													return
										__lowerCAmelCase							       =     self.scheduler_classes[0]
										__lowerCAmelCase							       =     self.get_scheduler_config()
										__lowerCAmelCase							       =     scheduler_class(**__a      )

										scheduler.set_timesteps(self.num_inference_steps       ,  device=__a      )

										__lowerCAmelCase							       =     self.dummy_model()
										__lowerCAmelCase							       =     self.dummy_sample_deter.to(__a      ) * scheduler.init_noise_sigma

										for t in scheduler.timesteps:
													__lowerCAmelCase							       =     scheduler.scale_model_input(__a       ,  __a      )

													__lowerCAmelCase							       =     model(__a       ,  __a      )

													__lowerCAmelCase							       =     scheduler.step(__a       ,  __a       ,  __a      )
													__lowerCAmelCase							       =     output.prev_sample

										__lowerCAmelCase							       =     torch.sum(torch.abs(__a      )      )
										__lowerCAmelCase							       =     torch.mean(torch.abs(__a      )      )

										if str(__a      ).startswith("cpu"      ):
													# The following sum varies between 148 and 156 on mps. Why?
													assert abs(result_sum.item() - 2_0.4_1_2_5      ) < 1e-2
													assert abs(result_mean.item() - 0.0_2_6_6      ) < 1e-3
										else:
													# CUDA
													assert abs(result_sum.item() - 2_0.4_1_2_5      ) < 1e-2
													assert abs(result_mean.item() - 0.0_2_6_6      ) < 1e-3




