"""simple docstring"""





import collections
import inspect
import unittest

from transformers import DonutSwinConfig
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import is_torch_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 DonutSwinModel
       from transformers.models.donut.modeling_donut_swin import DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST


class 					UpperCAmelCase_     :


      def __init__(       self		,      a		,      a=1_3		,      a=3_2		,      a=2		,      a=3		,      a=1_6		,      a=[1, 2, 1]		,      a=[2, 2, 4]		,      a=2		,      a=2.0		,      a=True		,      a=0.0		,      a=0.0		,      a=0.1		,      a="gelu"		,      a=False		,      a=True		,      a=0.02		,      a=1e-5		,      a=True		,      a=None		,      a=True		,      a=1_0		,      a=8		,      )	-> Optional[Any]:
        lowercase__      :		Union[str, Any]          =     parent
        lowercase__      :		Union[str, Any]          =     batch_size
        lowercase__      :		int          =     image_size
        lowercase__      :		Tuple          =     patch_size
        lowercase__      :		Optional[int]          =     num_channels
        lowercase__      :		List[str]          =     embed_dim
        lowercase__      :		Dict          =     depths
        lowercase__      :		Union[str, Any]          =     num_heads
        lowercase__      :		Tuple          =     window_size
        lowercase__      :		Tuple          =     mlp_ratio
        lowercase__      :		Tuple          =     qkv_bias
        lowercase__      :		Dict          =     hidden_dropout_prob
        lowercase__      :		List[Any]          =     attention_probs_dropout_prob
        lowercase__      :		Optional[int]          =     drop_path_rate
        lowercase__      :		Union[str, Any]          =     hidden_act
        lowercase__      :		Optional[int]          =     use_absolute_embeddings
        lowercase__      :		List[str]          =     patch_norm
        lowercase__      :		Optional[Any]          =     layer_norm_eps
        lowercase__      :		str          =     initializer_range
        lowercase__      :		Optional[Any]          =     is_training
        lowercase__      :		Union[str, Any]          =     scope
        lowercase__      :		List[str]          =     use_labels
        lowercase__      :		int          =     type_sequence_label_size
        lowercase__      :		Dict          =     encoder_stride


      def 		_UpperCAmelCase	(       self							)	-> Any:
        lowercase__      :		Optional[Any]          =     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

        lowercase__      :		Tuple          =     None
        if self.use_labels:
          lowercase__      :		int          =     ids_tensor([self.batch_size]		,      self.type_sequence_label_size							)

        lowercase__      :		Optional[Any]          =     self.get_config()

        return config, pixel_values, labels


      def 		_UpperCAmelCase	(       self							)	-> Optional[int]:
        return DonutSwinConfig(
            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 		_UpperCAmelCase	(       self		,      a		,      a		,      a							)	-> str:
        lowercase__      :		Tuple          =     DonutSwinModel(config=a							)
        model.to(a							)
        model.eval()
        lowercase__      :		Dict          =     model(a							)

        lowercase__      :		Optional[int]          =     ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths							) - 1))
        lowercase__      :		int          =     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 		_UpperCAmelCase	(       self							)	-> Any:
        lowercase__      :		Dict          =     self.prepare_config_and_inputs()
        (
            lowercase__				
        )      :		Dict          =     config_and_inputs
        lowercase__      :		Union[str, Any]          =     {'pixel_values': pixel_values}
        return config, inputs_dict





@require_torch
class 					UpperCAmelCase_     (   _a			,							_a			,							unittest.TestCase):
      lowerCamelCase__		:   str			  =  (DonutSwinModel,) if is_torch_available() else ()
      lowerCamelCase__		:   int			  =  {"feature-extraction": DonutSwinModel} if is_torch_available() else {}
      lowerCamelCase__		:   Dict			  =  True

      lowerCamelCase__		:   Any			  =  False
      lowerCamelCase__		:   Dict			  =  False
      lowerCamelCase__		:   Tuple			  =  False


      def 		_UpperCAmelCase	(       self							)	-> int:
        lowercase__      :		Dict          =     DonutSwinModelTester(self							)
        lowercase__      :		Dict          =     ConfigTester(self		,      config_class=a		,      embed_dim=3_7							)


      def 		_UpperCAmelCase	(       self							)	-> Tuple:
        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 		_UpperCAmelCase	(       self							)	-> Any:
        return


      def 		_UpperCAmelCase	(       self							)	-> Optional[Any]:
        lowercase__      :		int          =     self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*a							)


      def 		_UpperCAmelCase	(       self							)	-> str:
        # DonutSwin does not use inputs_embeds
        pass


      def 		_UpperCAmelCase	(       self							)	-> List[str]:
        lowercase__      :		Any          =     self.model_tester.prepare_config_and_inputs_for_common()

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


      def 		_UpperCAmelCase	(       self							)	-> List[str]:
        lowercase__      :		List[str]          =     self.model_tester.prepare_config_and_inputs_for_common()

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

          lowercase__      :		Dict          =     ['pixel_values']
          self.assertListEqual(arg_names[:1]		,      a							)


      def 		_UpperCAmelCase	(       self							)	-> List[Any]:
        lowercase__      :		Union[str, Any]          =     self.model_tester.prepare_config_and_inputs_for_common()
        lowercase__      :		Any          =     True

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

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

          # Check attention is always last and order is fine
          lowercase__      :		Optional[int]          =     True
          lowercase__      :		Dict          =     True
          lowercase__      :		List[str]          =     model_class(a							)
          model.to(a							)
          model.eval()
          with torch.no_grad():
            lowercase__      :		Tuple          =     model(**self._prepare_for_class(a		,      a							)							)

          if hasattr(self.model_tester		,      'num_hidden_states_types'							):
            lowercase__      :		Dict          =     self.model_tester.num_hidden_states_types
          else:
            # also another +1 for reshaped_hidden_states
            lowercase__      :		Union[str, Any]          =     2
          self.assertEqual(out_len + added_hidden_states		,      len(a							)							)

          lowercase__      :		Tuple          =     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 		_UpperCAmelCase	(       self		,      a		,      a		,      a		,      a							)	-> int:
        lowercase__      :		Tuple          =     model_class(a							)
        model.to(a							)
        model.eval()

        with torch.no_grad():
          lowercase__      :		List[str]          =     model(**self._prepare_for_class(a		,      a							)							)

        lowercase__      :		Tuple          =     outputs.hidden_states

        lowercase__      :		Optional[Any]          =     getattr(
            self.model_tester		,      'expected_num_hidden_layers'		,      len(self.model_tester.depths							) + 1							)
        self.assertEqual(len(a							)		,      a							)

        # DonutSwin has a different seq_length
        lowercase__      :		int          =     (
            config.patch_size
            if isinstance(config.patch_size		,      collections.abc.Iterable							)
            else (config.patch_size, config.patch_size)
        )

        lowercase__      :		Union[str, Any]          =     (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

        self.assertListEqual(
            list(hidden_states[0].shape[-2:]							)		,      [num_patches, self.model_tester.embed_dim]		,      )

        lowercase__      :		Tuple          =     outputs.reshaped_hidden_states
        self.assertEqual(len(a							)		,      a							)

        lowercase__      :		Any          =     reshaped_hidden_states[0].shape
        lowercase__      :		int          =     (
            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 		_UpperCAmelCase	(       self							)	-> int:
        lowercase__      :		List[Any]          =     self.model_tester.prepare_config_and_inputs_for_common()

        lowercase__      :		Tuple          =     (
            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__      :		Optional[Any]          =     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"]
          lowercase__      :		Tuple          =     True

          self.check_hidden_states_output(a		,      a		,      a		,      a							)


      def 		_UpperCAmelCase	(       self							)	-> str:
        lowercase__      :		Tuple          =     self.model_tester.prepare_config_and_inputs_for_common()
        lowercase__      :		Optional[Any]          =     3

        lowercase__      :		Dict          =     (
            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__      :		Any          =     (
            config.patch_size
            if isinstance(config.patch_size		,      collections.abc.Iterable							)
            else (config.patch_size, config.patch_size)
        )

        lowercase__      :		Any          =     image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
        lowercase__      :		Tuple          =     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(a		,      a		,      a		,      (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(a		,      a		,      a		,      (padded_height, padded_width)							)


      @slow
      def 		_UpperCAmelCase	(       self							)	-> str:
        for model_name in DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowercase__      :		Optional[Any]          =     DonutSwinModel.from_pretrained(a							)
          self.assertIsNotNone(a							)


      def 		_UpperCAmelCase	(       self							)	-> List[str]:
        lowercase__      :		List[str]          =     self.model_tester.prepare_config_and_inputs_for_common()

        lowercase__      :		int          =     _config_zero_init(a							)
        for model_class in self.all_model_classes:
          lowercase__      :		Union[str, Any]          =     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"""		,      )


"""simple docstring"""





import unittest

import numpy as np
import torch
from torch import nn
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import KandinskyPriorPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


class 					UpperCAmelCase_     (   _a			,							unittest.TestCase):
      lowerCamelCase__		:   Tuple			  =  KandinskyPriorPipeline
      lowerCamelCase__		:   Tuple			  =  ["prompt"]
      lowerCamelCase__		:   List[str]			  =  ["prompt", "negative_prompt"]
      lowerCamelCase__		:   List[Any]			  =  [
          "num_images_per_prompt",
          "generator",
          "num_inference_steps",
          "latents",
          "negative_prompt",
          "guidance_scale",
          "output_type",
          "return_dict",
      ]
      lowerCamelCase__		:   Optional[Any]			  =  False


      @property
      def 		_UpperCAmelCase	(       self							)	-> Union[str, Any]:
             return 3_2


      @property
      def 		_UpperCAmelCase	(       self							)	-> str:
             return 3_2


      @property
      def 		_UpperCAmelCase	(       self							)	-> Optional[Any]:
             return self.time_input_dim


      @property
      def 		_UpperCAmelCase	(       self							)	-> Any:
             return self.time_input_dim * 4


      @property
      def 		_UpperCAmelCase	(       self							)	-> Optional[int]:
             return 1_0_0


      @property
      def 		_UpperCAmelCase	(       self							)	-> List[Any]:
             lowercase__      :		Tuple          =     CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'							)
             return tokenizer


      @property
      def 		_UpperCAmelCase	(       self							)	-> Any:
             torch.manual_seed(0							)
             lowercase__      :		Optional[int]          =     CLIPTextConfig(
                 bos_token_id=0		,      eos_token_id=2		,      hidden_size=self.text_embedder_hidden_size		,      projection_dim=self.text_embedder_hidden_size		,      intermediate_size=3_7		,      layer_norm_eps=1e-05		,      num_attention_heads=4		,      num_hidden_layers=5		,      pad_token_id=1		,      vocab_size=1_0_0_0		,      )
             return CLIPTextModelWithProjection(a							)


      @property
      def 		_UpperCAmelCase	(       self							)	-> Union[str, Any]:
             torch.manual_seed(0							)

             lowercase__      :		int          =     {
                 'num_attention_heads': 2,
                 'attention_head_dim': 1_2,
                 'embedding_dim': self.text_embedder_hidden_size,
                 'num_layers': 1,
             }

             lowercase__      :		Tuple          =     PriorTransformer(**a							)
             # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
             lowercase__      :		int          =     nn.Parameter(torch.ones(model.clip_std.shape							)							)
             return model


      @property
      def 		_UpperCAmelCase	(       self							)	-> Optional[int]:
             torch.manual_seed(0							)
             lowercase__      :		Optional[int]          =     CLIPVisionConfig(
                 hidden_size=self.text_embedder_hidden_size		,      image_size=2_2_4		,      projection_dim=self.text_embedder_hidden_size		,      intermediate_size=3_7		,      num_attention_heads=4		,      num_channels=3		,      num_hidden_layers=5		,      patch_size=1_4		,      )

             lowercase__      :		Optional[Any]          =     CLIPVisionModelWithProjection(a							)
             return model


      @property
      def 		_UpperCAmelCase	(       self							)	-> Tuple:
             lowercase__      :		Dict          =     CLIPImageProcessor(
                 crop_size=2_2_4		,      do_center_crop=a		,      do_normalize=a		,      do_resize=a		,      image_mean=[0.48_145_466, 0.4_578_275, 0.40_821_073]		,      image_std=[0.26_862_954, 0.26_130_258, 0.27_577_711]		,      resample=3		,      size=2_2_4		,      )

             return image_processor


      def 		_UpperCAmelCase	(       self							)	-> Dict:
             lowercase__      :		List[str]          =     self.dummy_prior
             lowercase__      :		Tuple          =     self.dummy_image_encoder
             lowercase__      :		Optional[Any]          =     self.dummy_text_encoder
             lowercase__      :		List[str]          =     self.dummy_tokenizer
             lowercase__      :		Optional[int]          =     self.dummy_image_processor

             lowercase__      :		List[Any]          =     UnCLIPScheduler(
                 variance_type='fixed_small_log'		,      prediction_type='sample'		,      num_train_timesteps=1_0_0_0		,      clip_sample=a		,      clip_sample_range=10.0		,      )

             lowercase__      :		List[str]          =     {
                 'prior': prior,
                 'image_encoder': image_encoder,
                 'text_encoder': text_encoder,
                 'tokenizer': tokenizer,
                 'scheduler': scheduler,
                 'image_processor': image_processor,
             }

             return components


      def 		_UpperCAmelCase	(       self		,      a		,      a=0							)	-> Union[str, Any]:
             if str(a							).startswith('mps'							):
                    lowercase__      :		Optional[Any]          =     torch.manual_seed(a							)
             else:
                    lowercase__      :		Any          =     torch.Generator(device=a							).manual_seed(a							)
             lowercase__      :		str          =     {
                 'prompt': 'horse',
                 'generator': generator,
                 'guidance_scale': 4.0,
                 'num_inference_steps': 2,
                 'output_type': 'np',
             }
             return inputs


      def 		_UpperCAmelCase	(       self							)	-> Dict:
             lowercase__      :		Optional[int]          =     'cpu'

             lowercase__      :		Optional[Any]          =     self.get_dummy_components()

             lowercase__      :		Optional[Any]          =     self.pipeline_class(**a							)
             lowercase__      :		Optional[int]          =     pipe.to(a							)

             pipe.set_progress_bar_config(disable=a							)

             lowercase__      :		Optional[int]          =     pipe(**self.get_dummy_inputs(a							)							)
             lowercase__      :		Optional[Any]          =     output.image_embeds

             lowercase__      :		Dict          =     pipe(
                 **self.get_dummy_inputs(a							)		,      return_dict=a		,      )[0]

             lowercase__      :		List[str]          =     image[0, -1_0:]
             lowercase__      :		Optional[Any]          =     image_from_tuple[0, -1_0:]

             assert image.shape == (1, 3_2)

             lowercase__      :		Union[str, Any]          =     np.array(
                 [-0.0_532, 1.7_120, 0.3_656, -1.0_852, -0.8_946, -1.1_756, 0.4_348, 0.2_482, 0.5_146, -0.1_156]							)

             assert np.abs(image_slice.flatten() - expected_slice							).max() < 1e-2
             assert np.abs(image_from_tuple_slice.flatten() - expected_slice							).max() < 1e-2


      @skip_mps
      def 		_UpperCAmelCase	(       self							)	-> List[Any]:
             lowercase__      :		Tuple          =     torch_device == 'cpu'
             lowercase__      :		Union[str, Any]          =     True
             lowercase__      :		int          =     False

             self._test_inference_batch_single_identical(
                 test_max_difference=a		,      relax_max_difference=a		,      test_mean_pixel_difference=a		,      )


      @skip_mps
      def 		_UpperCAmelCase	(       self							)	-> int:
             lowercase__      :		Tuple          =     torch_device == 'cpu'
             lowercase__      :		Any          =     False

             self._test_attention_slicing_forward_pass(
                 test_max_difference=a		,      test_mean_pixel_difference=a		,      )

