





"""simple docstring"""






from random import shuffle

import tensorflow as tf
from numpy import array

def       __A (     a_  :List[Any]				,    a_  :str)						->						Tuple:
				__a	:  Optional[int]         =					int(a_)
				assert noofclusters < len(a_)

				# Find out the dimensionality
				__a	:  Dict         =					len(vectors[0])

				# Will help select random centroids from among the available vectors
				__a	:  int         =					list(range(len(a_)))
				shuffle(a_)

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

				__a	:  List[str]         =					tf.Graph()

				with graph.as_default():
								# SESSION OF COMPUTATION

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

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

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

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

								# Initialize all variables
								sess.run(a_)

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

        # Return centroids and assignments
								__a	:  int         =					sess.run(a_)
								__a	:  Dict         =					sess.run(a_)
								return centroids, assignments






"""simple docstring"""






import unittest

from transformers import SqueezeBertConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    SqueezeBertForMaskedLM,
		    SqueezeBertForMultipleChoice,
		    SqueezeBertForQuestionAnswering,
		    SqueezeBertForSequenceClassification,
		    SqueezeBertForTokenClassification,
		    SqueezeBertModel,
		)




class 			__lowercase     (     _UpperCamelCase							):







					'''simple docstring'''




					def __init__(  self		,     _UpperCAmelCase		,     _UpperCAmelCase=13		,     _UpperCAmelCase=7		,     _UpperCAmelCase=True		,     _UpperCAmelCase=True		,     _UpperCAmelCase=False		,     _UpperCAmelCase=True		,     _UpperCAmelCase=99		,     _UpperCAmelCase=32		,     _UpperCAmelCase=5		,     _UpperCAmelCase=4		,     _UpperCAmelCase=64		,     _UpperCAmelCase="gelu"		,     _UpperCAmelCase=0.1		,     _UpperCAmelCase=0.1		,     _UpperCAmelCase=512		,     _UpperCAmelCase=16		,     _UpperCAmelCase=2		,     _UpperCAmelCase=0.0_2		,     _UpperCAmelCase=3		,     _UpperCAmelCase=4		,     _UpperCAmelCase=None		,     _UpperCAmelCase=2		,     _UpperCAmelCase=2		,     _UpperCAmelCase=2		,     _UpperCAmelCase=2		,     _UpperCAmelCase=4		,     _UpperCAmelCase=1		,     ):
									__a	:  Dict         =					parent
									__a	:  str         =					batch_size
									__a	:  Union[str, Any]         =					seq_length
									__a	:  Any         =					is_training
									__a	:  int         =					use_input_mask
									__a	:  Optional[int]         =					use_token_type_ids
									__a	:  int         =					use_labels
									__a	:  int         =					vocab_size
									__a	:  int         =					hidden_size
									__a	:  str         =					num_hidden_layers
									__a	:  str         =					num_attention_heads
									__a	:  Any         =					intermediate_size
									__a	:  Union[str, Any]         =					hidden_act
									__a	:  Optional[int]         =					hidden_dropout_prob
									__a	:  str         =					attention_probs_dropout_prob
									__a	:  int         =					max_position_embeddings
									__a	:  Union[str, Any]         =					type_vocab_size
									__a	:  List[str]         =					type_sequence_label_size
									__a	:  List[str]         =					initializer_range
									__a	:  Optional[int]         =					num_labels
									__a	:  List[str]         =					num_choices
									__a	:  int         =					scope
									__a	:  Union[str, Any]         =					q_groups
									__a	:  Dict         =					k_groups
									__a	:  List[str]         =					v_groups
									__a	:  Any         =					post_attention_groups
									__a	:  Optional[int]         =					intermediate_groups
									__a	:  List[str]         =					output_groups







					def 	_lowerCamelCase						(  self       ):
									__a	:  int         =					ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size       )

									__a	:  Optional[Any]         =					None
									if self.use_input_mask:
													__a	:  int         =					random_attention_mask([self.batch_size, self.seq_length]       )

									__a	:  List[str]         =					None
									__a	:  Union[str, Any]         =					None
									__a	:  int         =					None
									if self.use_labels:
													__a	:  List[str]         =					ids_tensor([self.batch_size]		,     self.type_sequence_label_size       )
													__a	:  int         =					ids_tensor([self.batch_size, self.seq_length]		,     self.num_labels       )
													__a	:  Dict         =					ids_tensor([self.batch_size]		,     self.num_choices       )

									__a	:  int         =					self.get_config()

									return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels







					def 	_lowerCamelCase						(  self       ):
									return SqueezeBertConfig(
									    embedding_size=self.hidden_size		,     vocab_size=self.vocab_size		,     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		,     attention_probs_dropout_prob=self.hidden_dropout_prob		,     attention_dropout=self.attention_probs_dropout_prob		,     max_position_embeddings=self.max_position_embeddings		,     initializer_range=self.initializer_range		,     q_groups=self.q_groups		,     k_groups=self.k_groups		,     v_groups=self.v_groups		,     post_attention_groups=self.post_attention_groups		,     intermediate_groups=self.intermediate_groups		,     output_groups=self.output_groups		,     )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  Dict         =					SqueezeBertModel(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  Optional[Any]         =					model(_UpperCAmelCase		,     _UpperCAmelCase       )
									__a	:  Dict         =					model(_UpperCAmelCase       )
									self.parent.assertEqual(result.last_hidden_state.shape		,     (self.batch_size, self.seq_length, self.hidden_size)       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  Optional[Any]         =					SqueezeBertForMaskedLM(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  int         =					model(_UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     labels=_UpperCAmelCase       )
									self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.vocab_size)       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  int         =					SqueezeBertForQuestionAnswering(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  Optional[int]         =					model(
									    _UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     start_positions=_UpperCAmelCase		,     end_positions=_UpperCAmelCase       )
									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		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  Any         =					self.num_labels
									__a	:  List[Any]         =					SqueezeBertForSequenceClassification(_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  int         =					model(_UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     labels=_UpperCAmelCase       )
									self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_labels)       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  List[Any]         =					self.num_labels
									__a	:  List[str]         =					SqueezeBertForTokenClassification(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()

									__a	:  Any         =					model(_UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     labels=_UpperCAmelCase       )
									self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.num_labels)       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  List[str]         =					self.num_choices
									__a	:  Union[str, Any]         =					SqueezeBertForMultipleChoice(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  int         =					input_ids.unsqueeze(1       ).expand(-1		,     self.num_choices		,     -1       ).contiguous()
									__a	:  Optional[Any]         =					input_mask.unsqueeze(1       ).expand(-1		,     self.num_choices		,     -1       ).contiguous()
									__a	:  Union[str, Any]         =					model(
									    _UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     labels=_UpperCAmelCase		,     )
									self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_choices)       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Any         =					self.prepare_config_and_inputs()
									((__a)    ,  (__a)    ,  (__a)    ,  (__a)    ,  (__a)    ,  (__a))	:  Optional[Any]         =					config_and_inputs
									__a	:  List[Any]         =					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
									return config, inputs_dict






@require_torch
class 			__lowercase     (     _UpperCamelCase				, _UpperCamelCase				, unittest.TestCase							):







					'''simple docstring'''




					__lowerCAmelCase					=					(
					    (
					        SqueezeBertModel,
					        SqueezeBertForMaskedLM,
					        SqueezeBertForMultipleChoice,
					        SqueezeBertForQuestionAnswering,
					        SqueezeBertForSequenceClassification,
					        SqueezeBertForTokenClassification,
					    )
					    if is_torch_available()
					    else None
					)
					__lowerCAmelCase					=					(
					    {
					        '''feature-extraction''': SqueezeBertModel,
					        '''fill-mask''': SqueezeBertForMaskedLM,
					        '''question-answering''': SqueezeBertForQuestionAnswering,
					        '''text-classification''': SqueezeBertForSequenceClassification,
					        '''token-classification''': SqueezeBertForTokenClassification,
					        '''zero-shot''': SqueezeBertForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					__lowerCAmelCase					=					False
					__lowerCAmelCase					=					True
					__lowerCAmelCase					=					False







					def 	_lowerCamelCase						(  self       ):
									__a	:  Union[str, Any]         =					SqueezeBertModelTester(self       )
									__a	:  Dict         =					ConfigTester(self		,     config_class=_UpperCAmelCase		,     dim=37       )







					def 	_lowerCamelCase						(  self       ):
									self.config_tester.run_common_tests()







					def 	_lowerCamelCase						(  self       ):
									__a	:  Tuple         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_squeezebert_model(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  List[Any]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_squeezebert_for_masked_lm(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  str         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_squeezebert_for_question_answering(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[Any]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_squeezebert_for_sequence_classification(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[int]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_squeezebert_for_token_classification(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  List[str]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_squeezebert_for_multiple_choice(*_UpperCAmelCase       )







					@slow
					def 	_lowerCamelCase						(  self       ):
									for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__a	:  Any         =					SqueezeBertModel.from_pretrained(_UpperCAmelCase       )
													self.assertIsNotNone(_UpperCAmelCase       )






@require_sentencepiece
@require_tokenizers
@require_torch
class 			__lowercase     (     unittest.TestCase							):







					'''simple docstring'''




					@slow
					def 	_lowerCamelCase						(  self       ):
									__a	:  int         =					SqueezeBertForSequenceClassification.from_pretrained('''squeezebert/squeezebert-mnli'''       )

									__a	:  Tuple         =					torch.tensor([[1, 29414, 232, 328, 740, 1140, 12695, 69, 13, 1588, 2]]       )
									__a	:  List[str]         =					model(_UpperCAmelCase       )[0]
									__a	:  int         =					torch.Size((1, 3)       )
									self.assertEqual(output.shape		,     _UpperCAmelCase       )
									__a	:  int         =					torch.tensor([[0.6_4_0_1, -0.0_3_4_9, -0.6_0_4_1]]       )
									self.assertTrue(torch.allclose(_UpperCAmelCase		,     _UpperCAmelCase		,     atol=1e-4       )       )