


'''simple docstring'''







import unittest
from unittest.util import safe_repr

from transformers import AutoTokenizer, RwkvConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
       import torch

       from transformers import (
           RWKV_PRETRAINED_MODEL_ARCHIVE_LIST,
           RwkvForCausalLM,
           RwkvModel,
       )
       from transformers.pytorch_utils import is_torch_greater_or_equal_than_2_0
else:
       lowerCAmelCase			:   List[Any] 		=  False
class 		UpperCamelCase__    :



     """simple docstring"""






     def __init__(       self					,		snake_case__					,		snake_case__=14					,		snake_case__=7					,		snake_case__=True					,		snake_case__=False					,		snake_case__=True					,		snake_case__=True					,		snake_case__=True					,		snake_case__=99					,		snake_case__=32					,		snake_case__=5					,		snake_case__=37					,		snake_case__="gelu"					,		snake_case__=0.1					,		snake_case__=0.1					,		snake_case__=512					,		snake_case__=16					,		snake_case__=2					,		snake_case__=3					,		snake_case__=4					,		snake_case__=None					,		):

            '''simple docstring'''

            _lowerCAmelCase			:							int      	=				parent
            _lowerCAmelCase			:							Union[str, Any]      	=				batch_size
            _lowerCAmelCase			:							List[Any]      	=				seq_length
            _lowerCAmelCase			:							Tuple      	=				is_training
            _lowerCAmelCase			:							Dict      	=				use_token_type_ids
            _lowerCAmelCase			:							Tuple      	=				use_input_mask
            _lowerCAmelCase			:							Dict      	=				use_labels
            _lowerCAmelCase			:							Any      	=				use_mc_token_ids
            _lowerCAmelCase			:							List[str]      	=				vocab_size
            _lowerCAmelCase			:							str      	=				hidden_size
            _lowerCAmelCase			:							str      	=				num_hidden_layers
            _lowerCAmelCase			:							List[Any]      	=				intermediate_size
            _lowerCAmelCase			:							int      	=				hidden_act
            _lowerCAmelCase			:							Union[str, Any]      	=				hidden_dropout_prob
            _lowerCAmelCase			:							List[str]      	=				attention_probs_dropout_prob
            _lowerCAmelCase			:							Any      	=				max_position_embeddings
            _lowerCAmelCase			:							List[str]      	=				type_vocab_size
            _lowerCAmelCase			:							int      	=				type_sequence_label_size
            _lowerCAmelCase			:							List[Any]      	=				num_labels
            _lowerCAmelCase			:							Any      	=				num_choices
            _lowerCAmelCase			:							Optional[Any]      	=				scope
            _lowerCAmelCase			:							Union[str, Any]      	=				vocab_size - 1
            _lowerCAmelCase			:							Optional[int]      	=				vocab_size - 1
            _lowerCAmelCase			:							Union[str, Any]      	=				vocab_size - 1







     def a		(       self      ):

            '''simple docstring'''

            return RwkvConfig.from_pretrained('sgugger/rwkv-4-pile-7b'      )







     def a		(       self					,		snake_case__=False					,		snake_case__=False					,		snake_case__=False      ):

            '''simple docstring'''

            _lowerCAmelCase			:							Any      	=				ids_tensor([self.batch_size, self.seq_length]					,		self.vocab_size      )

            _lowerCAmelCase			:							Dict      	=				None
            if self.use_input_mask:
                   _lowerCAmelCase			:							Optional[int]      	=				random_attention_mask([self.batch_size, self.seq_length]      )

            _lowerCAmelCase			:							Tuple      	=				None
            if self.use_token_type_ids:
                   _lowerCAmelCase			:							List[Any]      	=				ids_tensor([self.batch_size, self.seq_length]					,		self.type_vocab_size      )

            _lowerCAmelCase			:							Optional[int]      	=				None
            if self.use_mc_token_ids:
                   _lowerCAmelCase			:							Optional[Any]      	=				ids_tensor([self.batch_size, self.num_choices]					,		self.seq_length      )

            _lowerCAmelCase			:							List[Any]      	=				None
            _lowerCAmelCase			:							Any      	=				None
            _lowerCAmelCase			:							List[str]      	=				None
            if self.use_labels:
                   _lowerCAmelCase			:							Optional[int]      	=				ids_tensor([self.batch_size]					,		self.type_sequence_label_size      )
                   _lowerCAmelCase			:							Union[str, Any]      	=				ids_tensor([self.batch_size, self.seq_length]					,		self.num_labels      )
                   _lowerCAmelCase			:							str      	=				ids_tensor([self.batch_size]					,		self.num_choices      )

            _lowerCAmelCase			:							List[str]      	=				self.get_config(
                gradient_checkpointing=_SCREAMING_SNAKE_CASE					,		scale_attn_by_inverse_layer_idx=_SCREAMING_SNAKE_CASE					,		reorder_and_upcast_attn=_SCREAMING_SNAKE_CASE					,		)

            return (
                config,
                input_ids,
                input_mask,
                None,
                token_type_ids,
                mc_token_ids,
                sequence_labels,
                token_labels,
                choice_labels,
            )







     def a		(       self					,		snake_case__=False					,		snake_case__=False					,		snake_case__=False      ):

            '''simple docstring'''

            return RwkvConfig(
                vocab_size=self.vocab_size					,		hidden_size=self.hidden_size					,		num_hidden_layers=self.num_hidden_layers					,		intermediate_size=self.intermediate_size					,		activation_function=self.hidden_act					,		resid_pdrop=self.hidden_dropout_prob					,		attn_pdrop=self.attention_probs_dropout_prob					,		n_positions=self.max_position_embeddings					,		type_vocab_size=self.type_vocab_size					,		use_cache=_SCREAMING_SNAKE_CASE					,		bos_token_id=self.bos_token_id					,		eos_token_id=self.eos_token_id					,		pad_token_id=self.pad_token_id					,		gradient_checkpointing=_SCREAMING_SNAKE_CASE					,		scale_attn_by_inverse_layer_idx=_SCREAMING_SNAKE_CASE					,		reorder_and_upcast_attn=_SCREAMING_SNAKE_CASE					,		)







     def a		(       self      ):

            '''simple docstring'''

            _lowerCAmelCase			:							Optional[int]      	=				self.get_config()
            _lowerCAmelCase			:							int      	=				300
            return config







     def a		(       self      ):

            '''simple docstring'''

            (
                _lowerCAmelCase				
            )			:							Optional[Any]      	=				self.prepare_config_and_inputs()

            _lowerCAmelCase			:							str      	=				floats_tensor([self.batch_size, self.seq_length, self.hidden_size]      )
            _lowerCAmelCase			:							Tuple      	=				ids_tensor([self.batch_size, self.seq_length]					,		vocab_size=2      )

            return (
                config,
                input_ids,
                input_mask,
                head_mask,
                token_type_ids,
                sequence_labels,
                token_labels,
                choice_labels,
                encoder_hidden_states,
                encoder_attention_mask,
            )







     def a		(       self					,		snake_case__					,		snake_case__					,		snake_case__					,		snake_case__					,		snake_case__					,		*snake_case__      ):

            '''simple docstring'''

            _lowerCAmelCase			:							int      	=				True
            _lowerCAmelCase			:							List[Any]      	=				RwkvModel(config=_SCREAMING_SNAKE_CASE      )
            model.to(_SCREAMING_SNAKE_CASE      )
            model.eval()

            _lowerCAmelCase			:							Any      	=				model(_SCREAMING_SNAKE_CASE      )

            self.parent.assertEqual(result.last_hidden_state.shape					,		(self.batch_size, self.seq_length, self.hidden_size)      )
            self.parent.assertEqual(len(result.hidden_states      )					,		config.num_hidden_layers + 1      )







     def a		(       self					,		snake_case__					,		snake_case__					,		snake_case__					,		snake_case__					,		snake_case__					,		*snake_case__      ):

            '''simple docstring'''

            _lowerCAmelCase			:							List[str]      	=				RwkvForCausalLM(_SCREAMING_SNAKE_CASE      )
            model.to(_SCREAMING_SNAKE_CASE      )
            model.eval()

            _lowerCAmelCase			:							Optional[int]      	=				model(_SCREAMING_SNAKE_CASE					,		labels=_SCREAMING_SNAKE_CASE      )
            self.parent.assertEqual(result.loss.shape					,		()      )
            self.parent.assertEqual(result.logits.shape					,		(self.batch_size, self.seq_length, self.vocab_size)      )







     def a		(       self					,		snake_case__					,		snake_case__					,		snake_case__					,		snake_case__					,		snake_case__					,		*snake_case__      ):

            '''simple docstring'''

            _lowerCAmelCase			:							int      	=				RwkvModel(config=_SCREAMING_SNAKE_CASE      )
            model.to(_SCREAMING_SNAKE_CASE      )
            model.eval()

            _lowerCAmelCase			:							Optional[Any]      	=				model(_SCREAMING_SNAKE_CASE      )
            _lowerCAmelCase			:							Dict      	=				outputs.last_hidden_state

            _lowerCAmelCase			:							Union[str, Any]      	=				model(input_ids[:, :2]      )
            _lowerCAmelCase			:							Optional[int]      	=				outputs.last_hidden_state

            # Using the state computed on the first inputs, we will get the same output
            _lowerCAmelCase			:							Optional[int]      	=				model(input_ids[:, 2:]					,		state=outputs.state      )
            _lowerCAmelCase			:							int      	=				outputs.last_hidden_state

            self.parent.assertTrue(torch.allclose(torch.cat([output_one, output_two]					,		dim=1      )					,		_SCREAMING_SNAKE_CASE					,		atol=1E-5      )      )







     def a		(       self					,		snake_case__					,		snake_case__					,		snake_case__					,		snake_case__					,		snake_case__					,		*snake_case__					,		snake_case__=False      ):

            '''simple docstring'''

            _lowerCAmelCase			:							Optional[int]      	=				RwkvForCausalLM(_SCREAMING_SNAKE_CASE      )
            model.to(_SCREAMING_SNAKE_CASE      )
            if gradient_checkpointing:
                   model.gradient_checkpointing_enable()

            _lowerCAmelCase			:							List[Any]      	=				model(_SCREAMING_SNAKE_CASE					,		labels=_SCREAMING_SNAKE_CASE      )
            self.parent.assertEqual(result.loss.shape					,		()      )
            self.parent.assertEqual(result.logits.shape					,		(self.batch_size, self.seq_length, self.vocab_size)      )
            result.loss.backward()







     def a		(       self      ):

            '''simple docstring'''

            _lowerCAmelCase			:							Tuple      	=				self.prepare_config_and_inputs()

            (
                _lowerCAmelCase				
            )			:							List[Any]      	=				config_and_inputs

            _lowerCAmelCase			:							Union[str, Any]      	=				{"input_ids": input_ids}

            return config, inputs_dict
@unittest.skipIf(
    not is_torch_greater_or_equal_than_2_0			,    reason="See https://github.com/huggingface/transformers/pull/24204"		)
@require_torch
class 		UpperCamelCase__    (       SCREAMING_SNAKE_CASE__			,    SCREAMING_SNAKE_CASE__			,    SCREAMING_SNAKE_CASE__			,    unittest.TestCase		):



     """simple docstring"""






     __magic_name__				=			(RwkvModel, RwkvForCausalLM) if is_torch_available() else ()
     __magic_name__				=			(
         {'feature-extraction': RwkvModel, 'text-generation': RwkvForCausalLM} if is_torch_available() else {}
     )
     # all_generative_model_classes = (RwkvForCausalLM,) if is_torch_available() else ()
     __magic_name__				=			False
     __magic_name__				=			False
     __magic_name__				=			False
     __magic_name__				=			False
     __magic_name__				=			False  # Rwkv does not support head masking







     def a		(       self      ):

            '''simple docstring'''

            _lowerCAmelCase			:							Optional[Any]      	=				RwkvModelTester(self      )
            _lowerCAmelCase			:							Optional[Any]      	=				ConfigTester(
                self					,		config_class=_SCREAMING_SNAKE_CASE					,		n_embd=37					,		common_properties=['hidden_size', 'num_hidden_layers']      )







     def a		(       self					,		snake_case__					,		snake_case__					,		snake_case__=None      ):

            '''simple docstring'''

            if isinstance(_SCREAMING_SNAKE_CASE					,		torch.Tensor      ):
                   _lowerCAmelCase			:							List[str]      	=				member.max().item(), member.min().item()
            elif isinstance(_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      ) or isinstance(_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      ):
                   _lowerCAmelCase			:							Dict      	=				max(_SCREAMING_SNAKE_CASE      ), min(_SCREAMING_SNAKE_CASE      )

            if not isinstance(_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      ):
                   raise TypeError('container should be a list or tuple'      )
            elif len(_SCREAMING_SNAKE_CASE      ) != 2:
                   raise ValueError('container should have 2 elements'      )

            _lowerCAmelCase			:							Optional[Any]      	=				container

            _lowerCAmelCase			:							Optional[Any]      	=				(min_value >= expected_min) and (max_value <= expected_max)

            if not is_inside_interval:
                   _lowerCAmelCase			:							str      	=				"%s not found in %s" % (safe_repr(_SCREAMING_SNAKE_CASE      ), safe_repr(_SCREAMING_SNAKE_CASE      ))
                   self.fail(self._formatMessage(_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      )      )







     def a		(       self      ):

            '''simple docstring'''

            self.config_tester.run_common_tests()







     def a		(       self      ):

            '''simple docstring'''

            _lowerCAmelCase			:							List[Any]      	=				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_rwkv_model(*_SCREAMING_SNAKE_CASE      )







     def a		(       self      ):

            '''simple docstring'''

            _lowerCAmelCase			:							str      	=				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_causl_lm(*_SCREAMING_SNAKE_CASE      )







     def a		(       self      ):

            '''simple docstring'''

            _lowerCAmelCase			:							Optional[Any]      	=				self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_state_equivalency(*_SCREAMING_SNAKE_CASE      )







     def a		(       self      ):

            '''simple docstring'''

            _lowerCAmelCase			:							Union[str, Any]      	=				self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                   _lowerCAmelCase			:							Any      	=				model_class(config=_SCREAMING_SNAKE_CASE      )
                   for name, param in model.named_parameters():
                          if "time_decay" in name:
                                 if param.requires_grad:
                                        self.assertTrue(param.data.max().item() == 3.0      )
                                        self.assertTrue(param.data.min().item() == -5.0      )
                          elif "time_first" in name:
                                 if param.requires_grad:
                                        # check if it's a ones like
                                        self.assertTrue(torch.allclose(param.data					,		torch.ones_like(param.data      )					,		atol=1E-5					,		rtol=1E-5      )      )
                          elif any(x in name for x in ['time_mix_key', 'time_mix_receptance']      ):
                                 if param.requires_grad:
                                        self.assertInterval(
                                            param.data					,		[0.0, 1.0]					,		msg=F'Parameter {name} of model {model_class} seems not properly initialized'					,		)
                          elif "time_mix_value" in name:
                                 if param.requires_grad:
                                        self.assertInterval(
                                            param.data					,		[0.0, 1.3]					,		msg=F'Parameter {name} of model {model_class} seems not properly initialized'					,		)







     def a		(       self      ):

            '''simple docstring'''

            _lowerCAmelCase			:							Union[str, Any]      	=				self.model_tester.prepare_config_and_inputs_for_common()
            _lowerCAmelCase			:							List[Any]      	=				True

            _lowerCAmelCase			:							Tuple      	=				getattr(self.model_tester					,		'seq_length'					,		_SCREAMING_SNAKE_CASE      )

            for model_class in self.all_model_classes:
                   _lowerCAmelCase			:							str      	=				True
                   _lowerCAmelCase			:							List[Any]      	=				False
                   _lowerCAmelCase			:							Tuple      	=				True
                   _lowerCAmelCase			:							Optional[int]      	=				model_class(_SCREAMING_SNAKE_CASE      )
                   model.to(_SCREAMING_SNAKE_CASE      )
                   model.eval()

                   _lowerCAmelCase			:							Dict      	=				self._prepare_for_class(_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      )
                   _lowerCAmelCase			:							Optional[Any]      	=				inputs["input_ids"].shape[0]
                   with torch.no_grad():
                          _lowerCAmelCase			:							Union[str, Any]      	=				model(**_SCREAMING_SNAKE_CASE      )
                   _lowerCAmelCase			:							Tuple      	=				outputs.attentions
                   self.assertEqual(len(_SCREAMING_SNAKE_CASE      )					,		self.model_tester.num_hidden_layers      )

                   # check that output_attentions also work using config
                   del inputs_dict["output_attentions"]
                   _lowerCAmelCase			:							Dict      	=				True
                   _lowerCAmelCase			:							Tuple      	=				model_class(_SCREAMING_SNAKE_CASE      )
                   model.to(_SCREAMING_SNAKE_CASE      )
                   model.eval()

                   _lowerCAmelCase			:							Any      	=				self._prepare_for_class(_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      )
                   _lowerCAmelCase			:							int      	=				inputs["input_ids"].shape[0]
                   with torch.no_grad():
                          _lowerCAmelCase			:							Optional[int]      	=				model(**_SCREAMING_SNAKE_CASE      )
                   _lowerCAmelCase			:							Optional[Any]      	=				outputs.attentions
                   self.assertEqual(len(_SCREAMING_SNAKE_CASE      )					,		self.model_tester.num_hidden_layers      )

                   self.assertListEqual(
                       list(attentions[0].shape[-3:]      )					,		[batch_size, seq_len, config.hidden_size]					,		)
                   _lowerCAmelCase			:							List[Any]      	=				len(_SCREAMING_SNAKE_CASE      )

                   # Check attention is always last and order is fine
                   _lowerCAmelCase			:							Optional[Any]      	=				True
                   _lowerCAmelCase			:							Optional[int]      	=				True
                   _lowerCAmelCase			:							int      	=				model_class(_SCREAMING_SNAKE_CASE      )
                   model.to(_SCREAMING_SNAKE_CASE      )
                   model.eval()

                   _lowerCAmelCase			:							List[str]      	=				self._prepare_for_class(_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      )
                   _lowerCAmelCase			:							Any      	=				inputs["input_ids"].shape[0]
                   with torch.no_grad():
                          _lowerCAmelCase			:							Optional[int]      	=				model(**_SCREAMING_SNAKE_CASE      )

                   _lowerCAmelCase			:							Optional[int]      	=				1
                   self.assertEqual(out_len + added_hidden_states					,		len(_SCREAMING_SNAKE_CASE      )      )

                   _lowerCAmelCase			:							Union[str, Any]      	=				outputs.attentions

                   self.assertEqual(len(_SCREAMING_SNAKE_CASE      )					,		self.model_tester.num_hidden_layers      )
                   self.assertListEqual(
                       list(self_attentions[0].shape[-3:]      )					,		[batch_size, seq_len, config.hidden_size]					,		)







     @slow
     def a		(       self      ):

            '''simple docstring'''

            for model_name in RWKV_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   _lowerCAmelCase			:							Optional[int]      	=				RwkvModel.from_pretrained(_SCREAMING_SNAKE_CASE      )
                   self.assertIsNotNone(_SCREAMING_SNAKE_CASE      )
@unittest.skipIf(
    not is_torch_greater_or_equal_than_2_0			,    reason="See https://github.com/huggingface/transformers/pull/24204"		)
@slow
class 		UpperCamelCase__    (       unittest.TestCase		):



     """simple docstring"""






     def a		(       self      ):

            '''simple docstring'''

            _lowerCAmelCase			:							List[Any]      	=				"RWKV/rwkv-4-169m-pile"
            _lowerCAmelCase			:							Tuple      	=				AutoTokenizer.from_pretrained(self.model_id      )







     def a		(       self      ):

            '''simple docstring'''

            _lowerCAmelCase			:							Any      	=				"Hello my name is Jasmine and I am a newbie to the"
            _lowerCAmelCase			:							int      	=				RwkvForCausalLM.from_pretrained(self.model_id      ).to(_SCREAMING_SNAKE_CASE      )

            _lowerCAmelCase			:							Tuple      	=				self.tokenizer('Hello my name is'					,		return_tensors='pt'      ).input_ids.to(_SCREAMING_SNAKE_CASE      )
            _lowerCAmelCase			:							Optional[int]      	=				model.generate(_SCREAMING_SNAKE_CASE					,		max_new_tokens=10      )
            _lowerCAmelCase			:							Optional[int]      	=				self.tokenizer.decode(output[0].tolist()      )

            self.assertEqual(_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      )







     def a		(       self      ):

            '''simple docstring'''

            _lowerCAmelCase			:							Union[str, Any]      	=				"Hello my name is Jasmine and I am a newbie to the"

            _lowerCAmelCase			:							str      	=				self.tokenizer('Hello my name is'					,		return_tensors='pt'      ).input_ids.to(_SCREAMING_SNAKE_CASE      )
            _lowerCAmelCase			:							List[str]      	=				RwkvForCausalLM.from_pretrained(self.model_id					,		torch_dtype=torch.bfloataa      ).to(_SCREAMING_SNAKE_CASE      )

            _lowerCAmelCase			:							List[str]      	=				model.generate(_SCREAMING_SNAKE_CASE					,		max_new_tokens=10      )
            _lowerCAmelCase			:							List[str]      	=				self.tokenizer.decode(output[0].tolist()      )

            self.assertEqual(_SCREAMING_SNAKE_CASE					,		_SCREAMING_SNAKE_CASE      )









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