
"""simple docstring"""






import gc
import unittest

import torch
from parameterized import parameterized

from diffusers import AutoencoderKL
from diffusers.utils import floats_tensor, load_hf_numpy, require_torch_gpu, slow, torch_all_close, torch_device
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


enable_full_determinism()




class 				lowercase__(				_UpperCAmelCase		,						_UpperCAmelCase		,						unittest.TestCase   ):
	'''simple docstring'''

	UpperCamelCase				=  AutoencoderKL
	UpperCamelCase				=  """sample"""
	UpperCamelCase				=  1E-2





	@property
	def   __lowerCAmelCase (						self    :List[str]					)							->			Optional[Any]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Optional[int]				= 4
			SCREAMING_SNAKE_CASE       :     str				= 3
			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= (32, 32)

			SCREAMING_SNAKE_CASE       :     Any				= floats_tensor((batch_size, num_channels) + sizes					).to(lowerCamelCase_					)

			return {"sample": image}





	@property
	def   __lowerCAmelCase (						self    :Optional[int]					)							->			Union[str, Any]:







			'''simple docstring'''




			return (3, 32, 32)





	@property
	def   __lowerCAmelCase (						self    :List[Any]					)							->			List[str]:







			'''simple docstring'''




			return (3, 32, 32)





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







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     str				= {
			    '''block_out_channels''': [32, 64],
			    '''in_channels''': 3,
			    '''out_channels''': 3,
			    '''down_block_types''': ['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''],
			    '''up_block_types''': ['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''],
			    '''latent_channels''': 4,
			}
			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= self.dummy_input
			return init_dict, inputs_dict





	def   __lowerCAmelCase (						self    :Dict					)							->			str:







			'''simple docstring'''




			pass





	def   __lowerCAmelCase (						self    :str					)							->			str:







			'''simple docstring'''




			pass





	@unittest.skipIf(torch_device == '''mps'''		, '''Gradient checkpointing skipped on MPS'''					)
	def   __lowerCAmelCase (						self    :int					)							->			int:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE       :     Tuple				= self.prepare_init_args_and_inputs_for_common()
			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= self.model_class(**lowerCamelCase_					)
			model.to(lowerCamelCase_					)

			assert not model.is_gradient_checkpointing and model.training

			SCREAMING_SNAKE_CASE       :     List[str]				= model(**lowerCamelCase_					).sample
			# run the backwards pass on the model. For backwards pass, for simplicity purpose,
			# we won't calculate the loss and rather backprop on out.sum()
			model.zero_grad()

			SCREAMING_SNAKE_CASE       :     str				= torch.randn_like(lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= (out - labels).mean()
			loss.backward()

			# re-instantiate the model now enabling gradient checkpointing
			SCREAMING_SNAKE_CASE       :     List[Any]				= self.model_class(**lowerCamelCase_					)
			# clone model
			model_a.load_state_dict(model.state_dict()					)
			model_a.to(lowerCamelCase_					)
			model_a.enable_gradient_checkpointing()

			assert model_a.is_gradient_checkpointing and model_a.training

			SCREAMING_SNAKE_CASE       :     List[Any]				= model_a(**lowerCamelCase_					).sample
			# run the backwards pass on the model. For backwards pass, for simplicity purpose,
			# we won't calculate the loss and rather backprop on out.sum()
			model_a.zero_grad()
			SCREAMING_SNAKE_CASE       :     List[Any]				= (out_a - labels).mean()
			loss_a.backward()

			# compare the output and parameters gradients
			self.assertTrue((loss - loss_a).abs() < 1E-5					)
			SCREAMING_SNAKE_CASE       :     Dict				= dict(model.named_parameters()					)
			SCREAMING_SNAKE_CASE       :     int				= dict(model_a.named_parameters()					)
			for name, param in named_params.items():
					self.assertTrue(torch_all_close(param.grad.data		, named_params_a[name].grad.data		, atol=5E-5					)					)





	def   __lowerCAmelCase (						self    :Tuple					)							->			List[str]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE,					SCREAMING_SNAKE_CASE       :     str				= AutoencoderKL.from_pretrained('''fusing/autoencoder-kl-dummy'''		, output_loading_info=lowerCamelCase_					)
			self.assertIsNotNone(lowerCamelCase_					)
			self.assertEqual(len(loading_info['''missing_keys''']					)		, 0					)

			model.to(lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     Any				= model(**self.dummy_input					)

			assert image is not None, "Make sure output is not None"







	def   __lowerCAmelCase (						self    :int					)							->			List[str]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= AutoencoderKL.from_pretrained('''fusing/autoencoder-kl-dummy'''					)
			SCREAMING_SNAKE_CASE       :     str				= model.to(lowerCamelCase_					)
			model.eval()

			if torch_device == "mps":
					SCREAMING_SNAKE_CASE       :     List[Any]				= torch.manual_seed(0					)
			else:
					SCREAMING_SNAKE_CASE       :     int				= torch.Generator(device=lowerCamelCase_					).manual_seed(0					)

			SCREAMING_SNAKE_CASE       :     str				= torch.randn(
			    1		, model.config.in_channels		, model.config.sample_size		, model.config.sample_size		, generator=torch.manual_seed(0					)		, )
			SCREAMING_SNAKE_CASE       :     int				= image.to(lowerCamelCase_					)
			with torch.no_grad():
					SCREAMING_SNAKE_CASE       :     Union[str, Any]				= model(lowerCamelCase_		, sample_posterior=lowerCamelCase_		, generator=lowerCamelCase_					).sample

			SCREAMING_SNAKE_CASE       :     Optional[Any]				= output[0, -1, -3:, -3:].flatten().cpu()

			# Since the VAE Gaussian prior's generator is seeded on the appropriate device,
			# the expected output slices are not the same for CPU and GPU.
			if torch_device == "mps":
					SCREAMING_SNAKE_CASE       :     int				= torch.tensor(
					    [
					        -4.0078E-01,
					        -3.8323E-04,
					        -1.2681E-01,
					        -1.1462E-01,
					        2.0095E-01,
					        1.0893E-01,
					        -8.8247E-02,
					        -3.0361E-01,
					        -9.8644E-03,
					    ]					)
			elif torch_device == "cpu":
					SCREAMING_SNAKE_CASE       :     Optional[Any]				= torch.tensor(
					    [-0.1_3_5_2, 0.0_8_7_8, 0.0_4_1_9, -0.0_8_1_8, -0.1_0_6_9, 0.0_6_8_8, -0.1_4_5_8, -0.4_4_4_6, -0.0_0_2_6]					)
			else:
					SCREAMING_SNAKE_CASE       :     Tuple				= torch.tensor(
					    [-0.2_4_2_1, 0.4_6_4_2, 0.2_5_0_7, -0.0_4_3_8, 0.0_6_8_2, 0.3_1_6_0, -0.2_0_1_8, -0.0_7_2_7, 0.2_4_8_5]					)

			self.assertTrue(torch_all_close(lowerCamelCase_		, lowerCamelCase_		, rtol=1E-2					)					)





@slow
class 				lowercase__(				unittest.TestCase   ):
	'''simple docstring'''

	def   __lowerCAmelCase (						self    :Dict		, lowerCamelCase_    :str		, lowerCamelCase_    :Dict					)							->			Union[str, Any]:







			'''simple docstring'''




			return f"gaussian_noise_s={seed}_shape={'_'.join([str(lowerCamelCase_					) for s in shape]					)}.npy"





	def   __lowerCAmelCase (						self    :Union[str, Any]					)							->			Optional[int]:







			'''simple docstring'''




			super().tearDown()
			gc.collect()
			torch.cuda.empty_cache()





	def   __lowerCAmelCase (						self    :str		, lowerCamelCase_    :Optional[Any]=0		, lowerCamelCase_    :Optional[Any]=(4, 3, 5_12, 5_12)		, lowerCamelCase_    :Union[str, Any]=False					)							->			str:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Dict				= torch.floataa if fpaa else torch.floataa
			SCREAMING_SNAKE_CASE       :     List[str]				= torch.from_numpy(load_hf_numpy(self.get_file_format(lowerCamelCase_		, lowerCamelCase_					)					)					).to(lowerCamelCase_					).to(lowerCamelCase_					)
			return image





	def   __lowerCAmelCase (						self    :Tuple		, lowerCamelCase_    :str="CompVis/stable-diffusion-v1-4"		, lowerCamelCase_    :int=False					)							->			Union[str, Any]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     List[Any]				= '''fp16''' if fpaa else None
			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= torch.floataa if fpaa else torch.floataa

			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= AutoencoderKL.from_pretrained(
			    lowerCamelCase_		, subfolder='''vae'''		, torch_dtype=lowerCamelCase_		, revision=lowerCamelCase_		, )
			model.to(lowerCamelCase_					).eval()

			return model





	def   __lowerCAmelCase (						self    :Union[str, Any]		, lowerCamelCase_    :Tuple=0					)							->			Optional[Any]:







			'''simple docstring'''




			if torch_device == "mps":
					return torch.manual_seed(lowerCamelCase_					)
			return torch.Generator(device=lowerCamelCase_					).manual_seed(lowerCamelCase_					)





	@parameterized.expand(
	    [
	        # fmt: off
	        [33, [-0.1_6_0_3, 0.9_8_7_8, -0.0_4_9_5, -0.0_7_9_0, -0.2_7_0_9, 0.8_3_7_5, -0.2_0_6_0, -0.0_8_2_4], [-0.2_3_9_5, 0.0_0_9_8, 0.0_1_0_2, -0.0_7_0_9, -0.2_8_4_0, -0.0_2_7_4, -0.0_7_1_8, -0.1_8_2_4]],
	        [47, [-0.2_3_7_6, 0.1_1_6_8, 0.1_3_3_2, -0.4_8_4_0, -0.2_5_0_8, -0.0_7_9_1, -0.0_4_9_3, -0.4_0_8_9], [0.0_3_5_0, 0.0_8_4_7, 0.0_4_6_7, 0.0_3_4_4, -0.0_8_4_2, -0.0_5_4_7, -0.0_6_3_3, -0.1_1_3_1]],
	        # fmt: on
	    ]					)
	def   __lowerCAmelCase (						self    :Union[str, Any]		, lowerCamelCase_    :Dict		, lowerCamelCase_    :str		, lowerCamelCase_    :Optional[Any]					)							->			Union[str, Any]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     int				= self.get_sd_vae_model()
			SCREAMING_SNAKE_CASE       :     List[str]				= self.get_sd_image(lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     Optional[Any]				= self.get_generator(lowerCamelCase_					)

			with torch.no_grad():
					SCREAMING_SNAKE_CASE       :     str				= model(lowerCamelCase_		, generator=lowerCamelCase_		, sample_posterior=lowerCamelCase_					).sample

			assert sample.shape == image.shape

			SCREAMING_SNAKE_CASE       :     List[str]				= sample[-1, -2:, -2:, :2].flatten().float().cpu()
			SCREAMING_SNAKE_CASE       :     Optional[Any]				= torch.tensor(expected_slice_mps if torch_device == '''mps''' else expected_slice					)

			assert torch_all_close(lowerCamelCase_		, lowerCamelCase_		, atol=3E-3					)





	@parameterized.expand(
	    [
	        # fmt: off
	        [33, [-0.0_5_1_3, 0.0_2_8_9, 1.3_7_9_9, 0.2_1_6_6, -0.2_5_7_3, -0.0_8_7_1, 0.5_1_0_3, -0.0_9_9_9]],
	        [47, [-0.4_1_2_8, -0.1_3_2_0, -0.3_7_0_4, 0.1_9_6_5, -0.4_1_1_6, -0.2_3_3_2, -0.3_3_4_0, 0.2_2_4_7]],
	        # fmt: on
	    ]					)
	@require_torch_gpu
	def   __lowerCAmelCase (						self    :Optional[int]		, lowerCamelCase_    :str		, lowerCamelCase_    :List[Any]					)							->			Union[str, Any]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= self.get_sd_vae_model(fpaa=lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     List[str]				= self.get_sd_image(lowerCamelCase_		, fpaa=lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     int				= self.get_generator(lowerCamelCase_					)

			with torch.no_grad():
					SCREAMING_SNAKE_CASE       :     List[str]				= model(lowerCamelCase_		, generator=lowerCamelCase_		, sample_posterior=lowerCamelCase_					).sample

			assert sample.shape == image.shape

			SCREAMING_SNAKE_CASE       :     Tuple				= sample[-1, -2:, :2, -2:].flatten().float().cpu()
			SCREAMING_SNAKE_CASE       :     List[str]				= torch.tensor(lowerCamelCase_					)

			assert torch_all_close(lowerCamelCase_		, lowerCamelCase_		, atol=1E-2					)





	@parameterized.expand(
	    [
	        # fmt: off
	        [33, [-0.1_6_0_9, 0.9_8_6_6, -0.0_4_8_7, -0.0_7_7_7, -0.2_7_1_6, 0.8_3_6_8, -0.2_0_5_5, -0.0_8_1_4], [-0.2_3_9_5, 0.0_0_9_8, 0.0_1_0_2, -0.0_7_0_9, -0.2_8_4_0, -0.0_2_7_4, -0.0_7_1_8, -0.1_8_2_4]],
	        [47, [-0.2_3_7_7, 0.1_1_4_7, 0.1_3_3_3, -0.4_8_4_1, -0.2_5_0_6, -0.0_8_0_5, -0.0_4_9_1, -0.4_0_8_5], [0.0_3_5_0, 0.0_8_4_7, 0.0_4_6_7, 0.0_3_4_4, -0.0_8_4_2, -0.0_5_4_7, -0.0_6_3_3, -0.1_1_3_1]],
	        # fmt: on
	    ]					)
	def   __lowerCAmelCase (						self    :List[Any]		, lowerCamelCase_    :int		, lowerCamelCase_    :Optional[int]		, lowerCamelCase_    :int					)							->			Tuple:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     List[Any]				= self.get_sd_vae_model()
			SCREAMING_SNAKE_CASE       :     Any				= self.get_sd_image(lowerCamelCase_					)

			with torch.no_grad():
					SCREAMING_SNAKE_CASE       :     Dict				= model(lowerCamelCase_					).sample

			assert sample.shape == image.shape

			SCREAMING_SNAKE_CASE       :     Any				= sample[-1, -2:, -2:, :2].flatten().float().cpu()
			SCREAMING_SNAKE_CASE       :     Dict				= torch.tensor(expected_slice_mps if torch_device == '''mps''' else expected_slice					)

			assert torch_all_close(lowerCamelCase_		, lowerCamelCase_		, atol=3E-3					)





	@parameterized.expand(
	    [
	        # fmt: off
	        [13, [-0.2_0_5_1, -0.1_8_0_3, -0.2_3_1_1, -0.2_1_1_4, -0.3_2_9_2, -0.3_5_7_4, -0.2_9_5_3, -0.3_3_2_3]],
	        [37, [-0.2_6_3_2, -0.2_6_2_5, -0.2_1_9_9, -0.2_7_4_1, -0.4_5_3_9, -0.4_9_9_0, -0.3_7_2_0, -0.4_9_2_5]],
	        # fmt: on
	    ]					)
	@require_torch_gpu
	def   __lowerCAmelCase (						self    :Union[str, Any]		, lowerCamelCase_    :Optional[int]		, lowerCamelCase_    :Optional[Any]					)							->			str:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Tuple				= self.get_sd_vae_model()
			SCREAMING_SNAKE_CASE       :     Dict				= self.get_sd_image(lowerCamelCase_		, shape=(3, 4, 64, 64)					)

			with torch.no_grad():
					SCREAMING_SNAKE_CASE       :     Any				= model.decode(lowerCamelCase_					).sample

			assert list(sample.shape					) == [3, 3, 5_12, 5_12]

			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= sample[-1, -2:, :2, -2:].flatten().cpu()
			SCREAMING_SNAKE_CASE       :     Optional[Any]				= torch.tensor(lowerCamelCase_					)

			assert torch_all_close(lowerCamelCase_		, lowerCamelCase_		, atol=1E-3					)





	@parameterized.expand(
	    [
	        # fmt: off
	        [27, [-0.0_3_6_9, 0.0_2_0_7, -0.0_7_7_6, -0.0_6_8_2, -0.1_7_4_7, -0.1_9_3_0, -0.1_4_6_5, -0.2_0_3_9]],
	        [16, [-0.1_6_2_8, -0.2_1_3_4, -0.2_7_4_7, -0.2_6_4_2, -0.3_7_7_4, -0.4_4_0_4, -0.3_6_8_7, -0.4_2_7_7]],
	        # fmt: on
	    ]					)
	@require_torch_gpu
	def   __lowerCAmelCase (						self    :str		, lowerCamelCase_    :Any		, lowerCamelCase_    :Optional[int]					)							->			List[Any]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     int				= self.get_sd_vae_model(fpaa=lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     List[Any]				= self.get_sd_image(lowerCamelCase_		, shape=(3, 4, 64, 64)		, fpaa=lowerCamelCase_					)

			with torch.no_grad():
					SCREAMING_SNAKE_CASE       :     Union[str, Any]				= model.decode(lowerCamelCase_					).sample

			assert list(sample.shape					) == [3, 3, 5_12, 5_12]

			SCREAMING_SNAKE_CASE       :     str				= sample[-1, -2:, :2, -2:].flatten().float().cpu()
			SCREAMING_SNAKE_CASE       :     Any				= torch.tensor(lowerCamelCase_					)

			assert torch_all_close(lowerCamelCase_		, lowerCamelCase_		, atol=5E-3					)





	@parameterized.expand([(13,), (16,), (27,)]					)
	@require_torch_gpu
	@unittest.skipIf(not is_xformers_available()		, reason='''xformers is not required when using PyTorch 2.0.'''					)
	def   __lowerCAmelCase (						self    :Dict		, lowerCamelCase_    :Optional[Any]					)							->			List[str]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Tuple				= self.get_sd_vae_model(fpaa=lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     Optional[int]				= self.get_sd_image(lowerCamelCase_		, shape=(3, 4, 64, 64)		, fpaa=lowerCamelCase_					)

			with torch.no_grad():
					SCREAMING_SNAKE_CASE       :     List[str]				= model.decode(lowerCamelCase_					).sample

			model.enable_xformers_memory_efficient_attention()
			with torch.no_grad():
					SCREAMING_SNAKE_CASE       :     Optional[int]				= model.decode(lowerCamelCase_					).sample

			assert list(sample.shape					) == [3, 3, 5_12, 5_12]

			assert torch_all_close(lowerCamelCase_		, lowerCamelCase_		, atol=1E-1					)





	@parameterized.expand([(13,), (16,), (37,)]					)
	@require_torch_gpu
	@unittest.skipIf(not is_xformers_available()		, reason='''xformers is not required when using PyTorch 2.0.'''					)
	def   __lowerCAmelCase (						self    :Tuple		, lowerCamelCase_    :Optional[int]					)							->			int:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= self.get_sd_vae_model()
			SCREAMING_SNAKE_CASE       :     Dict				= self.get_sd_image(lowerCamelCase_		, shape=(3, 4, 64, 64)					)

			with torch.no_grad():
					SCREAMING_SNAKE_CASE       :     List[str]				= model.decode(lowerCamelCase_					).sample

			model.enable_xformers_memory_efficient_attention()
			with torch.no_grad():
					SCREAMING_SNAKE_CASE       :     str				= model.decode(lowerCamelCase_					).sample

			assert list(sample.shape					) == [3, 3, 5_12, 5_12]

			assert torch_all_close(lowerCamelCase_		, lowerCamelCase_		, atol=1E-2					)







	@parameterized.expand(
	    [
	        # fmt: off
	        [33, [-0.3_0_0_1, 0.0_9_1_8, -2.6_9_8_4, -3.9_7_2_0, -3.2_0_9_9, -5.0_3_5_3, 1.7_3_3_8, -0.2_0_6_5, 3.4_2_6_7]],
	        [47, [-1.5_0_3_0, -4.3_8_7_1, -6.0_3_5_5, -9.1_1_5_7, -1.6_6_6_1, -2.7_8_5_3, 2.1_6_0_7, -5.0_8_2_3, 2.5_6_3_3]],
	        # fmt: on
	    ]					)
	def   __lowerCAmelCase (						self    :Dict		, lowerCamelCase_    :List[Any]		, lowerCamelCase_    :Dict					)							->			int:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     int				= self.get_sd_vae_model()
			SCREAMING_SNAKE_CASE       :     List[Any]				= self.get_sd_image(lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     List[Any]				= self.get_generator(lowerCamelCase_					)

			with torch.no_grad():
					SCREAMING_SNAKE_CASE       :     Optional[int]				= model.encode(lowerCamelCase_					).latent_dist
					SCREAMING_SNAKE_CASE       :     Dict				= dist.sample(generator=lowerCamelCase_					)

			assert list(sample.shape					) == [image.shape[0], 4] + [i // 8 for i in image.shape[2:]]

			SCREAMING_SNAKE_CASE       :     Optional[Any]				= sample[0, -1, -3:, -3:].flatten().cpu()
			SCREAMING_SNAKE_CASE       :     Any				= torch.tensor(lowerCamelCase_					)

			SCREAMING_SNAKE_CASE       :     List[Any]				= 3E-3 if torch_device != '''mps''' else 1E-2
			assert torch_all_close(lowerCamelCase_		, lowerCamelCase_		, atol=lowerCamelCase_					)







"""simple docstring"""






import copy
import unittest

from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import (
					    MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
					    MODEL_FOR_QUESTION_ANSWERING_MAPPING,
					    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
					    MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
					    LayoutLMvaConfig,
					    LayoutLMvaForQuestionAnswering,
					    LayoutLMvaForSequenceClassification,
					    LayoutLMvaForTokenClassification,
					    LayoutLMvaModel,
					)
					from transformers.models.layoutlmva.modeling_layoutlmva import LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
					from PIL import Image

					from transformers import LayoutLMvaImageProcessor




class 				lowercase__:
	'''simple docstring'''

	def __init__(						self    :Tuple		, lowerCamelCase_    :Tuple		, lowerCamelCase_    :Union[str, Any]=2		, lowerCamelCase_    :Any=3		, lowerCamelCase_    :Union[str, Any]=4		, lowerCamelCase_    :List[str]=2		, lowerCamelCase_    :str=7		, lowerCamelCase_    :Tuple=True		, lowerCamelCase_    :Dict=True		, lowerCamelCase_    :int=True		, lowerCamelCase_    :List[Any]=True		, lowerCamelCase_    :Optional[Any]=99		, lowerCamelCase_    :Any=36		, lowerCamelCase_    :Any=3		, lowerCamelCase_    :str=4		, lowerCamelCase_    :Tuple=37		, lowerCamelCase_    :Optional[int]="gelu"		, lowerCamelCase_    :str=0.1		, lowerCamelCase_    :Optional[int]=0.1		, lowerCamelCase_    :Tuple=5_12		, lowerCamelCase_    :Optional[Any]=16		, lowerCamelCase_    :List[str]=2		, lowerCamelCase_    :Optional[int]=0.0_2		, lowerCamelCase_    :int=6		, lowerCamelCase_    :str=6		, lowerCamelCase_    :Optional[Any]=3		, lowerCamelCase_    :Union[str, Any]=4		, lowerCamelCase_    :List[Any]=None		, lowerCamelCase_    :Tuple=10_00		, )							->			Union[str, Any]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Dict				= parent
			SCREAMING_SNAKE_CASE       :     Optional[Any]				= batch_size
			SCREAMING_SNAKE_CASE       :     List[str]				= num_channels
			SCREAMING_SNAKE_CASE       :     str				= image_size
			SCREAMING_SNAKE_CASE       :     Optional[int]				= patch_size
			SCREAMING_SNAKE_CASE       :     Tuple				= text_seq_length
			SCREAMING_SNAKE_CASE       :     Optional[int]				= is_training
			SCREAMING_SNAKE_CASE       :     Dict				= use_input_mask
			SCREAMING_SNAKE_CASE       :     Any				= use_token_type_ids
			SCREAMING_SNAKE_CASE       :     List[Any]				= use_labels
			SCREAMING_SNAKE_CASE       :     List[Any]				= vocab_size
			SCREAMING_SNAKE_CASE       :     Optional[Any]				= hidden_size
			SCREAMING_SNAKE_CASE       :     Tuple				= num_hidden_layers
			SCREAMING_SNAKE_CASE       :     Optional[int]				= num_attention_heads
			SCREAMING_SNAKE_CASE       :     Optional[int]				= intermediate_size
			SCREAMING_SNAKE_CASE       :     List[str]				= hidden_act
			SCREAMING_SNAKE_CASE       :     Any				= hidden_dropout_prob
			SCREAMING_SNAKE_CASE       :     Optional[Any]				= attention_probs_dropout_prob
			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= max_position_embeddings
			SCREAMING_SNAKE_CASE       :     int				= type_vocab_size
			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= type_sequence_label_size
			SCREAMING_SNAKE_CASE       :     Optional[Any]				= initializer_range
			SCREAMING_SNAKE_CASE       :     Dict				= coordinate_size
			SCREAMING_SNAKE_CASE       :     List[Any]				= shape_size
			SCREAMING_SNAKE_CASE       :     Dict				= num_labels
			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= num_choices
			SCREAMING_SNAKE_CASE       :     List[str]				= scope
			SCREAMING_SNAKE_CASE       :     Optional[int]				= range_bbox

			# LayoutLMv3's sequence length equals the number of text tokens + number of patches + 1 (we add 1 for the CLS token)
			SCREAMING_SNAKE_CASE       :     str				= text_seq_length
			SCREAMING_SNAKE_CASE       :     int				= (image_size // patch_size) ** 2 + 1
			SCREAMING_SNAKE_CASE       :     Optional[Any]				= self.text_seq_length + self.image_seq_length





	def   __lowerCAmelCase (						self    :List[Any]					)							->			Optional[int]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Optional[int]				= ids_tensor([self.batch_size, self.text_seq_length]		, self.vocab_size					)

			SCREAMING_SNAKE_CASE       :     List[Any]				= ids_tensor([self.batch_size, self.text_seq_length, 4]		, self.range_bbox					)
			# Ensure that bbox is legal
			for i in range(bbox.shape[0]					):
					for j in range(bbox.shape[1]					):
							if bbox[i, j, 3] < bbox[i, j, 1]:
									SCREAMING_SNAKE_CASE       :     str				= bbox[i, j, 3]
									SCREAMING_SNAKE_CASE       :     List[str]				= bbox[i, j, 1]
									SCREAMING_SNAKE_CASE       :     Any				= t
							if bbox[i, j, 2] < bbox[i, j, 0]:
									SCREAMING_SNAKE_CASE       :     Any				= bbox[i, j, 2]
									SCREAMING_SNAKE_CASE       :     Any				= bbox[i, j, 0]
									SCREAMING_SNAKE_CASE       :     Optional[Any]				= t

			SCREAMING_SNAKE_CASE       :     Dict				= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

			SCREAMING_SNAKE_CASE       :     Optional[Any]				= None
			if self.use_input_mask:
					SCREAMING_SNAKE_CASE       :     Dict				= random_attention_mask([self.batch_size, self.text_seq_length]					)

			SCREAMING_SNAKE_CASE       :     Any				= None
			if self.use_token_type_ids:
					SCREAMING_SNAKE_CASE       :     Tuple				= ids_tensor([self.batch_size, self.text_seq_length]		, self.type_vocab_size					)

			SCREAMING_SNAKE_CASE       :     Tuple				= None
			SCREAMING_SNAKE_CASE       :     Optional[int]				= None
			if self.use_labels:
					SCREAMING_SNAKE_CASE       :     List[str]				= ids_tensor([self.batch_size]		, self.type_sequence_label_size					)
					SCREAMING_SNAKE_CASE       :     Dict				= ids_tensor([self.batch_size, self.text_seq_length]		, self.num_labels					)

			SCREAMING_SNAKE_CASE       :     List[str]				= LayoutLMvaConfig(
			    vocab_size=self.vocab_size		, hidden_size=self.hidden_size		, num_hidden_layers=self.num_hidden_layers		, num_attention_heads=self.num_attention_heads		, intermediate_size=self.intermediate_size		, hidden_act=self.hidden_act		, hidden_dropout_prob=self.hidden_dropout_prob		, attention_probs_dropout_prob=self.attention_probs_dropout_prob		, max_position_embeddings=self.max_position_embeddings		, type_vocab_size=self.type_vocab_size		, initializer_range=self.initializer_range		, coordinate_size=self.coordinate_size		, shape_size=self.shape_size		, input_size=self.image_size		, patch_size=self.patch_size		, )

			return config, input_ids, bbox, pixel_values, token_type_ids, input_mask, sequence_labels, token_labels





	def   __lowerCAmelCase (						self    :int		, lowerCamelCase_    :Union[str, Any]		, lowerCamelCase_    :int		, lowerCamelCase_    :str		, lowerCamelCase_    :int		, lowerCamelCase_    :Union[str, Any]		, lowerCamelCase_    :Any		, lowerCamelCase_    :Dict		, lowerCamelCase_    :Union[str, Any]					)							->			Any:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Dict				= LayoutLMvaModel(config=lowerCamelCase_					)
			model.to(lowerCamelCase_					)
			model.eval()

			# text + image
			SCREAMING_SNAKE_CASE       :     Optional[Any]				= model(lowerCamelCase_		, pixel_values=lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     List[str]				= model(
			    lowerCamelCase_		, bbox=lowerCamelCase_		, pixel_values=lowerCamelCase_		, attention_mask=lowerCamelCase_		, token_type_ids=lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     List[Any]				= model(lowerCamelCase_		, bbox=lowerCamelCase_		, pixel_values=lowerCamelCase_		, token_type_ids=lowerCamelCase_					)
			SCREAMING_SNAKE_CASE       :     Optional[Any]				= model(lowerCamelCase_		, bbox=lowerCamelCase_		, pixel_values=lowerCamelCase_					)

			self.parent.assertEqual(result.last_hidden_state.shape		, (self.batch_size, self.seq_length, self.hidden_size)					)

			# text only
			SCREAMING_SNAKE_CASE       :     List[str]				= model(lowerCamelCase_					)
			self.parent.assertEqual(
			    result.last_hidden_state.shape		, (self.batch_size, self.text_seq_length, self.hidden_size)					)

			# image only
			SCREAMING_SNAKE_CASE       :     List[str]				= model(pixel_values=lowerCamelCase_					)
			self.parent.assertEqual(
			    result.last_hidden_state.shape		, (self.batch_size, self.image_seq_length, self.hidden_size)					)





	def   __lowerCAmelCase (						self    :str		, lowerCamelCase_    :Union[str, Any]		, lowerCamelCase_    :Union[str, Any]		, lowerCamelCase_    :Tuple		, lowerCamelCase_    :List[str]		, lowerCamelCase_    :Any		, lowerCamelCase_    :int		, lowerCamelCase_    :List[Any]		, lowerCamelCase_    :List[str]					)							->			Tuple:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Optional[Any]				= self.num_labels
			SCREAMING_SNAKE_CASE       :     Dict				= LayoutLMvaForSequenceClassification(lowerCamelCase_					)
			model.to(lowerCamelCase_					)
			model.eval()
			SCREAMING_SNAKE_CASE       :     int				= model(
			    lowerCamelCase_		, bbox=lowerCamelCase_		, pixel_values=lowerCamelCase_		, attention_mask=lowerCamelCase_		, token_type_ids=lowerCamelCase_		, labels=lowerCamelCase_		, )
			self.parent.assertEqual(result.logits.shape		, (self.batch_size, self.num_labels)					)





	def   __lowerCAmelCase (						self    :Union[str, Any]		, lowerCamelCase_    :Tuple		, lowerCamelCase_    :Any		, lowerCamelCase_    :Optional[int]		, lowerCamelCase_    :Any		, lowerCamelCase_    :Optional[int]		, lowerCamelCase_    :int		, lowerCamelCase_    :str		, lowerCamelCase_    :Any					)							->			Union[str, Any]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     int				= self.num_labels
			SCREAMING_SNAKE_CASE       :     int				= LayoutLMvaForTokenClassification(config=lowerCamelCase_					)
			model.to(lowerCamelCase_					)
			model.eval()
			SCREAMING_SNAKE_CASE       :     Optional[Any]				= model(
			    lowerCamelCase_		, bbox=lowerCamelCase_		, pixel_values=lowerCamelCase_		, attention_mask=lowerCamelCase_		, token_type_ids=lowerCamelCase_		, labels=lowerCamelCase_		, )
			self.parent.assertEqual(result.logits.shape		, (self.batch_size, self.text_seq_length, self.num_labels)					)





	def   __lowerCAmelCase (						self    :Optional[Any]		, lowerCamelCase_    :Dict		, lowerCamelCase_    :Dict		, lowerCamelCase_    :Dict		, lowerCamelCase_    :int		, lowerCamelCase_    :List[str]		, lowerCamelCase_    :int		, lowerCamelCase_    :str		, lowerCamelCase_    :Optional[int]					)							->			Union[str, Any]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Optional[int]				= LayoutLMvaForQuestionAnswering(config=lowerCamelCase_					)
			model.to(lowerCamelCase_					)
			model.eval()
			SCREAMING_SNAKE_CASE       :     List[Any]				= model(
			    lowerCamelCase_		, bbox=lowerCamelCase_		, pixel_values=lowerCamelCase_		, attention_mask=lowerCamelCase_		, token_type_ids=lowerCamelCase_		, start_positions=lowerCamelCase_		, end_positions=lowerCamelCase_		, )
			self.parent.assertEqual(result.start_logits.shape		, (self.batch_size, self.seq_length)					)
			self.parent.assertEqual(result.end_logits.shape		, (self.batch_size, self.seq_length)					)







	def   __lowerCAmelCase (						self    :Optional[int]					)							->			Optional[Any]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Optional[int]				= self.prepare_config_and_inputs()
			(
			    (
			    SCREAMING_SNAKE_CASE
			),					(
			    SCREAMING_SNAKE_CASE
			),					(
			    SCREAMING_SNAKE_CASE
			),					(
			    SCREAMING_SNAKE_CASE
			),					(
			    SCREAMING_SNAKE_CASE
			),					(
			    SCREAMING_SNAKE_CASE
			),					(
			    SCREAMING_SNAKE_CASE
			),					(
			    SCREAMING_SNAKE_CASE
			),					
			)       :     Tuple				= config_and_inputs
			SCREAMING_SNAKE_CASE       :     Dict				= {
			    '''input_ids''': input_ids,
			    '''bbox''': bbox,
			    '''pixel_values''': pixel_values,
			    '''token_type_ids''': token_type_ids,
			    '''attention_mask''': input_mask,
			}
			return config, inputs_dict





@require_torch
class 				lowercase__(				_UpperCAmelCase		,						_UpperCAmelCase		,						unittest.TestCase   ):
	'''simple docstring'''

	UpperCamelCase				=  False
	UpperCamelCase				=  False
	UpperCamelCase				=  False

	UpperCamelCase				=  (
	    (
	        LayoutLMvaModel,
	        LayoutLMvaForSequenceClassification,
	        LayoutLMvaForTokenClassification,
	        LayoutLMvaForQuestionAnswering,
	    )
	    if is_torch_available()
	    else ()
	)
	UpperCamelCase				=  (
	    {"""document-question-answering""": LayoutLMvaForQuestionAnswering, """feature-extraction""": LayoutLMvaModel}
	    if is_torch_available()
	    else {}
	)





	def   __lowerCAmelCase (						self    :int		, lowerCamelCase_    :Union[str, Any]		, lowerCamelCase_    :str		, lowerCamelCase_    :str		, lowerCamelCase_    :Optional[Any]		, lowerCamelCase_    :List[str]					)							->			Union[str, Any]:







			'''simple docstring'''




			return True





	def   __lowerCAmelCase (						self    :List[str]					)							->			Union[str, Any]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     str				= LayoutLMvaModelTester(self					)
			SCREAMING_SNAKE_CASE       :     List[Any]				= ConfigTester(self		, config_class=lowerCamelCase_		, hidden_size=37					)





	def   __lowerCAmelCase (						self    :Any		, lowerCamelCase_    :Dict		, lowerCamelCase_    :List[str]		, lowerCamelCase_    :str=False					)							->			Tuple:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Optional[Any]				= copy.deepcopy(lowerCamelCase_					)
			if model_class in get_values(lowerCamelCase_					):
					SCREAMING_SNAKE_CASE       :     Tuple				= {
					    k: v.unsqueeze(1					).expand(-1		, self.model_tester.num_choices		, -1					).contiguous()
					    if isinstance(lowerCamelCase_		, torch.Tensor					) and v.ndim > 1
					    else v
					    for k, v in inputs_dict.items()
					}
			if return_labels:
					if model_class in get_values(lowerCamelCase_					):
							SCREAMING_SNAKE_CASE       :     Union[str, Any]				= torch.ones(self.model_tester.batch_size		, dtype=torch.long		, device=lowerCamelCase_					)
					elif model_class in get_values(lowerCamelCase_					):
							SCREAMING_SNAKE_CASE       :     List[Any]				= torch.zeros(
							    self.model_tester.batch_size		, dtype=torch.long		, device=lowerCamelCase_					)
							SCREAMING_SNAKE_CASE       :     Any				= torch.zeros(
							    self.model_tester.batch_size		, dtype=torch.long		, device=lowerCamelCase_					)
					elif model_class in [
					    *get_values(lowerCamelCase_					),
					]:
							SCREAMING_SNAKE_CASE       :     str				= torch.zeros(
							    self.model_tester.batch_size		, dtype=torch.long		, device=lowerCamelCase_					)
					elif model_class in [
					    *get_values(lowerCamelCase_					),
					]:
							SCREAMING_SNAKE_CASE       :     Dict				= torch.zeros(
							    (self.model_tester.batch_size, self.model_tester.text_seq_length)		, dtype=torch.long		, device=lowerCamelCase_		, )

			return inputs_dict





	def   __lowerCAmelCase (						self    :Optional[Any]					)							->			Optional[int]:







			'''simple docstring'''




			self.config_tester.run_common_tests()





	def   __lowerCAmelCase (						self    :str					)							->			List[Any]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     int				= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*lowerCamelCase_					)





	def   __lowerCAmelCase (						self    :Any					)							->			List[str]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     int				= self.model_tester.prepare_config_and_inputs()
			for type in ["absolute", "relative_key", "relative_key_query"]:
					SCREAMING_SNAKE_CASE       :     str				= type
					self.model_tester.create_and_check_model(*lowerCamelCase_					)





	def   __lowerCAmelCase (						self    :Union[str, Any]					)							->			str:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     List[Any]				= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase_					)





	def   __lowerCAmelCase (						self    :str					)							->			Any:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Optional[Any]				= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_					)





	def   __lowerCAmelCase (						self    :List[str]					)							->			Union[str, Any]:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     int				= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_question_answering(*lowerCamelCase_					)







	@slow
	def   __lowerCAmelCase (						self    :int					)							->			Union[str, Any]:







			'''simple docstring'''




			for model_name in LAYOUTLMV3_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					SCREAMING_SNAKE_CASE       :     Optional[int]				= LayoutLMvaModel.from_pretrained(lowerCamelCase_					)
					self.assertIsNotNone(lowerCamelCase_					)





def __A						(   )->							Optional[int]:




		'''simple docstring'''






		SCREAMING_SNAKE_CASE       :     Tuple				= Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''     )
		return image




@require_torch
class 				lowercase__(				unittest.TestCase   ):
	'''simple docstring'''

	@cached_property
	def   __lowerCAmelCase (						self    :str					)							->			int:







			'''simple docstring'''




			return LayoutLMvaImageProcessor(apply_ocr=lowerCamelCase_					) if is_vision_available() else None







	@slow
	def   __lowerCAmelCase (						self    :Optional[Any]					)							->			Dict:







			'''simple docstring'''




			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= LayoutLMvaModel.from_pretrained('''microsoft/layoutlmv3-base'''					).to(lowerCamelCase_					)

			SCREAMING_SNAKE_CASE       :     Any				= self.default_image_processor
			SCREAMING_SNAKE_CASE       :     List[Any]				= prepare_img()
			SCREAMING_SNAKE_CASE       :     Tuple				= image_processor(images=lowerCamelCase_		, return_tensors='''pt'''					).pixel_values.to(lowerCamelCase_					)

			SCREAMING_SNAKE_CASE       :     Optional[Any]				= torch.tensor([[1, 2]]					)
			SCREAMING_SNAKE_CASE       :     Dict				= torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]]					).unsqueeze(0					)

			# forward pass
			SCREAMING_SNAKE_CASE       :     Tuple				= model(
			    input_ids=input_ids.to(lowerCamelCase_					)		, bbox=bbox.to(lowerCamelCase_					)		, pixel_values=pixel_values.to(lowerCamelCase_					)		, )

			# verify the logits
			SCREAMING_SNAKE_CASE       :     Union[str, Any]				= torch.Size((1, 1_99, 7_68)					)
			self.assertEqual(outputs.last_hidden_state.shape		, lowerCamelCase_					)

			SCREAMING_SNAKE_CASE       :     List[Any]				= torch.tensor(
			    [[-0.0_5_2_9, 0.3_6_1_8, 0.1_6_3_2], [-0.1_5_8_7, -0.1_6_6_7, -0.0_4_0_0], [-0.1_5_5_7, -0.1_6_7_1, -0.0_5_0_5]]					).to(lowerCamelCase_					)

			self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3]		, lowerCamelCase_		, atol=1E-4					)					)





