




'''simple docstring'''



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   ( __lowerCamelCase):


       def       lowerCAmelCase      (   self  )      ->  List[str]:



            '''simple docstring'''




            __snake_case    					=  self.config_class(**self.inputs_dict  )
            self.parent.assertTrue(hasattr(__SCREAMING_SNAKE_CASE    ,  '''hidden_sizes'''  )  )
            self.parent.assertTrue(hasattr(__SCREAMING_SNAKE_CASE    ,  '''num_encoder_blocks'''  )  )





class     lowerCAmelCase   :


       def __init__(   self    ,  __SCREAMING_SNAKE_CASE    ,  __SCREAMING_SNAKE_CASE=13    ,  __SCREAMING_SNAKE_CASE=64    ,  __SCREAMING_SNAKE_CASE=3    ,  __SCREAMING_SNAKE_CASE=4    ,  __SCREAMING_SNAKE_CASE=[2, 2, 2, 2]    ,  __SCREAMING_SNAKE_CASE=[8, 4, 2, 1]    ,  __SCREAMING_SNAKE_CASE=[16, 32, 64, 128]    ,  __SCREAMING_SNAKE_CASE=[1, 4, 8, 16]    ,  __SCREAMING_SNAKE_CASE=False    ,  __SCREAMING_SNAKE_CASE=True    ,  __SCREAMING_SNAKE_CASE="gelu"    ,  __SCREAMING_SNAKE_CASE=0.1    ,  __SCREAMING_SNAKE_CASE=0.02    ,  __SCREAMING_SNAKE_CASE=3    ,  __SCREAMING_SNAKE_CASE=None    ,  )      ->  Dict:



            '''simple docstring'''




            __snake_case    					=  parent
            __snake_case    					=  batch_size
            __snake_case    					=  image_size
            __snake_case    					=  num_channels
            __snake_case    					=  num_encoder_blocks
            __snake_case    					=  sr_ratios
            __snake_case    					=  depths
            __snake_case    					=  hidden_sizes
            __snake_case    					=  downsampling_rates
            __snake_case    					=  is_training
            __snake_case    					=  use_labels
            __snake_case    					=  hidden_act
            __snake_case    					=  hidden_dropout_prob
            __snake_case    					=  initializer_range
            __snake_case    					=  num_labels
            __snake_case    					=  scope


       def       lowerCAmelCase      (   self  )      ->  Optional[int]:



            '''simple docstring'''




            __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.image_size, self.image_size]    ,  self.num_labels  )

            __snake_case    					=  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       lowerCAmelCase      (   self    ,  __SCREAMING_SNAKE_CASE    ,  __SCREAMING_SNAKE_CASE    ,  __SCREAMING_SNAKE_CASE  )      ->  Any:



            '''simple docstring'''




            __snake_case    					=  PoolFormerModel(config=__SCREAMING_SNAKE_CASE  )
            model.to(__SCREAMING_SNAKE_CASE  )
            model.eval()
            __snake_case    					=  model(__SCREAMING_SNAKE_CASE  )
            __snake_case    					=  __snake_case    					=  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       lowerCAmelCase      (   self  )      ->  str:



            '''simple docstring'''




            __snake_case    					=  self.prepare_config_and_inputs()
            __snake_case					,    __snake_case					,    __snake_case    					=  config_and_inputs
            __snake_case    					=  {'''pixel_values''': pixel_values}
            return config, inputs_dict





@require_torch
class     lowerCAmelCase   ( __lowerCamelCase				,   __lowerCamelCase				,   unittest.TestCase):
       __lowercase  :      Tuple        =			(PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
       __lowercase  :      Union[str, Any]        =			(
           {'feature-extraction': PoolFormerModel, 'image-classification': PoolFormerForImageClassification}
           if is_torch_available()
           else {}
       )

       __lowercase  :      Dict        =			False
       __lowercase  :      Dict        =			False
       __lowercase  :      Dict        =			False
       __lowercase  :      Optional[Any]        =			False
       __lowercase  :      Any        =			False


       def       lowerCAmelCase      (   self  )      ->  List[Any]:



            '''simple docstring'''




            __snake_case    					=  PoolFormerModelTester(self  )
            __snake_case    					=  PoolFormerConfigTester(self    ,  config_class=__SCREAMING_SNAKE_CASE  )


       def       lowerCAmelCase      (   self  )      ->  List[str]:



            '''simple docstring'''




            self.config_tester.run_common_tests()


       def       lowerCAmelCase      (   self  )      ->  Optional[int]:



            '''simple docstring'''




            __snake_case    					=  self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE  )


       @unittest.skip('''PoolFormer does not use inputs_embeds'''  )
       def       lowerCAmelCase      (   self  )      ->  Union[str, Any]:



            '''simple docstring'''




            pass


       @unittest.skip('''PoolFormer does not have get_input_embeddings method and get_output_embeddings methods'''  )
       def       lowerCAmelCase      (   self  )      ->  Union[str, Any]:



            '''simple docstring'''




            pass


       def       lowerCAmelCase      (   self  )      ->  Dict:



            '''simple docstring'''




            def check_hidden_states_output(__SCREAMING_SNAKE_CASE    ,  __SCREAMING_SNAKE_CASE    ,  __SCREAMING_SNAKE_CASE  ):
                 __snake_case    					=  model_class(__SCREAMING_SNAKE_CASE  )
                 model.to(__SCREAMING_SNAKE_CASE  )
                 model.eval()

                 with torch.no_grad():
                      __snake_case    					=  model(**self._prepare_for_class(__SCREAMING_SNAKE_CASE    ,  __SCREAMING_SNAKE_CASE  )  )

                 __snake_case    					=  outputs.hidden_states

                 __snake_case    					=  self.model_tester.num_encoder_blocks
                 self.assertEqual(len(__SCREAMING_SNAKE_CASE  )    ,  __SCREAMING_SNAKE_CASE  )

                 # 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					,    __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(__SCREAMING_SNAKE_CASE    ,  __SCREAMING_SNAKE_CASE    ,  __SCREAMING_SNAKE_CASE  )

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

                 check_hidden_states_output(__SCREAMING_SNAKE_CASE    ,  __SCREAMING_SNAKE_CASE    ,  __SCREAMING_SNAKE_CASE  )


       def       lowerCAmelCase      (   self  )      ->  List[str]:



            '''simple docstring'''




            if not self.model_tester.is_training:
                 return

            __snake_case					,    __snake_case    					=  self.model_tester.prepare_config_and_inputs_for_common()
            __snake_case    					=  True

            for model_class in self.all_model_classes:
                 if model_class in get_values(__SCREAMING_SNAKE_CASE  ):
                      continue
                 __snake_case    					=  model_class(__SCREAMING_SNAKE_CASE  )
                 model.to(__SCREAMING_SNAKE_CASE  )
                 model.train()
                 __snake_case    					=  self._prepare_for_class(__SCREAMING_SNAKE_CASE    ,  __SCREAMING_SNAKE_CASE    ,  return_labels=__SCREAMING_SNAKE_CASE  )
                 __snake_case    					=  model(**__SCREAMING_SNAKE_CASE  ).loss
                 loss.backward()


       def       lowerCAmelCase      (   self  )      ->  Optional[int]:



            '''simple docstring'''




            __snake_case					,    __snake_case    					=  self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                 __snake_case    					=  model_class(__SCREAMING_SNAKE_CASE  )
                 __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]    ,  __SCREAMING_SNAKE_CASE  )




       @slow
       def       lowerCAmelCase      (   self  )      ->  Optional[Any]:



            '''simple docstring'''




            for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 __snake_case    					=  PoolFormerModel.from_pretrained(__SCREAMING_SNAKE_CASE  )
                 self.assertIsNotNone(__SCREAMING_SNAKE_CASE  )







def 			_UpperCamelCase ()->   Dict:




     '''simple docstring'''





     __snake_case    					=  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
     return image







@require_torch
class     lowerCAmelCase   ( unittest.TestCase):


       @slow
       def       lowerCAmelCase      (   self  )      ->  List[str]:



            '''simple docstring'''




            __snake_case    					=  PoolFormerImageProcessor()
            __snake_case    					=  PoolFormerForImageClassification.from_pretrained('''sail/poolformer_s12'''  ).to(__SCREAMING_SNAKE_CASE  )

            __snake_case    					=  image_processor(images=prepare_img()    ,  return_tensors='''pt'''  ).to(__SCREAMING_SNAKE_CASE  )

            # forward pass
            with torch.no_grad():
                 __snake_case    					=  model(**__SCREAMING_SNAKE_CASE  )

            # verify the logits
            __snake_case    					=  torch.Size((1, 1000)  )
            self.assertEqual(outputs.logits.shape    ,  __SCREAMING_SNAKE_CASE  )

            __snake_case    					=  torch.tensor([-0.6_113, 0.1_685, -0.0_492]  ).to(__SCREAMING_SNAKE_CASE  )
            self.assertTrue(torch.allclose(outputs.logits[0, :3]    ,  __SCREAMING_SNAKE_CASE    ,  atol=1E-4  )  )








'''simple docstring'''



# Copyright 2022 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 os
from typing import List







def 			_UpperCamelCase (_lowerCamelCase							:     List[str]   ,	_lowerCamelCase							:     str	)->   str:




     '''simple docstring'''





     if name != "training_function" and name != "main":
          raise ValueError(f'''Incorrect function name passed: {name}, choose either \'main\' or \'training_function\''''	)
     __snake_case					,    __snake_case    					=  [], False
     for line in lines:
          if not found_start and f'''def {name}''' in line:
               __snake_case    					=  True
               good_lines.append(_lowerCamelCase	)
               continue
          if found_start:
               if name == "training_function" and "def main" in line:
                    return good_lines
               if name == "main" and "if __name__" in line:
                    return good_lines
               good_lines.append(_lowerCamelCase	)







def 			_UpperCamelCase (_lowerCamelCase							:     List[str]	)->   int:




     '''simple docstring'''





     return [line for line in lines if not line.lstrip().startswith('''#'''	) and line != "\n"]







def 			_UpperCamelCase (_lowerCamelCase							:     str   ,	_lowerCamelCase							:     str   ,	_lowerCamelCase							:     bool   ,	_lowerCamelCase							:     str = None	)->   Optional[Any]:




     '''simple docstring'''





     with open(_lowerCamelCase   ,	'''r'''	) as f:
          __snake_case    					=  f.readlines()
     with open(os.path.abspath(os.path.join('''examples'''   ,	'''nlp_example.py'''	)	)   ,	'''r'''	) as f:
          __snake_case    					=  f.readlines()
     with open(_lowerCamelCase   ,	'''r'''	) as f:
          __snake_case    					=  f.readlines()
     if secondary_filename is not None:
          with open(_lowerCamelCase   ,	'''r'''	) as f:
               __snake_case    					=  f.readlines()

    # This is our base, we remove all the code from here in our `full_filename` and `feature_filename` to find the new content
     if parser_only:
          __snake_case    					=  clean_lines(get_function_contents_by_name(_lowerCamelCase   ,	'''main'''	)	)
          __snake_case    					=  clean_lines(get_function_contents_by_name(_lowerCamelCase   ,	'''main'''	)	)
          __snake_case    					=  clean_lines(get_function_contents_by_name(_lowerCamelCase   ,	'''main'''	)	)
          if secondary_filename is not None:
               __snake_case    					=  clean_lines(get_function_contents_by_name(_lowerCamelCase   ,	'''main'''	)	)
     else:
          __snake_case    					=  clean_lines(get_function_contents_by_name(_lowerCamelCase   ,	'''training_function'''	)	)
          __snake_case    					=  clean_lines(get_function_contents_by_name(_lowerCamelCase   ,	'''training_function'''	)	)
          __snake_case    					=  clean_lines(get_function_contents_by_name(_lowerCamelCase   ,	'''training_function'''	)	)
          if secondary_filename is not None:
               __snake_case    					=  clean_lines(
                   get_function_contents_by_name(_lowerCamelCase   ,	'''training_function'''	)	)

     __snake_case    					=  '''train_dataloader, eval_dataloader = get_dataloaders(accelerator, batch_size)\n'''

     # Specific code in our script that differs from the full version, aka what is new
     __snake_case    					=  []
     __snake_case    					=  []  # We keep track of the idxs just in case it's a repeated statement
     __snake_case    					=  iter(_lowerCamelCase	)
     for i in range(len(_lowerCamelCase	) - 1	):
          if i not in passed_idxs:
               __snake_case    					=  next(_lowerCamelCase	)
               if (line not in full_file_func) and (line.lstrip() != _dl_line):
                    if "TESTING_MOCKED_DATALOADERS" not in line:
                         new_feature_code.append(_lowerCamelCase	)
                         passed_idxs.append(_lowerCamelCase	)
                    else:
                         # Skip over the `config['num_epochs'] = 2` statement
                         __snake_case    					=  next(_lowerCamelCase	)

    # Extract out just the new parts from the full_file_training_func
     __snake_case    					=  []
     __snake_case    					=  []  # We keep track of the idxs just in case it's a repeated statement
     for i, line in enumerate(_lowerCamelCase	):
          if i not in passed_idxs:
               if (line not in full_file_func) and (line.lstrip() != _dl_line):
                    if "TESTING_MOCKED_DATALOADERS" not in line:
                         new_full_example_parts.append(_lowerCamelCase	)
                         passed_idxs.append(_lowerCamelCase	)

    # Finally, get the overall diff
     __snake_case    					=  [line for line in new_feature_code if line not in new_full_example_parts]
     if secondary_filename is not None:
          __snake_case    					=  [line for line in full_file_contents if line not in secondary_file_func]
          __snake_case    					=  [line for line in diff_from_example if line not in diff_from_two]

     return diff_from_example






