



'''simple docstring'''


import gc
import unittest

from transformers import CTRLConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
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 (
						    CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
						    CTRLForSequenceClassification,
						    CTRLLMHeadModel,
						    CTRLModel,
						)




class      __UpperCamelCase       :







	def __init__(    self ,				_lowerCAmelCase ,				_lowerCAmelCase=14 ,				_lowerCAmelCase=7 ,				_lowerCAmelCase=True ,				_lowerCAmelCase=True ,				_lowerCAmelCase=True ,				_lowerCAmelCase=True ,				_lowerCAmelCase=True ,				_lowerCAmelCase=99 ,				_lowerCAmelCase=32 ,				_lowerCAmelCase=5 ,				_lowerCAmelCase=4 ,				_lowerCAmelCase=37 ,				_lowerCAmelCase="gelu" ,				_lowerCAmelCase=0.1 ,				_lowerCAmelCase=0.1 ,				_lowerCAmelCase=512 ,				_lowerCAmelCase=16 ,				_lowerCAmelCase=2 ,				_lowerCAmelCase=0.02 ,				_lowerCAmelCase=3 ,				_lowerCAmelCase=4 ,				_lowerCAmelCase=None ,				)    ->    str:
							'''simple docstring'''






							lowercase        =							parent
							lowercase        =							batch_size
							lowercase        =							seq_length
							lowercase        =							is_training
							lowercase        =							use_token_type_ids
							lowercase        =							use_input_mask
							lowercase        =							use_labels
							lowercase        =							use_mc_token_ids
							lowercase        =							vocab_size
							lowercase        =							hidden_size
							lowercase        =							num_hidden_layers
							lowercase        =							num_attention_heads
							lowercase        =							intermediate_size
							lowercase        =							hidden_act
							lowercase        =							hidden_dropout_prob
							lowercase        =							attention_probs_dropout_prob
							lowercase        =							max_position_embeddings
							lowercase        =							type_vocab_size
							lowercase        =							type_sequence_label_size
							lowercase        =							initializer_range
							lowercase        =							num_labels
							lowercase        =							num_choices
							lowercase        =							scope
							lowercase        =							self.vocab_size - 1







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






							lowercase        =							ids_tensor([self.batch_size, self.seq_length] ,				self.vocab_size						)

							lowercase        =							None
							if self.use_input_mask:
													lowercase        =							random_attention_mask([self.batch_size, self.seq_length]						)

							lowercase        =							None
							if self.use_token_type_ids:
													lowercase        =							ids_tensor([self.batch_size, self.seq_length] ,				self.type_vocab_size						)

							lowercase        =							None
							if self.use_mc_token_ids:
													lowercase        =							ids_tensor([self.batch_size, self.num_choices] ,				self.seq_length						)

							lowercase        =							None
							lowercase        =							None
							lowercase        =							None
							if self.use_labels:
													lowercase        =							ids_tensor([self.batch_size] ,				self.type_sequence_label_size						)
													lowercase        =							ids_tensor([self.batch_size, self.seq_length] ,				self.num_labels						)
													lowercase        =							ids_tensor([self.batch_size] ,				self.num_choices						)

							lowercase        =							self.get_config()

							lowercase        =							ids_tensor([self.num_hidden_layers, self.num_attention_heads] ,				2						)

							return (
							    config,
							    input_ids,
							    input_mask,
							    head_mask,
							    token_type_ids,
							    mc_token_ids,
							    sequence_labels,
							    token_labels,
							    choice_labels,
							)







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






							return CTRLConfig(
							    vocab_size=self.vocab_size ,				n_embd=self.hidden_size ,				n_layer=self.num_hidden_layers ,				n_head=self.num_attention_heads ,				n_positions=self.max_position_embeddings ,				pad_token_id=self.pad_token_id ,				)







	def   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				*_lowerCAmelCase						)    ->    Any:
							'''simple docstring'''






							lowercase        =							CTRLModel(config=_lowerCAmelCase						)
							model.to(_lowerCAmelCase						)
							model.eval()

							model(_lowerCAmelCase ,				token_type_ids=_lowerCAmelCase ,				head_mask=_lowerCAmelCase						)
							model(_lowerCAmelCase ,				token_type_ids=_lowerCAmelCase						)
							lowercase        =							model(_lowerCAmelCase						)
							self.parent.assertEqual(result.last_hidden_state.shape ,				(self.batch_size, self.seq_length, self.hidden_size)						)
							self.parent.assertEqual(len(result.past_key_values						) ,				config.n_layer						)







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






							lowercase        =							CTRLLMHeadModel(_lowerCAmelCase						)
							model.to(_lowerCAmelCase						)
							model.eval()

							lowercase        =							model(_lowerCAmelCase ,				token_type_ids=_lowerCAmelCase ,				labels=_lowerCAmelCase						)
							self.parent.assertEqual(result.loss.shape ,				()						)
							self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.seq_length, self.vocab_size)						)







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






							lowercase        =							self.prepare_config_and_inputs()

							(
							    (
							    lowercase
							)     ,   (
							    lowercase
							)     ,   (
							    lowercase
							)     ,   (
							    lowercase
							)     ,   (
							    lowercase
							)     ,   (
							    lowercase
							)     ,   (
							    lowercase
							)     ,   (
							    lowercase
							)     ,   (
							    lowercase
							)     ,   
							)        =							config_and_inputs

							lowercase        =							{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """head_mask""": head_mask}

							return config, inputs_dict







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






							lowercase        =							self.num_labels
							lowercase        =							CTRLForSequenceClassification(_lowerCAmelCase						)
							model.to(_lowerCAmelCase						)
							model.eval()
							lowercase        =							ids_tensor([self.batch_size] ,				self.type_sequence_label_size						)
							lowercase        =							model(_lowerCAmelCase ,				token_type_ids=_lowerCAmelCase ,				labels=_lowerCAmelCase						)
							self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_labels)						)




@require_torch
class      __UpperCamelCase       (_UpperCAmelCase			,   _UpperCAmelCase			,   _UpperCAmelCase			,   unittest.TestCase			):
	__A			  =					(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
	__A			  =					(CTRLLMHeadModel,) if is_torch_available() else ()
	__A			  =					(
	    {
	        '''feature-extraction''': CTRLModel,
	        '''text-classification''': CTRLForSequenceClassification,
	        '''text-generation''': CTRLLMHeadModel,
	        '''zero-shot''': CTRLForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	__A			  =					True
	__A			  =					False
	__A			  =					False







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






							if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
													# Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
													# `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny
													# config could not be created.
													return True

							return False







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






							lowercase        =							CTRLModelTester(self						)
							lowercase        =							ConfigTester(self ,				config_class=_lowerCAmelCase ,				n_embd=37						)







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






							super().tearDown()
							# clean-up as much as possible GPU memory occupied by PyTorch
							gc.collect()
							torch.cuda.empty_cache()







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






							self.config_tester.run_common_tests()







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






							lowercase        =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_ctrl_model(*_lowerCAmelCase						)







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






							lowercase        =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_lm_head_model(*_lowerCAmelCase						)







	@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""						)
	def   _a   (    self						)    ->    str:
							'''simple docstring'''






							pass







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






							for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													lowercase        =							CTRLModel.from_pretrained(_lowerCAmelCase						)
													self.assertIsNotNone(_lowerCAmelCase						)







	@unittest.skip("""The model doesn't support left padding"""						)  # and it's not used enough to be worth fixing :)
	def   _a   (    self						)    ->    List[str]:
							'''simple docstring'''






							pass




@require_torch
class      __UpperCamelCase       (unittest.TestCase			):







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






							super().tearDown()
							# clean-up as much as possible GPU memory occupied by PyTorch
							gc.collect()
							torch.cuda.empty_cache()







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






							lowercase        =							CTRLLMHeadModel.from_pretrained("""ctrl"""						)
							model.to(_lowerCAmelCase						)
							lowercase        =							torch.tensor(
							    [[1_1859, 0, 1611, 8]] ,				dtype=torch.long ,				device=_lowerCAmelCase						)  # Legal the president is
							lowercase        =							[
							    1_1859,
							    0,
							    1611,
							    8,
							    5,
							    150,
							    2_6449,
							    2,
							    19,
							    348,
							    469,
							    3,
							    2595,
							    48,
							    2_0740,
							    24_6533,
							    24_6533,
							    19,
							    30,
							    5,
							]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

							lowercase        =							model.generate(_lowerCAmelCase ,				do_sample=_lowerCAmelCase						)
							self.assertListEqual(output_ids[0].tolist() ,				_lowerCAmelCase						)







'''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


