




'''simple docstring'''







from __future__ import annotations

import inspect
import unittest

from transformers import ViTConfig
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 TFViTForImageClassification, TFViTModel


if is_vision_available():
   from PIL import Image

   from transformers import ViTImageProcessor


class 			_a  :
       def __init__(	self						:	int   ,				lowercase						:	Union[str, Any]   ,				lowercase						:	List[Any]=13   ,				lowercase						:	Optional[int]=30   ,				lowercase						:	Optional[Any]=2   ,				lowercase						:	Optional[int]=3   ,				lowercase						:	List[Any]=True   ,				lowercase						:	List[str]=True   ,				lowercase						:	Union[str, Any]=32   ,				lowercase						:	List[Any]=2   ,				lowercase						:	List[str]=4   ,				lowercase						:	int=37   ,				lowercase						:	Any="gelu"   ,				lowercase						:	int=0.1   ,				lowercase						:	Tuple=0.1   ,				lowercase						:	Tuple=10   ,				lowercase						:	Dict=0.02   ,				lowercase						:	Optional[int]=3   ,				lowercase						:	Dict=None   ,				):



           '''simple docstring'''

           UpperCAmelCase			       =		parent
           UpperCAmelCase			       =		batch_size
           UpperCAmelCase			       =		image_size
           UpperCAmelCase			       =		patch_size
           UpperCAmelCase			       =		num_channels
           UpperCAmelCase			       =		is_training
           UpperCAmelCase			       =		use_labels
           UpperCAmelCase			       =		hidden_size
           UpperCAmelCase			       =		num_hidden_layers
           UpperCAmelCase			       =		num_attention_heads
           UpperCAmelCase			       =		intermediate_size
           UpperCAmelCase			       =		hidden_act
           UpperCAmelCase			       =		hidden_dropout_prob
           UpperCAmelCase			       =		attention_probs_dropout_prob
           UpperCAmelCase			       =		type_sequence_label_size
           UpperCAmelCase			       =		initializer_range
           UpperCAmelCase			       =		scope

           # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
           UpperCAmelCase			       =		(image_size // patch_size) ** 2
           UpperCAmelCase			       =		num_patches + 1
       def     A      (	self						:	Union[str, Any]				):



           '''simple docstring'''

           UpperCAmelCase			       =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

           UpperCAmelCase			       =		None
           if self.use_labels:
               UpperCAmelCase			       =		ids_tensor([self.batch_size]   ,				self.type_sequence_label_size				)

           UpperCAmelCase			       =		self.get_config()

           return config, pixel_values, labels
       def     A      (	self						:	Dict				):



           '''simple docstring'''

           return ViTConfig(
               image_size=self.image_size   ,				patch_size=self.patch_size   ,				num_channels=self.num_channels   ,				hidden_size=self.hidden_size   ,				num_hidden_layers=self.num_hidden_layers   ,				num_attention_heads=self.num_attention_heads   ,				intermediate_size=self.intermediate_size   ,				hidden_act=self.hidden_act   ,				hidden_dropout_prob=self.hidden_dropout_prob   ,				attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,				is_decoder=_lowerCamelCase   ,				initializer_range=self.initializer_range   ,				)
       def     A      (	self						:	Tuple   ,				lowercase						:	List[str]   ,				lowercase						:	Union[str, Any]   ,				lowercase						:	Any				):



           '''simple docstring'''

           UpperCAmelCase			       =		TFViTModel(config=_lowerCamelCase				)
           UpperCAmelCase			       =		model(_lowerCamelCase   ,				training=_lowerCamelCase				)
           self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.seq_length, self.hidden_size)				)

           # Test with an image with different size than the one specified in config.
           UpperCAmelCase			       =		self.image_size // 2
           UpperCAmelCase			       =		pixel_values[:, :, :image_size, :image_size]
           UpperCAmelCase			       =		model(_lowerCamelCase   ,				interpolate_pos_encoding=_lowerCamelCase   ,				training=_lowerCamelCase				)
           UpperCAmelCase			       =		(image_size // self.patch_size) ** 2 + 1
           self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, seq_length, self.hidden_size)				)
       def     A      (	self						:	Union[str, Any]   ,				lowercase						:	List[Any]   ,				lowercase						:	Tuple   ,				lowercase						:	str				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.type_sequence_label_size
           UpperCAmelCase			       =		TFViTForImageClassification(_lowerCamelCase				)
           UpperCAmelCase			       =		model(_lowerCamelCase   ,				labels=_lowerCamelCase   ,				training=_lowerCamelCase				)
           self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.type_sequence_label_size)				)

           # Test with an image with different size than the one specified in config.
           UpperCAmelCase			       =		self.image_size // 2
           UpperCAmelCase			       =		pixel_values[:, :, :image_size, :image_size]
           UpperCAmelCase			       =		model(_lowerCamelCase   ,				interpolate_pos_encoding=_lowerCamelCase   ,				training=_lowerCamelCase				)
           self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.type_sequence_label_size)				)

           # test greyscale images
           UpperCAmelCase			       =		1
           UpperCAmelCase			       =		TFViTForImageClassification(_lowerCamelCase				)
           UpperCAmelCase			       =		floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
           UpperCAmelCase			       =		model(_lowerCamelCase				)
           self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.type_sequence_label_size)				)







       def     A      (	self						:	Optional[Any]				):



           '''simple docstring'''

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






@require_tf
class 			_a  (	A_							,							A_							,							unittest.TestCase ):
       __a				:     Optional[Any]   				=					(TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
       __a				:     str   				=					(
           {"""feature-extraction""": TFViTModel, """image-classification""": TFViTForImageClassification}
           if is_tf_available()
           else {}
       )

       __a				:     List[str]   				=					False
       __a				:     List[Any]   				=					False
       __a				:     Tuple   				=					False
       def     A      (	self						:	Union[str, Any]				):



           '''simple docstring'''

           UpperCAmelCase			       =		TFViTModelTester(self				)
           UpperCAmelCase			       =		ConfigTester(self   ,				config_class=_lowerCamelCase   ,				has_text_modality=_lowerCamelCase   ,				hidden_size=37				)
       def     A      (	self						:	Union[str, Any]				):



           '''simple docstring'''

           self.config_tester.run_common_tests()
       @unittest.skip(reason='''ViT does not use inputs_embeds'''				)
       def     A      (	self						:	int				):



           '''simple docstring'''

           pass
       @unittest.skip(reason='''ViT does not use inputs_embeds'''				)
       def     A      (	self						:	Optional[int]				):



           '''simple docstring'''

           pass
       def     A      (	self						:	str				):



           '''simple docstring'''

           UpperCAmelCase       ,     UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               UpperCAmelCase			       =		model_class(_lowerCamelCase				)
               self.assertIsInstance(model.get_input_embeddings()   ,				(tf.keras.layers.Layer)				)
               UpperCAmelCase			       =		model.get_output_embeddings()
               self.assertTrue(x is None or isinstance(_lowerCamelCase   ,				tf.keras.layers.Layer				)				)
       def     A      (	self						:	List[str]				):



           '''simple docstring'''

           UpperCAmelCase       ,     UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs_for_common()

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

               UpperCAmelCase			       =		['''pixel_values''']
               self.assertListEqual(arg_names[:1]   ,				_lowerCamelCase				)
       def     A      (	self						:	Optional[Any]				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*_lowerCamelCase				)
       def     A      (	self						:	Dict				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase				)







       @slow
       def     A      (	self						:	Tuple				):



           '''simple docstring'''

           UpperCAmelCase			       =		TFViTModel.from_pretrained('''google/vit-base-patch16-224'''				)
           self.assertIsNotNone(_lowerCamelCase				)






def 						snake_case_							():
    UpperCAmelCase			       =		Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''     )
    return image







@require_tf
@require_vision
class 			_a  (	unittest.TestCase ):
       @cached_property
       def     A      (	self						:	Optional[int]				):



           '''simple docstring'''

           return ViTImageProcessor.from_pretrained('''google/vit-base-patch16-224'''				) if is_vision_available() else None







       @slow
       def     A      (	self						:	Any				):



           '''simple docstring'''

           UpperCAmelCase			       =		TFViTForImageClassification.from_pretrained('''google/vit-base-patch16-224'''				)

           UpperCAmelCase			       =		self.default_image_processor
           UpperCAmelCase			       =		prepare_img()
           UpperCAmelCase			       =		image_processor(images=_lowerCamelCase   ,				return_tensors='''tf'''				)

           # forward pass
           UpperCAmelCase			       =		model(**_lowerCamelCase				)

           # verify the logits
           UpperCAmelCase			       =		tf.TensorShape((1, 1_000)				)
           self.assertEqual(outputs.logits.shape   ,				_lowerCamelCase				)

           UpperCAmelCase			       =		tf.constant([-0.2744, 0.8215, -0.0836]				)

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






"""simple docstring"""



from __future__ import annotations

from bisect import bisect_left
from functools import total_ordering
from heapq import merge

@total_ordering
class       lowerCAmelCase__				(						A_	):







     def __lt__(							self			:			Any    ,      _lowerCamelCase			:			int			):
       return self[-1] < other[-1]







     def __eq__(							self			:			int    ,      _lowerCamelCase			:			Optional[Any]			):
       return self[-1] == other[-1]






def 					_UpperCAmelCase      ( __lowerCamelCase   :       list		)     ->					list:
  _snake_case					     =       []
  # sort into stacks
  for element in collection:
    _snake_case					     =       Stack([element]		)
    _snake_case					     =       bisect_left(__lowerCamelCase ,					__lowerCamelCase		)
    if i != len(__lowerCamelCase		):
      stacks[i].append(__lowerCamelCase		)
    else:
      stacks.append(__lowerCamelCase		)

    # use a heap-based merge to merge stack efficiently
  _snake_case					     =       merge(*(reversed(__lowerCamelCase		) for stack in stacks)		)
  return collection


if __name__ == "__main__":
 UpperCAmelCase__          = input('Enter numbers separated by a comma:\n').strip()
 UpperCAmelCase__          = [int(item) for item in user_input.split(',')]
 print(patience_sort(unsorted))



