




'''simple docstring'''







import unittest

import numpy as np

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

if is_vision_available():
						from PIL import Image

						from transformers import ViltImageProcessor





class 					lowerCamelCase_							(	unittest.TestCase							):




					'''simple docstring'''






					def __init__(   self,  __UpperCamelCase,  __UpperCamelCase=7,  __UpperCamelCase=3,  __UpperCamelCase=18,  __UpperCamelCase=30,  __UpperCamelCase=400,  __UpperCamelCase=True,  __UpperCamelCase=None,  __UpperCamelCase=2,  __UpperCamelCase=True,  __UpperCamelCase=[0.5, 0.5, 0.5],  __UpperCamelCase=[0.5, 0.5, 0.5],  ):
							_snake_case     	=	size if size is not None else {"shortest_edge": 30}
							_snake_case     	=	parent
							_snake_case     	=	batch_size
							_snake_case     	=	num_channels
							_snake_case     	=	image_size
							_snake_case     	=	min_resolution
							_snake_case     	=	max_resolution
							_snake_case     	=	do_resize
							_snake_case     	=	size
							_snake_case     	=	size_divisor
							_snake_case     	=	do_normalize
							_snake_case     	=	image_mean
							_snake_case     	=	image_std






					def     __SCREAMING_SNAKE_CASE     (   self			):
							return {
							    "image_mean": self.image_mean,
							    "image_std": self.image_std,
							    "do_normalize": self.do_normalize,
							    "do_resize": self.do_resize,
							    "size": self.size,
							    "size_divisor": self.size_divisor,
							}






					def     __SCREAMING_SNAKE_CASE     (   self,  __UpperCamelCase,  __UpperCamelCase=False			):
							if not batched:
									_snake_case     	=	self.size["shortest_edge"]
									_snake_case     	=	image_inputs[0]
									if isinstance(__UpperCamelCase,  Image.Image			):
											_snake_case	,   _snake_case     	=	image.size
									else:
											_snake_case	,   _snake_case     	=	image.shape[1], image.shape[2]
									_snake_case     	=	size / min(__UpperCamelCase,  __UpperCamelCase			)
									if h < w:
											_snake_case	,   _snake_case     	=	size, scale * w
									else:
											_snake_case	,   _snake_case     	=	scale * h, size

									_snake_case     	=	int((1_333 / 800) * size			)
									if max(__UpperCamelCase,  __UpperCamelCase			) > max_size:
											_snake_case     	=	max_size / max(__UpperCamelCase,  __UpperCamelCase			)
											_snake_case     	=	newh * scale
											_snake_case     	=	neww * scale

									_snake_case	,   _snake_case     	=	int(newh + 0.5			), int(neww + 0.5			)
									_snake_case	,   _snake_case     	=	(
									    newh // self.size_divisor * self.size_divisor,
									    neww // self.size_divisor * self.size_divisor,
									)

							else:
									_snake_case     	=	[]
									for image in image_inputs:
											_snake_case	,   _snake_case     	=	self.get_expected_values([image]			)
											expected_values.append((expected_height, expected_width)			)
									_snake_case     	=	max(__UpperCamelCase,  key=lambda __UpperCamelCase			: item[0]			)[0]
									_snake_case     	=	max(__UpperCamelCase,  key=lambda __UpperCamelCase			: item[1]			)[1]

							return expected_height, expected_width





@require_torch
@require_vision
class 					lowerCamelCase_							(	UpperCAmelCase_,	unittest.TestCase							):




					'''simple docstring'''






					lowercase__   =			ViltImageProcessor if is_vision_available() else None






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	ViltImageProcessingTester(self			)






					@property
					def     __SCREAMING_SNAKE_CASE     (   self			):
							return self.image_processor_tester.prepare_image_processor_dict()






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.image_processing_class(**self.image_processor_dict			)
							self.assertTrue(hasattr(__UpperCamelCase,  "image_mean"			)			)
							self.assertTrue(hasattr(__UpperCamelCase,  "image_std"			)			)
							self.assertTrue(hasattr(__UpperCamelCase,  "do_normalize"			)			)
							self.assertTrue(hasattr(__UpperCamelCase,  "do_resize"			)			)
							self.assertTrue(hasattr(__UpperCamelCase,  "size"			)			)
							self.assertTrue(hasattr(__UpperCamelCase,  "size_divisor"			)			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.image_processing_class.from_dict(self.image_processor_dict			)
							self.assertEqual(image_processor.size,  {"shortest_edge": 30}			)

							_snake_case     	=	self.image_processing_class.from_dict(self.image_processor_dict,  size=42			)
							self.assertEqual(image_processor.size,  {"shortest_edge": 42}			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							pass






					def     __SCREAMING_SNAKE_CASE     (   self			):
							# Initialize image_processing
							_snake_case     	=	self.image_processing_class(**self.image_processor_dict			)
							# create random PIL images
							_snake_case     	=	prepare_image_inputs(self.image_processor_tester,  equal_resolution=__UpperCamelCase			)
							for image in image_inputs:
									self.assertIsInstance(__UpperCamelCase,  Image.Image			)

							# Test not batched input
							_snake_case     	=	image_processing(image_inputs[0],  return_tensors="pt"			).pixel_values

							_snake_case	,   _snake_case     	=	self.image_processor_tester.get_expected_values(__UpperCamelCase			)
							self.assertEqual(
							    encoded_images.shape,  (1, self.image_processor_tester.num_channels, expected_height, expected_width),  )

							# Test batched
							_snake_case     	=	image_processing(__UpperCamelCase,  return_tensors="pt"			).pixel_values

							_snake_case	,   _snake_case     	=	self.image_processor_tester.get_expected_values(__UpperCamelCase,  batched=__UpperCamelCase			)
							self.assertEqual(
							    encoded_images.shape,  (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        expected_height,
							        expected_width,
							    ),  )






					def     __SCREAMING_SNAKE_CASE     (   self			):
							# Initialize image_processing
							_snake_case     	=	self.image_processing_class(**self.image_processor_dict			)
							# create random numpy tensors
							_snake_case     	=	prepare_image_inputs(self.image_processor_tester,  equal_resolution=__UpperCamelCase,  numpify=__UpperCamelCase			)
							for image in image_inputs:
									self.assertIsInstance(__UpperCamelCase,  np.ndarray			)

							# Test not batched input
							_snake_case     	=	image_processing(image_inputs[0],  return_tensors="pt"			).pixel_values

							_snake_case	,   _snake_case     	=	self.image_processor_tester.get_expected_values(__UpperCamelCase			)
							self.assertEqual(
							    encoded_images.shape,  (1, self.image_processor_tester.num_channels, expected_height, expected_width),  )

							# Test batched
							_snake_case     	=	image_processing(__UpperCamelCase,  return_tensors="pt"			).pixel_values

							_snake_case	,   _snake_case     	=	self.image_processor_tester.get_expected_values(__UpperCamelCase,  batched=__UpperCamelCase			)
							self.assertEqual(
							    encoded_images.shape,  (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        expected_height,
							        expected_width,
							    ),  )






					def     __SCREAMING_SNAKE_CASE     (   self			):
							# Initialize image_processing
							_snake_case     	=	self.image_processing_class(**self.image_processor_dict			)
							# create random PyTorch tensors
							_snake_case     	=	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
							_snake_case     	=	image_processing(image_inputs[0],  return_tensors="pt"			).pixel_values

							_snake_case	,   _snake_case     	=	self.image_processor_tester.get_expected_values(__UpperCamelCase			)
							self.assertEqual(
							    encoded_images.shape,  (1, self.image_processor_tester.num_channels, expected_height, expected_width),  )

							# Test batched
							_snake_case     	=	image_processing(__UpperCamelCase,  return_tensors="pt"			).pixel_values

							_snake_case	,   _snake_case     	=	self.image_processor_tester.get_expected_values(__UpperCamelCase,  batched=__UpperCamelCase			)
							self.assertEqual(
							    encoded_images.shape,  (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_channels,
							        expected_height,
							        expected_width,
							    ),  )











'''simple docstring'''







import logging
import os
import sys
from dataclasses import dataclass, field
from typing import Optional

import transformers
from transformers import (
    AutoConfig,
    AutoModelForQuestionAnswering,
    AutoTokenizer,
    DataCollatorWithPadding,
    HfArgumentParser,
    SquadDataset,
    Trainer,
    TrainingArguments,
)
from transformers import SquadDataTrainingArguments as DataTrainingArguments
from transformers.trainer_utils import is_main_process


_lowerCamelCase							:						Tuple							     =  logging.getLogger(__name__)





@dataclass
class 					lowerCamelCase_							:




					'''simple docstring'''






					lowercase__   =			field(
					    metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""}							)
					lowercase__   =			field(
					    default=UpperCAmelCase_,	metadata={"""help""": """Pretrained config name or path if not the same as model_name"""}							)
					lowercase__   =			field(
					    default=UpperCAmelCase_,	metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""}							)
					lowercase__   =			field(default=UpperCAmelCase_,	metadata={"""help""": """Set this flag to use fast tokenization."""}							)
					# If you want to tweak more attributes on your tokenizer, you should do it in a distinct script,
					# or just modify its tokenizer_config.json.
					lowercase__   =			field(
					    default=UpperCAmelCase_,	metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""},	)






def 		_lowercase		(     ):



		'''simple docstring'''







		_snake_case     	=	HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments)  )

		if len(sys.argv  ) == 2 and sys.argv[1].endswith(".json"  ):
				# If we pass only one argument to the script and it's the path to a json file,
				# let's parse it to get our arguments.
				_snake_case	,   _snake_case	,   _snake_case     	=	parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]  )  )
		else:
				_snake_case	,   _snake_case	,   _snake_case     	=	parser.parse_args_into_dataclasses()

		if (
		    os.path.exists(training_args.output_dir  )
		    and os.listdir(training_args.output_dir  )
		    and training_args.do_train
		    and not training_args.overwrite_output_dir
		):
				raise ValueError(
				    f'''Output directory ({training_args.output_dir}) already exists and is not empty. Use'''
				    " --overwrite_output_dir to overcome."  )

		# Setup logging
		logging.basicConfig(
		    format="%(asctime)s - %(levelname)s - %(name)s - %(message)s"						,							datefmt="%m/%d/%Y %H:%M:%S"						,							level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN						,							)
		logger.warning(
		    "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s"						,							training_args.local_rank						,							training_args.device						,							training_args.n_gpu						,							bool(training_args.local_rank != -1  )						,							training_args.fpaa						,							)
		# Set the verbosity to info of the Transformers logger (on main process only):
		if is_main_process(training_args.local_rank  ):
				transformers.utils.logging.set_verbosity_info()
				transformers.utils.logging.enable_default_handler()
				transformers.utils.logging.enable_explicit_format()
		logger.info("Training/evaluation parameters %s"						,							lowercase_  )

		# Prepare Question-Answering task
		# Load pretrained model and tokenizer
		#
		# Distributed training:
		# The .from_pretrained methods guarantee that only one local process can concurrently
		# download model & vocab.

		_snake_case     	=	AutoConfig.from_pretrained(
		    model_args.config_name if model_args.config_name else model_args.model_name_or_path						,							cache_dir=model_args.cache_dir						,							)
		_snake_case     	=	AutoTokenizer.from_pretrained(
		    model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path						,							cache_dir=model_args.cache_dir						,							use_fast=lowercase_						,							)
		_snake_case     	=	AutoModelForQuestionAnswering.from_pretrained(
		    model_args.model_name_or_path						,							from_tf=bool(".ckpt" in model_args.model_name_or_path  )						,							config=lowercase_						,							cache_dir=model_args.cache_dir						,							)

		# Get datasets
		_snake_case     	=	hasattr(model.config						,							"lang2id"  )
		_snake_case     	=	(
		    SquadDataset(
		        lowercase_						,							tokenizer=lowercase_						,							is_language_sensitive=lowercase_						,							cache_dir=model_args.cache_dir  )
		    if training_args.do_train
		    else None
		)
		_snake_case     	=	(
		    SquadDataset(
		        lowercase_						,							tokenizer=lowercase_						,							mode="dev"						,							is_language_sensitive=lowercase_						,							cache_dir=model_args.cache_dir						,							)
		    if training_args.do_eval
		    else None
		)

		# Data collator
		_snake_case     	=	DataCollatorWithPadding(lowercase_						,							pad_to_multiple_of=8  ) if training_args.fpaa else None

		# Initialize our Trainer
		_snake_case     	=	Trainer(
		    model=lowercase_						,							args=lowercase_						,							train_dataset=lowercase_						,							eval_dataset=lowercase_						,							data_collator=lowercase_						,							)

		# Training
		if training_args.do_train:
				trainer.train(
				    model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path  ) else None  )
				trainer.save_model()
				# For convenience, we also re-save the tokenizer to the same directory,
				# so that you can share your model easily on huggingface.co/models =)
				if trainer.is_world_master():
						tokenizer.save_pretrained(training_args.output_dir  )






def 		_lowercase		(     lowercase_  ):



		'''simple docstring'''







		main()


if __name__ == "__main__":
						main()





