



'''simple docstring'''




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 			UpperCamelCase_ :





								@staticmethod
								def  _lowercase(  *A		,		**A    )			->			List[str]:
															pass




							def       __lowerCamelCase  (					_lowercase		)      ->			Tuple:
														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.
a   :			List[str]					  =    (
    """https://huggingface.co/spaces/impira/docquery/resolve/2f6c96314dc84dfda62d40de9da55f2f5165d403/invoice.png"""
)

@is_pipeline_test
@require_torch
@require_vision
class 			UpperCamelCase_ (							unittest.TestCase			):
	lowercase          = MODEL_FOR_DOCUMENT_QUESTION_ANSWERING_MAPPING





	@require_pytesseract
	@require_vision
	def  _lowercase(  self		,		A		,		A		,		A    )			->			Dict:
								UpperCAmelCase     :		Union[str, Any]							     =		pipeline(
								    """document-question-answering"""		,		model=A		,		tokenizer=A		,		image_processor=A    )

								UpperCAmelCase     :		List[str]							     =		INVOICE_URL
								UpperCAmelCase     :		Optional[Any]							     =		list(zip(*apply_tesseract(load_image(A    )		,		A		,		""""""    )    )    )
								UpperCAmelCase     :		Dict							     =		"""What is the placebo?"""
								UpperCAmelCase     :		str							     =		[
								    {
								        """image""": load_image(A    ),
								        """question""": question,
								    },
								    {
								        """image""": image,
								        """question""": question,
								    },
								    {
								        """image""": image,
								        """question""": question,
								        """word_boxes""": word_boxes,
								    },
								]
								return dqa_pipeline, examples





	def  _lowercase(  self		,		A		,		A    )			->			Dict:
								UpperCAmelCase     :		int							     =		dqa_pipeline(A		,		top_k=2    )
								self.assertEqual(
								    A		,		[
								        [
								            {"""score""": ANY(A    ), """answer""": ANY(A    ), """start""": ANY(A    ), """end""": ANY(A    )},
								            {"""score""": ANY(A    ), """answer""": ANY(A    ), """start""": ANY(A    ), """end""": ANY(A    )},
								        ]
								    ]
								    * 3		,		)





	@require_torch
	@require_detectrona
	@require_pytesseract
	def  _lowercase(  self    )			->			List[str]:
								UpperCAmelCase     :		Any							     =		pipeline("""document-question-answering"""		,		model="""hf-internal-testing/tiny-random-layoutlmv2"""    )
								UpperCAmelCase     :		Tuple							     =		INVOICE_URL
								UpperCAmelCase     :		Optional[int]							     =		"""How many cats are there?"""

								UpperCAmelCase     :		List[str]							     =		[
								    {"""score""": 0.0_0_0_1, """answer""": """oy 2312/2019""", """start""": 38, """end""": 39},
								    {"""score""": 0.0_0_0_1, """answer""": """oy 2312/2019 DUE""", """start""": 38, """end""": 40},
								]
								UpperCAmelCase     :		Union[str, Any]							     =		dqa_pipeline(image=A		,		question=A		,		top_k=2    )
								self.assertEqual(nested_simplify(A		,		decimals=4    )		,		A    )

								UpperCAmelCase     :		Any							     =		dqa_pipeline({"""image""": image, """question""": question}		,		top_k=2    )
								self.assertEqual(nested_simplify(A		,		decimals=4    )		,		A    )

								# This image does not detect ANY text in it, meaning layoutlmv2 should fail.
								# Empty answer probably
								UpperCAmelCase     :		Optional[Any]							     =		"""./tests/fixtures/tests_samples/COCO/000000039769.png"""
								UpperCAmelCase     :		List[str]							     =		dqa_pipeline(image=A		,		question=A		,		top_k=2    )
								self.assertEqual(A		,		[]    )

								# We can optionnally pass directly the words and bounding boxes
								UpperCAmelCase     :		Optional[Any]							     =		"""./tests/fixtures/tests_samples/COCO/000000039769.png"""
								UpperCAmelCase     :		Tuple							     =		[]
								UpperCAmelCase     :		Union[str, Any]							     =		[]
								UpperCAmelCase     :		Optional[int]							     =		dqa_pipeline(image=A		,		question=A		,		words=A		,		boxes=A		,		top_k=2    )
								self.assertEqual(A		,		[]    )





	@slow
	@require_torch
	@require_detectrona
	@require_pytesseract
	def  _lowercase(  self    )			->			List[Any]:
								UpperCAmelCase     :		List[Any]							     =		pipeline(
								    """document-question-answering"""		,		model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""		,		revision="""9977165"""		,		)
								UpperCAmelCase     :		List[str]							     =		INVOICE_URL
								UpperCAmelCase     :		Any							     =		"""What is the invoice number?"""

								UpperCAmelCase     :		int							     =		dqa_pipeline(image=A		,		question=A		,		top_k=2    )
								self.assertEqual(
								    nested_simplify(A		,		decimals=4    )		,		[
								        {"""score""": 0.9_9_4_4, """answer""": """us-001""", """start""": 16, """end""": 16},
								        {"""score""": 0.0_0_0_9, """answer""": """us-001""", """start""": 16, """end""": 16},
								    ]		,		)

								UpperCAmelCase     :		List[Any]							     =		dqa_pipeline({"""image""": image, """question""": question}		,		top_k=2    )
								self.assertEqual(
								    nested_simplify(A		,		decimals=4    )		,		[
								        {"""score""": 0.9_9_4_4, """answer""": """us-001""", """start""": 16, """end""": 16},
								        {"""score""": 0.0_0_0_9, """answer""": """us-001""", """start""": 16, """end""": 16},
								    ]		,		)

								UpperCAmelCase     :		Any							     =		dqa_pipeline(
								    [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]		,		top_k=2    )
								self.assertEqual(
								    nested_simplify(A		,		decimals=4    )		,		[
								        [
								            {"""score""": 0.9_9_4_4, """answer""": """us-001""", """start""": 16, """end""": 16},
								            {"""score""": 0.0_0_0_9, """answer""": """us-001""", """start""": 16, """end""": 16},
								        ],
								    ]
								    * 2		,		)





	@slow
	@require_torch
	@require_detectrona
	@require_pytesseract
	def  _lowercase(  self    )			->			List[str]:
								UpperCAmelCase     :		Union[str, Any]							     =		pipeline(
								    """document-question-answering"""		,		model="""tiennvcs/layoutlmv2-base-uncased-finetuned-docvqa"""		,		revision="""9977165"""		,		max_seq_len=50		,		)
								UpperCAmelCase     :		Optional[int]							     =		INVOICE_URL
								UpperCAmelCase     :		Any							     =		"""What is the invoice number?"""

								UpperCAmelCase     :		int							     =		dqa_pipeline(image=A		,		question=A		,		top_k=2    )
								self.assertEqual(
								    nested_simplify(A		,		decimals=4    )		,		[
								        {"""score""": 0.9_9_7_4, """answer""": """1110212019""", """start""": 23, """end""": 23},
								        {"""score""": 0.9_9_4_8, """answer""": """us-001""", """start""": 16, """end""": 16},
								    ]		,		)

								UpperCAmelCase     :		Optional[Any]							     =		dqa_pipeline({"""image""": image, """question""": question}		,		top_k=2    )
								self.assertEqual(
								    nested_simplify(A		,		decimals=4    )		,		[
								        {"""score""": 0.9_9_7_4, """answer""": """1110212019""", """start""": 23, """end""": 23},
								        {"""score""": 0.9_9_4_8, """answer""": """us-001""", """start""": 16, """end""": 16},
								    ]		,		)

								UpperCAmelCase     :		List[str]							     =		dqa_pipeline(
								    [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]		,		top_k=2    )
								self.assertEqual(
								    nested_simplify(A		,		decimals=4    )		,		[
								        [
								            {"""score""": 0.9_9_7_4, """answer""": """1110212019""", """start""": 23, """end""": 23},
								            {"""score""": 0.9_9_4_8, """answer""": """us-001""", """start""": 16, """end""": 16},
								        ]
								    ]
								    * 2		,		)





	@slow
	@require_torch
	@require_pytesseract
	@require_vision
	def  _lowercase(  self    )			->			Any:
								UpperCAmelCase     :		Union[str, Any]							     =		AutoTokenizer.from_pretrained(
								    """impira/layoutlm-document-qa"""		,		revision="""3dc6de3"""		,		add_prefix_space=A    )
								UpperCAmelCase     :		Tuple							     =		pipeline(
								    """document-question-answering"""		,		model="""impira/layoutlm-document-qa"""		,		tokenizer=A		,		revision="""3dc6de3"""		,		)
								UpperCAmelCase     :		List[str]							     =		INVOICE_URL
								UpperCAmelCase     :		List[str]							     =		"""What is the invoice number?"""

								UpperCAmelCase     :		Union[str, Any]							     =		dqa_pipeline(image=A		,		question=A		,		top_k=2    )
								self.assertEqual(
								    nested_simplify(A		,		decimals=4    )		,		[
								        {"""score""": 0.4_2_5_1, """answer""": """us-001""", """start""": 16, """end""": 16},
								        {"""score""": 0.0_8_1_9, """answer""": """1110212019""", """start""": 23, """end""": 23},
								    ]		,		)

								UpperCAmelCase     :		Tuple							     =		dqa_pipeline({"""image""": image, """question""": question}		,		top_k=2    )
								self.assertEqual(
								    nested_simplify(A		,		decimals=4    )		,		[
								        {"""score""": 0.4_2_5_1, """answer""": """us-001""", """start""": 16, """end""": 16},
								        {"""score""": 0.0_8_1_9, """answer""": """1110212019""", """start""": 23, """end""": 23},
								    ]		,		)

								UpperCAmelCase     :		Union[str, Any]							     =		dqa_pipeline(
								    [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]		,		top_k=2    )
								self.assertEqual(
								    nested_simplify(A		,		decimals=4    )		,		[
								        [
								            {"""score""": 0.4_2_5_1, """answer""": """us-001""", """start""": 16, """end""": 16},
								            {"""score""": 0.0_8_1_9, """answer""": """1110212019""", """start""": 23, """end""": 23},
								        ]
								    ]
								    * 2		,		)

								UpperCAmelCase     :		List[Any]							     =		list(zip(*apply_tesseract(load_image(A    )		,		A		,		""""""    )    )    )

								# This model should also work if `image` is set to None
								UpperCAmelCase     :		str							     =		dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question}		,		top_k=2    )
								self.assertEqual(
								    nested_simplify(A		,		decimals=4    )		,		[
								        {"""score""": 0.4_2_5_1, """answer""": """us-001""", """start""": 16, """end""": 16},
								        {"""score""": 0.0_8_1_9, """answer""": """1110212019""", """start""": 23, """end""": 23},
								    ]		,		)





	@slow
	@require_torch
	@require_pytesseract
	@require_vision
	def  _lowercase(  self    )			->			List[Any]:
								UpperCAmelCase     :		Optional[int]							     =		AutoTokenizer.from_pretrained(
								    """impira/layoutlm-document-qa"""		,		revision="""3dc6de3"""		,		add_prefix_space=A    )
								UpperCAmelCase     :		Optional[int]							     =		pipeline(
								    """document-question-answering"""		,		model="""impira/layoutlm-document-qa"""		,		tokenizer=A		,		revision="""3dc6de3"""		,		max_seq_len=50		,		)
								UpperCAmelCase     :		Tuple							     =		INVOICE_URL
								UpperCAmelCase     :		Union[str, Any]							     =		"""What is the invoice number?"""

								UpperCAmelCase     :		Any							     =		dqa_pipeline(image=A		,		question=A		,		top_k=2    )
								self.assertEqual(
								    nested_simplify(A		,		decimals=4    )		,		[
								        {"""score""": 0.9_9_9_9, """answer""": """us-001""", """start""": 16, """end""": 16},
								        {"""score""": 0.9_9_9_8, """answer""": """us-001""", """start""": 16, """end""": 16},
								    ]		,		)

								UpperCAmelCase     :		List[Any]							     =		dqa_pipeline(
								    [{"""image""": image, """question""": question}, {"""image""": image, """question""": question}]		,		top_k=2    )
								self.assertEqual(
								    nested_simplify(A		,		decimals=4    )		,		[
								        [
								            {"""score""": 0.9_9_9_9, """answer""": """us-001""", """start""": 16, """end""": 16},
								            {"""score""": 0.9_9_9_8, """answer""": """us-001""", """start""": 16, """end""": 16},
								        ]
								    ]
								    * 2		,		)

								UpperCAmelCase     :		List[str]							     =		list(zip(*apply_tesseract(load_image(A    )		,		A		,		""""""    )    )    )

								# This model should also work if `image` is set to None
								UpperCAmelCase     :		int							     =		dqa_pipeline({"""image""": None, """word_boxes""": word_boxes, """question""": question}		,		top_k=2    )
								self.assertEqual(
								    nested_simplify(A		,		decimals=4    )		,		[
								        {"""score""": 0.9_9_9_9, """answer""": """us-001""", """start""": 16, """end""": 16},
								        {"""score""": 0.9_9_9_8, """answer""": """us-001""", """start""": 16, """end""": 16},
								    ]		,		)





	@slow
	@require_torch
	def  _lowercase(  self    )			->			Optional[Any]:
								UpperCAmelCase     :		int							     =		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"""		,		)

								UpperCAmelCase     :		Optional[Any]							     =		INVOICE_URL
								UpperCAmelCase     :		Optional[int]							     =		"""What is the invoice number?"""
								UpperCAmelCase     :		Optional[Any]							     =		dqa_pipeline(image=A		,		question=A		,		top_k=2    )
								self.assertEqual(nested_simplify(A		,		decimals=4    )		,		[{"""answer""": """us-001"""}]    )






	@require_tf
	@unittest.skip("""Document question answering not implemented in TF"""    )
	def  _lowercase(  self    )			->			int:
								pass






'''simple docstring'''




# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import torch

from accelerate import PartialState
from accelerate.utils.operations import broadcast, gather, gather_object, pad_across_processes, reduce



def       __lowerCamelCase  (					_lowercase		)      ->			Tuple:
							return (torch.arange(state.num_processes		) + 1.0 + (state.num_processes * state.process_index)).to(state.device		)



def       __lowerCamelCase  (					_lowercase		)      ->			List[Any]:
							UpperCAmelCase     :		Any							     =		create_tensor(_lowercase		)
							UpperCAmelCase     :		Union[str, Any]							     =		gather(_lowercase		)
							assert gathered_tensor.tolist() == list(range(1	,				state.num_processes**2 + 1		)		)



def       __lowerCamelCase  (					_lowercase		)      ->			Optional[int]:
							UpperCAmelCase     :		Any							     =		[state.process_index]
							UpperCAmelCase     :		Union[str, Any]							     =		gather_object(_lowercase		)
							assert len(_lowercase		) == state.num_processes, F'''{gathered_obj}, {len(_lowercase		)} != {state.num_processes}'''
							assert gathered_obj == list(range(state.num_processes		)		), F'''{gathered_obj} != {list(range(state.num_processes		)		)}'''



def       __lowerCamelCase  (					_lowercase		)      ->			List[Any]:
							UpperCAmelCase     :		Optional[int]							     =		create_tensor(_lowercase		)
							UpperCAmelCase     :		List[str]							     =		broadcast(_lowercase		)
							assert broadcasted_tensor.shape == torch.Size([state.num_processes]		)
							assert broadcasted_tensor.tolist() == list(range(1	,				state.num_processes + 1		)		)



def       __lowerCamelCase  (					_lowercase		)      ->			Tuple:
							# We need to pad the tensor with one more element if we are the main process
							# to ensure that we can pad
							if state.is_main_process:
														UpperCAmelCase     :		Optional[Any]							     =		torch.arange(state.num_processes + 1		).to(state.device		)
							else:
														UpperCAmelCase     :		Tuple							     =		torch.arange(state.num_processes		).to(state.device		)
							UpperCAmelCase     :		Optional[Any]							     =		pad_across_processes(_lowercase		)
							assert padded_tensor.shape == torch.Size([state.num_processes + 1]		)
							if not state.is_main_process:
														assert padded_tensor.tolist() == list(range(0	,				state.num_processes		)		) + [0]



def       __lowerCamelCase  (					_lowercase		)      ->			Dict:
							# For now runs on only two processes
							if state.num_processes != 2:
														return
							UpperCAmelCase     :		Optional[Any]							     =		create_tensor(_lowercase		)
							UpperCAmelCase     :		Optional[Any]							     =		reduce(_lowercase	,				"""sum"""		)
							UpperCAmelCase     :		Optional[Any]							     =		torch.tensor([4.0, 6]		).to(state.device		)
							assert torch.allclose(_lowercase	,				_lowercase		), F'''{reduced_tensor} != {truth_tensor}'''



def       __lowerCamelCase  (					_lowercase		)      ->			Optional[Any]:
							# For now runs on only two processes
							if state.num_processes != 2:
														return
							UpperCAmelCase     :		Tuple							     =		create_tensor(_lowercase		)
							UpperCAmelCase     :		Optional[int]							     =		reduce(_lowercase	,				"""mean"""		)
							UpperCAmelCase     :		str							     =		torch.tensor([2.0, 3]		).to(state.device		)
							assert torch.allclose(_lowercase	,				_lowercase		), F'''{reduced_tensor} != {truth_tensor}'''



def       __lowerCamelCase  (					_lowercase		)      ->			Optional[int]:
							# For xla_spawn (TPUs)
							main()



def       __lowerCamelCase  (					)      ->			int:
							UpperCAmelCase     :		List[Any]							     =		PartialState()
							state.print(F'''State: {state}'''		)
							state.print("""testing gather"""		)
							test_gather(_lowercase		)
							state.print("""testing gather_object"""		)
							test_gather_object(_lowercase		)
							state.print("""testing broadcast"""		)
							test_broadcast(_lowercase		)
							state.print("""testing pad_across_processes"""		)
							test_pad_across_processes(_lowercase		)
							state.print("""testing reduce_sum"""		)
							test_reduce_sum(_lowercase		)
							state.print("""testing reduce_mean"""		)
							test_reduce_mean(_lowercase		)


if __name__ == "__main__":
							main()

