






'''simple docstring'''







import torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin

class _lowercase	(				_lowercase				,      _lowercase		):




							@register_to_config
							def __init__(							self:				Optional[Any]			,							*,
							    UpperCamelCase__:				int = 4			,							UpperCamelCase__:				int = 768			,							UpperCamelCase__:				int			,							UpperCamelCase__:				int			,							):
														super().__init__()

														lowerCamelCase__					:	Dict         =       nn.Parameter(torch.zeros(UpperCamelCase__     )     )

														# parameters for additional clip time embeddings
														lowerCamelCase__					:	int         =       nn.Linear(UpperCamelCase__			,							UpperCamelCase__     )
														lowerCamelCase__					:	Tuple         =       nn.Linear(UpperCamelCase__			,							UpperCamelCase__     )

														# parameters for encoder hidden states
														lowerCamelCase__					:	Optional[int]         =       clip_extra_context_tokens
														lowerCamelCase__					:	Optional[int]         =       nn.Linear(
														    UpperCamelCase__			,							self.clip_extra_context_tokens * cross_attention_dim     )
														lowerCamelCase__					:	List[Any]         =       nn.Linear(UpperCamelCase__			,							UpperCamelCase__     )
														lowerCamelCase__					:	List[Any]         =       nn.LayerNorm(UpperCamelCase__     )




							def 		lowerCamelCase_				(							self:				int			,							*, UpperCamelCase__:				Union[str, Any]			,							UpperCamelCase__:				str			,							UpperCamelCase__:				int			,							UpperCamelCase__:				Union[str, Any]     ):
														if do_classifier_free_guidance:
																					# Add the classifier free guidance embeddings to the image embeddings
																					lowerCamelCase__					:	Dict         =       image_embeddings.shape[0]
																					lowerCamelCase__					:	Optional[int]         =       self.learned_classifier_free_guidance_embeddings.unsqueeze(0     )
																					lowerCamelCase__					:	List[str]         =       classifier_free_guidance_embeddings.expand(
																					    UpperCamelCase__			,							-1     )
																					lowerCamelCase__					:	str         =       torch.cat([classifier_free_guidance_embeddings, image_embeddings]			,							dim=0     )

														# The image embeddings batch size and the text embeddings batch size are equal
														assert image_embeddings.shape[0] == prompt_embeds.shape[0]

														lowerCamelCase__					:	Dict         =       prompt_embeds.shape[0]

														# "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and
														# adding CLIP embeddings to the existing timestep embedding, ...
														lowerCamelCase__					:	List[Any]         =       self.embedding_proj(UpperCamelCase__     )
														lowerCamelCase__					:	List[str]         =       self.clip_image_embeddings_project_to_time_embeddings(UpperCamelCase__     )
														lowerCamelCase__					:	Dict         =       time_projected_image_embeddings + time_projected_prompt_embeds

														# ... and by projecting CLIP embeddings into four
														# extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder"
														lowerCamelCase__					:	List[str]         =       self.clip_extra_context_tokens_proj(UpperCamelCase__     )
														lowerCamelCase__					:	Optional[int]         =       clip_extra_context_tokens.reshape(UpperCamelCase__			,							-1			,							self.clip_extra_context_tokens     )
														lowerCamelCase__					:	int         =       clip_extra_context_tokens.permute(0			,							2			,							1     )

														lowerCamelCase__					:	Optional[int]         =       self.encoder_hidden_states_proj(UpperCamelCase__     )
														lowerCamelCase__					:	str         =       self.text_encoder_hidden_states_norm(UpperCamelCase__     )
														lowerCamelCase__					:	Optional[int]         =       torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]			,							dim=1     )

														return text_encoder_hidden_states, additive_clip_time_embeddings









'''simple docstring'''







import unittest

import numpy as np

from transformers.testing_utils import is_flaky, 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 DonutImageProcessor

class _lowercase	(				unittest.TestCase		):




							def __init__(							self:				str			,							UpperCamelCase__:				Optional[Any]			,							UpperCamelCase__:				Any=7			,							UpperCamelCase__:				Optional[int]=3			,							UpperCamelCase__:				List[str]=18			,							UpperCamelCase__:				Union[str, Any]=30			,							UpperCamelCase__:				List[str]=400			,							UpperCamelCase__:				Any=True			,							UpperCamelCase__:				Union[str, Any]=None			,							UpperCamelCase__:				List[Any]=True			,							UpperCamelCase__:				List[Any]=False			,							UpperCamelCase__:				Tuple=True			,							UpperCamelCase__:				Tuple=True			,							UpperCamelCase__:				Any=[0.5, 0.5, 0.5]			,							UpperCamelCase__:				Optional[Any]=[0.5, 0.5, 0.5]			,							):
														lowerCamelCase__					:	int         =       parent
														lowerCamelCase__					:	Any         =       batch_size
														lowerCamelCase__					:	Optional[int]         =       num_channels
														lowerCamelCase__					:	Union[str, Any]         =       image_size
														lowerCamelCase__					:	Optional[int]         =       min_resolution
														lowerCamelCase__					:	Optional[Any]         =       max_resolution
														lowerCamelCase__					:	Union[str, Any]         =       do_resize
														lowerCamelCase__					:	Optional[Any]         =       size if size is not None else {"""height""": 18, """width""": 20}
														lowerCamelCase__					:	Dict         =       do_thumbnail
														lowerCamelCase__					:	Optional[int]         =       do_align_axis
														lowerCamelCase__					:	Any         =       do_pad
														lowerCamelCase__					:	Optional[Any]         =       do_normalize
														lowerCamelCase__					:	Union[str, Any]         =       image_mean
														lowerCamelCase__					:	Union[str, Any]         =       image_std




							def 		lowerCamelCase_				(							self:				str     ):
														return {
														    "do_resize": self.do_resize,
														    "size": self.size,
														    "do_thumbnail": self.do_thumbnail,
														    "do_align_long_axis": self.do_align_axis,
														    "do_pad": self.do_pad,
														    "do_normalize": self.do_normalize,
														    "image_mean": self.image_mean,
														    "image_std": self.image_std,
														}



@require_torch
@require_vision
class _lowercase	(				_lowercase				,      unittest.TestCase		):
							a				      =       DonutImageProcessor if is_vision_available() else None




							def 		lowerCamelCase_				(							self:				Optional[int]     ):
														lowerCamelCase__					:	Any         =       DonutImageProcessingTester(self     )




							@property
							def 		lowerCamelCase_				(							self:				Optional[int]     ):
														return self.image_processor_tester.prepare_image_processor_dict()




							def 		lowerCamelCase_				(							self:				Tuple     ):
														lowerCamelCase__					:	Dict         =       self.image_processing_class(**self.image_processor_dict     )
														self.assertTrue(hasattr(UpperCamelCase__			,							"""do_resize"""     )     )
														self.assertTrue(hasattr(UpperCamelCase__			,							"""size"""     )     )
														self.assertTrue(hasattr(UpperCamelCase__			,							"""do_thumbnail"""     )     )
														self.assertTrue(hasattr(UpperCamelCase__			,							"""do_align_long_axis"""     )     )
														self.assertTrue(hasattr(UpperCamelCase__			,							"""do_pad"""     )     )
														self.assertTrue(hasattr(UpperCamelCase__			,							"""do_normalize"""     )     )
														self.assertTrue(hasattr(UpperCamelCase__			,							"""image_mean"""     )     )
														self.assertTrue(hasattr(UpperCamelCase__			,							"""image_std"""     )     )




							def 		lowerCamelCase_				(							self:				Optional[Any]     ):
														lowerCamelCase__					:	Optional[int]         =       self.image_processing_class.from_dict(self.image_processor_dict     )
														self.assertEqual(image_processor.size			,							{"""height""": 18, """width""": 20}     )

														lowerCamelCase__					:	Optional[int]         =       self.image_processing_class.from_dict(self.image_processor_dict			,							size=42     )
														self.assertEqual(image_processor.size			,							{"""height""": 42, """width""": 42}     )

														# Previous config had dimensions in (width, height) order
														lowerCamelCase__					:	List[str]         =       self.image_processing_class.from_dict(self.image_processor_dict			,							size=(42, 84)     )
														self.assertEqual(image_processor.size			,							{"""height""": 84, """width""": 42}     )




							def 		lowerCamelCase_				(							self:				List[str]     ):
														pass




							@is_flaky()
							def 		lowerCamelCase_				(							self:				Union[str, Any]     ):
														# Initialize image_processing
														lowerCamelCase__					:	str         =       self.image_processing_class(**self.image_processor_dict     )
														# create random PIL images
														lowerCamelCase__					:	Dict         =       prepare_image_inputs(self.image_processor_tester			,							equal_resolution=UpperCamelCase__     )
														for image in image_inputs:
																					self.assertIsInstance(UpperCamelCase__			,							Image.Image     )

														# Test not batched input
														lowerCamelCase__					:	Union[str, Any]         =       image_processing(image_inputs[0]			,							return_tensors="""pt"""     ).pixel_values
														self.assertEqual(
														    encoded_images.shape			,							(
														        1,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.size["""height"""],
														        self.image_processor_tester.size["""width"""],
														    )			,							)

														# Test batched
														lowerCamelCase__					:	List[str]         =       image_processing(UpperCamelCase__			,							return_tensors="""pt"""     ).pixel_values
														self.assertEqual(
														    encoded_images.shape			,							(
														        self.image_processor_tester.batch_size,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.size["""height"""],
														        self.image_processor_tester.size["""width"""],
														    )			,							)




							@is_flaky()
							def 		lowerCamelCase_				(							self:				Optional[int]     ):
														# Initialize image_processing
														lowerCamelCase__					:	Dict         =       self.image_processing_class(**self.image_processor_dict     )
														# create random numpy tensors
														lowerCamelCase__					:	Dict         =       prepare_image_inputs(self.image_processor_tester			,							equal_resolution=UpperCamelCase__			,							numpify=UpperCamelCase__     )
														for image in image_inputs:
																					self.assertIsInstance(UpperCamelCase__			,							np.ndarray     )

														# Test not batched input
														lowerCamelCase__					:	List[Any]         =       image_processing(image_inputs[0]			,							return_tensors="""pt"""     ).pixel_values
														self.assertEqual(
														    encoded_images.shape			,							(
														        1,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.size["""height"""],
														        self.image_processor_tester.size["""width"""],
														    )			,							)

														# Test batched
														lowerCamelCase__					:	Optional[Any]         =       image_processing(UpperCamelCase__			,							return_tensors="""pt"""     ).pixel_values
														self.assertEqual(
														    encoded_images.shape			,							(
														        self.image_processor_tester.batch_size,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.size["""height"""],
														        self.image_processor_tester.size["""width"""],
														    )			,							)




							@is_flaky()
							def 		lowerCamelCase_				(							self:				Dict     ):
														# Initialize image_processing
														lowerCamelCase__					:	int         =       self.image_processing_class(**self.image_processor_dict     )
														# create random PyTorch tensors
														lowerCamelCase__					:	Optional[int]         =       prepare_image_inputs(self.image_processor_tester			,							equal_resolution=UpperCamelCase__			,							torchify=UpperCamelCase__     )
														for image in image_inputs:
																					self.assertIsInstance(UpperCamelCase__			,							torch.Tensor     )

														# Test not batched input
														lowerCamelCase__					:	Union[str, Any]         =       image_processing(image_inputs[0]			,							return_tensors="""pt"""     ).pixel_values
														self.assertEqual(
														    encoded_images.shape			,							(
														        1,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.size["""height"""],
														        self.image_processor_tester.size["""width"""],
														    )			,							)

														# Test batched
														lowerCamelCase__					:	Tuple         =       image_processing(UpperCamelCase__			,							return_tensors="""pt"""     ).pixel_values
														self.assertEqual(
														    encoded_images.shape			,							(
														        self.image_processor_tester.batch_size,
														        self.image_processor_tester.num_channels,
														        self.image_processor_tester.size["""height"""],
														        self.image_processor_tester.size["""width"""],
														    )			,							)

