
import argparse
import os
import shutil
from pathlib import Path

import onnx
import torch
from packaging import version
from torch.onnx import export

from diffusers import OnnxRuntimeModel, OnnxStableDiffusionPipeline, StableDiffusionPipeline


__lowerCAmelCase					:			List[Any]			      =		version.parse(version.parse(torch.__version__).base_version) < version.parse("1.11")


def     UpperCAmelCase_  (						__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase=False	,					) ->						Any:
    output_path.parent.mkdir(parents=__lowerCAmelCase	,					exist_ok=__lowerCAmelCase	)
    # PyTorch deprecated the `enable_onnx_checker` and `use_external_data_format` arguments in v1.11,
    # so we check the torch version for backwards compatibility
    if is_torch_less_than_1_11:
        export(
            __lowerCAmelCase	,					__lowerCAmelCase	,					f=output_path.as_posix()	,					input_names=__lowerCAmelCase	,					output_names=__lowerCAmelCase	,					dynamic_axes=__lowerCAmelCase	,					do_constant_folding=__lowerCAmelCase	,					use_external_data_format=__lowerCAmelCase	,					enable_onnx_checker=__lowerCAmelCase	,					opset_version=__lowerCAmelCase	,					)
    else:
        export(
            __lowerCAmelCase	,					__lowerCAmelCase	,					f=output_path.as_posix()	,					input_names=__lowerCAmelCase	,					output_names=__lowerCAmelCase	,					dynamic_axes=__lowerCAmelCase	,					do_constant_folding=__lowerCAmelCase	,					opset_version=__lowerCAmelCase	,					)


@torch.no_grad()
def     UpperCAmelCase_  (						__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase = False	) ->						Dict:
    __lowercase		:			Any	    =       torch.floataa if fpaa else torch.floataa
    if fpaa and torch.cuda.is_available():
        __lowercase		:			List[str]	    =       '''cuda'''
    elif fpaa and not torch.cuda.is_available():
        raise ValueError('''`float16` model export is only supported on GPUs with CUDA'''	)
    else:
        __lowercase		:			Union[str, Any]	    =       '''cpu'''
    __lowercase		:			Optional[int]	    =       StableDiffusionPipeline.from_pretrained(__lowerCAmelCase	,					torch_dtype=__lowerCAmelCase	).to(__lowerCAmelCase	)
    __lowercase		:			Dict	    =       Path(__lowerCAmelCase	)

    # TEXT ENCODER
    __lowercase		:			str	    =       pipeline.text_encoder.config.max_position_embeddings
    __lowercase		:			Dict	    =       pipeline.text_encoder.config.hidden_size
    __lowercase		:			Dict	    =       pipeline.tokenizer(
        '''A sample prompt'''	,					padding='''max_length'''	,					max_length=pipeline.tokenizer.model_max_length	,					truncation=__lowerCAmelCase	,					return_tensors='''pt'''	,					)
    onnx_export(
        pipeline.text_encoder	,					model_args=(text_input.input_ids.to(device=__lowerCAmelCase	,					dtype=torch.intaa	))	,					output_path=output_path / '''text_encoder''' / '''model.onnx'''	,					ordered_input_names=['''input_ids''']	,					output_names=['''last_hidden_state''', '''pooler_output''']	,					dynamic_axes={
            '''input_ids''': {0: '''batch''', 1: '''sequence'''},
        }	,					opset=__lowerCAmelCase	,					)
    del pipeline.text_encoder

    # UNET
    __lowercase		:			List[str]	    =       pipeline.unet.config.in_channels
    __lowercase		:			Optional[Any]	    =       pipeline.unet.config.sample_size
    __lowercase		:			List[Any]	    =       output_path / '''unet''' / '''model.onnx'''
    onnx_export(
        pipeline.unet	,					model_args=(
            torch.randn(2	,					__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase	).to(device=__lowerCAmelCase	,					dtype=__lowerCAmelCase	),
            torch.randn(2	).to(device=__lowerCAmelCase	,					dtype=__lowerCAmelCase	),
            torch.randn(2	,					__lowerCAmelCase	,					__lowerCAmelCase	).to(device=__lowerCAmelCase	,					dtype=__lowerCAmelCase	),
            False,
        )	,					output_path=__lowerCAmelCase	,					ordered_input_names=['''sample''', '''timestep''', '''encoder_hidden_states''', '''return_dict''']	,					output_names=['''out_sample''']	,					dynamic_axes={
            '''sample''': {0: '''batch''', 1: '''channels''', 2: '''height''', 3: '''width'''},
            '''timestep''': {0: '''batch'''},
            '''encoder_hidden_states''': {0: '''batch''', 1: '''sequence'''},
        }	,					opset=__lowerCAmelCase	,					use_external_data_format=__lowerCAmelCase	,					)
    __lowercase		:			Union[str, Any]	    =       str(unet_path.absolute().as_posix()	)
    __lowercase		:			Optional[int]	    =       os.path.dirname(__lowerCAmelCase	)
    __lowercase		:			Optional[Any]	    =       onnx.load(__lowerCAmelCase	)
    # clean up existing tensor files
    shutil.rmtree(__lowerCAmelCase	)
    os.mkdir(__lowerCAmelCase	)
    # collate external tensor files into one
    onnx.save_model(
        __lowerCAmelCase	,					__lowerCAmelCase	,					save_as_external_data=__lowerCAmelCase	,					all_tensors_to_one_file=__lowerCAmelCase	,					location='''weights.pb'''	,					convert_attribute=__lowerCAmelCase	,					)
    del pipeline.unet

    # VAE ENCODER
    __lowercase		:			Optional[int]	    =       pipeline.vae
    __lowercase		:			Dict	    =       vae_encoder.config.in_channels
    __lowercase		:			List[Any]	    =       vae_encoder.config.sample_size
    # need to get the raw tensor output (sample) from the encoder
    __lowercase		:			List[str]	    =       lambda __lowerCAmelCase	,					__lowerCAmelCase	: vae_encoder.encode(__lowerCAmelCase	,					__lowerCAmelCase	)[0].sample()
    onnx_export(
        __lowerCAmelCase	,					model_args=(
            torch.randn(1	,					__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase	).to(device=__lowerCAmelCase	,					dtype=__lowerCAmelCase	),
            False,
        )	,					output_path=output_path / '''vae_encoder''' / '''model.onnx'''	,					ordered_input_names=['''sample''', '''return_dict''']	,					output_names=['''latent_sample''']	,					dynamic_axes={
            '''sample''': {0: '''batch''', 1: '''channels''', 2: '''height''', 3: '''width'''},
        }	,					opset=__lowerCAmelCase	,					)

    # VAE DECODER
    __lowercase		:			Union[str, Any]	    =       pipeline.vae
    __lowercase		:			Union[str, Any]	    =       vae_decoder.config.latent_channels
    __lowercase		:			Any	    =       vae_decoder.config.out_channels
    # forward only through the decoder part
    __lowercase		:			List[Any]	    =       vae_encoder.decode
    onnx_export(
        __lowerCAmelCase	,					model_args=(
            torch.randn(1	,					__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase	).to(device=__lowerCAmelCase	,					dtype=__lowerCAmelCase	),
            False,
        )	,					output_path=output_path / '''vae_decoder''' / '''model.onnx'''	,					ordered_input_names=['''latent_sample''', '''return_dict''']	,					output_names=['''sample''']	,					dynamic_axes={
            '''latent_sample''': {0: '''batch''', 1: '''channels''', 2: '''height''', 3: '''width'''},
        }	,					opset=__lowerCAmelCase	,					)
    del pipeline.vae

    # SAFETY CHECKER
    if pipeline.safety_checker is not None:
        __lowercase		:			Optional[int]	    =       pipeline.safety_checker
        __lowercase		:			Optional[Any]	    =       safety_checker.config.vision_config.num_channels
        __lowercase		:			Union[str, Any]	    =       safety_checker.config.vision_config.image_size
        __lowercase		:			str	    =       safety_checker.forward_onnx
        onnx_export(
            pipeline.safety_checker	,					model_args=(
                torch.randn(
                    1	,					__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase	,					).to(device=__lowerCAmelCase	,					dtype=__lowerCAmelCase	),
                torch.randn(1	,					__lowerCAmelCase	,					__lowerCAmelCase	,					__lowerCAmelCase	).to(device=__lowerCAmelCase	,					dtype=__lowerCAmelCase	),
            )	,					output_path=output_path / '''safety_checker''' / '''model.onnx'''	,					ordered_input_names=['''clip_input''', '''images''']	,					output_names=['''out_images''', '''has_nsfw_concepts''']	,					dynamic_axes={
                '''clip_input''': {0: '''batch''', 1: '''channels''', 2: '''height''', 3: '''width'''},
                '''images''': {0: '''batch''', 1: '''height''', 2: '''width''', 3: '''channels'''},
            }	,					opset=__lowerCAmelCase	,					)
        del pipeline.safety_checker
        __lowercase		:			Union[str, Any]	    =       OnnxRuntimeModel.from_pretrained(output_path / '''safety_checker'''	)
        __lowercase		:			int	    =       pipeline.feature_extractor
    else:
        __lowercase		:			Optional[int]	    =       None
        __lowercase		:			Tuple	    =       None

    __lowercase		:			Optional[int]	    =       OnnxStableDiffusionPipeline(
        vae_encoder=OnnxRuntimeModel.from_pretrained(output_path / '''vae_encoder'''	)	,					vae_decoder=OnnxRuntimeModel.from_pretrained(output_path / '''vae_decoder'''	)	,					text_encoder=OnnxRuntimeModel.from_pretrained(output_path / '''text_encoder'''	)	,					tokenizer=pipeline.tokenizer	,					unet=OnnxRuntimeModel.from_pretrained(output_path / '''unet'''	)	,					scheduler=pipeline.scheduler	,					safety_checker=__lowerCAmelCase	,					feature_extractor=__lowerCAmelCase	,					requires_safety_checker=safety_checker is not None	,					)

    onnx_pipeline.save_pretrained(__lowerCAmelCase	)
    print('''ONNX pipeline saved to'''	,					__lowerCAmelCase	)

    del pipeline
    del onnx_pipeline
    __lowercase		:			List[Any]	    =       OnnxStableDiffusionPipeline.from_pretrained(__lowerCAmelCase	,					provider='''CPUExecutionProvider'''	)
    print('''ONNX pipeline is loadable'''	)


if __name__ == "__main__":
 __lowerCAmelCase					:			int			      =		argparse.ArgumentParser()

 parser.add_argument(
     "--model_path",
     type=str,
     required=True,
     help="Path to the `diffusers` checkpoint to convert (either a local directory or on the Hub).",
 )

 parser.add_argument("--output_path", type=str, required=True, help="Path to the output model.")

 parser.add_argument(
     "--opset",
     default=14,
     type=int,
     help="The version of the ONNX operator set to use.",
 )
 parser.add_argument("--fp16", action="store_true", default=False, help="Export the models in `float16` mode")

 __lowerCAmelCase					:			Union[str, Any]			      =		parser.parse_args()

 convert_models(args.model_path, args.output_path, args.opset, args.fpaa)




import collections
import inspect
import unittest

from transformers import SwinvaConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

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


if is_torch_available():
 import torch
 from torch import nn

 from transformers import SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel
 from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
 from PIL import Image

 from transformers import AutoImageProcessor


class 						__lowerCAmelCase	:



      """simple docstring"""







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




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

          __lowercase		:			str	    =       None
          if self.use_labels:
              __lowercase		:			List[str]	    =       ids_tensor([self.batch_size]				,       self.type_sequence_label_size				)

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

          return config, pixel_values, labels




      def 		snake_case_			(      self		:       Tuple				):
          return SwinvaConfig(
              image_size=self.image_size				,       patch_size=self.patch_size				,       num_channels=self.num_channels				,       embed_dim=self.embed_dim				,       depths=self.depths				,       num_heads=self.num_heads				,       window_size=self.window_size				,       mlp_ratio=self.mlp_ratio				,       qkv_bias=self.qkv_bias				,       hidden_dropout_prob=self.hidden_dropout_prob				,       attention_probs_dropout_prob=self.attention_probs_dropout_prob				,       drop_path_rate=self.drop_path_rate				,       hidden_act=self.hidden_act				,       use_absolute_embeddings=self.use_absolute_embeddings				,       path_norm=self.patch_norm				,       layer_norm_eps=self.layer_norm_eps				,       initializer_range=self.initializer_range				,       encoder_stride=self.encoder_stride				,       )




      def 		snake_case_			(      self		:       int				,       _snake_case		:       str				,       _snake_case		:       Union[str, Any]				,       _snake_case		:       int				):
          __lowercase		:			int	    =       SwinvaModel(config=_snake_case				)
          model.to(_snake_case				)
          model.eval()
          __lowercase		:			Dict	    =       model(_snake_case				)

          __lowercase		:			Union[str, Any]	    =       ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths				) - 1))
          __lowercase		:			Union[str, 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 		snake_case_			(      self		:       str				,       _snake_case		:       List[Any]				,       _snake_case		:       str				,       _snake_case		:       str				):
          __lowercase		:			List[Any]	    =       SwinvaForMaskedImageModeling(config=_snake_case				)
          model.to(_snake_case				)
          model.eval()
          __lowercase		:			str	    =       model(_snake_case				)
          self.parent.assertEqual(
              result.logits.shape				,       (self.batch_size, self.num_channels, self.image_size, self.image_size)				)

          # test greyscale images
          __lowercase		:			Optional[Any]	    =       1
          __lowercase		:			int	    =       SwinvaForMaskedImageModeling(_snake_case				)
          model.to(_snake_case				)
          model.eval()

          __lowercase		:			List[str]	    =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
          __lowercase		:			Union[str, Any]	    =       model(_snake_case				)
          self.parent.assertEqual(result.logits.shape				,       (self.batch_size, 1, self.image_size, self.image_size)				)




      def 		snake_case_			(      self		:       Dict				,       _snake_case		:       Optional[Any]				,       _snake_case		:       str				,       _snake_case		:       Optional[Any]				):
          __lowercase		:			Any	    =       self.type_sequence_label_size
          __lowercase		:			Optional[Any]	    =       SwinvaForImageClassification(_snake_case				)
          model.to(_snake_case				)
          model.eval()
          __lowercase		:			List[str]	    =       model(_snake_case				,       labels=_snake_case				)
          self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.type_sequence_label_size)				)




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






@require_torch
class 						__lowerCAmelCase	(	lowerCAmelCase_ , lowerCAmelCase_ , unittest.TestCase      ):



      """simple docstring"""







      A__	:			Optional[Any]        =    (
          (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
      )
      A__	:			Dict        =    (
          {'''feature-extraction''': SwinvaModel, '''image-classification''': SwinvaForImageClassification}
          if is_torch_available()
          else {}
      )

      A__	:			Any        =    False
      A__	:			List[str]        =    False
      A__	:			int        =    False
      A__	:			Tuple        =    False




      def 		snake_case_			(      self		:       List[Any]				):
          __lowercase		:			Optional[int]	    =       SwinvaModelTester(self				)
          __lowercase		:			List[Any]	    =       ConfigTester(self				,       config_class=_snake_case				,       embed_dim=37				)




      def 		snake_case_			(      self		:       Optional[Any]				):
          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		:       List[str]				):
          __lowercase		:			Optional[int]	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_snake_case				)




      @unittest.skip(reason='''Got `CUDA error: misaligned address` with PyTorch 2.0.0.'''				)
      def 		snake_case_			(      self		:       Optional[int]				):
          pass




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




      def 		snake_case_			(      self		:       Union[str, Any]				):
          __lowercase    ,      __lowercase		:			Optional[int]	    =       self.model_tester.prepare_config_and_inputs_for_common()

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




      def 		snake_case_			(      self		:       List[Any]				):
          __lowercase    ,      __lowercase		:			Tuple	    =       self.model_tester.prepare_config_and_inputs_for_common()

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

              __lowercase		:			List[Any]	    =       ['''pixel_values''']
              self.assertListEqual(arg_names[:1]				,       _snake_case				)




      def 		snake_case_			(      self		:       List[Any]				):
          __lowercase    ,      __lowercase		:			Optional[int]	    =       self.model_tester.prepare_config_and_inputs_for_common()
          __lowercase		:			Dict	    =       True

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

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

              self.assertListEqual(
                  list(attentions[0].shape[-3:]				)				,       [self.model_tester.num_heads[0], window_size_squared, window_size_squared]				,       )
              __lowercase		:			List[Any]	    =       len(_snake_case				)

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

              if hasattr(self.model_tester				,       '''num_hidden_states_types'''				):
                  __lowercase		:			int	    =       self.model_tester.num_hidden_states_types
              else:
                  # also another +1 for reshaped_hidden_states
                  __lowercase		:			Optional[Any]	    =       2
              self.assertEqual(out_len + added_hidden_states				,       len(_snake_case				)				)

              __lowercase		:			Any	    =       outputs.attentions

              self.assertEqual(len(_snake_case				)				,       _snake_case				)

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




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

          with torch.no_grad():
              __lowercase		:			Optional[int]	    =       model(**self._prepare_for_class(_snake_case				,       _snake_case				)				)

          __lowercase		:			Union[str, Any]	    =       outputs.hidden_states

          __lowercase		:			Any	    =       getattr(
              self.model_tester				,       '''expected_num_hidden_layers'''				,       len(self.model_tester.depths				) + 1				)
          self.assertEqual(len(_snake_case				)				,       _snake_case				)

          # Swinv2 has a different seq_length
          __lowercase		:			Any	    =       (
              config.patch_size
              if isinstance(config.patch_size				,       collections.abc.Iterable				)
              else (config.patch_size, config.patch_size)
          )

          __lowercase		:			Tuple	    =       (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]				,       )

          __lowercase		:			Any	    =       outputs.reshaped_hidden_states
          self.assertEqual(len(_snake_case				)				,       _snake_case				)

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




      def 		snake_case_			(      self		:       int				):
          __lowercase    ,      __lowercase		:			List[Any]	    =       self.model_tester.prepare_config_and_inputs_for_common()

          __lowercase		:			List[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:
              __lowercase		:			List[str]	    =       True
              self.check_hidden_states_output(_snake_case				,       _snake_case				,       _snake_case				,       _snake_case				)

              # check that output_hidden_states also work using config
              del inputs_dict["output_hidden_states"]
              __lowercase		:			str	    =       True

              self.check_hidden_states_output(_snake_case				,       _snake_case				,       _snake_case				,       _snake_case				)




      def 		snake_case_			(      self		:       List[Any]				):
          __lowercase    ,      __lowercase		:			Optional[int]	    =       self.model_tester.prepare_config_and_inputs_for_common()
          __lowercase		:			Any	    =       3

          __lowercase		:			Optional[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)
          )
          __lowercase		:			Optional[Any]	    =       (
              config.patch_size
              if isinstance(config.patch_size				,       collections.abc.Iterable				)
              else (config.patch_size, config.patch_size)
          )

          __lowercase		:			Optional[Any]	    =       image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
          __lowercase		:			Optional[Any]	    =       image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

          for model_class in self.all_model_classes:
              __lowercase		:			List[Any]	    =       True
              self.check_hidden_states_output(_snake_case				,       _snake_case				,       _snake_case				,       (padded_height, padded_width)				)

              # check that output_hidden_states also work using config
              del inputs_dict["output_hidden_states"]
              __lowercase		:			Union[str, Any]	    =       True
              self.check_hidden_states_output(_snake_case				,       _snake_case				,       _snake_case				,       (padded_height, padded_width)				)




      def 		snake_case_			(      self		:       Optional[int]				):
          __lowercase		:			Any	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_masked_image_modeling(*_snake_case				)




      def 		snake_case_			(      self		:       Optional[int]				):
          __lowercase		:			Dict	    =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*_snake_case				)




      @slow
      def 		snake_case_			(      self		:       Dict				):
          for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              __lowercase		:			Optional[Any]	    =       SwinvaModel.from_pretrained(_snake_case				)
              self.assertIsNotNone(_snake_case				)




      def 		snake_case_			(      self		:       str				):
          __lowercase    ,      __lowercase		:			Optional[int]	    =       self.model_tester.prepare_config_and_inputs_for_common()

          __lowercase		:			Optional[int]	    =       _config_zero_init(_snake_case				)
          for model_class in self.all_model_classes:
              __lowercase		:			Tuple	    =       model_class(config=_snake_case				)
              for name, param in model.named_parameters():
                  if "embeddings" not in name and "logit_scale" not in name and param.requires_grad:
                      self.assertIn(
                          ((param.data.mean() * 1E9).round() / 1E9).item()				,       [0.0, 1.0]				,       msg=F'Parameter {name} of model {model_class} seems not properly initialized'				,       )






@require_vision
@require_torch
class 						__lowerCAmelCase	(	unittest.TestCase      ):



      """simple docstring"""







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




      @slow
      def 		snake_case_			(      self		:       Optional[Any]				):
          __lowercase		:			Optional[Any]	    =       SwinvaForImageClassification.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256'''				).to(
              _snake_case				)
          __lowercase		:			int	    =       self.default_image_processor

          __lowercase		:			Optional[Any]	    =       Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''				)
          __lowercase		:			Optional[int]	    =       image_processor(images=_snake_case				,       return_tensors='''pt'''				).to(_snake_case				)

          # forward pass
          with torch.no_grad():
              __lowercase		:			Optional[int]	    =       model(**_snake_case				)

          # verify the logits
          __lowercase		:			List[str]	    =       torch.Size((1, 1000)				)
          self.assertEqual(outputs.logits.shape				,       _snake_case				)
          __lowercase		:			Optional[Any]	    =       torch.tensor([-0.39_47, -0.43_06, 0.00_26]				).to(_snake_case				)
          self.assertTrue(torch.allclose(outputs.logits[0, :3]				,       _snake_case				,       atol=1E-4				)				)


