



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 		__SCREAMING_SNAKE_CASE      (				lowercase__      ):






				"""simple docstring"""

				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					_snake_case					:							Tuple					     =   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 		__SCREAMING_SNAKE_CASE      :






				"""simple docstring"""

				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, 1_28]						,	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						,	):






					"""simple docstring"""

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







				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					_snake_case					:							Union[str, Any]					     =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

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

					_snake_case					:							Union[str, Any]					     =   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__      ):






					"""simple docstring"""

					_snake_case					:							Dict					     =   PoolFormerModel(config=SCREAMING_SNAKE_CASE__      )
					model.to(SCREAMING_SNAKE_CASE__      )
					model.eval()
					_snake_case					:							Dict					     =   model(SCREAMING_SNAKE_CASE__      )
					_snake_case					:							Union[str, Any]					     =   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      ):






					"""simple docstring"""

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





@require_torch
class 		__SCREAMING_SNAKE_CASE      (				lowercase__					,			lowercase__					,			unittest.TestCase      ):






				"""simple docstring"""

				SCREAMING_SNAKE_CASE_ 				=  (PoolFormerModel, PoolFormerForImageClassification) if is_torch_available() else ()
				SCREAMING_SNAKE_CASE_ 				=  (
				    {'feature-extraction': PoolFormerModel, 'image-classification': PoolFormerForImageClassification}
				    if is_torch_available()
				    else {}
				)

				SCREAMING_SNAKE_CASE_ 				=  False
				SCREAMING_SNAKE_CASE_ 				=  False
				SCREAMING_SNAKE_CASE_ 				=  False
				SCREAMING_SNAKE_CASE_ 				=  False
				SCREAMING_SNAKE_CASE_ 				=  False







				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					_snake_case					:							Tuple					     =   PoolFormerModelTester(self      )
					_snake_case					:							Any					     =   PoolFormerConfigTester(self						,	config_class=SCREAMING_SNAKE_CASE__      )







				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					self.config_tester.run_common_tests()







				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					_snake_case					:							Dict					     =   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      ):






					"""simple docstring"""

					pass







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






					"""simple docstring"""

					pass







				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

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

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

						_snake_case					:							Dict					     =   outputs.hidden_states

						_snake_case					:							List[str]					     =   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					:							List[str]					     =   self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
						_snake_case					:							Optional[Any]					     =   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					:							Optional[int]					     =   True

						check_hidden_states_output(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__      )







				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					if not self.model_tester.is_training:
						return

					_snake_case     , _snake_case					:							Optional[Any]					     =   self.model_tester.prepare_config_and_inputs_for_common()
					_snake_case					:							str					     =   True

					for model_class in self.all_model_classes:
						if model_class in get_values(SCREAMING_SNAKE_CASE__      ):
							continue
						_snake_case					:							List[str]					     =   model_class(SCREAMING_SNAKE_CASE__      )
						model.to(SCREAMING_SNAKE_CASE__      )
						model.train()
						_snake_case					:							str					     =   self._prepare_for_class(SCREAMING_SNAKE_CASE__						,	SCREAMING_SNAKE_CASE__						,	return_labels=SCREAMING_SNAKE_CASE__      )
						_snake_case					:							List[str]					     =   model(**SCREAMING_SNAKE_CASE__      ).loss
						loss.backward()







				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					_snake_case     , _snake_case					:							Dict					     =   self.model_tester.prepare_config_and_inputs_for_common()

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

						_snake_case					:							Union[str, Any]					     =   ["""pixel_values"""]
						self.assertListEqual(arg_names[:1]						,	SCREAMING_SNAKE_CASE__      )







				@slow
				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					for model_name in POOLFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						_snake_case					:							Any					     =   PoolFormerModel.from_pretrained(SCREAMING_SNAKE_CASE__      )
						self.assertIsNotNone(SCREAMING_SNAKE_CASE__      )
def   UpperCAmelCase							(		)							->					Union[str, Any]:
	_snake_case					:							int					     =   Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""     )
	return image





@require_torch
class 		__SCREAMING_SNAKE_CASE      (				unittest.TestCase      ):






				"""simple docstring"""

				@slow
				def 		__lowerCamelCase(				self      ):






					"""simple docstring"""

					_snake_case					:							Any					     =   PoolFormerImageProcessor()
					_snake_case					:							Any					     =   PoolFormerForImageClassification.from_pretrained("""sail/poolformer_s12"""      ).to(SCREAMING_SNAKE_CASE__      )

					_snake_case					:							Union[str, Any]					     =   image_processor(images=prepare_img()						,	return_tensors="""pt"""      ).to(SCREAMING_SNAKE_CASE__      )

					# forward pass
					with torch.no_grad():
						_snake_case					:							Optional[int]					     =   model(**SCREAMING_SNAKE_CASE__      )

					# verify the logits
					_snake_case					:							Tuple					     =   torch.Size((1, 10_00)      )
					self.assertEqual(outputs.logits.shape						,	SCREAMING_SNAKE_CASE__      )

					_snake_case					:							List[Any]					     =   torch.tensor([-0.6113, 0.1685, -0.0492]      ).to(SCREAMING_SNAKE_CASE__      )
					self.assertTrue(torch.allclose(outputs.logits[0, :3]						,	SCREAMING_SNAKE_CASE__						,	atol=1e-4      )      )










# 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							(		A__				,  A__     )							->					Any:
	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					:							Union[str, Any]					     =   [], False
	for line in lines:
		if not found_start and f'''def {name}''' in line:
			_snake_case					:							Tuple					     =   True
			good_lines.append(A__     )
			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(A__     )
def   UpperCAmelCase							(		A__     )							->					Dict:
	return [line for line in lines if not line.lstrip().startswith("""#"""     ) and line != "\n"]


def   UpperCAmelCase							(		A__				,  A__				,  A__				,  A__ = None     )							->					str:
	with open(A__				,  """r"""     ) as f:
		_snake_case					:							Tuple					     =   f.readlines()
	with open(os.path.abspath(os.path.join("""examples"""				,  """nlp_example.py"""     )     )				,  """r"""     ) as f:
		_snake_case					:							Optional[Any]					     =   f.readlines()
	with open(A__				,  """r"""     ) as f:
		_snake_case					:							Optional[Any]					     =   f.readlines()
	if secondary_filename is not None:
		with open(A__				,  """r"""     ) as f:
			_snake_case					:							List[str]					     =   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					:							Dict					     =   clean_lines(get_function_contents_by_name(A__				,  """main"""     )     )
		_snake_case					:							int					     =   clean_lines(get_function_contents_by_name(A__				,  """main"""     )     )
		_snake_case					:							Optional[Any]					     =   clean_lines(get_function_contents_by_name(A__				,  """main"""     )     )
		if secondary_filename is not None:
			_snake_case					:							Optional[Any]					     =   clean_lines(get_function_contents_by_name(A__				,  """main"""     )     )
	else:
		_snake_case					:							str					     =   clean_lines(get_function_contents_by_name(A__				,  """training_function"""     )     )
		_snake_case					:							List[Any]					     =   clean_lines(get_function_contents_by_name(A__				,  """training_function"""     )     )
		_snake_case					:							Union[str, Any]					     =   clean_lines(get_function_contents_by_name(A__				,  """training_function"""     )     )
		if secondary_filename is not None:
			_snake_case					:							List[Any]					     =   clean_lines(
			    get_function_contents_by_name(A__				,  """training_function"""     )     )

	_snake_case					:							str					     =   """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					:							Dict					     =   []
	_snake_case					:							str					     =   []  # We keep track of the idxs just in case it's a repeated statement
	_snake_case					:							Optional[Any]					     =   iter(A__     )
	for i in range(len(A__     ) - 1     ):
		if i not in passed_idxs:
			_snake_case					:							Any					     =   next(A__     )
			if (line not in full_file_func) and (line.lstrip() != _dl_line):
				if "TESTING_MOCKED_DATALOADERS" not in line:
					new_feature_code.append(A__     )
					passed_idxs.append(A__     )
				else:
					# Skip over the `config['num_epochs'] = 2` statement
					_snake_case					:							Optional[int]					     =   next(A__     )

    # Extract out just the new parts from the full_file_training_func
	_snake_case					:							Any					     =   []
	_snake_case					:							Tuple					     =   []  # We keep track of the idxs just in case it's a repeated statement
	for i, line in enumerate(A__     ):
		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(A__     )
					passed_idxs.append(A__     )

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

	return diff_from_example





