


'''simple docstring'''



from random import shuffle

import tensorflow as tf
from numpy import array



def      UpperCAmelCase__   (      UpperCAmelCase_       :							str ,       UpperCAmelCase_       :							Union[str, Any]				)	->						List[str]:
     __lowerCamelCase  :      str														=						int(UpperCAmelCase_				)
     assert noofclusters < len(UpperCAmelCase_				)

     # Find out the dimensionality
     __lowerCamelCase  :      Any														=						len(vectors[0]				)

     # Will help select random centroids from among the available vectors
     __lowerCamelCase  :      Union[str, Any]														=						list(range(len(UpperCAmelCase_				)				)				)
     shuffle(UpperCAmelCase_				)

     # 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.

     __lowerCamelCase  :      List[Any]														=						tf.Graph()

     with graph.as_default():
          # SESSION OF COMPUTATION

          __lowerCamelCase  :      str														=						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
          __lowerCamelCase  :      Any														=						[
              tf.Variable(vectors[vector_indices[i]]				) for i in range(UpperCAmelCase_				)
          ]
          ##These nodes will assign the centroid Variables the appropriate
          ##values
          __lowerCamelCase  :      int														=						tf.placeholder('float64' ,       [dim]				)
          __lowerCamelCase  :      Union[str, Any]														=						[]
          for centroid in centroids:
               cent_assigns.append(tf.assign(UpperCAmelCase_ ,       UpperCAmelCase_				)				)

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

          ##Now lets construct the node that will compute the mean
          # The placeholder for the input
          __lowerCamelCase  :      List[str]														=						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
          __lowerCamelCase  :      int														=						tf.reduce_mean(UpperCAmelCase_ ,       0				)

          ##Node for computing Euclidean distances
          # Placeholders for input
          __lowerCamelCase  :      List[str]														=						tf.placeholder('float' ,       [dim]				)
          __lowerCamelCase  :      List[Any]														=						tf.placeholder('float' ,       [dim]				)
          __lowerCamelCase  :      Dict														=						tf.sqrt(tf.reduce_sum(tf.pow(tf.sub(UpperCAmelCase_ ,       UpperCAmelCase_				) ,       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
          __lowerCamelCase  :      List[Any]														=						tf.placeholder('float' ,       [noofclusters]				)
          __lowerCamelCase  :      Tuple														=						tf.argmin(UpperCAmelCase_ ,       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.
          __lowerCamelCase  :      Any														=						tf.initialize_all_variables()

          # Initialize all variables
          sess.run(UpperCAmelCase_				)

          ##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.
          __lowerCamelCase  :      List[Any]														=						1_00
          for _ in range(UpperCAmelCase_				):
               ##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(UpperCAmelCase_				)				):
                    __lowerCamelCase  :      int														=						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.
                    __lowerCamelCase  :      List[str]														=						[
                        sess.run(UpperCAmelCase_ ,       feed_dict={va: vect, va: sess.run(UpperCAmelCase_				)}				)
                        for centroid in centroids
                    ]
                    # Now use the cluster assignment node, with the distances
                    # as the input
                    __lowerCamelCase  :      Dict														=						sess.run(
                        UpperCAmelCase_ ,       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(UpperCAmelCase_				):
                    # Collect all the vectors assigned to this cluster
                    __lowerCamelCase  :      Optional[Any]														=						[
                        vectors[i]
                        for i in range(len(UpperCAmelCase_				)				)
                        if sess.run(assignments[i]				) == cluster_n
                    ]
                    # Compute new centroid location
                    __lowerCamelCase  :      Tuple														=						sess.run(
                        UpperCAmelCase_ ,       feed_dict={mean_input: array(UpperCAmelCase_				)}				)
                    # Assign value to appropriate variable
                    sess.run(
                        cent_assigns[cluster_n] ,       feed_dict={centroid_value: new_location}				)

        # Return centroids and assignments
          __lowerCamelCase  :      str														=						sess.run(UpperCAmelCase_				)
          __lowerCamelCase  :      Any														=						sess.run(UpperCAmelCase_				)
          return centroids, assignments








'''simple docstring'''




from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow

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


if is_tf_available():
      import numpy as np
      import tensorflow as tf

      from transformers import (
          TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
          FlaubertConfig,
          TFFlaubertForMultipleChoice,
          TFFlaubertForQuestionAnsweringSimple,
          TFFlaubertForSequenceClassification,
          TFFlaubertForTokenClassification,
          TFFlaubertModel,
          TFFlaubertWithLMHeadModel,
      )





class 					A							:





     def __init__(				self		,     SCREAMING_SNAKE_CASE		,     )					->							Optional[Any]:






         """simple docstring"""






         A			:  List[str]   =		parent
         A			:  int   =		13
         A			:  Dict   =		7
         A			:  str   =		True
         A			:  Dict   =		True
         A			:  Tuple   =		True
         A			:  Union[str, Any]   =		True
         A			:  Dict   =		True
         A			:  str   =		False
         A			:  Union[str, Any]   =		False
         A			:  Union[str, Any]   =		False
         A			:  List[str]   =		2
         A			:  Optional[int]   =		99
         A			:  List[str]   =		0
         A			:  int   =		32
         A			:  Any   =		2
         A			:  Optional[Any]   =		4
         A			:  List[Any]   =		0.1
         A			:  List[str]   =		0.1
         A			:  Tuple   =		512
         A			:  Optional[Any]   =		16
         A			:  List[str]   =		2
         A			:  Tuple   =		0.02
         A			:  List[str]   =		3
         A			:  List[Any]   =		4
         A			:  Any   =		'''last'''
         A			:  int   =		True
         A			:  Union[str, Any]   =		None
         A			:  Dict   =		0





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






         """simple docstring"""






         A			:  int   =		ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size      )
         A			:  Any   =		random_attention_mask([self.batch_size, self.seq_length]		,     dtype=tf.floataa      )

         A			:  int   =		None
         if self.use_input_lengths:
             A			:  int   =		(
                 ids_tensor([self.batch_size]		,     vocab_size=2      ) + self.seq_length - 2
             )  # small variation of seq_length

         A			:  Tuple   =		None
         if self.use_token_type_ids:
             A			:  Union[str, Any]   =		ids_tensor([self.batch_size, self.seq_length]		,     self.n_langs      )

         A			:  List[Any]   =		None
         A			:  List[Any]   =		None
         A			:  Tuple   =		None
         if self.use_labels:
             A			:  Dict   =		ids_tensor([self.batch_size]		,     self.type_sequence_label_size      )
             A			:  Dict   =		ids_tensor([self.batch_size, self.seq_length]		,     self.num_labels      )
             A			:  List[Any]   =		ids_tensor([self.batch_size]		,     2		,     dtype=tf.floataa      )
             A			:  Optional[Any]   =		ids_tensor([self.batch_size]		,     self.num_choices      )

         A			:  Dict   =		FlaubertConfig(
             vocab_size=self.vocab_size		,     n_special=self.n_special		,     emb_dim=self.hidden_size		,     n_layers=self.num_hidden_layers		,     n_heads=self.num_attention_heads		,     dropout=self.hidden_dropout_prob		,     attention_dropout=self.attention_probs_dropout_prob		,     gelu_activation=self.gelu_activation		,     sinusoidal_embeddings=self.sinusoidal_embeddings		,     asm=self.asm		,     causal=self.causal		,     n_langs=self.n_langs		,     max_position_embeddings=self.max_position_embeddings		,     initializer_range=self.initializer_range		,     summary_type=self.summary_type		,     use_proj=self.use_proj		,     bos_token_id=self.bos_token_id		,     )

         return (
             config,
             input_ids,
             token_type_ids,
             input_lengths,
             sequence_labels,
             token_labels,
             is_impossible_labels,
             choice_labels,
             input_mask,
         )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     )					->							List[str]:






         """simple docstring"""






         A			:  int   =		TFFlaubertModel(config=SCREAMING_SNAKE_CASE      )
         A			:  Optional[int]   =		{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
         A			:  int   =		model(SCREAMING_SNAKE_CASE      )

         A			:  List[str]   =		[input_ids, input_mask]
         A			:  str   =		model(SCREAMING_SNAKE_CASE      )
         self.parent.assertEqual(result.last_hidden_state.shape		,     (self.batch_size, self.seq_length, self.hidden_size)      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     )					->							List[str]:






         """simple docstring"""






         A			:  List[str]   =		TFFlaubertWithLMHeadModel(SCREAMING_SNAKE_CASE      )

         A			:  Union[str, Any]   =		{'''input_ids''': input_ids, '''lengths''': input_lengths, '''langs''': token_type_ids}
         A			:  Tuple   =		model(SCREAMING_SNAKE_CASE      )

         self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.vocab_size)      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     )					->							Optional[Any]:






         """simple docstring"""






         A			:  Union[str, Any]   =		TFFlaubertForQuestionAnsweringSimple(SCREAMING_SNAKE_CASE      )

         A			:  List[str]   =		{'''input_ids''': input_ids, '''lengths''': input_lengths}

         A			:  Tuple   =		model(SCREAMING_SNAKE_CASE      )

         self.parent.assertEqual(result.start_logits.shape		,     (self.batch_size, self.seq_length)      )
         self.parent.assertEqual(result.end_logits.shape		,     (self.batch_size, self.seq_length)      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     )					->							Any:






         """simple docstring"""






         A			:  Union[str, Any]   =		TFFlaubertForSequenceClassification(SCREAMING_SNAKE_CASE      )

         A			:  Dict   =		{'''input_ids''': input_ids, '''lengths''': input_lengths}

         A			:  Optional[Any]   =		model(SCREAMING_SNAKE_CASE      )

         self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.type_sequence_label_size)      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     )					->							str:






         """simple docstring"""






         A			:  Optional[Any]   =		self.num_labels
         A			:  str   =		TFFlaubertForTokenClassification(config=SCREAMING_SNAKE_CASE      )
         A			:  List[str]   =		{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
         A			:  int   =		model(SCREAMING_SNAKE_CASE      )
         self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.num_labels)      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     )					->							Any:






         """simple docstring"""






         A			:  str   =		self.num_choices
         A			:  int   =		TFFlaubertForMultipleChoice(config=SCREAMING_SNAKE_CASE      )
         A			:  str   =		tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE		,     1      )		,     (1, self.num_choices, 1)      )
         A			:  Optional[Any]   =		tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE		,     1      )		,     (1, self.num_choices, 1)      )
         A			:  str   =		tf.tile(tf.expand_dims(SCREAMING_SNAKE_CASE		,     1      )		,     (1, self.num_choices, 1)      )
         A			:  List[str]   =		{
             '''input_ids''': multiple_choice_inputs_ids,
             '''attention_mask''': multiple_choice_input_mask,
             '''token_type_ids''': multiple_choice_token_type_ids,
         }
         A			:  List[Any]   =		model(SCREAMING_SNAKE_CASE      )
         self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_choices)      )





     def 							__lowerCAmelCase				(				self      )					->							Any:






         """simple docstring"""






         A			:  Tuple   =		self.prepare_config_and_inputs()
         (
             (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    (
             A
         ),    
         )			:  Any   =		config_and_inputs
         A			:  List[str]   =		{
             '''input_ids''': input_ids,
             '''token_type_ids''': token_type_ids,
             '''langs''': token_type_ids,
             '''lengths''': input_lengths,
         }
         return config, inputs_dict





@require_tf
class 					A							(				__snake_case       ,     __snake_case       ,     unittest.TestCase		):
     __magic_name__          =						(
         (
             TFFlaubertModel,
             TFFlaubertWithLMHeadModel,
             TFFlaubertForSequenceClassification,
             TFFlaubertForQuestionAnsweringSimple,
             TFFlaubertForTokenClassification,
             TFFlaubertForMultipleChoice,
         )
         if is_tf_available()
         else ()
     )
     __magic_name__          =						(
         (TFFlaubertWithLMHeadModel,) if is_tf_available() else ()
     )  # TODO (PVP): Check other models whether language generation is also applicable
     __magic_name__          =						(
         {
             '''feature-extraction''': TFFlaubertModel,
             '''fill-mask''': TFFlaubertWithLMHeadModel,
             '''question-answering''': TFFlaubertForQuestionAnsweringSimple,
             '''text-classification''': TFFlaubertForSequenceClassification,
             '''token-classification''': TFFlaubertForTokenClassification,
             '''zero-shot''': TFFlaubertForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     __magic_name__          =						False
     __magic_name__          =						False





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							List[Any]:






         """simple docstring"""






         if (
             pipeline_test_casse_name == "QAPipelineTests"
             and tokenizer_name is not None
             and not tokenizer_name.endswith('''Fast'''      )
         ):
             # `QAPipelineTests` fails for a few models when the slower tokenizer are used.
             # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
             # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
             return True

         return False





     def 							__lowerCAmelCase				(				self      )					->							Any:






         """simple docstring"""






         A			:  int   =		TFFlaubertModelTester(self      )
         A			:  Dict   =		ConfigTester(self		,     config_class=SCREAMING_SNAKE_CASE		,     emb_dim=37      )





     def 							__lowerCAmelCase				(				self      )					->							Optional[Any]:






         """simple docstring"""






         self.config_tester.run_common_tests()





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






         """simple docstring"""






         A			:  List[Any]   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_model(*SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							Dict:






         """simple docstring"""






         A			:  int   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_lm_head(*SCREAMING_SNAKE_CASE      )





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






         """simple docstring"""






         A			:  Union[str, Any]   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_qa(*SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							Union[str, Any]:






         """simple docstring"""






         A			:  Optional[int]   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_sequence_classif(*SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							Any:






         """simple docstring"""






         A			:  Tuple   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_for_token_classification(*SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self      )					->							str:






         """simple docstring"""






         A			:  str   =		self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_flaubert_for_multiple_choice(*SCREAMING_SNAKE_CASE      )





     @slow
     def 							__lowerCAmelCase				(				self      )					->							Union[str, Any]:






         """simple docstring"""






         for model_name in TF_FLAUBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             A			:  Union[str, Any]   =		TFFlaubertModel.from_pretrained(SCREAMING_SNAKE_CASE      )
             self.assertIsNotNone(SCREAMING_SNAKE_CASE      )





@require_tf
@require_sentencepiece
@require_tokenizers
class 					A							(				unittest.TestCase		):





     @slow
     def 							__lowerCAmelCase				(				self      )					->							int:






         """simple docstring"""






         A			:  Tuple   =		TFFlaubertModel.from_pretrained('''jplu/tf-flaubert-small-cased'''      )

         A			:  List[str]   =		tf.convert_to_tensor(
             [[0, 158, 735, 2592, 1424, 6727, 82, 1]]		,     dtype=tf.intaa		,     )  # "J'aime flaubert !"

         A			:  int   =		model(SCREAMING_SNAKE_CASE      )[0]
         A			:  str   =		tf.TensorShape((1, 8, 512)      )
         self.assertEqual(output.shape		,     SCREAMING_SNAKE_CASE      )
         # compare the actual values for a slice.
         A			:  Union[str, Any]   =		tf.convert_to_tensor(
             [
                 [
                     [-1.8_768_773, -1.566_555, 0.27_072_418],
                     [-1.6_920_038, -0.5_873_505, 1.9_329_599],
                     [-2.9_563_985, -1.6_993_835, 1.7_972_052],
                 ]
             ]		,     dtype=tf.floataa		,     )

         self.assertTrue(np.allclose(output[:, :3, :3].numpy()		,     expected_slice.numpy()		,     atol=1e-4      )      )




