




import collections
import inspect
import unittest

from transformers import SwinConfig
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_backbone_common import BackboneTesterMixin
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 SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel
 from transformers.models.swin.modeling_swin import SWIN_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import AutoImageProcessor







class 		__UpperCAmelCase:





     """simple docstring"""



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






         """simple docstring"""
         a__  			=							parent
         a__  			=							batch_size
         a__  			=							image_size
         a__  			=							patch_size
         a__  			=							num_channels
         a__  			=							embed_dim
         a__  			=							depths
         a__  			=							num_heads
         a__  			=							window_size
         a__  			=							mlp_ratio
         a__  			=							qkv_bias
         a__  			=							hidden_dropout_prob
         a__  			=							attention_probs_dropout_prob
         a__  			=							drop_path_rate
         a__  			=							hidden_act
         a__  			=							use_absolute_embeddings
         a__  			=							patch_norm
         a__  			=							layer_norm_eps
         a__  			=							initializer_range
         a__  			=							is_training
         a__  			=							scope
         a__  			=							use_labels
         a__  			=							type_sequence_label_size
         a__  			=							encoder_stride
         a__  			=							out_features
         a__  			=							out_indices

     def 					__SCREAMING_SNAKE_CASE		(   self			:							Dict     )      ->      List[Any]:






         """simple docstring"""
         a__  			=							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

         a__  			=							None
         if self.use_labels:
             a__  			=							ids_tensor([self.batch_size]    ,					self.type_sequence_label_size     )

         a__  			=							self.get_config()

         return config, pixel_values, labels

     def 					__SCREAMING_SNAKE_CASE		(   self			:							Dict     )      ->      str:






         """simple docstring"""
         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    ,					out_features=self.out_features    ,					out_indices=self.out_indices    ,					)

     def 					__SCREAMING_SNAKE_CASE		(   self			:							str    ,					A			:							Dict    ,					A			:							Optional[int]    ,					A			:							List[str]     )      ->      Dict:






         """simple docstring"""
         a__  			=							SwinModel(config=A     )
         model.to(A     )
         model.eval()
         a__  			=							model(A     )

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






         """simple docstring"""
         a__  			=							SwinBackbone(config=A     )
         model.to(A     )
         model.eval()
         a__  			=							model(A     )

         # verify hidden states
         self.parent.assertEqual(len(result.feature_maps     )    ,					len(config.out_features     )     )
         self.parent.assertListEqual(list(result.feature_maps[0].shape     )    ,					[self.batch_size, model.channels[0], 16, 16]     )

         # verify channels
         self.parent.assertEqual(len(model.channels     )    ,					len(config.out_features     )     )

         # verify backbone works with out_features=None
         a__  			=							None
         a__  			=							SwinBackbone(config=A     )
         model.to(A     )
         model.eval()
         a__  			=							model(A     )

         # verify feature maps
         self.parent.assertEqual(len(result.feature_maps     )    ,					1     )
         self.parent.assertListEqual(list(result.feature_maps[0].shape     )    ,					[self.batch_size, model.channels[-1], 4, 4]     )

         # verify channels
         self.parent.assertEqual(len(model.channels     )    ,					1     )

     def 					__SCREAMING_SNAKE_CASE		(   self			:							Optional[int]    ,					A			:							List[str]    ,					A			:							Optional[Any]    ,					A			:							Optional[Any]     )      ->      str:






         """simple docstring"""
         a__  			=							SwinForMaskedImageModeling(config=A     )
         model.to(A     )
         model.eval()
         a__  			=							model(A     )
         self.parent.assertEqual(
             result.logits.shape    ,					(self.batch_size, self.num_channels, self.image_size, self.image_size)     )

         # test greyscale images
         a__  			=							1
         a__  			=							SwinForMaskedImageModeling(A     )
         model.to(A     )
         model.eval()

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

     def 					__SCREAMING_SNAKE_CASE		(   self			:							Optional[Any]    ,					A			:							Any    ,					A			:							Dict    ,					A			:							Any     )      ->      Tuple:






         """simple docstring"""
         a__  			=							self.type_sequence_label_size
         a__  			=							SwinForImageClassification(A     )
         model.to(A     )
         model.eval()
         a__  			=							model(A    ,					labels=A     )
         self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.type_sequence_label_size)     )

         # test greyscale images
         a__  			=							1
         a__  			=							SwinForImageClassification(A     )
         model.to(A     )
         model.eval()

         a__  			=							floats_tensor([self.batch_size, 1, self.image_size, self.image_size]     )
         a__  			=							model(A     )
         self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.type_sequence_label_size)     )






     def 					__SCREAMING_SNAKE_CASE		(   self			:							List[str]     )      ->      Any:






         """simple docstring"""
         a__  			=							self.prepare_config_and_inputs()
         (
             (
             a__
         )			,		(
             a__
         )			,		(
             a__
         )			,		
         )  			=							config_and_inputs
         a__  			=							{"""pixel_values""": pixel_values}
         return config, inputs_dict







@require_torch
class 		__UpperCAmelCase(		lowercase__							,						lowercase__							,						unittest.TestCase			):





     """simple docstring"""



     __UpperCAmelCase						:			List[str]							=     (
         (
             SwinModel,
             SwinBackbone,
             SwinForImageClassification,
             SwinForMaskedImageModeling,
         )
         if is_torch_available()
         else ()
     )
     __UpperCAmelCase						:			Tuple							=     (
         {"""feature-extraction""": SwinModel, """image-classification""": SwinForImageClassification}
         if is_torch_available()
         else {}
     )
     __UpperCAmelCase						:			Any							=     True

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

     def 					__SCREAMING_SNAKE_CASE		(   self			:							Tuple     )      ->      Tuple:






         """simple docstring"""
         a__  			=							SwinModelTester(self     )
         a__  			=							ConfigTester(self    ,					config_class=A    ,					embed_dim=37     )

     def 					__SCREAMING_SNAKE_CASE		(   self			:							int     )      ->      Optional[Any]:






         """simple docstring"""
         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 					__SCREAMING_SNAKE_CASE		(   self			:							List[Any]     )      ->      Optional[Any]:






         """simple docstring"""
         return

     def 					__SCREAMING_SNAKE_CASE		(   self			:							str     )      ->      Tuple:






         """simple docstring"""
         a__  			=							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 					__SCREAMING_SNAKE_CASE		(   self			:							Dict     )      ->      Optional[int]:






         """simple docstring"""
         pass

     def 					__SCREAMING_SNAKE_CASE		(   self			:							Tuple     )      ->      Optional[Any]:






         """simple docstring"""
         super().test_training_gradient_checkpointing()

     def 					__SCREAMING_SNAKE_CASE		(   self			:							Dict     )      ->      Any:






         """simple docstring"""
         a__  			=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_backbone(*A     )

     def 					__SCREAMING_SNAKE_CASE		(   self			:							Tuple     )      ->      Union[str, Any]:






         """simple docstring"""
         a__  			=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_image_modeling(*A     )

     def 					__SCREAMING_SNAKE_CASE		(   self			:							Dict     )      ->      List[Any]:






         """simple docstring"""
         a__  			=							self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*A     )

     @unittest.skip(reason="""Swin does not use inputs_embeds"""     )
     def 					__SCREAMING_SNAKE_CASE		(   self			:							Optional[int]     )      ->      List[str]:






         """simple docstring"""
         pass

     @unittest.skip(reason="""Swin Transformer does not use feedforward chunking"""     )
     def 					__SCREAMING_SNAKE_CASE		(   self			:							List[Any]     )      ->      Any:






         """simple docstring"""
         pass

     def 					__SCREAMING_SNAKE_CASE		(   self			:							List[Any]     )      ->      str:






         """simple docstring"""
         a__			,		a__  			=							self.model_tester.prepare_config_and_inputs_for_common()

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

     def 					__SCREAMING_SNAKE_CASE		(   self			:							Union[str, Any]     )      ->      Union[str, Any]:






         """simple docstring"""
         a__			,		a__  			=							self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             a__  			=							model_class(A     )
             a__  			=							inspect.signature(model.forward     )
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             a__  			=							[*signature.parameters.keys()]

             a__  			=							["""pixel_values"""]
             self.assertListEqual(arg_names[:1]    ,					A     )

     def 					__SCREAMING_SNAKE_CASE		(   self			:							Tuple     )      ->      Optional[Any]:






         """simple docstring"""
         a__			,		a__  			=							self.model_tester.prepare_config_and_inputs_for_common()
         a__  			=							True

         for model_class in self.all_model_classes:
             a__  			=							True
             a__  			=							False
             a__  			=							True
             a__  			=							model_class(A     )
             model.to(A     )
             model.eval()
             with torch.no_grad():
                 a__  			=							model(**self._prepare_for_class(A    ,					A     )     )
             a__  			=							outputs.attentions
             a__  			=							len(self.model_tester.depths     )
             self.assertEqual(len(A     )    ,					A     )

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

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

             # also another +1 for reshaped_hidden_states
             a__  			=							1 if model_class.__name__ == """SwinBackbone""" else 2
             self.assertEqual(out_len + added_hidden_states    ,					len(A     )     )

             a__  			=							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 					__SCREAMING_SNAKE_CASE		(   self			:							Any    ,					A			:							List[str]    ,					A			:							Optional[int]    ,					A			:							int    ,					A			:							List[Any]     )      ->      int:






         """simple docstring"""
         a__  			=							model_class(A     )
         model.to(A     )
         model.eval()

         with torch.no_grad():
             a__  			=							model(**self._prepare_for_class(A    ,					A     )     )

         a__  			=							outputs.hidden_states

         a__  			=							getattr(
             self.model_tester    ,					"""expected_num_hidden_layers"""    ,					len(self.model_tester.depths     ) + 1     )
         self.assertEqual(len(A     )    ,					A     )

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

         a__  			=							(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]    ,					)

         if not model_class.__name__ == "SwinBackbone":
             a__  			=							outputs.reshaped_hidden_states
             self.assertEqual(len(A     )    ,					A     )

             a__			,		a__			,		a__			,		a__  			=							reshaped_hidden_states[0].shape
             a__  			=							(
                 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 					__SCREAMING_SNAKE_CASE		(   self			:							Optional[Any]     )      ->      Dict:






         """simple docstring"""
         a__			,		a__  			=							self.model_tester.prepare_config_and_inputs_for_common()

         a__  			=							(
             self.model_tester.image_size
             if isinstance(self.model_tester.image_size    ,					collections.abc.Iterable     )
             else (self.model_tester.image_size, self.model_tester.image_size)
         )

         for model_class in self.all_model_classes:
             a__  			=							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"]
             a__  			=							True

             self.check_hidden_states_output(A    ,					A    ,					A    ,					A     )

     def 					__SCREAMING_SNAKE_CASE		(   self			:							Optional[Any]     )      ->      Optional[Any]:






         """simple docstring"""
         a__			,		a__  			=							self.model_tester.prepare_config_and_inputs_for_common()
         a__  			=							3

         a__  			=							(
             self.model_tester.image_size
             if isinstance(self.model_tester.image_size    ,					collections.abc.Iterable     )
             else (self.model_tester.image_size, self.model_tester.image_size)
         )
         a__  			=							(
             config.patch_size
             if isinstance(config.patch_size    ,					collections.abc.Iterable     )
             else (config.patch_size, config.patch_size)
         )

         a__  			=							image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
         a__  			=							image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

         for model_class in self.all_model_classes:
             a__  			=							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"]
             a__  			=							True
             self.check_hidden_states_output(A    ,					A    ,					A    ,					(padded_height, padded_width)     )

     @slow
     def 					__SCREAMING_SNAKE_CASE		(   self			:							List[Any]     )      ->      Tuple:






         """simple docstring"""
         for model_name in SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             a__  			=							SwinModel.from_pretrained(A     )
             self.assertIsNotNone(A     )






     def 					__SCREAMING_SNAKE_CASE		(   self			:							List[str]     )      ->      Optional[int]:






         """simple docstring"""
         a__			,		a__  			=							self.model_tester.prepare_config_and_inputs_for_common()

         a__  			=							_config_zero_init(A     )
         for model_class in self.all_model_classes:
             a__  			=							model_class(config=A     )
             for name, param in model.named_parameters():
                 if "embeddings" 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 					__SCREAMING_SNAKE_CASE		(   self			:							List[str]     )      ->      str:






         """simple docstring"""
         return (
             AutoImageProcessor.from_pretrained("""microsoft/swin-tiny-patch4-window7-224"""     )
             if is_vision_available()
             else None
         )






     @slow
     def 					__SCREAMING_SNAKE_CASE		(   self			:							List[str]     )      ->      Any:






         """simple docstring"""
         a__  			=							SwinForImageClassification.from_pretrained("""microsoft/swin-tiny-patch4-window7-224"""     ).to(A     )
         a__  			=							self.default_image_processor

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

         # forward pass
         with torch.no_grad():
             a__  			=							model(**A     )

         # verify the logits
         a__  			=							torch.Size((1, 10_00)     )
         self.assertEqual(outputs.logits.shape    ,					A     )
         a__  			=							torch.tensor([-0.0_9_4_8, -0.6_4_5_4, -0.0_9_2_1]     ).to(A     )
         self.assertTrue(torch.allclose(outputs.logits[0, :3]    ,					A    ,					atol=1E-4     )     )







@require_torch
class 		__UpperCAmelCase(		unittest.TestCase							,						lowercase__			):





     """simple docstring"""



     __UpperCAmelCase						:			List[str]							=     (SwinBackbone,) if is_torch_available() else ()
     __UpperCAmelCase						:			List[Any]							=     SwinConfig

     def 					__SCREAMING_SNAKE_CASE		(   self			:							int     )      ->      int:






         """simple docstring"""
         a__  			=							SwinModelTester(self     )












# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

from ..utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


SCREAMING_SNAKE_CASE_    :       int       =							{
    '''agents''': ['''Agent''', '''AzureOpenAiAgent''', '''HfAgent''', '''LocalAgent''', '''OpenAiAgent'''],
    '''base''': ['''PipelineTool''', '''RemoteTool''', '''Tool''', '''launch_gradio_demo''', '''load_tool'''],
}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 SCREAMING_SNAKE_CASE_    :       int       =							['''DocumentQuestionAnsweringTool''']
 SCREAMING_SNAKE_CASE_    :       List[Any]       =							['''ImageCaptioningTool''']
 SCREAMING_SNAKE_CASE_    :       Union[str, Any]       =							['''ImageQuestionAnsweringTool''']
 SCREAMING_SNAKE_CASE_    :       str       =							['''ImageSegmentationTool''']
 SCREAMING_SNAKE_CASE_    :       Any       =							['''SpeechToTextTool''']
 SCREAMING_SNAKE_CASE_    :       Union[str, Any]       =							['''TextClassificationTool''']
 SCREAMING_SNAKE_CASE_    :       List[Any]       =							['''TextQuestionAnsweringTool''']
 SCREAMING_SNAKE_CASE_    :       Dict       =							['''TextSummarizationTool''']
 SCREAMING_SNAKE_CASE_    :       Optional[int]       =							['''TextToSpeechTool''']
 SCREAMING_SNAKE_CASE_    :       Dict       =							['''TranslationTool''']

if TYPE_CHECKING:
 from .agents import Agent, AzureOpenAiAgent, HfAgent, LocalAgent, OpenAiAgent
 from .base import PipelineTool, RemoteTool, Tool, launch_gradio_demo, load_tool

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .document_question_answering import DocumentQuestionAnsweringTool
  from .image_captioning import ImageCaptioningTool
  from .image_question_answering import ImageQuestionAnsweringTool
  from .image_segmentation import ImageSegmentationTool
  from .speech_to_text import SpeechToTextTool
  from .text_classification import TextClassificationTool
  from .text_question_answering import TextQuestionAnsweringTool
  from .text_summarization import TextSummarizationTool
  from .text_to_speech import TextToSpeechTool
  from .translation import TranslationTool
else:
 import sys

 SCREAMING_SNAKE_CASE_    :       Optional[Any]       =							_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)






