






import unittest

import numpy as np
import requests

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

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
		import torch

		from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
		__UpperCAmelCase								=						False

if is_vision_available():
		from PIL import Image

		from transformers import PixaStructImageProcessor



class   lowerCamelCase      (unittest.TestCase			):







		'''simple docstring'''



		def __init__(							self				,		_UpperCamelCase				,		_UpperCamelCase=7				,		_UpperCamelCase=3				,		_UpperCamelCase=1_8				,		_UpperCamelCase=3_0				,		_UpperCamelCase=4_0_0				,		_UpperCamelCase=None				,		_UpperCamelCase=True				,		_UpperCamelCase=True				,		_UpperCamelCase=None				,		)	->							Optional[int]:
							UpperCAmelCase_    :      List[Any]            =						size if size is not None else {'height': 2_0, 'width': 2_0}
							UpperCAmelCase_    :      str            =						parent
							UpperCAmelCase_    :      Tuple            =						batch_size
							UpperCAmelCase_    :      List[str]            =						num_channels
							UpperCAmelCase_    :      Tuple            =						image_size
							UpperCAmelCase_    :      Union[str, Any]            =						min_resolution
							UpperCAmelCase_    :      Union[str, Any]            =						max_resolution
							UpperCAmelCase_    :      Optional[int]            =						size
							UpperCAmelCase_    :      Union[str, Any]            =						do_normalize
							UpperCAmelCase_    :      Union[str, Any]            =						do_convert_rgb
							UpperCAmelCase_    :      Dict            =						[5_1_2, 1_0_2_4, 2_0_4_8, 4_0_9_6]
							UpperCAmelCase_    :      Optional[int]            =						patch_size if patch_size is not None else {'height': 1_6, 'width': 1_6}



		def 					__UpperCAmelCase			(							self   )	->							Optional[Any]:
							return {"do_normalize": self.do_normalize, "do_convert_rgb": self.do_convert_rgb}



		def 					__UpperCAmelCase			(							self   )	->							Any:
							UpperCAmelCase_    :      List[str]            =						'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/australia.jpg'
							UpperCAmelCase_    :      int            =						Image.open(requests.get(_UpperCamelCase				,		stream=_UpperCamelCase   ).raw   ).convert('RGB'   )
							return raw_image





@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11 ,  reason='''`Pix2StructImageProcessor` requires `torch>=1.11.0`.''' ,  )
@require_torch
@require_vision
class   lowerCamelCase      (_snake_case ,  unittest.TestCase			):







		'''simple docstring'''



		_snake_case    :    Dict       						=  PixaStructImageProcessor if is_vision_available() else None



		def 					__UpperCAmelCase			(							self   )	->							str:
							UpperCAmelCase_    :      Union[str, Any]            =						PixaStructImageProcessingTester(self   )



		@property
		def 					__UpperCAmelCase			(							self   )	->							Any:
							return self.image_processor_tester.prepare_image_processor_dict()



		def 					__UpperCAmelCase			(							self   )	->							List[Any]:
							UpperCAmelCase_    :      Dict            =						self.image_processing_class(**self.image_processor_dict   )
							self.assertTrue(hasattr(_UpperCamelCase				,		'do_normalize'   )   )
							self.assertTrue(hasattr(_UpperCamelCase				,		'do_convert_rgb'   )   )



		def 					__UpperCAmelCase			(							self   )	->							int:
							UpperCAmelCase_    :      Union[str, Any]            =						self.image_processor_tester.prepare_dummy_image()

							UpperCAmelCase_    :      List[str]            =						self.image_processing_class(**self.image_processor_dict   )
							UpperCAmelCase_    :      List[str]            =						2_0_4_8

							UpperCAmelCase_    :      List[str]            =						image_processor(_UpperCamelCase				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   )
							self.assertTrue(torch.allclose(inputs.flattened_patches.mean()				,		torch.tensor(0.06_06   )				,		atol=1E-3				,		rtol=1E-3   )   )



		def 					__UpperCAmelCase			(							self   )	->							Union[str, Any]:
							# Initialize image_processor
							UpperCAmelCase_    :      str            =						self.image_processing_class(**self.image_processor_dict   )
							# create random PIL images
							UpperCAmelCase_    :      Tuple            =						prepare_image_inputs(self.image_processor_tester				,		equal_resolution=_UpperCamelCase   )
							for image in image_inputs:
												self.assertIsInstance(_UpperCamelCase				,		Image.Image   )

							# Test not batched input
							UpperCAmelCase_    :      Dict            =						(
							    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
							    * self.image_processor_tester.num_channels
							) + 2

							for max_patch in self.image_processor_tester.max_patches:
												# Test not batched input
												UpperCAmelCase_    :      Dict            =						image_processor(
												    image_inputs[0]				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(1, max_patch, expected_hidden_dim)				,		)

												# Test batched
												UpperCAmelCase_    :      Optional[Any]            =						image_processor(
												    _UpperCamelCase				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)				,		)



		def 					__UpperCAmelCase			(							self   )	->							Tuple:
							# Initialize image_processor
							UpperCAmelCase_    :      Union[str, Any]            =						self.image_processing_class(**self.image_processor_dict   )
							# create random PIL images
							UpperCAmelCase_    :      int            =						prepare_image_inputs(self.image_processor_tester				,		equal_resolution=_UpperCamelCase   )
							for image in image_inputs:
												self.assertIsInstance(_UpperCamelCase				,		Image.Image   )

							# Test not batched input
							UpperCAmelCase_    :      List[str]            =						(
							    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
							    * self.image_processor_tester.num_channels
							) + 2

							UpperCAmelCase_    :      List[str]            =						True

							for max_patch in self.image_processor_tester.max_patches:
												# Test not batched input
												with self.assertRaises(_UpperCamelCase   ):
																	UpperCAmelCase_    :      List[str]            =						image_processor(
																	    image_inputs[0]				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   ).flattened_patches

												UpperCAmelCase_    :      Any            =						'Hello'

												UpperCAmelCase_    :      Optional[int]            =						image_processor(
												    image_inputs[0]				,		return_tensors='pt'				,		max_patches=_UpperCamelCase				,		header_text=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(1, max_patch, expected_hidden_dim)				,		)

												# Test batched
												UpperCAmelCase_    :      Any            =						image_processor(
												    _UpperCamelCase				,		return_tensors='pt'				,		max_patches=_UpperCamelCase				,		header_text=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)				,		)



		def 					__UpperCAmelCase			(							self   )	->							Any:
							# Initialize image_processor
							UpperCAmelCase_    :      int            =						self.image_processing_class(**self.image_processor_dict   )
							# create random numpy tensors
							UpperCAmelCase_    :      Optional[Any]            =						prepare_image_inputs(self.image_processor_tester				,		equal_resolution=_UpperCamelCase				,		numpify=_UpperCamelCase   )
							for image in image_inputs:
												self.assertIsInstance(_UpperCamelCase				,		np.ndarray   )

							UpperCAmelCase_    :      List[Any]            =						(
							    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
							    * self.image_processor_tester.num_channels
							) + 2

							for max_patch in self.image_processor_tester.max_patches:
												# Test not batched input
												UpperCAmelCase_    :      Optional[int]            =						image_processor(
												    image_inputs[0]				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(1, max_patch, expected_hidden_dim)				,		)

												# Test batched
												UpperCAmelCase_    :      int            =						image_processor(
												    _UpperCamelCase				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)				,		)



		def 					__UpperCAmelCase			(							self   )	->							int:
							# Initialize image_processor
							UpperCAmelCase_    :      Union[str, Any]            =						self.image_processing_class(**self.image_processor_dict   )
							# create random PyTorch tensors
							UpperCAmelCase_    :      Dict            =						prepare_image_inputs(self.image_processor_tester				,		equal_resolution=_UpperCamelCase				,		torchify=_UpperCamelCase   )
							for image in image_inputs:
												self.assertIsInstance(_UpperCamelCase				,		torch.Tensor   )

							# Test not batched input
							UpperCAmelCase_    :      Any            =						(
							    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
							    * self.image_processor_tester.num_channels
							) + 2

							for max_patch in self.image_processor_tester.max_patches:
												# Test not batched input
												UpperCAmelCase_    :      List[str]            =						image_processor(
												    image_inputs[0]				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(1, max_patch, expected_hidden_dim)				,		)

												# Test batched
												UpperCAmelCase_    :      List[str]            =						image_processor(
												    _UpperCamelCase				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)				,		)





@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11 ,  reason='''`Pix2StructImageProcessor` requires `torch>=1.11.0`.''' ,  )
@require_torch
@require_vision
class   lowerCamelCase      (_snake_case ,  unittest.TestCase			):







		'''simple docstring'''



		_snake_case    :    List[str]       						=  PixaStructImageProcessor if is_vision_available() else None



		def 					__UpperCAmelCase			(							self   )	->							Union[str, Any]:
							UpperCAmelCase_    :      Optional[Any]            =						PixaStructImageProcessingTester(self				,		num_channels=4   )
							UpperCAmelCase_    :      int            =						3



		@property
		def 					__UpperCAmelCase			(							self   )	->							Union[str, Any]:
							return self.image_processor_tester.prepare_image_processor_dict()



		def 					__UpperCAmelCase			(							self   )	->							List[Any]:
							UpperCAmelCase_    :      Dict            =						self.image_processing_class(**self.image_processor_dict   )
							self.assertTrue(hasattr(_UpperCamelCase				,		'do_normalize'   )   )
							self.assertTrue(hasattr(_UpperCamelCase				,		'do_convert_rgb'   )   )



		def 					__UpperCAmelCase			(							self   )	->							str:
							# Initialize image_processor
							UpperCAmelCase_    :      List[str]            =						self.image_processing_class(**self.image_processor_dict   )
							# create random PIL images
							UpperCAmelCase_    :      List[Any]            =						prepare_image_inputs(self.image_processor_tester				,		equal_resolution=_UpperCamelCase   )
							for image in image_inputs:
												self.assertIsInstance(_UpperCamelCase				,		Image.Image   )

							# Test not batched input
							UpperCAmelCase_    :      List[str]            =						(
							    (self.image_processor_tester.patch_size['height'] * self.image_processor_tester.patch_size['width'])
							    * (self.image_processor_tester.num_channels - 1)
							) + 2

							for max_patch in self.image_processor_tester.max_patches:
												# Test not batched input
												UpperCAmelCase_    :      List[str]            =						image_processor(
												    image_inputs[0]				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(1, max_patch, expected_hidden_dim)				,		)

												# Test batched
												UpperCAmelCase_    :      Any            =						image_processor(
												    _UpperCamelCase				,		return_tensors='pt'				,		max_patches=_UpperCamelCase   ).flattened_patches
												self.assertEqual(
												    encoded_images.shape				,		(self.image_processor_tester.batch_size, max_patch, expected_hidden_dim)				,		)














import json
import logging
import os
import sys
from pathlib import Path

import finetune_rag

from transformers.file_utils import is_apex_available
from transformers.testing_utils import (
    TestCasePlus,
    execute_subprocess_async,
    require_ray,
    require_torch_gpu,
    require_torch_multi_gpu,
)


logging.basicConfig(level=logging.DEBUG)
_a      	=  logging.getLogger()

_a      	=  logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)





class __lowerCamelCase (      snake_case__):





	"""simple docstring"""






	def        UpperCamelCase    (							self       ,    UpperCAmelCase    ):
							"""simple docstring"""







							os.makedirs(UpperCAmelCase       ,    exist_ok=UpperCAmelCase    )
							_UpperCAmelCase									=			{'source': 'What is love ?', 'target': 'life'}
							_UpperCAmelCase									=			{'train': 12, 'val': 2, 'test': 2}
							for split in ["train", "test", "val"]:
													for field in ["source", "target"]:
																			_UpperCAmelCase									=			'\n'.join([contents[field]] * n_lines[split]    )
																			with open(os.path.join(UpperCAmelCase       ,    F"""{split}.{field}"""    )       ,    'w'    ) as f:
																									f.write(UpperCAmelCase    )

	def        UpperCamelCase    (							self       ,    UpperCAmelCase       ,    UpperCAmelCase = "pytorch"    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self.get_auto_remove_tmp_dir()
							_UpperCAmelCase									=			os.path.join(UpperCAmelCase       ,    'output'    )
							_UpperCAmelCase									=			os.path.join(UpperCAmelCase       ,    'data'    )
							self._create_dummy_data(data_dir=UpperCAmelCase    )

							_UpperCAmelCase									=			F"""
                --data_dir {data_dir} \
                --output_dir {output_dir} \
                --model_name_or_path facebook/rag-sequence-base \
                --model_type rag_sequence \
                --do_train \
                --do_predict \
                --n_val -1 \
                --val_check_interval 1.0 \
                --train_batch_size 2 \
                --eval_batch_size 1 \
                --max_source_length 25 \
                --max_target_length 25 \
                --val_max_target_length 25 \
                --test_max_target_length 25 \
                --label_smoothing 0.1 \
                --dropout 0.1 \
                --attention_dropout 0.1 \
                --weight_decay 0.001 \
                --adam_epsilon 1e-08 \
                --max_grad_norm 0.1 \
                --lr_scheduler polynomial \
                --learning_rate 3e-04 \
                --num_train_epochs 1 \
                --warmup_steps 4 \
                --gradient_accumulation_steps 1 \
                --distributed-port 8787 \
                --use_dummy_dataset 1 \
                --distributed_retriever {distributed_retriever} \
            """.split()

							if gpus > 0:
													testargs.append(F"""--gpus={gpus}"""    )
													if is_apex_available():
																			testargs.append('--fp16'    )
							else:
													testargs.append('--gpus=0'    )
													testargs.append('--distributed_backend=ddp_cpu'    )
													testargs.append('--num_processes=2'    )

							_UpperCAmelCase									=			[sys.executable, str(Path(finetune_rag.__file__    ).resolve()    )] + testargs
							execute_subprocess_async(UpperCAmelCase       ,    env=self.get_env()    )

							_UpperCAmelCase									=			os.path.join(UpperCAmelCase       ,    'metrics.json'    )
							with open(UpperCAmelCase    ) as f:
													_UpperCAmelCase									=			json.load(UpperCAmelCase    )
							return result

	@require_torch_gpu
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self._run_finetune(gpus=1    )
							self.assertGreaterEqual(result['test'][0]['test_avg_em']       ,    0.2    )

	@require_torch_multi_gpu
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self._run_finetune(gpus=2    )
							self.assertGreaterEqual(result['test'][0]['test_avg_em']       ,    0.2    )

	@require_torch_gpu
	@require_ray
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self._run_finetune(gpus=1       ,    distributed_retriever='ray'    )
							self.assertGreaterEqual(result['test'][0]['test_avg_em']       ,    0.2    )

	@require_torch_multi_gpu
	@require_ray
	def        UpperCamelCase    (							self    ):
							"""simple docstring"""







							_UpperCAmelCase									=			self._run_finetune(gpus=1       ,    distributed_retriever='ray'    )
							self.assertGreaterEqual(result['test'][0]['test_avg_em']       ,    0.2    )
