



"""simple docstring"""

import collections
import inspect
import unittest
from typing import Dict, List, Tuple

from transformers import MaskFormerSwinConfig
from transformers.testing_utils import require_torch, require_torch_multi_gpu, torch_device
from transformers.utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch
     from torch import nn

     from transformers import MaskFormerSwinBackbone
     from transformers.models.maskformer import MaskFormerSwinModel




class  _UpperCAmelCase :

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

 def 		_snake_case      (  self	: Optional[Any]       ):
     snake_case_				:  str       =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

     snake_case_				:  Optional[int]       =			self.get_config()

     return config, pixel_values, labels

 def 		_snake_case      (  self	: List[str]       ):
     return MaskFormerSwinConfig(
         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						,  out_features=self.out_features						,  out_indices=self.out_indices						,  )

 def 		_snake_case      (  self	: List[str]						,  lowercase_	: List[Any]						,  lowercase_	: Tuple						,  lowercase_	: Any       ):
     snake_case_				:  int       =			MaskFormerSwinModel(config=lowercase_       )
     model.to(lowercase_       )
     model.eval()
     snake_case_				:  Any       =			model(lowercase_       )

     snake_case_				:  Union[str, Any]       =			((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths       ) - 1))
     snake_case_				:  Dict       =			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	: List[str]						,  lowercase_	: Optional[Any]						,  lowercase_	: List[str]						,  lowercase_	: Any       ):
     snake_case_				:  List[str]       =			MaskFormerSwinBackbone(config=lowercase_       )
     model.to(lowercase_       )
     model.eval()
     snake_case_				:  Optional[Any]       =			model(lowercase_       )

     # verify feature maps
     self.parent.assertEqual(len(result.feature_maps       )						,  len(config.out_features       )       )
     self.parent.assertListEqual(list(result.feature_maps[0].shape       )						,  [13, 16, 16, 16]       )

     # verify channels
     self.parent.assertEqual(len(model.channels       )						,  len(config.out_features       )       )
     self.parent.assertListEqual(model.channels						,  [16, 32, 64]       )

     # verify ValueError
     with self.parent.assertRaises(lowercase_       ):
         snake_case_				:  int       =			['''stem''']
         snake_case_				:  int       =			MaskFormerSwinBackbone(config=lowercase_       )






 def 		_snake_case      (  self	: Tuple       ):
     snake_case_				:  Dict       =			self.prepare_config_and_inputs()
     snake_case_,			snake_case_,			snake_case_				:  Union[str, Any]       =			config_and_inputs
     snake_case_				:  int       =			{'''pixel_values''': pixel_values}
     return config, inputs_dict





@require_torch
class  _UpperCAmelCase (    lowerCAmelCase__					,  lowerCAmelCase__					,  unittest.TestCase):
 _lowerCAmelCase     :    Dict	      =	(
     (
         MaskFormerSwinModel,
         MaskFormerSwinBackbone,
     )
     if is_torch_available()
     else ()
 )
 _lowerCAmelCase     :    Optional[int]	      =	{"""feature-extraction""": MaskFormerSwinModel} if is_torch_available() else {}
 _lowerCAmelCase     :    Optional[int]	      =	False
 _lowerCAmelCase     :    int	      =	False
 _lowerCAmelCase     :    int	      =	False
 _lowerCAmelCase     :    Union[str, Any]	      =	False
 _lowerCAmelCase     :    Tuple	      =	False

 def 		_snake_case      (  self	: str       ):
     snake_case_				:  Any       =			MaskFormerSwinModelTester(self       )
     snake_case_				:  Any       =			ConfigTester(self						,  config_class=lowercase_						,  embed_dim=37       )

 @require_torch_multi_gpu
 @unittest.skip(
     reason=(
         '''`MaskFormerSwinModel` outputs `hidden_states_spatial_dimensions` which doesn\'t work well with'''
         ''' `nn.DataParallel`'''
     )       )
 def 		_snake_case      (  self	: Optional[int]       ):
     pass

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

 def 		_snake_case      (  self	: List[str]       ):
     snake_case_				:  Union[str, Any]       =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowercase_       )

 def 		_snake_case      (  self	: int       ):
     snake_case_				:  Tuple       =			self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_backbone(*lowercase_       )

 @unittest.skip('''Swin does not use inputs_embeds'''       )
 def 		_snake_case      (  self	: Dict       ):
     pass

 @unittest.skip('''Swin does not support feedforward chunking'''       )
 def 		_snake_case      (  self	: Dict       ):
     pass

 def 		_snake_case      (  self	: str       ):
     snake_case_,			snake_case_				:  str       =			self.model_tester.prepare_config_and_inputs_for_common()

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

 def 		_snake_case      (  self	: Any       ):
     snake_case_,			snake_case_				:  Any       =			self.model_tester.prepare_config_and_inputs_for_common()

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

         snake_case_				:  Any       =			['''pixel_values''']
         self.assertListEqual(arg_names[:1]						,  lowercase_       )

 @unittest.skip(reason='''MaskFormerSwin is only used as backbone and doesn\'t support output_attentions'''       )
 def 		_snake_case      (  self	: Optional[Any]       ):
     pass

 @unittest.skip(reason='''MaskFormerSwin is only used as an internal backbone'''       )
 def 		_snake_case      (  self	: List[str]       ):
     pass

 def 		_snake_case      (  self	: List[str]						,  lowercase_	: int						,  lowercase_	: Optional[int]						,  lowercase_	: Dict						,  lowercase_	: Optional[Any]       ):
     snake_case_				:  Union[str, Any]       =			model_class(lowercase_       )
     model.to(lowercase_       )
     model.eval()

     with torch.no_grad():
         snake_case_				:  int       =			model(**self._prepare_for_class(lowercase_						,  lowercase_       )       )

     snake_case_				:  int       =			outputs.hidden_states

     snake_case_				:  List[Any]       =			getattr(
         self.model_tester						,  '''expected_num_hidden_layers'''						,  len(self.model_tester.depths       ) + 1       )
     self.assertEqual(len(lowercase_       )						,  lowercase_       )

     # Swin has a different seq_length
     snake_case_				:  str       =			(
         config.patch_size
         if isinstance(config.patch_size						,  collections.abc.Iterable       )
         else (config.patch_size, config.patch_size)
     )

     snake_case_				:  Union[str, Any]       =			(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]						,  )

 def 		_snake_case      (  self	: Tuple       ):
     snake_case_,			snake_case_				:  Optional[Any]       =			self.model_tester.prepare_config_and_inputs_for_common()

     snake_case_				:  str       =			(
         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:
         snake_case_				:  Dict       =			True
         self.check_hidden_states_output(lowercase_						,  lowercase_						,  lowercase_						,  lowercase_       )

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

         self.check_hidden_states_output(lowercase_						,  lowercase_						,  lowercase_						,  lowercase_       )

 def 		_snake_case      (  self	: int       ):
     snake_case_,			snake_case_				:  Optional[int]       =			self.model_tester.prepare_config_and_inputs_for_common()
     snake_case_				:  Dict       =			3

     snake_case_				:  List[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)
     )
     snake_case_				:  str       =			(
         config.patch_size
         if isinstance(config.patch_size						,  collections.abc.Iterable       )
         else (config.patch_size, config.patch_size)
     )

     snake_case_				:  str       =			image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
     snake_case_				:  Optional[Any]       =			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(lowercase_						,  lowercase_						,  lowercase_						,  (padded_height, padded_width)       )

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

 @unittest.skip(reason='''MaskFormerSwin doesn\'t have pretrained checkpoints'''       )
 def 		_snake_case      (  self	: Optional[int]       ):
     pass

 @unittest.skip(reason='''This will be fixed once MaskFormerSwin is replaced by native Swin'''       )
 def 		_snake_case      (  self	: Tuple       ):
     pass

 @unittest.skip(reason='''This will be fixed once MaskFormerSwin is replaced by native Swin'''       )
 def 		_snake_case      (  self	: Optional[Any]       ):
     pass






 def 		_snake_case      (  self	: Any       ):
     snake_case_,			snake_case_				:  Optional[Any]       =			self.model_tester.prepare_config_and_inputs_for_common()

     def set_nan_tensor_to_zero(lowercase_	: List[str]       ):
         snake_case_				:  List[Any]       =			0
         return t

     def check_equivalence(lowercase_	: int						,  lowercase_	: Tuple						,  lowercase_	: str						,  lowercase_	: Optional[int]={}       ):
         with torch.no_grad():
             snake_case_				:  Any       =			model(**lowercase_						,  return_dict=lowercase_						,  **lowercase_       )
             snake_case_				:  str       =			model(**lowercase_						,  return_dict=lowercase_						,  **lowercase_       ).to_tuple()

             def recursive_check(lowercase_	: Dict						,  lowercase_	: Any       ):
                 if isinstance(lowercase_						,  (List, Tuple)       ):
                     for tuple_iterable_value, dict_iterable_value in zip(lowercase_						,  lowercase_       ):
                         recursive_check(lowercase_						,  lowercase_       )
                 elif isinstance(lowercase_						,  lowercase_       ):
                     for tuple_iterable_value, dict_iterable_value in zip(
                         tuple_object.values()						,  dict_object.values()       ):
                         recursive_check(lowercase_						,  lowercase_       )
                 elif tuple_object is None:
                     return
                 else:
                     self.assertTrue(
                         torch.allclose(
                             set_nan_tensor_to_zero(lowercase_       )						,  set_nan_tensor_to_zero(lowercase_       )						,  atol=1E-5       )						,  msg=(
                             '''Tuple and dict output are not equal. Difference:'''
                             f" {torch.max(torch.abs(tuple_object - dict_object       )       )}. Tuple has `nan`:"
                             f" {torch.isnan(lowercase_       ).any()} and `inf`: {torch.isinf(lowercase_       )}. Dict has"
                             f" `nan`: {torch.isnan(lowercase_       ).any()} and `inf`: {torch.isinf(lowercase_       )}."
                         )						,  )

             recursive_check(lowercase_						,  lowercase_       )

     for model_class in self.all_model_classes:
         snake_case_				:  int       =			model_class(lowercase_       )
         model.to(lowercase_       )
         model.eval()

         snake_case_				:  Optional[int]       =			self._prepare_for_class(lowercase_						,  lowercase_       )
         snake_case_				:  Union[str, Any]       =			self._prepare_for_class(lowercase_						,  lowercase_       )
         check_equivalence(lowercase_						,  lowercase_						,  lowercase_       )

         snake_case_				:  int       =			self._prepare_for_class(lowercase_						,  lowercase_						,  return_labels=lowercase_       )
         snake_case_				:  Any       =			self._prepare_for_class(lowercase_						,  lowercase_						,  return_labels=lowercase_       )
         check_equivalence(lowercase_						,  lowercase_						,  lowercase_       )

         snake_case_				:  int       =			self._prepare_for_class(lowercase_						,  lowercase_       )
         snake_case_				:  Any       =			self._prepare_for_class(lowercase_						,  lowercase_       )
         check_equivalence(lowercase_						,  lowercase_						,  lowercase_						,  {'''output_hidden_states''': True}       )

         snake_case_				:  Union[str, Any]       =			self._prepare_for_class(lowercase_						,  lowercase_						,  return_labels=lowercase_       )
         snake_case_				:  int       =			self._prepare_for_class(lowercase_						,  lowercase_						,  return_labels=lowercase_       )
         check_equivalence(lowercase_						,  lowercase_						,  lowercase_						,  {'''output_hidden_states''': True}       )





@require_torch
class  _UpperCAmelCase (    unittest.TestCase					,  lowerCAmelCase__):
 _lowerCAmelCase     :    Optional[int]	      =	(MaskFormerSwinBackbone,) if is_torch_available() else ()
 _lowerCAmelCase     :    List[Any]	      =	MaskFormerSwinConfig

 def 		_snake_case      (  self	: List[Any]       ):
     snake_case_				:  Tuple       =			MaskFormerSwinModelTester(self       )






 def 		_snake_case      (  self	: int       ):
     snake_case_,			snake_case_				:  List[str]       =			self.model_tester.prepare_config_and_inputs_for_common()
     snake_case_				:  Union[str, Any]       =			inputs_dict['''pixel_values'''].shape[0]

     for backbone_class in self.all_model_classes:
         snake_case_				:  Optional[Any]       =			backbone_class(lowercase_       )
         backbone.to(lowercase_       )
         backbone.eval()

         snake_case_				:  Dict       =			backbone(**lowercase_       )

         # Test default outputs and verify feature maps
         self.assertIsInstance(outputs.feature_maps						,  lowercase_       )
         self.assertTrue(len(outputs.feature_maps       ) == len(backbone.channels       )       )
         for feature_map, n_channels in zip(outputs.feature_maps						,  backbone.channels       ):
             self.assertTrue(feature_map.shape[:2]						,  (batch_size, n_channels)       )
         self.assertIsNone(outputs.hidden_states       )
         self.assertIsNone(outputs.attentions       )

         # Test output_hidden_states=True
         snake_case_				:  Tuple       =			backbone(**lowercase_						,  output_hidden_states=lowercase_       )
         self.assertIsNotNone(outputs.hidden_states       )
         self.assertTrue(len(outputs.hidden_states       )						,  len(backbone.stage_names       )       )
         # We skip the stem layer
         for hidden_states, n_channels in zip(outputs.hidden_states[1:]						,  backbone.channels       ):
             for hidden_state in hidden_states:
                 # Hidden states are in the format (batch_size, (height * width), n_channels)
                 snake_case_,			snake_case_,			snake_case_				:  List[str]       =			hidden_state.shape
                 self.assertTrue((h_batch_size, h_n_channels)						,  (batch_size, n_channels)       )

            # Test output_attentions=True
         if self.has_attentions:
             snake_case_				:  Any       =			backbone(**lowercase_						,  output_attentions=lowercase_       )
             self.assertIsNotNone(outputs.attentions       )










"""simple docstring"""

import numpy as np
from numpy import ndarray
from scipy.optimize import Bounds, LinearConstraint, minimize




def __lowercase    (							_a					):
    return np.dot(_a	,    _a					)




class  _UpperCAmelCase :

 def __init__(  self	: int						,  *,
     lowercase_	: float = np.inf						,  lowercase_	: str = "linear"						,  lowercase_	: float = 0.0						,  ):
     snake_case_				:  Optional[Any]       =			regularization
     snake_case_				:  Tuple       =			gamma
     if kernel == "linear":
         snake_case_				:  int       =			self.__linear
     elif kernel == "rbf":
         if self.gamma == 0:
             raise ValueError('''rbf kernel requires gamma'''       )
         if not isinstance(self.gamma						,  (float, int)       ):
             raise ValueError('''gamma must be float or int'''       )
         if not self.gamma > 0:
             raise ValueError('''gamma must be > 0'''       )
         snake_case_				:  int       =			self.__rbf
         # in the future, there could be a default value like in sklearn
         # sklear: def_gamma = 1/(n_features * X.var()) (wiki)
         # previously it was 1/(n_features)
     else:
         snake_case_				:  List[Any]       =			f"Unknown kernel: {kernel}"
         raise ValueError(lowercase_       )

 def 		_snake_case      (  self	: int						,  lowercase_	: ndarray						,  lowercase_	: ndarray       ):
     return np.dot(lowercase_						,  lowercase_       )

 def 		_snake_case      (  self	: int						,  lowercase_	: ndarray						,  lowercase_	: ndarray       ):
     return np.exp(-(self.gamma * norm_squared(vectora - vectora       ))       )

 def 		_snake_case      (  self	: Any						,  lowercase_	: list[ndarray]						,  lowercase_	: ndarray       ):
     snake_case_				:  Union[str, Any]       =			observations
     snake_case_				:  int       =			classes

     # using Wolfe's Dual to calculate w.
     # Primal problem: minimize 1/2*norm_squared(w)
     #   constraint: yn(w . xn + b) >= 1
     #
     # With l a vector
     # Dual problem: maximize sum_n(ln) -
     #       1/2 * sum_n(sum_m(ln*lm*yn*ym*xn . xm))
     #   constraint: self.C >= ln >= 0
     #           and sum_n(ln*yn) = 0
     # Then we get w using w = sum_n(ln*yn*xn)
     # At the end we can get b ~= mean(yn - w . xn)
     #
     # Since we use kernels, we only need l_star to calculate b
     # and to classify observations

     ((snake_case_),			)				:  List[str]       =			np.shape(lowercase_       )

     def to_minimize(lowercase_	: ndarray       ) -> float:
         snake_case_				:  Tuple       =			0
         ((snake_case_),			)				:  Optional[Any]       =			np.shape(lowercase_       )
         for i in range(lowercase_       ):
             for j in range(lowercase_       ):
                 s += (
                     candidate[i]
                     * candidate[j]
                     * classes[i]
                     * classes[j]
                     * self.kernel(observations[i]						,  observations[j]       )
                 )
         return 1 / 2 * s - sum(lowercase_       )

     snake_case_				:  Optional[Any]       =			LinearConstraint(lowercase_						,  0						,  0       )
     snake_case_				:  str       =			Bounds(0						,  self.regularization       )

     snake_case_				:  int       =			minimize(
         lowercase_						,  np.ones(lowercase_       )						,  bounds=lowercase_						,  constraints=[ly_contraint]       ).x
     snake_case_				:  Optional[Any]       =			l_star

     # calculating mean offset of separation plane to points
     snake_case_				:  List[Any]       =			0
     for i in range(lowercase_       ):
         for j in range(lowercase_       ):
             s += classes[i] - classes[i] * self.optimum[i] * self.kernel(
                 observations[i]						,  observations[j]       )
     snake_case_				:  Union[str, Any]       =			s / n






 def 		_snake_case      (  self	: List[str]						,  lowercase_	: ndarray       ):
     snake_case_				:  int       =			sum(
         self.optimum[n]
         * self.classes[n]
         * self.kernel(self.observations[n]						,  lowercase_       )
         for n in range(len(self.classes       )       )       )
     return 1 if s + self.offset >= 0 else -1


if __name__ == "__main__":
     import doctest

     doctest.testmod()





