'''simple docstring'''





from dataclasses import dataclass
from typing import Any, Dict, Optional

import torch
import torch.nn.functional as F
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..models.embeddings import ImagePositionalEmbeddings
from ..utils import BaseOutput, deprecate
from .attention import BasicTransformerBlock
from .embeddings import PatchEmbed
from .modeling_utils import ModelMixin




@dataclass
class 				_snake_case	(			UpperCamelCase__							):
			lowerCAmelCase_  :   List[Any]		 =					42




class 				_snake_case	(			UpperCamelCase__		,		UpperCamelCase__							):




			@register_to_config
			def __init__(      self       ,							a__ = 16       ,							a__ = 88       ,							a__ = None       ,							a__ = None       ,							a__ = 1       ,							a__ = 0.0       ,							a__ = 32       ,							a__ = None       ,							a__ = False       ,							a__ = None       ,							a__ = None       ,							a__ = None       ,							a__ = "geglu"       ,							a__ = None       ,							a__ = False       ,							a__ = False       ,							a__ = False       ,							a__ = "layer_norm"       ,							a__ = True       ,							)       ->       Tuple:

										'''simple docstring'''




										super().__init__()
										snake_case_			      =   use_linear_projection
										snake_case_			      =   num_attention_heads
										snake_case_			      =   attention_head_dim
										snake_case_			      =   num_attention_heads * attention_head_dim

										# 1. Transformer2DModel can process both standard continuous images of shape `(batch_size, num_channels, width, height)` as well as quantized image embeddings of shape `(batch_size, num_image_vectors)`
										# Define whether input is continuous or discrete depending on configuration
										snake_case_			      =   (in_channels is not None) and (patch_size is None)
										snake_case_			      =   num_vector_embeds is not None
										snake_case_			      =   in_channels is not None and patch_size is not None

										if norm_type == "layer_norm" and num_embeds_ada_norm is not None:
																	snake_case_			      =   (
																	    F'The configuration file of this model: {self.__class__} is outdated. `norm_type` is either not set or'
																	    ''' incorrectly set to `\'layer_norm\'`.Make sure to set `norm_type` to `\'ada_norm\'` in the config.'''
																	    ''' Please make sure to update the config accordingly as leaving `norm_type` might led to incorrect'''
																	    ''' results in future versions. If you have downloaded this checkpoint from the Hugging Face Hub, it'''
																	    ''' would be very nice if you could open a Pull request for the `transformer/config.json` file'''
																	)
																	deprecate("norm_type!=num_embeds_ada_norm"       ,							"1.0.0"       ,							snake_case_       ,							standard_warn=snake_case_							)
																	snake_case_			      =   '''ada_norm'''

										if self.is_input_continuous and self.is_input_vectorized:
																	raise ValueError(
																	    F'Cannot define both `in_channels`: {in_channels} and `num_vector_embeds`: {num_vector_embeds}. Make'
																	    " sure that either `in_channels` or `num_vector_embeds` is None."							)
										elif self.is_input_vectorized and self.is_input_patches:
																	raise ValueError(
																	    F'Cannot define both `num_vector_embeds`: {num_vector_embeds} and `patch_size`: {patch_size}. Make'
																	    " sure that either `num_vector_embeds` or `num_patches` is None."							)
										elif not self.is_input_continuous and not self.is_input_vectorized and not self.is_input_patches:
																	raise ValueError(
																	    F'Has to define `in_channels`: {in_channels}, `num_vector_embeds`: {num_vector_embeds}, or patch_size:'
																	    F' {patch_size}. Make sure that `in_channels`, `num_vector_embeds` or `num_patches` is not None.'							)

										# 2. Define input layers
										if self.is_input_continuous:
																	snake_case_			      =   in_channels

																	snake_case_			      =   torch.nn.GroupNorm(num_groups=snake_case_       ,							num_channels=snake_case_       ,							eps=1e-6       ,							affine=snake_case_							)
																	if use_linear_projection:
																								snake_case_			      =   nn.Linear(snake_case_       ,							snake_case_							)
																	else:
																								snake_case_			      =   nn.Convad(snake_case_       ,							snake_case_       ,							kernel_size=1       ,							stride=1       ,							padding=0							)
										elif self.is_input_vectorized:
																	assert sample_size is not None, "Transformer2DModel over discrete input must provide sample_size"
																	assert num_vector_embeds is not None, "Transformer2DModel over discrete input must provide num_embed"

																	snake_case_			      =   sample_size
																	snake_case_			      =   sample_size
																	snake_case_			      =   num_vector_embeds
																	snake_case_			      =   self.height * self.width

																	snake_case_			      =   ImagePositionalEmbeddings(
																	    num_embed=snake_case_       ,							embed_dim=snake_case_       ,							height=self.height       ,							width=self.width							)
										elif self.is_input_patches:
																	assert sample_size is not None, "Transformer2DModel over patched input must provide sample_size"

																	snake_case_			      =   sample_size
																	snake_case_			      =   sample_size

																	snake_case_			      =   patch_size
																	snake_case_			      =   PatchEmbed(
																	    height=snake_case_       ,							width=snake_case_       ,							patch_size=snake_case_       ,							in_channels=snake_case_       ,							embed_dim=snake_case_       ,							)

										# 3. Define transformers blocks
										snake_case_			      =   nn.ModuleList(
										    [
										        BasicTransformerBlock(
										            snake_case_       ,							snake_case_       ,							snake_case_       ,							dropout=snake_case_       ,							cross_attention_dim=snake_case_       ,							activation_fn=snake_case_       ,							num_embeds_ada_norm=snake_case_       ,							attention_bias=snake_case_       ,							only_cross_attention=snake_case_       ,							upcast_attention=snake_case_       ,							norm_type=snake_case_       ,							norm_elementwise_affine=snake_case_       ,							)
										        for d in range(snake_case_							)
										    ]							)

										# 4. Define output layers
										snake_case_			      =   in_channels if out_channels is None else out_channels
										if self.is_input_continuous:
																	# TODO: should use out_channels for continuous projections
																	if use_linear_projection:
																								snake_case_			      =   nn.Linear(snake_case_       ,							snake_case_							)
																	else:
																								snake_case_			      =   nn.Convad(snake_case_       ,							snake_case_       ,							kernel_size=1       ,							stride=1       ,							padding=0							)
										elif self.is_input_vectorized:
																	snake_case_			      =   nn.LayerNorm(snake_case_							)
																	snake_case_			      =   nn.Linear(snake_case_       ,							self.num_vector_embeds - 1							)
										elif self.is_input_patches:
																	snake_case_			      =   nn.LayerNorm(snake_case_       ,							elementwise_affine=snake_case_       ,							eps=1e-6							)
																	snake_case_			      =   nn.Linear(snake_case_       ,							2 * inner_dim							)
																	snake_case_			      =   nn.Linear(snake_case_       ,							patch_size * patch_size * self.out_channels							)




			def      lowerCAmelCase__       (      self       ,							a__       ,							a__ = None       ,							a__ = None       ,							a__ = None       ,							a__ = None       ,							a__ = None       ,							a__ = None       ,							a__ = True       ,							)       ->       Tuple:

										'''simple docstring'''




										if attention_mask is not None and attention_mask.ndim == 2:
																	# assume that mask is expressed as:
																	#   (1 = keep,      0 = discard)
																	# convert mask into a bias that can be added to attention scores:
																	#       (keep = +0,     discard = -10000.0)
																	snake_case_			      =   (1 - attention_mask.to(hidden_states.dtype							)) * -10_000.0
																	snake_case_			      =   attention_mask.unsqueeze(1							)

										# convert encoder_attention_mask to a bias the same way we do for attention_mask
										if encoder_attention_mask is not None and encoder_attention_mask.ndim == 2:
																	snake_case_			      =   (1 - encoder_attention_mask.to(hidden_states.dtype							)) * -10_000.0
																	snake_case_			      =   encoder_attention_mask.unsqueeze(1							)

										# 1. Input
										if self.is_input_continuous:
																	snake_case_			      =   hidden_states.shape
																	snake_case_			      =   hidden_states

																	snake_case_			      =   self.norm(snake_case_							)
																	if not self.use_linear_projection:
																								snake_case_			      =   self.proj_in(snake_case_							)
																								snake_case_			      =   hidden_states.shape[1]
																								snake_case_			      =   hidden_states.permute(0       ,							2       ,							3       ,							1							).reshape(snake_case_       ,							height * width       ,							snake_case_							)
																	else:
																								snake_case_			      =   hidden_states.shape[1]
																								snake_case_			      =   hidden_states.permute(0       ,							2       ,							3       ,							1							).reshape(snake_case_       ,							height * width       ,							snake_case_							)
																								snake_case_			      =   self.proj_in(snake_case_							)
										elif self.is_input_vectorized:
																	snake_case_			      =   self.latent_image_embedding(snake_case_							)
										elif self.is_input_patches:
																	snake_case_			      =   self.pos_embed(snake_case_							)

										# 2. Blocks
										for block in self.transformer_blocks:
																	snake_case_			      =   block(
																	    snake_case_       ,							attention_mask=snake_case_       ,							encoder_hidden_states=snake_case_       ,							encoder_attention_mask=snake_case_       ,							timestep=snake_case_       ,							cross_attention_kwargs=snake_case_       ,							class_labels=snake_case_       ,							)

										# 3. Output
										if self.is_input_continuous:
																	if not self.use_linear_projection:
																								snake_case_			      =   hidden_states.reshape(snake_case_       ,							snake_case_       ,							snake_case_       ,							snake_case_							).permute(0       ,							3       ,							1       ,							2							).contiguous()
																								snake_case_			      =   self.proj_out(snake_case_							)
																	else:
																								snake_case_			      =   self.proj_out(snake_case_							)
																								snake_case_			      =   hidden_states.reshape(snake_case_       ,							snake_case_       ,							snake_case_       ,							snake_case_							).permute(0       ,							3       ,							1       ,							2							).contiguous()

																	snake_case_			      =   hidden_states + residual
										elif self.is_input_vectorized:
																	snake_case_			      =   self.norm_out(snake_case_							)
																	snake_case_			      =   self.out(snake_case_							)
																	# (batch, self.num_vector_embeds - 1, self.num_latent_pixels)
																	snake_case_			      =   logits.permute(0       ,							2       ,							1							)

																	# log(p(x_0))
																	snake_case_			      =   F.log_softmax(logits.double()       ,							dim=1							).float()
										elif self.is_input_patches:
																	# TODO: cleanup!
																	snake_case_			      =   self.transformer_blocks[0].norma.emb(
																	    snake_case_       ,							snake_case_       ,							hidden_dtype=hidden_states.dtype							)
																	snake_case_			      =   self.proj_out_a(F.silu(snake_case_							)							).chunk(2       ,							dim=1							)
																	snake_case_			      =   self.norm_out(snake_case_							) * (1 + scale[:, None]) + shift[:, None]
																	snake_case_			      =   self.proj_out_a(snake_case_							)

																	# unpatchify
																	snake_case_			      =   int(hidden_states.shape[1] ** 0.5							)
																	snake_case_			      =   hidden_states.reshape(
																	    shape=(-1, height, width, self.patch_size, self.patch_size, self.out_channels)							)
																	snake_case_			      =   torch.einsum("nhwpqc->nchpwq"       ,							snake_case_							)
																	snake_case_			      =   hidden_states.reshape(
																	    shape=(-1, self.out_channels, height * self.patch_size, width * self.patch_size)							)

										if not return_dict:
																	return (output,)

										return TransformeraDModelOutput(sample=snake_case_							)










'''simple docstring'''





import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
		import torch

if is_vision_available():
		from PIL import Image

		from transformers import ViltImageProcessor




class 				_snake_case	(			unittest.TestCase							):




			def __init__(      self       ,							a__       ,							a__=7       ,							a__=3       ,							a__=18       ,							a__=30       ,							a__=400       ,							a__=True       ,							a__=None       ,							a__=2       ,							a__=True       ,							a__=[0.5, 0.5, 0.5]       ,							a__=[0.5, 0.5, 0.5]       ,							)       ->       Any:

										'''simple docstring'''




										snake_case_			      =   size if size is not None else {"shortest_edge": 30}
										snake_case_			      =   parent
										snake_case_			      =   batch_size
										snake_case_			      =   num_channels
										snake_case_			      =   image_size
										snake_case_			      =   min_resolution
										snake_case_			      =   max_resolution
										snake_case_			      =   do_resize
										snake_case_			      =   size
										snake_case_			      =   size_divisor
										snake_case_			      =   do_normalize
										snake_case_			      =   image_mean
										snake_case_			      =   image_std




			def      lowerCAmelCase__       (      self							)       ->       int:

										'''simple docstring'''




										return {
										    "image_mean": self.image_mean,
										    "image_std": self.image_std,
										    "do_normalize": self.do_normalize,
										    "do_resize": self.do_resize,
										    "size": self.size,
										    "size_divisor": self.size_divisor,
										}




			def      lowerCAmelCase__       (      self       ,							a__       ,							a__=False							)       ->       str:

										'''simple docstring'''




										if not batched:
																	snake_case_			      =   self.size["shortest_edge"]
																	snake_case_			      =   image_inputs[0]
																	if isinstance(a__       ,							Image.Image							):
																								snake_case_      ,   snake_case_			      =   image.size
																	else:
																								snake_case_      ,   snake_case_			      =   image.shape[1], image.shape[2]
																	snake_case_			      =   size / min(a__       ,							a__							)
																	if h < w:
																								snake_case_      ,   snake_case_			      =   size, scale * w
																	else:
																								snake_case_      ,   snake_case_			      =   scale * h, size

																	snake_case_			      =   int((1_333 / 800) * size							)
																	if max(a__       ,							a__							) > max_size:
																								snake_case_			      =   max_size / max(a__       ,							a__							)
																								snake_case_			      =   newh * scale
																								snake_case_			      =   neww * scale

																	snake_case_      ,   snake_case_			      =   int(newh + 0.5							), int(neww + 0.5							)
																	snake_case_      ,   snake_case_			      =   (
																	    newh // self.size_divisor * self.size_divisor,
																	    neww // self.size_divisor * self.size_divisor,
																	)

										else:
																	snake_case_			      =   []
																	for image in image_inputs:
																								snake_case_      ,   snake_case_			      =   self.get_expected_values([image]							)
																								expected_values.append((expected_height, expected_width)							)
																	snake_case_			      =   max(a__       ,							key=lambda a__							: item[0]							)[0]
																	snake_case_			      =   max(a__       ,							key=lambda a__							: item[1]							)[1]

										return expected_height, expected_width




@require_torch
@require_vision
class 				_snake_case	(			lowercase_		,		unittest.TestCase							):
			lowerCAmelCase_  :   Union[str, Any]		 =					ViltImageProcessor if is_vision_available() else None




			def      lowerCAmelCase__       (      self							)       ->       Dict:

										'''simple docstring'''




										snake_case_			      =   ViltImageProcessingTester(self							)




			@property
			def      lowerCAmelCase__       (      self							)       ->       Any:

										'''simple docstring'''




										return self.image_processor_tester.prepare_image_processor_dict()




			def      lowerCAmelCase__       (      self							)       ->       List[Any]:

										'''simple docstring'''




										snake_case_			      =   self.image_processing_class(**self.image_processor_dict							)
										self.assertTrue(hasattr(a__       ,							"image_mean"							)							)
										self.assertTrue(hasattr(a__       ,							"image_std"							)							)
										self.assertTrue(hasattr(a__       ,							"do_normalize"							)							)
										self.assertTrue(hasattr(a__       ,							"do_resize"							)							)
										self.assertTrue(hasattr(a__       ,							"size"							)							)
										self.assertTrue(hasattr(a__       ,							"size_divisor"							)							)




			def      lowerCAmelCase__       (      self							)       ->       Dict:

										'''simple docstring'''




										snake_case_			      =   self.image_processing_class.from_dict(self.image_processor_dict							)
										self.assertEqual(image_processor.size       ,							{"shortest_edge": 30}							)

										snake_case_			      =   self.image_processing_class.from_dict(self.image_processor_dict       ,							size=42							)
										self.assertEqual(image_processor.size       ,							{"shortest_edge": 42}							)




			def      lowerCAmelCase__       (      self							)       ->       Optional[int]:

										'''simple docstring'''




										pass




			def      lowerCAmelCase__       (      self							)       ->       Tuple:

										'''simple docstring'''




										snake_case_			      =   self.image_processing_class(**self.image_processor_dict							)
										# create random PIL images
										snake_case_			      =   prepare_image_inputs(self.image_processor_tester       ,							equal_resolution=a__							)
										for image in image_inputs:
																	self.assertIsInstance(a__       ,							Image.Image							)

										# Test not batched input
										snake_case_			      =   image_processing(image_inputs[0]       ,							return_tensors="pt"							).pixel_values

										snake_case_      ,   snake_case_			      =   self.image_processor_tester.get_expected_values(a__							)
										self.assertEqual(
										    encoded_images.shape       ,							(1, self.image_processor_tester.num_channels, expected_height, expected_width)       ,							)

										# Test batched
										snake_case_			      =   image_processing(a__       ,							return_tensors="pt"							).pixel_values

										snake_case_      ,   snake_case_			      =   self.image_processor_tester.get_expected_values(a__       ,							batched=a__							)
										self.assertEqual(
										    encoded_images.shape       ,							(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        expected_height,
										        expected_width,
										    )       ,							)




			def      lowerCAmelCase__       (      self							)       ->       Dict:

										'''simple docstring'''




										snake_case_			      =   self.image_processing_class(**self.image_processor_dict							)
										# create random numpy tensors
										snake_case_			      =   prepare_image_inputs(self.image_processor_tester       ,							equal_resolution=a__       ,							numpify=a__							)
										for image in image_inputs:
																	self.assertIsInstance(a__       ,							np.ndarray							)

										# Test not batched input
										snake_case_			      =   image_processing(image_inputs[0]       ,							return_tensors="pt"							).pixel_values

										snake_case_      ,   snake_case_			      =   self.image_processor_tester.get_expected_values(a__							)
										self.assertEqual(
										    encoded_images.shape       ,							(1, self.image_processor_tester.num_channels, expected_height, expected_width)       ,							)

										# Test batched
										snake_case_			      =   image_processing(a__       ,							return_tensors="pt"							).pixel_values

										snake_case_      ,   snake_case_			      =   self.image_processor_tester.get_expected_values(a__       ,							batched=a__							)
										self.assertEqual(
										    encoded_images.shape       ,							(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        expected_height,
										        expected_width,
										    )       ,							)




			def      lowerCAmelCase__       (      self							)       ->       Tuple:

										'''simple docstring'''




										snake_case_			      =   self.image_processing_class(**self.image_processor_dict							)
										# create random PyTorch tensors
										snake_case_			      =   prepare_image_inputs(self.image_processor_tester       ,							equal_resolution=a__       ,							torchify=a__							)
										for image in image_inputs:
																	self.assertIsInstance(a__       ,							torch.Tensor							)

										# Test not batched input
										snake_case_			      =   image_processing(image_inputs[0]       ,							return_tensors="pt"							).pixel_values

										snake_case_      ,   snake_case_			      =   self.image_processor_tester.get_expected_values(a__							)
										self.assertEqual(
										    encoded_images.shape       ,							(1, self.image_processor_tester.num_channels, expected_height, expected_width)       ,							)

										# Test batched
										snake_case_			      =   image_processing(a__       ,							return_tensors="pt"							).pixel_values

										snake_case_      ,   snake_case_			      =   self.image_processor_tester.get_expected_values(a__       ,							batched=a__							)
										self.assertEqual(
										    encoded_images.shape       ,							(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        expected_height,
										        expected_width,
										    )       ,							)




