




'''simple docstring'''







import inspect
import unittest

from transformers import EfficientNetConfig
from transformers.testing_utils import is_pipeline_test, require_torch, require_vision, 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
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
						import torch

						from transformers import EfficientNetForImageClassification, EfficientNetModel
						from transformers.models.efficientnet.modeling_efficientnet import EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import AutoImageProcessor





class 					lowerCamelCase_							:




					'''simple docstring'''






					def __init__(   self,  __UpperCamelCase,  __UpperCamelCase=13,  __UpperCamelCase=32,  __UpperCamelCase=3,  __UpperCamelCase=[3, 3, 5],  __UpperCamelCase=[32, 16, 24],  __UpperCamelCase=[16, 24, 40],  __UpperCamelCase=[1, 1, 2],  __UpperCamelCase=[1, 1, 2],  __UpperCamelCase=[1, 6, 6],  __UpperCamelCase=True,  __UpperCamelCase=True,  __UpperCamelCase=37,  __UpperCamelCase="gelu",  __UpperCamelCase=10,  ):
							_snake_case     	=	parent
							_snake_case     	=	batch_size
							_snake_case     	=	image_size
							_snake_case     	=	num_channels
							_snake_case     	=	kernel_sizes
							_snake_case     	=	in_channels
							_snake_case     	=	out_channels
							_snake_case     	=	strides
							_snake_case     	=	num_block_repeats
							_snake_case     	=	expand_ratios
							_snake_case     	=	is_training
							_snake_case     	=	hidden_act
							_snake_case     	=	num_labels
							_snake_case     	=	use_labels






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

							_snake_case     	=	None
							if self.use_labels:
									_snake_case     	=	ids_tensor([self.batch_size],  self.num_labels			)

							_snake_case     	=	self.get_config()
							return config, pixel_values, labels






					def     __SCREAMING_SNAKE_CASE     (   self			):
							return EfficientNetConfig(
							    num_channels=self.num_channels,  kernel_sizes=self.kernel_sizes,  in_channels=self.in_channels,  out_channels=self.out_channels,  strides=self.strides,  num_block_repeats=self.num_block_repeats,  expand_ratios=self.expand_ratios,  hidden_act=self.hidden_act,  num_labels=self.num_labels,  )






					def     __SCREAMING_SNAKE_CASE     (   self,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase			):
							_snake_case     	=	EfficientNetModel(config=__UpperCamelCase			)
							model.to(__UpperCamelCase			)
							model.eval()
							_snake_case     	=	model(__UpperCamelCase			)
							# expected last hidden states: B, C, H // 4, W // 4
							self.parent.assertEqual(
							    result.last_hidden_state.shape,  (self.batch_size, config.hidden_dim, self.image_size // 4, self.image_size // 4),  )






					def     __SCREAMING_SNAKE_CASE     (   self,  __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase			):
							_snake_case     	=	EfficientNetForImageClassification(__UpperCamelCase			)
							model.to(__UpperCamelCase			)
							model.eval()
							_snake_case     	=	model(__UpperCamelCase,  labels=__UpperCamelCase			)
							self.parent.assertEqual(result.logits.shape,  (self.batch_size, self.num_labels)			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.prepare_config_and_inputs()
							_snake_case     	=	config_and_inputs
							_snake_case     	=	{"pixel_values": pixel_values}
							return config, inputs_dict





@require_torch
class 					lowerCamelCase_							(	snake_case_,	snake_case_,	unittest.TestCase							):




					'''simple docstring'''






					lowercase__   =			(EfficientNetModel, EfficientNetForImageClassification) if is_torch_available() else ()
					lowercase__   =			(
					    {"""feature-extraction""": EfficientNetModel, """image-classification""": EfficientNetForImageClassification}
					    if is_torch_available()
					    else {}
					)

					lowercase__   =			False
					lowercase__   =			False
					lowercase__   =			False
					lowercase__   =			False
					lowercase__   =			False






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	EfficientNetModelTester(self			)
							_snake_case     	=	ConfigTester(
							    self,  config_class=__UpperCamelCase,  has_text_modality=__UpperCamelCase,  hidden_size=37			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							self.create_and_test_config_common_properties()
							self.config_tester.create_and_test_config_to_json_string()
							self.config_tester.create_and_test_config_to_json_file()
							self.config_tester.create_and_test_config_from_and_save_pretrained()
							self.config_tester.create_and_test_config_with_num_labels()
							self.config_tester.check_config_can_be_init_without_params()
							self.config_tester.check_config_arguments_init()






					def     __SCREAMING_SNAKE_CASE     (   self			):
							return






					@unittest.skip(reason="EfficientNet does not use inputs_embeds"			)
					def     __SCREAMING_SNAKE_CASE     (   self			):
							pass






					@unittest.skip(reason="EfficientNet does not support input and output embeddings"			)
					def     __SCREAMING_SNAKE_CASE     (   self			):
							pass






					@unittest.skip(reason="EfficientNet does not use feedforward chunking"			)
					def     __SCREAMING_SNAKE_CASE     (   self			):
							pass






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									_snake_case     	=	model_class(__UpperCamelCase			)
									_snake_case     	=	inspect.signature(model.forward			)
									# signature.parameters is an OrderedDict => so arg_names order is deterministic
									_snake_case     	=	[*signature.parameters.keys()]

									_snake_case     	=	["pixel_values"]
									self.assertListEqual(arg_names[:1],  __UpperCamelCase			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__UpperCamelCase			)






					def     __SCREAMING_SNAKE_CASE     (   self			):


							def check_hidden_states_output(__UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase			):
									_snake_case     	=	model_class(__UpperCamelCase			)
									model.to(__UpperCamelCase			)
									model.eval()

									with torch.no_grad():
											_snake_case     	=	model(**self._prepare_for_class(__UpperCamelCase,  __UpperCamelCase			)			)

									_snake_case     	=	outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states
									_snake_case     	=	sum(config.num_block_repeats			) * 4
									self.assertEqual(len(__UpperCamelCase			),  __UpperCamelCase			)

									# EfficientNet's feature maps are of shape (batch_size, num_channels, height, width)
									self.assertListEqual(
									    list(hidden_states[0].shape[-2:]			),  [self.model_tester.image_size // 2, self.model_tester.image_size // 2],  )

							_snake_case     	=	self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									_snake_case     	=	True
									check_hidden_states_output(__UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase			)

									# check that output_hidden_states also work using config
									del inputs_dict["output_hidden_states"]
									_snake_case     	=	True

									check_hidden_states_output(__UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase			)






					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*__UpperCamelCase			)






					@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."			)
					def     __SCREAMING_SNAKE_CASE     (   self			):
							pass






					@slow
					def     __SCREAMING_SNAKE_CASE     (   self			):


							for model_name in EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									_snake_case     	=	EfficientNetModel.from_pretrained(__UpperCamelCase			)
									self.assertIsNotNone(__UpperCamelCase			)






					@is_pipeline_test
					@require_vision
					@slow
					def     __SCREAMING_SNAKE_CASE     (   self			):
							super().test_pipeline_image_classification()






def 		_lowercase		(     ):



		'''simple docstring'''







		_snake_case     	=	Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"  )
		return image





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




					'''simple docstring'''






					@cached_property
					def     __SCREAMING_SNAKE_CASE     (   self			):
							return AutoImageProcessor.from_pretrained("google/efficientnet-b7"			) if is_vision_available() else None






					@slow
					def     __SCREAMING_SNAKE_CASE     (   self			):
							_snake_case     	=	EfficientNetForImageClassification.from_pretrained("google/efficientnet-b7"			).to(__UpperCamelCase			)

							_snake_case     	=	self.default_image_processor
							_snake_case     	=	prepare_img()
							_snake_case     	=	image_processor(images=__UpperCamelCase,  return_tensors="pt"			).to(__UpperCamelCase			)

							# forward pass
							with torch.no_grad():
									_snake_case     	=	model(**__UpperCamelCase			)

							# verify the logits
							_snake_case     	=	torch.Size((1, 1_000)			)
							self.assertEqual(outputs.logits.shape,  __UpperCamelCase			)

							_snake_case     	=	torch.tensor([0.00_01, 0.00_02, 0.00_02]			).to(__UpperCamelCase			)
							self.assertTrue(torch.allclose(outputs.logits[0, :3],  __UpperCamelCase,  atol=1E-4			)			)











'''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()





