


"""simple docstring"""






import json
import os
import tempfile

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


if is_torch_available():
	import numpy as np
	import torch

if is_vision_available():
	from PIL import Image


def __lowercase  (   lowerCamelCase_	:   Optional[int]    ,       lowerCamelCase_	:   int=False    ,       lowerCamelCase_	:   Dict=False    ,       lowerCamelCase_	:   int=False	):
				assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

				SCREAMING_SNAKE_CASE__  				=					[]
				for i in range(image_processor_tester.batch_size	):
								if equal_resolution:
												SCREAMING_SNAKE_CASE__  				=					SCREAMING_SNAKE_CASE__  				=					image_processor_tester.max_resolution
								else:
												# To avoid getting image width/height 0
												SCREAMING_SNAKE_CASE__  				=					image_processor_tester.min_resolution
												if getattr(_SCREAMING_SNAKE_CASE    ,       "size_divisor"    ,       _SCREAMING_SNAKE_CASE	):
																# If `size_divisor` is defined, the image needs to have width/size >= `size_divisor`
																SCREAMING_SNAKE_CASE__  				=					max(image_processor_tester.size_divisor    ,       _SCREAMING_SNAKE_CASE	)
												SCREAMING_SNAKE_CASE__				,   SCREAMING_SNAKE_CASE__  				=					np.random.choice(np.arange(_SCREAMING_SNAKE_CASE    ,       image_processor_tester.max_resolution	)    ,       2	)
								image_inputs.append(
								    np.random.randint(255    ,       size=(image_processor_tester.num_channels, width, height)    ,       dtype=np.uinta	)	)

				if not numpify and not torchify:
								# PIL expects the channel dimension as last dimension
								SCREAMING_SNAKE_CASE__  				=					[Image.fromarray(np.moveaxis(_SCREAMING_SNAKE_CASE    ,       0    ,       -1	)	) for image in image_inputs]

				if torchify:
								SCREAMING_SNAKE_CASE__  				=					[torch.from_numpy(_SCREAMING_SNAKE_CASE	) for image in image_inputs]

				return image_inputs


def __lowercase  (   lowerCamelCase_	:   List[Any]    ,       lowerCamelCase_	:   Dict=10    ,       lowerCamelCase_	:   str=10    ,       lowerCamelCase_	:   str=False    ,       lowerCamelCase_	:   Optional[int]=False	):
				SCREAMING_SNAKE_CASE__  				=					[]
				for i in range(image_processor_tester.num_frames	):
								video.append(np.random.randint(255    ,       size=(image_processor_tester.num_channels, width, height)    ,       dtype=np.uinta	)	)

				if not numpify and not torchify:
								# PIL expects the channel dimension as last dimension
								SCREAMING_SNAKE_CASE__  				=					[Image.fromarray(np.moveaxis(_SCREAMING_SNAKE_CASE    ,       0    ,       -1	)	) for frame in video]

				if torchify:
								SCREAMING_SNAKE_CASE__  				=					[torch.from_numpy(_SCREAMING_SNAKE_CASE	) for frame in video]

				return video


def __lowercase  (   lowerCamelCase_	:   Tuple    ,       lowerCamelCase_	:   int=False    ,       lowerCamelCase_	:   Optional[int]=False    ,       lowerCamelCase_	:   str=False	):
				assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

				SCREAMING_SNAKE_CASE__  				=					[]
				for i in range(image_processor_tester.batch_size	):
								if equal_resolution:
												SCREAMING_SNAKE_CASE__  				=					SCREAMING_SNAKE_CASE__  				=					image_processor_tester.max_resolution
								else:
												SCREAMING_SNAKE_CASE__				,   SCREAMING_SNAKE_CASE__  				=					np.random.choice(
												    np.arange(image_processor_tester.min_resolution    ,       image_processor_tester.max_resolution	)    ,       2	)
												SCREAMING_SNAKE_CASE__  				=					prepare_video(
												    image_processor_tester=_SCREAMING_SNAKE_CASE    ,       width=_SCREAMING_SNAKE_CASE    ,       height=_SCREAMING_SNAKE_CASE    ,       numpify=_SCREAMING_SNAKE_CASE    ,       torchify=_SCREAMING_SNAKE_CASE    ,       )
								video_inputs.append(_SCREAMING_SNAKE_CASE	)

				return video_inputs


class       lowerCamelCase_		:





				"""simple docstring"""







				_lowerCAmelCase   :   Union[str, Any]   				=       None



				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					self.image_processing_class(**self.image_processor_dict  )
								SCREAMING_SNAKE_CASE__  				=					json.loads(image_processor.to_json_string()  )
								for key, value in self.image_processor_dict.items():
												self.assertEqual(obj[key]							,		UpperCAmelCase_  )



				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					self.image_processing_class(**self.image_processor_dict  )

								with tempfile.TemporaryDirectory() as tmpdirname:
												SCREAMING_SNAKE_CASE__  				=					os.path.join(UpperCAmelCase_							,		"image_processor.json"  )
												image_processor_first.to_json_file(UpperCAmelCase_  )
												SCREAMING_SNAKE_CASE__  				=					self.image_processing_class.from_json_file(UpperCAmelCase_  )

								self.assertEqual(image_processor_second.to_dict()							,		image_processor_first.to_dict()  )



				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					self.image_processing_class(**self.image_processor_dict  )

								with tempfile.TemporaryDirectory() as tmpdirname:
												SCREAMING_SNAKE_CASE__  				=					image_processor_first.save_pretrained(UpperCAmelCase_  )[0]
												check_json_file_has_correct_format(UpperCAmelCase_  )
												SCREAMING_SNAKE_CASE__  				=					self.image_processing_class.from_pretrained(UpperCAmelCase_  )

								self.assertEqual(image_processor_second.to_dict()							,		image_processor_first.to_dict()  )



				def 	lowerCAmelCase__		(    self  ):
								SCREAMING_SNAKE_CASE__  				=					self.image_processing_class()
								self.assertIsNotNone(UpperCAmelCase_  )






				@require_torch
				@require_vision
				def 	lowerCAmelCase__		(    self  ):



								if self.test_cast_dtype is not None:
												# Initialize image_processor
												SCREAMING_SNAKE_CASE__  				=					self.image_processing_class(**self.image_processor_dict  )

												# create random PyTorch tensors
												SCREAMING_SNAKE_CASE__  				=					prepare_image_inputs(self.image_processor_tester							,		equal_resolution=UpperCAmelCase_							,		torchify=UpperCAmelCase_  )

												SCREAMING_SNAKE_CASE__  				=					image_processor(UpperCAmelCase_							,		return_tensors="pt"  )
												# for layoutLM compatiblity
												self.assertEqual(encoding.pixel_values.device							,		torch.device("cpu"  )  )
												self.assertEqual(encoding.pixel_values.dtype							,		torch.floataa  )

												SCREAMING_SNAKE_CASE__  				=					image_processor(UpperCAmelCase_							,		return_tensors="pt"  ).to(torch.floataa  )
												self.assertEqual(encoding.pixel_values.device							,		torch.device("cpu"  )  )
												self.assertEqual(encoding.pixel_values.dtype							,		torch.floataa  )

												SCREAMING_SNAKE_CASE__  				=					image_processor(UpperCAmelCase_							,		return_tensors="pt"  ).to("cpu"							,		torch.bfloataa  )
												self.assertEqual(encoding.pixel_values.device							,		torch.device("cpu"  )  )
												self.assertEqual(encoding.pixel_values.dtype							,		torch.bfloataa  )

												with self.assertRaises(UpperCAmelCase_  ):
																SCREAMING_SNAKE_CASE__  				=					image_processor(UpperCAmelCase_							,		return_tensors="pt"  ).to(torch.bfloataa							,		"cpu"  )

												# Try with text + image feature
												SCREAMING_SNAKE_CASE__  				=					image_processor(UpperCAmelCase_							,		return_tensors="pt"  )
												encoding.update({"input_ids": torch.LongTensor([[1, 2, 3], [4, 5, 6]]  )}  )
												SCREAMING_SNAKE_CASE__  				=					encoding.to(torch.floataa  )

												self.assertEqual(encoding.pixel_values.device							,		torch.device("cpu"  )  )
												self.assertEqual(encoding.pixel_values.dtype							,		torch.floataa  )
												self.assertEqual(encoding.input_ids.dtype							,		torch.long  )




"""simple docstring"""
import torch
import torch.nn as nn
from transformers.modeling_utils import ModuleUtilsMixin
from transformers.models.ta.modeling_ta import (
    TaBlock,
    TaConfig,
    TaLayerNorm,
)

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





class   __A  (snake_case__    ,		snake_case__    ,		snake_case__):





							'''simple docstring'''

							@register_to_config
							def __init__(					self				:   List[str]		,							UpperCAmelCase_				:   int		,							UpperCAmelCase_				:   int		,							UpperCAmelCase_				:   int		,							UpperCAmelCase_				:   float		,							UpperCAmelCase_				:   int		,							UpperCAmelCase_				:   int		,							UpperCAmelCase_				:   int		,							UpperCAmelCase_				:   int		,							UpperCAmelCase_				:   str		,							UpperCAmelCase_				:   bool = False		,							)       ->Dict:



														"""simple docstring"""



														super().__init__()

														snake_case_     							=		nn.Linear(UpperCAmelCase_		,							UpperCAmelCase_		,							bias=UpperCAmelCase_							)

														snake_case_     							=		nn.Embedding(UpperCAmelCase_		,							UpperCAmelCase_							)
														snake_case_     							=		False

														snake_case_     							=		nn.Dropout(p=UpperCAmelCase_							)

														snake_case_     							=		TaConfig(
														    d_model=UpperCAmelCase_		,							num_heads=UpperCAmelCase_		,							d_kv=UpperCAmelCase_		,							d_ff=UpperCAmelCase_		,							feed_forward_proj=UpperCAmelCase_		,							dropout_rate=UpperCAmelCase_		,							is_decoder=UpperCAmelCase_		,							is_encoder_decoder=UpperCAmelCase_		,							)
														snake_case_     							=		nn.ModuleList()
														for lyr_num in range(UpperCAmelCase_							):
																					snake_case_     							=		TaBlock(UpperCAmelCase_							)
																					self.encoders.append(UpperCAmelCase_							)

														snake_case_     							=		TaLayerNorm(UpperCAmelCase_							)
														snake_case_     							=		nn.Dropout(p=UpperCAmelCase_							)



							def 		lowerCAmelCase							(					self				:   Optional[Any]		,							UpperCAmelCase_				:   Any		,							UpperCAmelCase_				:   List[str]							)       ->List[Any]:



														"""simple docstring"""



														snake_case_     							=		self.input_proj(UpperCAmelCase_							)

														# terminal relative positional encodings
														snake_case_     							=		encoder_inputs.shape[1]
														snake_case_     							=		torch.arange(UpperCAmelCase_		,							device=encoder_inputs.device							)

														snake_case_     							=		encoder_inputs_mask.sum(-1							)
														snake_case_     							=		torch.roll(input_positions.unsqueeze(0							)		,							tuple(seq_lens.tolist()							)		,							dims=0							)
														x += self.position_encoding(UpperCAmelCase_							)

														snake_case_     							=		self.dropout_pre(UpperCAmelCase_							)

														# inverted the attention mask
														snake_case_     							=		encoder_inputs.size()
														snake_case_     							=		self.get_extended_attention_mask(UpperCAmelCase_		,							UpperCAmelCase_							)

														for lyr in self.encoders:
																					snake_case_     							=		lyr(UpperCAmelCase_		,							UpperCAmelCase_							)[0]
														snake_case_     							=		self.layer_norm(UpperCAmelCase_							)

														return self.dropout_post(UpperCAmelCase_							), encoder_inputs_mask



