




'''simple docstring'''



from logging import getLogger
from typing import Any, Callable, List, Optional, Union

import numpy as np
import PIL
import torch

from ...schedulers import DDPMScheduler
from ..onnx_utils import ORT_TO_NP_TYPE, OnnxRuntimeModel
from ..pipeline_utils import ImagePipelineOutput
from . import StableDiffusionUpscalePipeline


lowerCamelCase_		       =						getLogger(__name__)


lowerCamelCase_		       =						4
lowerCamelCase_		       =						7

lowerCamelCase_		       =						{
    """float16""": torch.floataa,
    """float32""": torch.floataa,
}
def  SCREAMING_SNAKE_CASE_ (							__A				: Optional[Any]  ) -> Tuple:






   if isinstance(SCREAMING_SNAKE_CASE_       ,     torch.Tensor  ):
      return image
   elif isinstance(SCREAMING_SNAKE_CASE_       ,     PIL.Image.Image  ):
      _SCREAMING_SNAKE_CASE       =      [image]

   if isinstance(image[0]       ,     PIL.Image.Image  ):
      _SCREAMING_SNAKE_CASE,       _SCREAMING_SNAKE_CASE       =      image[0].size
      _SCREAMING_SNAKE_CASE,       _SCREAMING_SNAKE_CASE       =      (x - x % 64 for x in (w, h))  # resize to integer multiple of 32

      _SCREAMING_SNAKE_CASE       =      [np.array(i.resize((w, h)  )  )[None, :] for i in image]
      _SCREAMING_SNAKE_CASE       =      np.concatenate(SCREAMING_SNAKE_CASE_       ,     axis=0  )
      _SCREAMING_SNAKE_CASE       =      np.array(SCREAMING_SNAKE_CASE_  ).astype(np.floataa  ) / 2_5_5.0
      _SCREAMING_SNAKE_CASE       =      image.transpose(0       ,     3       ,     1       ,     2  )
      _SCREAMING_SNAKE_CASE       =      2.0 * image - 1.0
      _SCREAMING_SNAKE_CASE       =      torch.from_numpy(SCREAMING_SNAKE_CASE_  )
   elif isinstance(image[0]       ,     torch.Tensor  ):
      _SCREAMING_SNAKE_CASE       =      torch.cat(SCREAMING_SNAKE_CASE_       ,     dim=0  )

   return image







class 		lowercase_    ( A   ):


   """simple docstring"""

   def __init__(    self					:	Dict  ,      __lowerCamelCase					:	Optional[Any]  ,      __lowerCamelCase					:	str  ,      __lowerCamelCase					:	Optional[int]  ,      __lowerCamelCase					:	List[Any]  ,      __lowerCamelCase					:	List[Any]  ,      __lowerCamelCase					:	Dict  ,      __lowerCamelCase					:	Any = 3_5_0  ,      ):

      """simple docstring"""
      super().__init__(
          vae=_lowercase  ,      text_encoder=_lowercase  ,      tokenizer=_lowercase  ,      unet=_lowercase  ,      low_res_scheduler=_lowercase  ,      scheduler=_lowercase  ,      safety_checker=_lowercase  ,      feature_extractor=_lowercase  ,      watermarker=_lowercase  ,      max_noise_level=_lowercase  ,      )



   def __call__(    self					:	List[str]  ,      __lowerCamelCase					:	Any  ,      __lowerCamelCase					:	List[Any]  ,      __lowerCamelCase					:	Tuple = 7_5  ,      __lowerCamelCase					:	Union[str, Any] = 9.0  ,      __lowerCamelCase					:	int = 2_0  ,      __lowerCamelCase					:	List[str] = None  ,      __lowerCamelCase					:	Union[str, Any] = 1  ,      __lowerCamelCase					:	List[Any] = 0.0  ,      __lowerCamelCase					:	Union[str, Any] = None  ,      __lowerCamelCase					:	List[str] = None  ,      __lowerCamelCase					:	List[str] = None  ,      __lowerCamelCase					:	Any = None  ,      __lowerCamelCase					:	int = "pil"  ,      __lowerCamelCase					:	Optional[int] = True  ,      __lowerCamelCase					:	Union[str, Any] = None  ,      __lowerCamelCase					:	List[Any] = 1  ,      ):

      """simple docstring"""
      self.check_inputs(_lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase    )

      # 2. Define call parameters
      if prompt is not None and isinstance(_lowercase  ,      _lowercase    ):
         _SCREAMING_SNAKE_CASE       =      1
      elif prompt is not None and isinstance(_lowercase  ,      _lowercase    ):
         _SCREAMING_SNAKE_CASE       =      len(_lowercase    )
      else:
         _SCREAMING_SNAKE_CASE       =      prompt_embeds.shape[0]

      _SCREAMING_SNAKE_CASE       =      self._execution_device
      # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
      # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
      # corresponds to doing no classifier free guidance.
      _SCREAMING_SNAKE_CASE       =      guidance_scale > 1.0

      # 3. Encode input prompt
      _SCREAMING_SNAKE_CASE       =      self._encode_prompt(
          _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      prompt_embeds=_lowercase  ,      negative_prompt_embeds=_lowercase  ,      )

      _SCREAMING_SNAKE_CASE       =      ORT_TO_PT_TYPE[str(text_embeddings.dtype    )]

      # 4. Preprocess image
      _SCREAMING_SNAKE_CASE       =      preprocess(_lowercase    )
      _SCREAMING_SNAKE_CASE       =      image.cpu()

      # 5. set timesteps
      self.scheduler.set_timesteps(_lowercase  ,      device=_lowercase    )
      _SCREAMING_SNAKE_CASE       =      self.scheduler.timesteps

      # 5. Add noise to image
      _SCREAMING_SNAKE_CASE       =      torch.tensor([noise_level]  ,      dtype=torch.long  ,      device=_lowercase    )
      _SCREAMING_SNAKE_CASE       =      torch.randn(image.shape  ,      generator=_lowercase  ,      device=_lowercase  ,      dtype=_lowercase    )
      _SCREAMING_SNAKE_CASE       =      self.low_res_scheduler.add_noise(_lowercase  ,      _lowercase  ,      _lowercase    )

      _SCREAMING_SNAKE_CASE       =      2 if do_classifier_free_guidance else 1
      _SCREAMING_SNAKE_CASE       =      np.concatenate([image] * batch_multiplier * num_images_per_prompt    )
      _SCREAMING_SNAKE_CASE       =      np.concatenate([noise_level] * image.shape[0]    )

      # 6. Prepare latent variables
      _SCREAMING_SNAKE_CASE,       _SCREAMING_SNAKE_CASE       =      image.shape[2:]
      _SCREAMING_SNAKE_CASE       =      self.prepare_latents(
          batch_size * num_images_per_prompt  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      _lowercase  ,      )

      # 7. Check that sizes of image and latents match
      _SCREAMING_SNAKE_CASE       =      image.shape[1]
      if NUM_LATENT_CHANNELS + num_channels_image != NUM_UNET_INPUT_CHANNELS:
         raise ValueError(
             "Incorrect configuration settings! The config of `pipeline.unet` expects"
             F""" {NUM_UNET_INPUT_CHANNELS} but received `num_channels_latents`: {NUM_LATENT_CHANNELS} +"""
             F""" `num_channels_image`: {num_channels_image} """
             F""" = {NUM_LATENT_CHANNELS+num_channels_image}. Please verify the config of"""
             " `pipeline.unet` or your `image` input."    )

      # 8. Prepare extra step kwargs. TODO: Logic should ideally just be moved out of the pipeline
      _SCREAMING_SNAKE_CASE       =      self.prepare_extra_step_kwargs(_lowercase  ,      _lowercase    )

      _SCREAMING_SNAKE_CASE       =      next(
          (input.type for input in self.unet.model.get_inputs() if input.name == "timestep")  ,      "tensor(float)"    )
      _SCREAMING_SNAKE_CASE       =      ORT_TO_NP_TYPE[timestep_dtype]

      # 9. Denoising loop
      _SCREAMING_SNAKE_CASE       =      len(_lowercase    ) - num_inference_steps * self.scheduler.order
      with self.progress_bar(total=_lowercase    ) as progress_bar:
         for i, t in enumerate(_lowercase    ):
            # expand the latents if we are doing classifier free guidance
            _SCREAMING_SNAKE_CASE       =      np.concatenate([latents] * 2    ) if do_classifier_free_guidance else latents

            # concat latents, mask, masked_image_latents in the channel dimension
            _SCREAMING_SNAKE_CASE       =      self.scheduler.scale_model_input(_lowercase  ,      _lowercase    )
            _SCREAMING_SNAKE_CASE       =      np.concatenate([latent_model_input, image]  ,      axis=1    )

            # timestep to tensor
            _SCREAMING_SNAKE_CASE       =      np.array([t]  ,      dtype=_lowercase    )

            # predict the noise residual
            _SCREAMING_SNAKE_CASE       =      self.unet(
                sample=_lowercase  ,      timestep=_lowercase  ,      encoder_hidden_states=_lowercase  ,      class_labels=noise_level.astype(np.intaa    )  ,      )[0]

            # perform guidance
            if do_classifier_free_guidance:
               _SCREAMING_SNAKE_CASE,       _SCREAMING_SNAKE_CASE       =      np.split(_lowercase  ,      2    )
            _SCREAMING_SNAKE_CASE       =      noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

            # compute the previous noisy sample x_t -> x_t-1
            _SCREAMING_SNAKE_CASE       =      self.scheduler.step(
                torch.from_numpy(_lowercase    )  ,      _lowercase  ,      _lowercase  ,      **_lowercase    ).prev_sample

            # call the callback, if provided
            if i == len(_lowercase    ) - 1 or ((i + 1) > num_warmup_steps and (i + 1) % self.scheduler.order == 0):
               progress_bar.update()
               if callback is not None and i % callback_steps == 0:
                  callback(_lowercase  ,      _lowercase  ,      _lowercase    )

        # 10. Post-processing
      _SCREAMING_SNAKE_CASE       =      self.decode_latents(latents.float()    )

      # 11. Convert to PIL
      if output_type == "pil":
         _SCREAMING_SNAKE_CASE       =      self.numpy_to_pil(_lowercase    )

      if not return_dict:
         return (image,)

      return ImagePipelineOutput(images=_lowercase    )



   def 							lowerCAmelCase_			(    self					:	str  ,      __lowerCamelCase					:	Optional[int]    ):

      """simple docstring"""
      _SCREAMING_SNAKE_CASE       =      1 / 0.0_8_3_3_3 * latents
      _SCREAMING_SNAKE_CASE       =      self.vae(latent_sample=_lowercase    )[0]
      _SCREAMING_SNAKE_CASE       =      np.clip(image / 2 + 0.5  ,      0  ,      1    )
      _SCREAMING_SNAKE_CASE       =      image.transpose((0, 2, 3, 1)    )
      return image



   def 							lowerCAmelCase_			(    self					:	Union[str, Any]  ,      __lowerCamelCase					:	str  ,      __lowerCamelCase					:	Dict  ,      __lowerCamelCase					:	List[str]  ,      __lowerCamelCase					:	Any  ,      __lowerCamelCase					:	Dict  ,      __lowerCamelCase					:	int = None  ,      __lowerCamelCase					:	Dict = None  ,      ):

      """simple docstring"""
      if prompt is not None and isinstance(_lowercase  ,      _lowercase    ):
         _SCREAMING_SNAKE_CASE       =      1
      elif prompt is not None and isinstance(_lowercase  ,      _lowercase    ):
         _SCREAMING_SNAKE_CASE       =      len(_lowercase    )
      else:
         _SCREAMING_SNAKE_CASE       =      prompt_embeds.shape[0]

      if prompt_embeds is None:
         _SCREAMING_SNAKE_CASE       =      self.tokenizer(
             _lowercase  ,      padding="max_length"  ,      max_length=self.tokenizer.model_max_length  ,      truncation=_lowercase  ,      return_tensors="pt"  ,      )
         _SCREAMING_SNAKE_CASE       =      text_inputs.input_ids
         _SCREAMING_SNAKE_CASE       =      self.tokenizer(_lowercase  ,      padding="longest"  ,      return_tensors="pt"    ).input_ids

         if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(
             _lowercase  ,      _lowercase    ):
            _SCREAMING_SNAKE_CASE       =      self.tokenizer.batch_decode(
                untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1]    )
            logger.warning(
                "The following part of your input was truncated because CLIP can only handle sequences up to"
                F""" {self.tokenizer.model_max_length} tokens: {removed_text}"""    )

         # no positional arguments to text_encoder
         _SCREAMING_SNAKE_CASE       =      self.text_encoder(
             input_ids=text_input_ids.int().to(_lowercase    )  ,      )
         _SCREAMING_SNAKE_CASE       =      prompt_embeds[0]

      _SCREAMING_SNAKE_CASE,       _SCREAMING_SNAKE_CASE,       _SCREAMING_SNAKE_CASE       =      prompt_embeds.shape
      # duplicate text embeddings for each generation per prompt, using mps friendly method
      _SCREAMING_SNAKE_CASE       =      prompt_embeds.repeat(1  ,      _lowercase    )
      _SCREAMING_SNAKE_CASE       =      prompt_embeds.reshape(bs_embed * num_images_per_prompt  ,      _lowercase  ,      -1    )

      # get unconditional embeddings for classifier free guidance
      if do_classifier_free_guidance and negative_prompt_embeds is None:
         _SCREAMING_SNAKE_CASE       =      4_2
         if negative_prompt is None:
            _SCREAMING_SNAKE_CASE       =      [""] * batch_size
         elif type(_lowercase    ) is not type(_lowercase    ):
            raise TypeError(
                F"""`negative_prompt` should be the same type to `prompt`, but got {type(_lowercase    )} !="""
                F""" {type(_lowercase    )}."""    )
         elif isinstance(_lowercase  ,      _lowercase    ):
            _SCREAMING_SNAKE_CASE       =      [negative_prompt]
         elif batch_size != len(_lowercase    ):
            raise ValueError(
                F"""`negative_prompt`: {negative_prompt} has batch size {len(_lowercase    )}, but `prompt`:"""
                F""" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"""
                " the batch size of `prompt`."    )
         else:
            _SCREAMING_SNAKE_CASE       =      negative_prompt

         _SCREAMING_SNAKE_CASE       =      text_input_ids.shape[-1]
         _SCREAMING_SNAKE_CASE       =      self.tokenizer(
             _lowercase  ,      padding="max_length"  ,      max_length=_lowercase  ,      truncation=_lowercase  ,      return_tensors="pt"  ,      )

         # if hasattr(uncond_input, "attention_mask"):
         #     attention_mask = uncond_input.attention_mask.to(device)
         # else:
         #     attention_mask = None

         _SCREAMING_SNAKE_CASE       =      self.text_encoder(
             input_ids=uncond_input.input_ids.int().to(_lowercase    )  ,      )
         _SCREAMING_SNAKE_CASE       =      uncond_embeddings[0]

      if do_classifier_free_guidance:
         _SCREAMING_SNAKE_CASE       =      uncond_embeddings.shape[1]
         # duplicate unconditional embeddings for each generation per prompt, using mps friendly method
         _SCREAMING_SNAKE_CASE       =      uncond_embeddings.repeat(1  ,      _lowercase    )
         _SCREAMING_SNAKE_CASE       =      uncond_embeddings.reshape(batch_size * num_images_per_prompt  ,      _lowercase  ,      -1    )

         # For classifier free guidance, we need to do two forward passes.
         # Here we concatenate the unconditional and text embeddings into a single batch
         # to avoid doing two forward passes
         _SCREAMING_SNAKE_CASE       =      np.concatenate([uncond_embeddings, prompt_embeds]    )

      return prompt_embeds




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 		__magic_name__ :






     def __init__(							self						,		_lowercase						,		_lowercase=13						,		_lowercase=32						,		_lowercase=2						,		_lowercase=3						,		_lowercase=16						,		_lowercase=[1, 2, 1]						,		_lowercase=[2, 2, 4]						,		_lowercase=2						,		_lowercase=2.0						,		_lowercase=True						,		_lowercase=0.0						,		_lowercase=0.0						,		_lowercase=0.1						,		_lowercase="gelu"						,		_lowercase=False						,		_lowercase=True						,		_lowercase=0.02						,		_lowercase=1e-5						,		_lowercase=True						,		_lowercase=None						,		_lowercase=True						,		_lowercase=10						,		_lowercase=8						,		)->			Tuple:
         UpperCamelCase_          =		parent
         UpperCamelCase_          =		batch_size
         UpperCamelCase_          =		image_size
         UpperCamelCase_          =		patch_size
         UpperCamelCase_          =		num_channels
         UpperCamelCase_          =		embed_dim
         UpperCamelCase_          =		depths
         UpperCamelCase_          =		num_heads
         UpperCamelCase_          =		window_size
         UpperCamelCase_          =		mlp_ratio
         UpperCamelCase_          =		qkv_bias
         UpperCamelCase_          =		hidden_dropout_prob
         UpperCamelCase_          =		attention_probs_dropout_prob
         UpperCamelCase_          =		drop_path_rate
         UpperCamelCase_          =		hidden_act
         UpperCamelCase_          =		use_absolute_embeddings
         UpperCamelCase_          =		patch_norm
         UpperCamelCase_          =		layer_norm_eps
         UpperCamelCase_          =		initializer_range
         UpperCamelCase_          =		is_training
         UpperCamelCase_          =		scope
         UpperCamelCase_          =		use_labels
         UpperCamelCase_          =		type_sequence_label_size
         UpperCamelCase_          =		encoder_stride






     def      UpperCAmelCase_							(							self     )->			Dict:
         UpperCamelCase_          =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )

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

         UpperCamelCase_          =		self.get_config()

         return config, pixel_values, labels






     def      UpperCAmelCase_							(							self     )->			Union[str, Any]:
         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						,		_lowercase						,		_lowercase						,		_lowercase     )->			Optional[Any]:
         UpperCamelCase_          =		DonutSwinModel(config=_lowercase     )
         model.to(_lowercase     )
         model.eval()
         UpperCamelCase_          =		model(_lowercase     )

         UpperCamelCase_          =		((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths     ) - 1))
         UpperCamelCase_          =		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     )->			Tuple:
         UpperCamelCase_          =		self.prepare_config_and_inputs()
         (
             (
             UpperCamelCase_
         )   ,    (
             UpperCamelCase_
         )   ,    (
             UpperCamelCase_
         )   ,    
         )          =		config_and_inputs
         UpperCamelCase_          =		{"pixel_values": pixel_values}
         return config, inputs_dict






@require_torch
class 		__magic_name__ (				snake_case       ,	snake_case       ,	unittest.TestCase							):
     UpperCamelCase_     :List[str]									=	(DonutSwinModel,) if is_torch_available() else ()
     UpperCamelCase_     :Optional[Any]									=	{"""feature-extraction""": DonutSwinModel} if is_torch_available() else {}
     UpperCamelCase_     :Any									=	True

     UpperCamelCase_     :Tuple									=	False
     UpperCamelCase_     :List[str]									=	False
     UpperCamelCase_     :List[str]									=	False






     def      UpperCAmelCase_							(							self     )->			Union[str, Any]:
         UpperCamelCase_          =		DonutSwinModelTester(self     )
         UpperCamelCase_          =		ConfigTester(self						,		config_class=_lowercase						,		embed_dim=37     )






     def      UpperCAmelCase_							(							self     )->			Optional[int]:
         self.create_and_test_config_common_properties()
         self.config_tester.create_and_test_config_to_json_string()
         self.config_tester.create_and_test_config_to_json_file()
         self.config_tester.create_and_test_config_from_and_save_pretrained()
         self.config_tester.create_and_test_config_with_num_labels()
         self.config_tester.check_config_can_be_init_without_params()
         self.config_tester.check_config_arguments_init()






     def      UpperCAmelCase_							(							self     )->			Optional[int]:
         return






     def      UpperCAmelCase_							(							self     )->			Tuple:
         UpperCamelCase_          =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_lowercase     )






     def      UpperCAmelCase_							(							self     )->			int:
         # DonutSwin does not use inputs_embeds
         pass






     def      UpperCAmelCase_							(							self     )->			Union[str, Any]:
         UpperCamelCase_   ,    UpperCamelCase_          =		self.model_tester.prepare_config_and_inputs_for_common()

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






     def      UpperCAmelCase_							(							self     )->			Union[str, Any]:
         UpperCamelCase_   ,    UpperCamelCase_          =		self.model_tester.prepare_config_and_inputs_for_common()

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

             UpperCamelCase_          =		["pixel_values"]
             self.assertListEqual(arg_names[:1]						,		_lowercase     )






     def      UpperCAmelCase_							(							self     )->			List[Any]:
         UpperCamelCase_   ,    UpperCamelCase_          =		self.model_tester.prepare_config_and_inputs_for_common()
         UpperCamelCase_          =		True

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

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

             self.assertListEqual(
                 list(attentions[0].shape[-3:]     )						,		[self.model_tester.num_heads[0], window_size_squared, window_size_squared]						,		)
             UpperCamelCase_          =		len(_lowercase     )

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

             if hasattr(self.model_tester						,		"num_hidden_states_types"     ):
                 UpperCamelCase_          =		self.model_tester.num_hidden_states_types
             else:
                 # also another +1 for reshaped_hidden_states
                 UpperCamelCase_          =		2
             self.assertEqual(out_len + added_hidden_states						,		len(_lowercase     )     )

             UpperCamelCase_          =		outputs.attentions

             self.assertEqual(len(_lowercase     )						,		_lowercase     )

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






     def      UpperCAmelCase_							(							self						,		_lowercase						,		_lowercase						,		_lowercase						,		_lowercase     )->			List[Any]:
         UpperCamelCase_          =		model_class(_lowercase     )
         model.to(_lowercase     )
         model.eval()

         with torch.no_grad():
             UpperCamelCase_          =		model(**self._prepare_for_class(_lowercase						,		_lowercase     )     )

         UpperCamelCase_          =		outputs.hidden_states

         UpperCamelCase_          =		getattr(
             self.model_tester						,		"expected_num_hidden_layers"						,		len(self.model_tester.depths     ) + 1     )
         self.assertEqual(len(_lowercase     )						,		_lowercase     )

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

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

         UpperCamelCase_          =		outputs.reshaped_hidden_states
         self.assertEqual(len(_lowercase     )						,		_lowercase     )

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






     def      UpperCAmelCase_							(							self     )->			List[str]:
         UpperCamelCase_   ,    UpperCamelCase_          =		self.model_tester.prepare_config_and_inputs_for_common()

         UpperCamelCase_          =		(
             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:
             UpperCamelCase_          =		True
             self.check_hidden_states_output(_lowercase						,		_lowercase						,		_lowercase						,		_lowercase     )

             # check that output_hidden_states also work using config
             del inputs_dict["output_hidden_states"]
             UpperCamelCase_          =		True

             self.check_hidden_states_output(_lowercase						,		_lowercase						,		_lowercase						,		_lowercase     )






     def      UpperCAmelCase_							(							self     )->			Any:
         UpperCamelCase_   ,    UpperCamelCase_          =		self.model_tester.prepare_config_and_inputs_for_common()
         UpperCamelCase_          =		3

         UpperCamelCase_          =		(
             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)
         )
         UpperCamelCase_          =		(
             config.patch_size
             if isinstance(config.patch_size						,		collections.abc.Iterable     )
             else (config.patch_size, config.patch_size)
         )

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

         for model_class in self.all_model_classes:
             UpperCamelCase_          =		True
             self.check_hidden_states_output(_lowercase						,		_lowercase						,		_lowercase						,		(padded_height, padded_width)     )

             # check that output_hidden_states also work using config
             del inputs_dict["output_hidden_states"]
             UpperCamelCase_          =		True
             self.check_hidden_states_output(_lowercase						,		_lowercase						,		_lowercase						,		(padded_height, padded_width)     )






     @slow
     def      UpperCAmelCase_							(							self     )->			List[str]:
         for model_name in DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             UpperCamelCase_          =		DonutSwinModel.from_pretrained(_lowercase     )
             self.assertIsNotNone(_lowercase     )






     def      UpperCAmelCase_							(							self     )->			Any:
         UpperCamelCase_   ,    UpperCamelCase_          =		self.model_tester.prepare_config_and_inputs_for_common()

         UpperCamelCase_          =		_config_zero_init(_lowercase     )
         for model_class in self.all_model_classes:
             UpperCamelCase_          =		model_class(config=_lowercase     )
             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"						,		)
