



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     __SCREAMING_SNAKE_CASE					(   __lowerCAmelCase		,					__lowerCAmelCase		,					unittest.TestCase):
						_SCREAMING_SNAKE_CASE	:       int   =					AutoencoderKL
						_SCREAMING_SNAKE_CASE	:       Union[str, Any]   =					'''sample'''
						_SCREAMING_SNAKE_CASE	:       int   =					1E-2






						@property
						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						4
												lowerCAmelCase__    						=						3
												lowerCAmelCase__    						=						(32, 32)

												lowerCAmelCase__    						=						floats_tensor((batch_size, num_channels) + sizes   ).to(lowerCAmelCase_   )

												return {"sample": image}






						@property
						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												return (3, 32, 32)






						@property
						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												return (3, 32, 32)






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						{
												    'block_out_channels': [32, 64],
												    'in_channels': 3,
												    'out_channels': 3,
												    'down_block_types': ['DownEncoderBlock2D', 'DownEncoderBlock2D'],
												    'up_block_types': ['UpDecoderBlock2D', 'UpDecoderBlock2D'],
												    'latent_channels': 4,
												}
												lowerCAmelCase__    						=						self.dummy_input
												return init_dict, inputs_dict






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												pass






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												pass






						@unittest.skipIf(torch_device == 'mps'     ,						'Gradient checkpointing skipped on MPS'   )
						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												# enable deterministic behavior for gradient checkpointing
												lowerCAmelCase__						,       lowerCAmelCase__    						=						self.prepare_init_args_and_inputs_for_common()
												lowerCAmelCase__    						=						self.model_class(**lowerCAmelCase_   )
												model.to(lowerCAmelCase_   )

												assert not model.is_gradient_checkpointing and model.training

												lowerCAmelCase__    						=						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()

												lowerCAmelCase__    						=						torch.randn_like(lowerCAmelCase_   )
												lowerCAmelCase__    						=						(out - labels).mean()
												loss.backward()

												# re-instantiate the model now enabling gradient checkpointing
												lowerCAmelCase__    						=						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

												lowerCAmelCase__    						=						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()
												lowerCAmelCase__    						=						(out_a - labels).mean()
												loss_a.backward()

												# compare the output and parameters gradients
												self.assertTrue((loss - loss_a).abs() < 1E-5   )
												lowerCAmelCase__    						=						dict(model.named_parameters()   )
												lowerCAmelCase__    						=						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       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__						,       lowerCAmelCase__    						=						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_   )
												lowerCAmelCase__    						=						model(**self.dummy_input   )

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







						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						AutoencoderKL.from_pretrained('fusing/autoencoder-kl-dummy'   )
												lowerCAmelCase__    						=						model.to(lowerCAmelCase_   )
												model.eval()

												if torch_device == "mps":
																		lowerCAmelCase__    						=						torch.manual_seed(0   )
												else:
																		lowerCAmelCase__    						=						torch.Generator(device=lowerCAmelCase_   ).manual_seed(0   )

												lowerCAmelCase__    						=						torch.randn(
												    1     ,						model.config.in_channels     ,						model.config.sample_size     ,						model.config.sample_size     ,						generator=torch.manual_seed(0   )     ,						)
												lowerCAmelCase__    						=						image.to(lowerCAmelCase_   )
												with torch.no_grad():
																		lowerCAmelCase__    						=						model(lowerCAmelCase_     ,						sample_posterior=lowerCAmelCase_     ,						generator=lowerCAmelCase_   ).sample

												lowerCAmelCase__    						=						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":
																		lowerCAmelCase__    						=						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":
																		lowerCAmelCase__    						=						torch.tensor(
																		    [-0.13_52, 0.08_78, 0.04_19, -0.08_18, -0.10_69, 0.06_88, -0.14_58, -0.44_46, -0.00_26]   )
												else:
																		lowerCAmelCase__    						=						torch.tensor(
																		    [-0.24_21, 0.46_42, 0.25_07, -0.04_38, 0.06_82, 0.31_60, -0.20_18, -0.07_27, 0.24_85]   )

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




@slow
class     __SCREAMING_SNAKE_CASE					(   unittest.TestCase):






						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase   ):






												"""simple docstring"""
												return F"gaussian_noise_s={seed}_shape={'_'.join([str(lowerCAmelCase_   ) for s in shape]   )}.npy"






						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												# clean up the VRAM after each test
												super().tearDown()
												gc.collect()
												torch.cuda.empty_cache()






						def       UpperCamelCase__      (	self     ,						_UpperCamelCase=0     ,						_UpperCamelCase=(4, 3, 5_12, 5_12)     ,						_UpperCamelCase=False   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						torch.floataa if fpaa else torch.floataa
												lowerCAmelCase__    						=						torch.from_numpy(load_hf_numpy(self.get_file_format(lowerCAmelCase_     ,						lowerCAmelCase_   )   )   ).to(lowerCAmelCase_   ).to(lowerCAmelCase_   )
												return image






						def       UpperCamelCase__      (	self     ,						_UpperCamelCase="CompVis/stable-diffusion-v1-4"     ,						_UpperCamelCase=False   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						'fp16' if fpaa else None
												lowerCAmelCase__    						=						torch.floataa if fpaa else torch.floataa

												lowerCAmelCase__    						=						AutoencoderKL.from_pretrained(
												    lowerCAmelCase_     ,						subfolder='vae'     ,						torch_dtype=lowerCAmelCase_     ,						revision=lowerCAmelCase_     ,						)
												model.to(lowerCAmelCase_   ).eval()

												return model






						def       UpperCamelCase__      (	self     ,						_UpperCamelCase=0   ):






												"""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.16_03, 0.98_78, -0.04_95, -0.07_90, -0.27_09, 0.83_75, -0.20_60, -0.08_24], [-0.23_95, 0.00_98, 0.01_02, -0.07_09, -0.28_40, -0.02_74, -0.07_18, -0.18_24]],
						        [47, [-0.23_76, 0.11_68, 0.13_32, -0.48_40, -0.25_08, -0.07_91, -0.04_93, -0.40_89], [0.03_50, 0.08_47, 0.04_67, 0.03_44, -0.08_42, -0.05_47, -0.06_33, -0.11_31]],
						        # fmt: on
						    ]   )
						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						self.get_sd_vae_model()
												lowerCAmelCase__    						=						self.get_sd_image(lowerCAmelCase_   )
												lowerCAmelCase__    						=						self.get_generator(lowerCAmelCase_   )

												with torch.no_grad():
																		lowerCAmelCase__    						=						model(lowerCAmelCase_     ,						generator=lowerCAmelCase_     ,						sample_posterior=lowerCAmelCase_   ).sample

												assert sample.shape == image.shape

												lowerCAmelCase__    						=						sample[-1, -2:, -2:, :2].flatten().float().cpu()
												lowerCAmelCase__    						=						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.05_13, 0.02_89, 1.37_99, 0.21_66, -0.25_73, -0.08_71, 0.51_03, -0.09_99]],
						        [47, [-0.41_28, -0.13_20, -0.37_04, 0.19_65, -0.41_16, -0.23_32, -0.33_40, 0.22_47]],
						        # fmt: on
						    ]   )
						@require_torch_gpu
						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						self.get_sd_vae_model(fpaa=lowerCAmelCase_   )
												lowerCAmelCase__    						=						self.get_sd_image(lowerCAmelCase_     ,						fpaa=lowerCAmelCase_   )
												lowerCAmelCase__    						=						self.get_generator(lowerCAmelCase_   )

												with torch.no_grad():
																		lowerCAmelCase__    						=						model(lowerCAmelCase_     ,						generator=lowerCAmelCase_     ,						sample_posterior=lowerCAmelCase_   ).sample

												assert sample.shape == image.shape

												lowerCAmelCase__    						=						sample[-1, -2:, :2, -2:].flatten().float().cpu()
												lowerCAmelCase__    						=						torch.tensor(lowerCAmelCase_   )

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






						@parameterized.expand(
						    [
						        # fmt: off
						        [33, [-0.16_09, 0.98_66, -0.04_87, -0.07_77, -0.27_16, 0.83_68, -0.20_55, -0.08_14], [-0.23_95, 0.00_98, 0.01_02, -0.07_09, -0.28_40, -0.02_74, -0.07_18, -0.18_24]],
						        [47, [-0.23_77, 0.11_47, 0.13_33, -0.48_41, -0.25_06, -0.08_05, -0.04_91, -0.40_85], [0.03_50, 0.08_47, 0.04_67, 0.03_44, -0.08_42, -0.05_47, -0.06_33, -0.11_31]],
						        # fmt: on
						    ]   )
						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						self.get_sd_vae_model()
												lowerCAmelCase__    						=						self.get_sd_image(lowerCAmelCase_   )

												with torch.no_grad():
																		lowerCAmelCase__    						=						model(lowerCAmelCase_   ).sample

												assert sample.shape == image.shape

												lowerCAmelCase__    						=						sample[-1, -2:, -2:, :2].flatten().float().cpu()
												lowerCAmelCase__    						=						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.20_51, -0.18_03, -0.23_11, -0.21_14, -0.32_92, -0.35_74, -0.29_53, -0.33_23]],
						        [37, [-0.26_32, -0.26_25, -0.21_99, -0.27_41, -0.45_39, -0.49_90, -0.37_20, -0.49_25]],
						        # fmt: on
						    ]   )
						@require_torch_gpu
						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						self.get_sd_vae_model()
												lowerCAmelCase__    						=						self.get_sd_image(lowerCAmelCase_     ,						shape=(3, 4, 64, 64)   )

												with torch.no_grad():
																		lowerCAmelCase__    						=						model.decode(lowerCAmelCase_   ).sample

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

												lowerCAmelCase__    						=						sample[-1, -2:, :2, -2:].flatten().cpu()
												lowerCAmelCase__    						=						torch.tensor(lowerCAmelCase_   )

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






						@parameterized.expand(
						    [
						        # fmt: off
						        [27, [-0.03_69, 0.02_07, -0.07_76, -0.06_82, -0.17_47, -0.19_30, -0.14_65, -0.20_39]],
						        [16, [-0.16_28, -0.21_34, -0.27_47, -0.26_42, -0.37_74, -0.44_04, -0.36_87, -0.42_77]],
						        # fmt: on
						    ]   )
						@require_torch_gpu
						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						self.get_sd_vae_model(fpaa=lowerCAmelCase_   )
												lowerCAmelCase__    						=						self.get_sd_image(lowerCAmelCase_     ,						shape=(3, 4, 64, 64)     ,						fpaa=lowerCAmelCase_   )

												with torch.no_grad():
																		lowerCAmelCase__    						=						model.decode(lowerCAmelCase_   ).sample

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

												lowerCAmelCase__    						=						sample[-1, -2:, :2, -2:].flatten().float().cpu()
												lowerCAmelCase__    						=						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       UpperCamelCase__      (	self     ,						_UpperCamelCase   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						self.get_sd_vae_model(fpaa=lowerCAmelCase_   )
												lowerCAmelCase__    						=						self.get_sd_image(lowerCAmelCase_     ,						shape=(3, 4, 64, 64)     ,						fpaa=lowerCAmelCase_   )

												with torch.no_grad():
																		lowerCAmelCase__    						=						model.decode(lowerCAmelCase_   ).sample

												model.enable_xformers_memory_efficient_attention()
												with torch.no_grad():
																		lowerCAmelCase__    						=						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       UpperCamelCase__      (	self     ,						_UpperCamelCase   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						self.get_sd_vae_model()
												lowerCAmelCase__    						=						self.get_sd_image(lowerCAmelCase_     ,						shape=(3, 4, 64, 64)   )

												with torch.no_grad():
																		lowerCAmelCase__    						=						model.decode(lowerCAmelCase_   ).sample

												model.enable_xformers_memory_efficient_attention()
												with torch.no_grad():
																		lowerCAmelCase__    						=						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.30_01, 0.09_18, -2.69_84, -3.97_20, -3.20_99, -5.03_53, 1.73_38, -0.20_65, 3.42_67]],
						        [47, [-1.50_30, -4.38_71, -6.03_55, -9.11_57, -1.66_61, -2.78_53, 2.16_07, -5.08_23, 2.56_33]],
						        # fmt: on
						    ]   )
						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						self.get_sd_vae_model()
												lowerCAmelCase__    						=						self.get_sd_image(lowerCAmelCase_   )
												lowerCAmelCase__    						=						self.get_generator(lowerCAmelCase_   )

												with torch.no_grad():
																		lowerCAmelCase__    						=						model.encode(lowerCAmelCase_   ).latent_dist
																		lowerCAmelCase__    						=						dist.sample(generator=lowerCAmelCase_   )

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

												lowerCAmelCase__    						=						sample[0, -1, -3:, -3:].flatten().cpu()
												lowerCAmelCase__    						=						torch.tensor(lowerCAmelCase_   )

												lowerCAmelCase__    						=						3E-3 if torch_device != 'mps' else 1E-2
												assert torch_all_close(lowerCAmelCase_     ,						lowerCAmelCase_     ,						atol=lowerCAmelCase_   )













import unittest

from transformers import MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING, AutoTokenizer, is_vision_available
from transformers.pipelines import pipeline
from transformers.pipelines.document_question_answering import apply_tesseract
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_detectrona,
    require_pytesseract,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
					from PIL import Image

					from transformers.image_utils import load_image
else:


					class     __SCREAMING_SNAKE_CASE					:






											@staticmethod
											def       UpperCamelCase__      (	*_UpperCamelCase     ,						**_UpperCamelCase   ):






																	"""simple docstring"""
																	pass







					def 	_UpperCamelCase   (    UpperCamelCase_       :					Tuple		)  ->   Any:

											"""simple docstring"""



											return None


# This is a pinned image from a specific revision of a document question answering space, hosted by HuggingFace,
# so we can expect it to be available.
__snake_case    :    List[str]         =     (
    """https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png"""
)


@is_pipeline_test
@require_torch
@require_vision
class     __SCREAMING_SNAKE_CASE					(   unittest.TestCase):
						_SCREAMING_SNAKE_CASE	:       Dict   =					MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING






						@require_pytesseract
						@require_vision
						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase     ,						_UpperCamelCase   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						pipeline(
												    'document-question-answering'     ,						model=_UpperCamelCase     ,						tokenizer=_UpperCamelCase     ,						image_processor=_UpperCamelCase   )

												lowerCAmelCase__    						=						INVOICE_URL
												lowerCAmelCase__    						=						list(zip(*apply_tesseract(load_image(_UpperCamelCase   )     ,						_UpperCamelCase     ,						''   )   )   )
												lowerCAmelCase__    						=						'What is the placebo?'
												lowerCAmelCase__    						=						[
												    {
												        'image': load_image(_UpperCamelCase   ),
												        'question': question,
												    },
												    {
												        'image': image,
												        'question': question,
												    },
												    {
												        'image': image,
												        'question': question,
												        'word_boxes': word_boxes,
												    },
												]
												return dqa_pipeline, examples






						def       UpperCamelCase__      (	self     ,						_UpperCamelCase     ,						_UpperCamelCase   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						dqa_pipeline(_UpperCamelCase     ,						top_k=2   )
												self.assertEqual(
												    _UpperCamelCase     ,						[
												        [
												            {'score': ANY(_UpperCamelCase   ), 'answer': ANY(_UpperCamelCase   ), 'start': ANY(_UpperCamelCase   ), 'end': ANY(_UpperCamelCase   )},
												            {'score': ANY(_UpperCamelCase   ), 'answer': ANY(_UpperCamelCase   ), 'start': ANY(_UpperCamelCase   ), 'end': ANY(_UpperCamelCase   )},
												        ]
												    ]
												    * 3     ,						)






						@require_torch
						@require_detectrona
						@require_pytesseract
						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						pipeline('document-question-answering'     ,						model='hf-internal-testing/tiny-random-layoutlmv2'   )
												lowerCAmelCase__    						=						INVOICE_URL
												lowerCAmelCase__    						=						'How many cats are there?'

												lowerCAmelCase__    						=						[
												    {'score': 0.00_01, 'answer': 'oy 2312/2019', 'start': 38, 'end': 39},
												    {'score': 0.00_01, 'answer': 'oy 2312/2019 DUE', 'start': 38, 'end': 40},
												]
												lowerCAmelCase__    						=						dqa_pipeline(image=_UpperCamelCase     ,						question=_UpperCamelCase     ,						top_k=2   )
												self.assertEqual(nested_simplify(_UpperCamelCase     ,						decimals=4   )     ,						_UpperCamelCase   )

												lowerCAmelCase__    						=						dqa_pipeline({'image': image, 'question': question}     ,						top_k=2   )
												self.assertEqual(nested_simplify(_UpperCamelCase     ,						decimals=4   )     ,						_UpperCamelCase   )

												# This image does not detect ANY text in it, meaning layoutlmv2 should fail.
												# Empty answer probably
												lowerCAmelCase__    						=						'./tests/fixtures/tests_samples/COCO/000000039769.png'
												lowerCAmelCase__    						=						dqa_pipeline(image=_UpperCamelCase     ,						question=_UpperCamelCase     ,						top_k=2   )
												self.assertEqual(_UpperCamelCase     ,						[]   )

												# We can optionnally pass directly the words and bounding boxes
												lowerCAmelCase__    						=						'./tests/fixtures/tests_samples/COCO/000000039769.png'
												lowerCAmelCase__    						=						[]
												lowerCAmelCase__    						=						[]
												lowerCAmelCase__    						=						dqa_pipeline(image=_UpperCamelCase     ,						question=_UpperCamelCase     ,						words=_UpperCamelCase     ,						boxes=_UpperCamelCase     ,						top_k=2   )
												self.assertEqual(_UpperCamelCase     ,						[]   )






						@slow
						@require_torch
						@require_detectrona
						@require_pytesseract
						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						pipeline(
												    'document-question-answering'     ,						model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'     ,						revision='9977165'     ,						)
												lowerCAmelCase__    						=						INVOICE_URL
												lowerCAmelCase__    						=						'What is the invoice number?'

												lowerCAmelCase__    						=						dqa_pipeline(image=_UpperCamelCase     ,						question=_UpperCamelCase     ,						top_k=2   )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase     ,						decimals=4   )     ,						[
												        {'score': 0.99_44, 'answer': 'us-001', 'start': 16, 'end': 16},
												        {'score': 0.00_09, 'answer': 'us-001', 'start': 16, 'end': 16},
												    ]     ,						)

												lowerCAmelCase__    						=						dqa_pipeline({'image': image, 'question': question}     ,						top_k=2   )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase     ,						decimals=4   )     ,						[
												        {'score': 0.99_44, 'answer': 'us-001', 'start': 16, 'end': 16},
												        {'score': 0.00_09, 'answer': 'us-001', 'start': 16, 'end': 16},
												    ]     ,						)

												lowerCAmelCase__    						=						dqa_pipeline(
												    [{'image': image, 'question': question}, {'image': image, 'question': question}]     ,						top_k=2   )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase     ,						decimals=4   )     ,						[
												        [
												            {'score': 0.99_44, 'answer': 'us-001', 'start': 16, 'end': 16},
												            {'score': 0.00_09, 'answer': 'us-001', 'start': 16, 'end': 16},
												        ],
												    ]
												    * 2     ,						)






						@slow
						@require_torch
						@require_detectrona
						@require_pytesseract
						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						pipeline(
												    'document-question-answering'     ,						model='tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa'     ,						revision='9977165'     ,						max_seq_len=50     ,						)
												lowerCAmelCase__    						=						INVOICE_URL
												lowerCAmelCase__    						=						'What is the invoice number?'

												lowerCAmelCase__    						=						dqa_pipeline(image=_UpperCamelCase     ,						question=_UpperCamelCase     ,						top_k=2   )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase     ,						decimals=4   )     ,						[
												        {'score': 0.99_74, 'answer': '1110212019', 'start': 23, 'end': 23},
												        {'score': 0.99_48, 'answer': 'us-001', 'start': 16, 'end': 16},
												    ]     ,						)

												lowerCAmelCase__    						=						dqa_pipeline({'image': image, 'question': question}     ,						top_k=2   )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase     ,						decimals=4   )     ,						[
												        {'score': 0.99_74, 'answer': '1110212019', 'start': 23, 'end': 23},
												        {'score': 0.99_48, 'answer': 'us-001', 'start': 16, 'end': 16},
												    ]     ,						)

												lowerCAmelCase__    						=						dqa_pipeline(
												    [{'image': image, 'question': question}, {'image': image, 'question': question}]     ,						top_k=2   )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase     ,						decimals=4   )     ,						[
												        [
												            {'score': 0.99_74, 'answer': '1110212019', 'start': 23, 'end': 23},
												            {'score': 0.99_48, 'answer': 'us-001', 'start': 16, 'end': 16},
												        ]
												    ]
												    * 2     ,						)






						@slow
						@require_torch
						@require_pytesseract
						@require_vision
						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						AutoTokenizer.from_pretrained(
												    'impira/layoutlm-document-qa'     ,						revision='3dc6de3'     ,						add_prefix_space=_UpperCamelCase   )
												lowerCAmelCase__    						=						pipeline(
												    'document-question-answering'     ,						model='impira/layoutlm-document-qa'     ,						tokenizer=_UpperCamelCase     ,						revision='3dc6de3'     ,						)
												lowerCAmelCase__    						=						INVOICE_URL
												lowerCAmelCase__    						=						'What is the invoice number?'

												lowerCAmelCase__    						=						dqa_pipeline(image=_UpperCamelCase     ,						question=_UpperCamelCase     ,						top_k=2   )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase     ,						decimals=4   )     ,						[
												        {'score': 0.42_51, 'answer': 'us-001', 'start': 16, 'end': 16},
												        {'score': 0.08_19, 'answer': '1110212019', 'start': 23, 'end': 23},
												    ]     ,						)

												lowerCAmelCase__    						=						dqa_pipeline({'image': image, 'question': question}     ,						top_k=2   )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase     ,						decimals=4   )     ,						[
												        {'score': 0.42_51, 'answer': 'us-001', 'start': 16, 'end': 16},
												        {'score': 0.08_19, 'answer': '1110212019', 'start': 23, 'end': 23},
												    ]     ,						)

												lowerCAmelCase__    						=						dqa_pipeline(
												    [{'image': image, 'question': question}, {'image': image, 'question': question}]     ,						top_k=2   )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase     ,						decimals=4   )     ,						[
												        [
												            {'score': 0.42_51, 'answer': 'us-001', 'start': 16, 'end': 16},
												            {'score': 0.08_19, 'answer': '1110212019', 'start': 23, 'end': 23},
												        ]
												    ]
												    * 2     ,						)

												lowerCAmelCase__    						=						list(zip(*apply_tesseract(load_image(_UpperCamelCase   )     ,						_UpperCamelCase     ,						''   )   )   )

												# This model should also work if `image` is set to None
												lowerCAmelCase__    						=						dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}     ,						top_k=2   )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase     ,						decimals=4   )     ,						[
												        {'score': 0.42_51, 'answer': 'us-001', 'start': 16, 'end': 16},
												        {'score': 0.08_19, 'answer': '1110212019', 'start': 23, 'end': 23},
												    ]     ,						)






						@slow
						@require_torch
						@require_pytesseract
						@require_vision
						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						AutoTokenizer.from_pretrained(
												    'impira/layoutlm-document-qa'     ,						revision='3dc6de3'     ,						add_prefix_space=_UpperCamelCase   )
												lowerCAmelCase__    						=						pipeline(
												    'document-question-answering'     ,						model='impira/layoutlm-document-qa'     ,						tokenizer=_UpperCamelCase     ,						revision='3dc6de3'     ,						max_seq_len=50     ,						)
												lowerCAmelCase__    						=						INVOICE_URL
												lowerCAmelCase__    						=						'What is the invoice number?'

												lowerCAmelCase__    						=						dqa_pipeline(image=_UpperCamelCase     ,						question=_UpperCamelCase     ,						top_k=2   )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase     ,						decimals=4   )     ,						[
												        {'score': 0.99_99, 'answer': 'us-001', 'start': 16, 'end': 16},
												        {'score': 0.99_98, 'answer': 'us-001', 'start': 16, 'end': 16},
												    ]     ,						)

												lowerCAmelCase__    						=						dqa_pipeline(
												    [{'image': image, 'question': question}, {'image': image, 'question': question}]     ,						top_k=2   )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase     ,						decimals=4   )     ,						[
												        [
												            {'score': 0.99_99, 'answer': 'us-001', 'start': 16, 'end': 16},
												            {'score': 0.99_98, 'answer': 'us-001', 'start': 16, 'end': 16},
												        ]
												    ]
												    * 2     ,						)

												lowerCAmelCase__    						=						list(zip(*apply_tesseract(load_image(_UpperCamelCase   )     ,						_UpperCamelCase     ,						''   )   )   )

												# This model should also work if `image` is set to None
												lowerCAmelCase__    						=						dqa_pipeline({'image': None, 'word_boxes': word_boxes, 'question': question}     ,						top_k=2   )
												self.assertEqual(
												    nested_simplify(_UpperCamelCase     ,						decimals=4   )     ,						[
												        {'score': 0.99_99, 'answer': 'us-001', 'start': 16, 'end': 16},
												        {'score': 0.99_98, 'answer': 'us-001', 'start': 16, 'end': 16},
												    ]     ,						)






						@slow
						@require_torch
						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												lowerCAmelCase__    						=						pipeline(
												    'document-question-answering'     ,						model='naver-clova-ix/donut-base-finetuned-docvqa'     ,						tokenizer=AutoTokenizer.from_pretrained('naver-clova-ix/donut-base-finetuned-docvqa'   )     ,						feature_extractor='naver-clova-ix/donut-base-finetuned-docvqa'     ,						)

												lowerCAmelCase__    						=						INVOICE_URL
												lowerCAmelCase__    						=						'What is the invoice number?'
												lowerCAmelCase__    						=						dqa_pipeline(image=_UpperCamelCase     ,						question=_UpperCamelCase     ,						top_k=2   )
												self.assertEqual(nested_simplify(_UpperCamelCase     ,						decimals=4   )     ,						[{'answer': 'us-001'}]   )







						@require_tf
						@unittest.skip('Document question answering not implemented in TF'   )
						def       UpperCamelCase__      (	self   ):






												"""simple docstring"""
												pass





