



'''simple docstring'''


from __future__ import annotations

import inspect
import unittest
from math import floor

import numpy as np

from transformers import CvtConfig
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 TFCvtForImageClassification, TFCvtModel
      from transformers.models.cvt.modeling_tf_cvt import TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      from PIL import Image

      from transformers import AutoImageProcessor




class      __UpperCamelCase       (UpperCamelCase__			):







 def   _a   (    self						)    ->    int:
       '''simple docstring'''






       lowercase        =							self.config_class(**self.inputs_dict						)
       self.parent.assertTrue(hasattr(_a ,				"""embed_dim"""						)						)
       self.parent.assertTrue(hasattr(_a ,				"""num_heads"""						)						)




class      __UpperCamelCase       :







 def __init__(    self ,				_lowerCAmelCase ,				_lowerCAmelCase=13 ,				_lowerCAmelCase=64 ,				_lowerCAmelCase=3 ,				_lowerCAmelCase=[16, 48, 96] ,				_lowerCAmelCase=[1, 3, 6] ,				_lowerCAmelCase=[1, 2, 10] ,				_lowerCAmelCase=[7, 3, 3] ,				_lowerCAmelCase=[4, 2, 2] ,				_lowerCAmelCase=[2, 1, 1] ,				_lowerCAmelCase=[2, 2, 2] ,				_lowerCAmelCase=[False, False, True] ,				_lowerCAmelCase=[0.0, 0.0, 0.0] ,				_lowerCAmelCase=0.02 ,				_lowerCAmelCase=1E-12 ,				_lowerCAmelCase=True ,				_lowerCAmelCase=True ,				_lowerCAmelCase=2 ,				)    ->    Optional[int]:
       '''simple docstring'''






       lowercase        =							parent
       lowercase        =							batch_size
       lowercase        =							image_size
       lowercase        =							patch_sizes
       lowercase        =							patch_stride
       lowercase        =							patch_padding
       lowercase        =							is_training
       lowercase        =							use_labels
       lowercase        =							num_labels
       lowercase        =							num_channels
       lowercase        =							embed_dim
       lowercase        =							num_heads
       lowercase        =							stride_kv
       lowercase        =							depth
       lowercase        =							cls_token
       lowercase        =							attention_drop_rate
       lowercase        =							initializer_range
       lowercase        =							layer_norm_eps







 def   _a   (    self						)    ->    Dict:
       '''simple docstring'''






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

       lowercase        =							None
       if self.use_labels:
             # create a random int32 tensor of given shape
             lowercase        =							ids_tensor([self.batch_size] ,				self.num_labels						)

       lowercase        =							self.get_config()
       return config, pixel_values, labels







 def   _a   (    self						)    ->    List[Any]:
       '''simple docstring'''






       return CvtConfig(
           image_size=self.image_size ,				num_labels=self.num_labels ,				num_channels=self.num_channels ,				embed_dim=self.embed_dim ,				num_heads=self.num_heads ,				patch_sizes=self.patch_sizes ,				patch_padding=self.patch_padding ,				patch_stride=self.patch_stride ,				stride_kv=self.stride_kv ,				depth=self.depth ,				cls_token=self.cls_token ,				attention_drop_rate=self.attention_drop_rate ,				initializer_range=self.initializer_range ,				)







 def   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)    ->    Optional[Any]:
       '''simple docstring'''






       lowercase        =							TFCvtModel(config=_a						)
       lowercase        =							model(_a ,				training=_a						)
       lowercase        =							(self.image_size, self.image_size)
       lowercase        =							image_size[0], image_size[1]
       for i in range(len(self.depth						)						):
             lowercase        =							floor(((height + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1						)
             lowercase        =							floor(((width + 2 * self.patch_padding[i] - self.patch_sizes[i]) / self.patch_stride[i]) + 1						)
       self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.embed_dim[-1], height, width)						)







 def   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)    ->    List[str]:
       '''simple docstring'''






       lowercase        =							self.num_labels
       lowercase        =							TFCvtForImageClassification(_a						)
       lowercase        =							model(_a ,				labels=_a ,				training=_a						)
       self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_labels)						)







 def   _a   (    self						)    ->    Tuple:
       '''simple docstring'''






       lowercase        =							self.prepare_config_and_inputs()
       lowercase        =							config_and_inputs
       lowercase        =							{"""pixel_values""": pixel_values}
       return config, inputs_dict




@require_tf
class      __UpperCamelCase       (UpperCamelCase__			,   UpperCamelCase__			,   unittest.TestCase			):
 __A			  =					(TFCvtModel, TFCvtForImageClassification) if is_tf_available() else ()
 __A			  =					(
     {'''feature-extraction''': TFCvtModel, '''image-classification''': TFCvtForImageClassification}
     if is_tf_available()
     else {}
 )
 __A			  =					False
 __A			  =					False
 __A			  =					False
 __A			  =					False
 __A			  =					False







 def   _a   (    self						)    ->    List[Any]:
       '''simple docstring'''






       lowercase        =							TFCvtModelTester(self						)
       lowercase        =							TFCvtConfigTester(self ,				config_class=_a ,				has_text_modality=_a ,				hidden_size=37						)







 def   _a   (    self						)    ->    str:
       '''simple docstring'''






       self.config_tester.create_and_test_config_common_properties()
       self.config_tester.create_and_test_config_to_json_string()
       self.config_tester.create_and_test_config_to_json_file()
       self.config_tester.create_and_test_config_from_and_save_pretrained()
       self.config_tester.create_and_test_config_with_num_labels()
       self.config_tester.check_config_can_be_init_without_params()
       self.config_tester.check_config_arguments_init()







 @unittest.skip(reason="""Cvt does not output attentions"""						)
 def   _a   (    self						)    ->    List[str]:
       '''simple docstring'''






       pass







 @unittest.skip(reason="""Cvt does not use inputs_embeds"""						)
 def   _a   (    self						)    ->    List[Any]:
       '''simple docstring'''






       pass







 @unittest.skip(reason="""Cvt does not support input and output embeddings"""						)
 def   _a   (    self						)    ->    str:
       '''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.""" ,				)
 def   _a   (    self						)    ->    Union[str, Any]:
       '''simple docstring'''






       super().test_dataset_conversion()







 @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   _a   (    self						)    ->    List[str]:
       '''simple docstring'''






       super().test_keras_fit()







 @unittest.skip(reason="""Get `Failed to determine best cudnn convolution algo.` error after using TF 2.12+cuda 11.8"""						)
 def   _a   (    self						)    ->    int:
       '''simple docstring'''






       lowercase        =							tf.keras.mixed_precision.Policy("""mixed_float16"""						)
       tf.keras.mixed_precision.set_global_policy(_a						)
       super().test_keras_fit()
       tf.keras.mixed_precision.set_global_policy("""float32"""						)







 def   _a   (    self						)    ->    Union[str, Any]:
       '''simple docstring'''






       lowercase        =							self.model_tester.prepare_config_and_inputs_for_common()

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

             lowercase        =							["""pixel_values"""]
             self.assertListEqual(arg_names[:1] ,				_a						)







 def   _a   (    self						)    ->    Optional[Any]:
       '''simple docstring'''






       def check_hidden_states_output(_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						):
             lowercase        =							model_class(_a						)

             lowercase        =							model(**self._prepare_for_class(_a ,				_a						)						)
             lowercase        =							outputs.hidden_states

             lowercase        =							len(self.model_tester.depth						)
             self.assertEqual(len(_a						) ,				_a						)

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

       lowercase        =							self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             lowercase        =							True
             check_hidden_states_output(_a ,				_a ,				_a						)

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

             check_hidden_states_output(_a ,				_a ,				_a						)







 def   _a   (    self						)    ->    Dict:
       '''simple docstring'''






       lowercase        =							self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_a						)







 def   _a   (    self						)    ->    Tuple:
       '''simple docstring'''






       lowercase        =							self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*_a						)







 @slow
 def   _a   (    self						)    ->    Any:
       '''simple docstring'''






       for model_name in TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowercase        =							TFCvtModel.from_pretrained(_a						)
             self.assertIsNotNone(_a						)







def      SCREAMING_SNAKE_CASE				(				):
      lowercase        =							Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						)
      return image




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







 @cached_property
 def   _a   (    self						)    ->    int:
       '''simple docstring'''






       return AutoImageProcessor.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]						)







 @slow
 def   _a   (    self						)    ->    Tuple:
       '''simple docstring'''






       lowercase        =							TFCvtForImageClassification.from_pretrained(TF_CVT_PRETRAINED_MODEL_ARCHIVE_LIST[0]						)

       lowercase        =							self.default_image_processor
       lowercase        =							prepare_img()
       lowercase        =							image_processor(images=_a ,				return_tensors="""tf"""						)

       # forward pass
       lowercase        =							model(**_a						)

       # verify the logits
       lowercase        =							tf.TensorShape((1, 1000)						)
       self.assertEqual(outputs.logits.shape ,				_a						)

       lowercase        =							tf.constant([0.9285, 0.9015, -0.3150]						)
       self.assertTrue(np.allclose(outputs.logits[0, :3].numpy() ,				_a ,				atol=1E-4						)						)







'''simple docstring'''


from random import shuffle

import tensorflow as tf
from numpy import array







def      SCREAMING_SNAKE_CASE				(				lowercase_    :      List[str] ,  lowercase_    :      Optional[int]						):
						lowercase        =							int(lowercase_						)
						assert noofclusters < len(lowercase_						)

						# Find out the dimensionality
						lowercase        =							len(vectors[0]						)

						# Will help select random centroids from among the available vectors
						lowercase        =							list(range(len(lowercase_						)						)						)
						shuffle(lowercase_						)

						# GRAPH OF COMPUTATION
						# We initialize a new graph and set it as the default during each run
						# of this algorithm. This ensures that as this function is called
						# multiple times, the default graph doesn't keep getting crowded with
						# unused ops and Variables from previous function calls.

						lowercase        =							tf.Graph()

						with graph.as_default():
												# SESSION OF COMPUTATION

												lowercase        =							tf.Session()

												##CONSTRUCTING THE ELEMENTS OF COMPUTATION

												##First lets ensure we have a Variable vector for each centroid,
												##initialized to one of the vectors from the available data points
												lowercase        =							[
												    tf.Variable(vectors[vector_indices[i]]						) for i in range(lowercase_						)
												]
												##These nodes will assign the centroid Variables the appropriate
												##values
												lowercase        =							tf.placeholder("""float64""" ,  [dim]						)
												lowercase        =							[]
												for centroid in centroids:
																		cent_assigns.append(tf.assign(lowercase_ ,  lowercase_						)						)

												##Variables for cluster assignments of individual vectors(initialized
												##to 0 at first)
												lowercase        =							[tf.Variable(0						) for i in range(len(lowercase_						)						)]
												##These nodes will assign an assignment Variable the appropriate
												##value
												lowercase        =							tf.placeholder("""int32"""						)
												lowercase        =							[]
												for assignment in assignments:
																		cluster_assigns.append(tf.assign(lowercase_ ,  lowercase_						)						)

												##Now lets construct the node that will compute the mean
												# The placeholder for the input
												lowercase        =							tf.placeholder("""float""" ,  [None, dim]						)
												# The Node/op takes the input and computes a mean along the 0th
												# dimension, i.e. the list of input vectors
												lowercase        =							tf.reduce_mean(lowercase_ ,  0						)

												##Node for computing Euclidean distances
												# Placeholders for input
												lowercase        =							tf.placeholder("""float""" ,  [dim]						)
												lowercase        =							tf.placeholder("""float""" ,  [dim]						)
												lowercase        =							tf.sqrt(tf.reduce_sum(tf.pow(tf.sub(lowercase_ ,  lowercase_						) ,  2						)						)						)

												##This node will figure out which cluster to assign a vector to,
												##based on Euclidean distances of the vector from the centroids.
												# Placeholder for input
												lowercase        =							tf.placeholder("""float""" ,  [noofclusters]						)
												lowercase        =							tf.argmin(lowercase_ ,  0						)

												##INITIALIZING STATE VARIABLES

												##This will help initialization of all Variables defined with respect
												##to the graph. The Variable-initializer should be defined after
												##all the Variables have been constructed, so that each of them
												##will be included in the initialization.
												lowercase        =							tf.initialize_all_variables()

												# Initialize all variables
												sess.run(lowercase_						)

												##CLUSTERING ITERATIONS

												# Now perform the Expectation-Maximization steps of K-Means clustering
												# iterations. To keep things simple, we will only do a set number of
												# iterations, instead of using a Stopping Criterion.
												lowercase        =							100
												for _ in range(lowercase_						):
																		##EXPECTATION STEP
																		##Based on the centroid locations till last iteration, compute
																		##the _expected_ centroid assignments.
																		# Iterate over each vector
																		for vector_n in range(len(lowercase_						)						):
																								lowercase        =							vectors[vector_n]
																								# Compute Euclidean distance between this vector and each
																								# centroid. Remember that this list cannot be named
																								#'centroid_distances', since that is the input to the
																								# cluster assignment node.
																								lowercase        =							[
																								    sess.run(lowercase_ ,  feed_dict={va: vect, va: sess.run(lowercase_						)}						)
																								    for centroid in centroids
																								]
																								# Now use the cluster assignment node, with the distances
																								# as the input
																								lowercase        =							sess.run(
																								    lowercase_ ,  feed_dict={centroid_distances: distances}						)
																								# Now assign the value to the appropriate state variable
																								sess.run(
																								    cluster_assigns[vector_n] ,  feed_dict={assignment_value: assignment}						)

																		##MAXIMIZATION STEP
																		# Based on the expected state computed from the Expectation Step,
																		# compute the locations of the centroids so as to maximize the
																		# overall objective of minimizing within-cluster Sum-of-Squares
																		for cluster_n in range(lowercase_						):
																								# Collect all the vectors assigned to this cluster
																								lowercase        =							[
																								    vectors[i]
																								    for i in range(len(lowercase_						)						)
																								    if sess.run(assignments[i]						) == cluster_n
																								]
																								# Compute new centroid location
																								lowercase        =							sess.run(
																								    lowercase_ ,  feed_dict={mean_input: array(lowercase_						)}						)
																								# Assign value to appropriate variable
																								sess.run(
																								    cent_assigns[cluster_n] ,  feed_dict={centroid_value: new_location}						)

        # Return centroids and assignments
												lowercase        =							sess.run(lowercase_						)
												lowercase        =							sess.run(lowercase_						)
												return centroids, assignments


