


import inspect
import unittest

from transformers import is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

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 MODEL_MAPPING, PoolFormerConfig, PoolFormerForImageClassification, PoolFormerModel
  from transformers.models.poolformer.modeling_poolformer import POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import PoolFormerImageProcessor

class   lowerCamelCase						(	lowercase__  ):

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Union[str, Any]							=   self.config_class(**self.inputs_dict       )
        self.parent.assertTrue(hasattr(a__						,    '''hidden_sizes'''       )       )
        self.parent.assertTrue(hasattr(a__						,    '''num_encoder_blocks'''       )       )







class   lowerCamelCase						:

       def __init__(      self						,    a__						,    a__=13						,    a__=64						,    a__=3						,    a__=4						,    a__=[2, 2, 2, 2]						,    a__=[8, 4, 2, 1]						,    a__=[16, 32, 64, 1_28]						,    a__=[1, 4, 8, 16]						,    a__=False						,    a__=True						,    a__="gelu"						,    a__=0.1						,    a__=0.02						,    a__=3						,    a__=None						,    ):






        '''simple docstring'''






        snake_case :			int							=   parent
        snake_case :			Dict							=   batch_size
        snake_case :			Any							=   image_size
        snake_case :			Any							=   num_channels
        snake_case :			Any							=   num_encoder_blocks
        snake_case :			Optional[Any]							=   sr_ratios
        snake_case :			Tuple							=   depths
        snake_case :			Tuple							=   hidden_sizes
        snake_case :			Optional[Any]							=   downsampling_rates
        snake_case :			List[Any]							=   is_training
        snake_case :			Any							=   use_labels
        snake_case :			List[str]							=   hidden_act
        snake_case :			Union[str, Any]							=   hidden_dropout_prob
        snake_case :			Dict							=   initializer_range
        snake_case :			Union[str, Any]							=   num_labels
        snake_case :			List[str]							=   scope

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Any							=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

        snake_case :			int							=   None
        if self.use_labels:
         snake_case :			List[str]							=   ids_tensor([self.batch_size, self.image_size, self.image_size]						,    self.num_labels       )

        snake_case :			Tuple							=   PoolFormerConfig(
            image_size=self.image_size						,    num_channels=self.num_channels						,    num_encoder_blocks=self.num_encoder_blocks						,    depths=self.depths						,    hidden_sizes=self.hidden_sizes						,    hidden_act=self.hidden_act						,    hidden_dropout_prob=self.hidden_dropout_prob						,    initializer_range=self.initializer_range						,    )

        return config, pixel_values, labels

       def 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__       ):






        '''simple docstring'''






        snake_case :			Any							=   PoolFormerModel(config=a__       )
        model.to(a__       )
        model.eval()
        snake_case :			Any							=   model(a__       )
        snake_case :			str							=   self.image_size // 32.0
        self.parent.assertEqual(
            result.last_hidden_state.shape						,    (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)       )



       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			List[Any]							=   self.prepare_config_and_inputs()
        snake_case :			Dict							=   config_and_inputs
        snake_case :			List[str]							=   {"pixel_values": pixel_values}
        return config, inputs_dict







@require_torch
class   lowerCamelCase						(	lowercase__,      lowercase__,      unittest.TestCase  ):
       _UpperCamelCase     		=   (PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
       _UpperCamelCase     		=   (
           {"""feature-extraction""": PoolFormerModel, """image-classification""": PoolFormerForImageClassification}
           if is_torch_available()
           else {}
       )

       _UpperCamelCase     		=   False
       _UpperCamelCase     		=   False
       _UpperCamelCase     		=   False
       _UpperCamelCase     		=   False
       _UpperCamelCase     		=   False

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			int							=   PoolFormerModelTester(self       )
        snake_case :			int							=   PoolFormerConfigTester(self						,    config_class=a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        self.config_tester.run_common_tests()

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			int							=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*a__       )

       @unittest.skip('''PoolFormer does not use inputs_embeds'''       )
       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        pass

       @unittest.skip('''PoolFormer does not have get_input_embeddings method and get_output_embeddings methods'''       )
       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        pass

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        def check_hidden_states_output(a__						,    a__						,    a__       ):
         snake_case :			List[Any]							=   model_class(a__       )
         model.to(a__       )
         model.eval()

         with torch.no_grad():
          snake_case :			Dict							=   model(**self._prepare_for_class(a__						,    a__       )       )

         snake_case :			Tuple							=   outputs.hidden_states

         snake_case :			Optional[int]							=   self.model_tester.num_encoder_blocks
         self.assertEqual(len(a__       )						,    a__       )

         # verify the first hidden states (first block)
         self.assertListEqual(
             list(hidden_states[0].shape[-3:]       )						,    [
                 self.model_tester.hidden_sizes[0],
                 self.model_tester.image_size // 4,
                 self.model_tester.image_size // 4,
             ]						,    )

        snake_case :			List[str]							=   self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
         snake_case :			Dict							=   True
         check_hidden_states_output(a__						,    a__						,    a__       )

         # check that output_hidden_states also work using config
         del inputs_dict["output_hidden_states"]
         snake_case :			Optional[int]							=   True

         check_hidden_states_output(a__						,    a__						,    a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        if not self.model_tester.is_training:
         return

        snake_case :			int							=   self.model_tester.prepare_config_and_inputs_for_common()
        snake_case :			List[Any]							=   True

        for model_class in self.all_model_classes:
         if model_class in get_values(a__       ):
          continue
         snake_case :			Optional[int]							=   model_class(a__       )
         model.to(a__       )
         model.train()
         snake_case :			Tuple							=   self._prepare_for_class(a__						,    a__						,    return_labels=a__       )
         snake_case :			Union[str, Any]							=   model(**a__       ).loss
         loss.backward()

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Tuple							=   self.model_tester.prepare_config_and_inputs_for_common()

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

         snake_case :			Optional[Any]							=   ["pixel_values"]
         self.assertListEqual(arg_names[:1]						,    a__       )



       @slow
       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         snake_case :			Any							=   PoolFormerModel.from_pretrained(a__       )
         self.assertIsNotNone(a__       )







def  _UpperCAmelCase						( )		->Union[str, Any]:


 '''simple docstring'''
 snake_case :			Any							=   Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''							)
 return image





@require_torch
class   lowerCamelCase						(	unittest.TestCase  ):

       @slow
       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Union[str, Any]							=   PoolFormerImageProcessor()
        snake_case :			Optional[Any]							=   PoolFormerForImageClassification.from_pretrained('''sail/poolformer_s12'''       ).to(a__       )

        snake_case :			Any							=   image_processor(images=prepare_img()						,    return_tensors='''pt'''       ).to(a__       )

        # forward pass
        with torch.no_grad():
         snake_case :			str							=   model(**a__       )

        # verify the logits
        snake_case :			str							=   torch.Size((1, 10_00)       )
        self.assertEqual(outputs.logits.shape						,    a__       )

        snake_case :			int							=   torch.tensor([-0.6113, 0.1685, -0.0492]       ).to(a__       )
        self.assertTrue(torch.allclose(outputs.logits[0, :3]						,    a__						,    atol=1e-4       )       )











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								=      logging.getLogger(__name__)


@dataclass
class 	snake_case_			:




       """simple docstring"""






       _lowerCamelCase           =					field(
           metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""}	)
       _lowerCamelCase           =					field(
           default=lowercase__			,			metadata={"""help""": """Pretrained config name or path if not the same as model_name"""}	)
       _lowerCamelCase           =					field(
           default=lowercase__			,			metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""}	)
       _lowerCamelCase           =					field(default=lowercase__			,			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.
       _lowerCamelCase           =					field(
           default=lowercase__			,			metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""}			,			)







def  _snake_case			(							)  ->  Union[str, Any]:






      '''simple docstring'''







      UpperCAmelCase_	:						Union[str, Any]					   =		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.
            UpperCAmelCase_   ,   UpperCAmelCase_   ,   UpperCAmelCase_	:						Any					   =		parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]     )     )
      else:
            UpperCAmelCase_   ,   UpperCAmelCase_   ,   UpperCAmelCase_	:						Union[str, Any]					   =		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"       ,	__snake_case     )

      # 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.

      UpperCAmelCase_	:						Any					   =		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       ,	)
      UpperCAmelCase_	:						List[str]					   =		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=__snake_case       ,	)
      UpperCAmelCase_	:						Optional[int]					   =		AutoModelForQuestionAnswering.from_pretrained(
          model_args.model_name_or_path       ,	from_tf=bool(".ckpt" in model_args.model_name_or_path     )       ,	config=__snake_case       ,	cache_dir=model_args.cache_dir       ,	)

      # Get datasets
      UpperCAmelCase_	:						str					   =		hasattr(model.config       ,	"lang2id"     )
      UpperCAmelCase_	:						Optional[Any]					   =		(
          SquadDataset(
              __snake_case       ,	tokenizer=__snake_case       ,	is_language_sensitive=__snake_case       ,	cache_dir=model_args.cache_dir     )
          if training_args.do_train
          else None
      )
      UpperCAmelCase_	:						Optional[Any]					   =		(
          SquadDataset(
              __snake_case       ,	tokenizer=__snake_case       ,	mode="dev"       ,	is_language_sensitive=__snake_case       ,	cache_dir=model_args.cache_dir       ,	)
          if training_args.do_eval
          else None
      )

      # Data collator
      UpperCAmelCase_	:						Optional[Any]					   =		DataCollatorWithPadding(__snake_case       ,	pad_to_multiple_of=8     ) if training_args.fpaa else None

      # Initialize our Trainer
      UpperCAmelCase_	:						Any					   =		Trainer(
          model=__snake_case       ,	args=__snake_case       ,	train_dataset=__snake_case       ,	eval_dataset=__snake_case       ,	data_collator=__snake_case       ,	)

      # 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  _snake_case			(							__snake_case     )  ->  List[str]:






      '''simple docstring'''







      main()


if __name__ == "__main__":
   main()
