






"""simple docstring"""







from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import ConvNextConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
			import tensorflow as tf

			from transformers import TFConvNextForImageClassification, TFConvNextModel


if is_vision_available():
			from PIL import Image

			from transformers import ConvNextImageProcessor


class     __UpperCAmelCase:




						"""simple docstring"""




						def __init__(   self       ,  snake_case__       ,  snake_case__=13       ,  snake_case__=32       ,  snake_case__=3       ,  snake_case__=4       ,  snake_case__=[10, 20, 30, 40]       ,  snake_case__=[2, 2, 3, 2]       ,  snake_case__=True       ,  snake_case__=True       ,  snake_case__=37       ,  snake_case__="gelu"       ,  snake_case__=10       ,  snake_case__=0.02       ,  snake_case__=3       ,  snake_case__=None       ,  ):
												'''simple docstring'''
												lowercase__				:      Tuple=       parent
												lowercase__				:      Dict=       batch_size
												lowercase__				:      Optional[Any]=       image_size
												lowercase__				:      Optional[int]=       num_channels
												lowercase__				:      Any=       num_stages
												lowercase__				:      Optional[int]=       hidden_sizes
												lowercase__				:      int=       depths
												lowercase__				:      Tuple=       is_training
												lowercase__				:      Tuple=       use_labels
												lowercase__				:      Dict=       intermediate_size
												lowercase__				:      Tuple=       hidden_act
												lowercase__				:      Dict=       type_sequence_label_size
												lowercase__				:      str=       initializer_range
												lowercase__				:      int=       scope




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      List[str]=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

												lowercase__				:      List[str]=       None
												if self.use_labels:
																		lowercase__				:      List[Any]=       ids_tensor([self.batch_size]       ,  self.type_sequence_label_size			)

												lowercase__				:      Optional[int]=       self.get_config()

												return config, pixel_values, labels




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												return ConvNextConfig(
												    num_channels=self.num_channels       ,  hidden_sizes=self.hidden_sizes       ,  depths=self.depths       ,  num_stages=self.num_stages       ,  hidden_act=self.hidden_act       ,  is_decoder=snake_case__       ,  initializer_range=self.initializer_range       ,  )




						def 		UpperCAmelCase_       (   self       ,  snake_case__       ,  snake_case__       ,  snake_case__			):
												'''simple docstring'''
												lowercase__				:      Tuple=       TFConvNextModel(config=snake_case__			)
												lowercase__				:      str=       model(snake_case__       ,  training=snake_case__			)
												# expected last hidden states: B, C, H // 32, W // 32
												self.parent.assertEqual(
												    result.last_hidden_state.shape       ,  (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)       ,  )




						def 		UpperCAmelCase_       (   self       ,  snake_case__       ,  snake_case__       ,  snake_case__			):
												'''simple docstring'''
												lowercase__				:      Dict=       self.type_sequence_label_size
												lowercase__				:      List[str]=       TFConvNextForImageClassification(snake_case__			)
												lowercase__				:      List[Any]=       model(snake_case__       ,  labels=snake_case__       ,  training=snake_case__			)
												self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.type_sequence_label_size)			)




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      Dict=       self.prepare_config_and_inputs()
												lowercase__,	lowercase__,	lowercase__				:      Optional[Any]=       config_and_inputs
												lowercase__				:      List[Any]=       {"pixel_values": pixel_values}
												return config, inputs_dict


@require_tf
class     __UpperCAmelCase(   __snake_case  ,  __snake_case  ,  unittest.TestCase							):




						"""simple docstring"""




						__lowerCamelCase				       =					(TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
						__lowerCamelCase				       =					(
						    {'feature-extraction': TFConvNextModel, 'image-classification': TFConvNextForImageClassification}
						    if is_tf_available()
						    else {}
						)

						__lowerCamelCase				       =					False
						__lowerCamelCase				       =					False
						__lowerCamelCase				       =					False
						__lowerCamelCase				       =					False
						__lowerCamelCase				       =					False




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      Union[str, Any]=       TFConvNextModelTester(self			)
												lowercase__				:      str=       ConfigTester(
												    self       ,  config_class=snake_case__       ,  has_text_modality=snake_case__       ,  hidden_size=37       ,  )




						@unittest.skip(reason="ConvNext does not use inputs_embeds"			)
						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												pass




						@unittest.skipIf(
						    not is_tf_available() or len(tf.config.list_physical_devices("GPU"			)			) == 0       ,  reason="TF does not support backprop for grouped convolutions on CPU."       ,  )
						@slow
						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												super().test_keras_fit()




						@unittest.skip(reason="ConvNext does not support input and output embeddings"			)
						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												pass




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__,	lowercase__				:      Optional[int]=       self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																		lowercase__				:      str=       model_class(snake_case__			)
																		lowercase__				:      List[Any]=       inspect.signature(model.call			)
																		# signature.parameters is an OrderedDict => so arg_names order is deterministic
																		lowercase__				:      int=       [*signature.parameters.keys()]

																		lowercase__				:      Optional[int]=       ["pixel_values"]
																		self.assertListEqual(arg_names[:1]       ,  snake_case__			)




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      str=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_model(*snake_case__			)




						@unittest.skipIf(
						    not is_tf_available() or len(tf.config.list_physical_devices("GPU"			)			) == 0       ,  reason="TF does not support backprop for grouped convolutions on CPU."       ,  )
						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												super().test_dataset_conversion()




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												def check_hidden_states_output(snake_case__       ,  snake_case__       ,  snake_case__			):
																		lowercase__				:      Optional[int]=       model_class(snake_case__			)

																		lowercase__				:      Optional[int]=       model(**self._prepare_for_class(snake_case__       ,  snake_case__			)			)
																		lowercase__				:      Optional[int]=       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

																		lowercase__				:      Optional[int]=       self.model_tester.num_stages
																		self.assertEqual(len(snake_case__			)       ,  expected_num_stages + 1			)

																		# ConvNext'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 // 4, self.model_tester.image_size // 4]       ,  )

												lowercase__,	lowercase__				:      Any=       self.model_tester.prepare_config_and_inputs_for_common()

												for model_class in self.all_model_classes:
																		lowercase__				:      List[str]=       True
																		check_hidden_states_output(snake_case__       ,  snake_case__       ,  snake_case__			)

																		# check that output_hidden_states also work using config
																		del inputs_dict["output_hidden_states"]
																		lowercase__				:      Dict=       True

																		check_hidden_states_output(snake_case__       ,  snake_case__       ,  snake_case__			)




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__,	lowercase__				:      List[Any]=       self.model_tester.prepare_config_and_inputs_for_common()

												def check_equivalence(snake_case__       ,  snake_case__       ,  snake_case__       ,  snake_case__={}			):
																		lowercase__				:      List[str]=       model(snake_case__       ,  return_dict=snake_case__       ,  **snake_case__			)
																		lowercase__				:      Any=       model(snake_case__       ,  return_dict=snake_case__       ,  **snake_case__			).to_tuple()

																		def recursive_check(snake_case__       ,  snake_case__			):
																								if isinstance(snake_case__       ,  (List, Tuple)			):
																														for tuple_iterable_value, dict_iterable_value in zip(snake_case__       ,  snake_case__			):
																																				recursive_check(snake_case__       ,  snake_case__			)
																								elif tuple_object is None:
																														return
																								else:
																														self.assertTrue(
																														    all(tf.equal(snake_case__       ,  snake_case__			)			)       ,  msg=(
																														        "Tuple and dict output are not equal. Difference:"
																														        F''' {tf.math.reduce_max(tf.abs(tuple_object - dict_object			)			)}'''
																														    )       ,  )

																								recursive_check(snake_case__       ,  snake_case__			)

												for model_class in self.all_model_classes:
																		lowercase__				:      List[str]=       model_class(snake_case__			)

																		lowercase__				:      List[Any]=       self._prepare_for_class(snake_case__       ,  snake_case__			)
																		lowercase__				:      Optional[int]=       self._prepare_for_class(snake_case__       ,  snake_case__			)
																		check_equivalence(snake_case__       ,  snake_case__       ,  snake_case__			)

																		lowercase__				:      str=       self._prepare_for_class(snake_case__       ,  snake_case__       ,  return_labels=snake_case__			)
																		lowercase__				:      int=       self._prepare_for_class(snake_case__       ,  snake_case__       ,  return_labels=snake_case__			)
																		check_equivalence(snake_case__       ,  snake_case__       ,  snake_case__			)

																		lowercase__				:      Tuple=       self._prepare_for_class(snake_case__       ,  snake_case__			)
																		lowercase__				:      Any=       self._prepare_for_class(snake_case__       ,  snake_case__			)
																		check_equivalence(snake_case__       ,  snake_case__       ,  snake_case__       ,  {"output_hidden_states": True}			)

																		lowercase__				:      Optional[Any]=       self._prepare_for_class(snake_case__       ,  snake_case__       ,  return_labels=snake_case__			)
																		lowercase__				:      Any=       self._prepare_for_class(snake_case__       ,  snake_case__       ,  return_labels=snake_case__			)
																		check_equivalence(snake_case__       ,  snake_case__       ,  snake_case__       ,  {"output_hidden_states": True}			)




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      Union[str, Any]=       self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_for_image_classification(*snake_case__			)




						@slow
						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      Optional[Any]=       TFConvNextModel.from_pretrained("facebook/convnext-tiny-224"			)
												self.assertIsNotNone(snake_case__			)





def 						lowercase__()				->Tuple:




						"""simple docstring"""



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







@require_tf
@require_vision
class     __UpperCAmelCase(   unittest.TestCase							):




						"""simple docstring"""




						@cached_property
						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												return ConvNextImageProcessor.from_pretrained("facebook/convnext-tiny-224"			) if is_vision_available() else None




						@slow
						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												lowercase__				:      Dict=       TFConvNextForImageClassification.from_pretrained("facebook/convnext-tiny-224"			)

												lowercase__				:      Optional[int]=       self.default_image_processor
												lowercase__				:      List[Any]=       prepare_img()
												lowercase__				:      Any=       image_processor(images=snake_case__       ,  return_tensors="tf"			)

												# forward pass
												lowercase__				:      Union[str, Any]=       model(**snake_case__			)

												# verify the logits
												lowercase__				:      List[str]=       tf.TensorShape((1, 1000)			)
												self.assertEqual(outputs.logits.shape       ,  snake_case__			)

												lowercase__				:      int=       tf.constant([-0.02_60, -0.47_39, 0.19_11]			)

												tf.debugging.assert_near(outputs.logits[0, :3]       ,  snake_case__       ,  atol=1e-4			)









from __future__ import annotations

from collections.abc import Generator



def    UpperCamelCase(					lowercase_							)	->				Generator[int, None, None]:





  '''simple docstring'''





  if not isinstance(lowercase_			,					lowercase_							) or n < 1:
    raise Exception("""Sequence only defined for positive integers"""							)

  yield n
  while n != 1:
    if n % 2 == 0:
      n //= 2
    else:
      snake_case_       						=      3 * n + 1
    yield n



def    UpperCamelCase(					)	->				Optional[int]:





  '''simple docstring'''





  snake_case_       						=      43
  snake_case_       						=      tuple(collatz_sequence(lowercase_							)							)
  print(lowercase_							)
  print(f'''Collatz sequence from {n} took {len(lowercase_							)} steps.'''							)


if __name__ == "__main__":
    main()