




"""simple docstring"""
from random import shuffle

import tensorflow as tf
from numpy import array





def 						UpperCAmelCase				(			A__:    Union[str, Any]							,				A__:    Union[str, Any]     )  ->     Optional[Any]:
			__lowerCamelCase					:	Any	       =				int(A__     )
			assert noofclusters < len(A__     )

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

			# Will help select random centroids from among the available vectors
			__lowerCamelCase					:	Optional[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.

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

			with graph.as_default():
						# SESSION OF COMPUTATION

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

						##Variables for cluster assignments of individual vectors(initialized
						##to 0 at first)
						__lowerCamelCase					:	Tuple	       =				[tf.Variable(0     ) for i in range(len(A__     )     )]
						##These nodes will assign an assignment Variable the appropriate
						##value
						__lowerCamelCase					:	Tuple	       =				tf.placeholder('int32'     )
						__lowerCamelCase					:	Optional[Any]	       =				[]
						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
						__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					:	List[str]	       =				tf.reduce_mean(A__							,				0     )

						##Node for computing Euclidean distances
						# Placeholders for input
						__lowerCamelCase					:	Union[str, Any]	       =				tf.placeholder('float'							,				[dim]     )
						__lowerCamelCase					:	List[Any]	       =				tf.placeholder('float'							,				[dim]     )
						__lowerCamelCase					:	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
						__lowerCamelCase					:	Tuple	       =				tf.placeholder('float'							,				[noofclusters]     )
						__lowerCamelCase					:	Any	       =				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.
						__lowerCamelCase					:	Union[str, Any]	       =				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.
						__lowerCamelCase					:	str	       =				100
						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__     )     ):
												__lowerCamelCase					:	Dict	       =				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[Any]	       =				[
												    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
												__lowerCamelCase					:	List[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
												__lowerCamelCase					:	Any	       =				[
												    vectors[i]
												    for i in range(len(A__     )     )
												    if sess.run(assignments[i]     ) == cluster_n
												]
												# Compute new centroid location
												__lowerCamelCase					:	List[str]	       =				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
						__lowerCamelCase					:	Any	       =				sess.run(A__     )
						__lowerCamelCase					:	Union[str, Any]	       =				sess.run(A__     )
						return centroids, assignments









"""simple docstring"""
import unittest

from transformers import GPTNeoXJapaneseConfig, is_torch_available
from transformers.models.gpt_neox_japanese.tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer
from transformers.testing_utils import 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 GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseModel

class __lowercase:




					'''simple docstring'''


					def __init__(    self						,	__a						,	__a=13						,	__a=7						,	__a=True						,	__a=True						,	__a=True						,	__a=True						,	__a=99						,	__a=32						,	__a=5						,	__a=4						,	__a=4						,	__a="gelu"						,	__a=0.0						,	__a=0.1						,	__a=True						,	__a=512						,	__a=16						,	__a=2						,	__a=0.02						,	__a=3						,	__a=4						,	__a=None						,	):
								__lowerCamelCase					:	List[str]	       =				parent
								__lowerCamelCase					:	List[str]	       =				batch_size
								__lowerCamelCase					:	Optional[Any]	       =				seq_length
								__lowerCamelCase					:	List[Any]	       =				is_training
								__lowerCamelCase					:	str	       =				use_input_mask
								__lowerCamelCase					:	List[str]	       =				use_token_type_ids
								__lowerCamelCase					:	str	       =				use_labels
								__lowerCamelCase					:	Tuple	       =				vocab_size
								__lowerCamelCase					:	Any	       =				hidden_size
								__lowerCamelCase					:	List[str]	       =				num_hidden_layers
								__lowerCamelCase					:	Tuple	       =				num_attention_heads
								__lowerCamelCase					:	str	       =				intermediate_multiple_size
								__lowerCamelCase					:	Tuple	       =				hidden_act
								__lowerCamelCase					:	str	       =				hidden_dropout
								__lowerCamelCase					:	List[str]	       =				attention_dropout
								__lowerCamelCase					:	Tuple	       =				weight_tying
								__lowerCamelCase					:	int	       =				max_position_embeddings
								__lowerCamelCase					:	Tuple	       =				type_vocab_size
								__lowerCamelCase					:	Union[str, Any]	       =				type_sequence_label_size
								__lowerCamelCase					:	Optional[int]	       =				initializer_range
								__lowerCamelCase					:	Optional[int]	       =				num_labels
								__lowerCamelCase					:	str	       =				num_choices
								__lowerCamelCase					:	Tuple	       =				scope
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	List[Any]	       =				ids_tensor([self.batch_size, self.seq_length]						,	self.vocab_size     )

								__lowerCamelCase					:	str	       =				None
								if self.use_input_mask:
											__lowerCamelCase					:	List[str]	       =				random_attention_mask([self.batch_size, self.seq_length]     )

								__lowerCamelCase					:	Optional[Any]	       =				None
								if self.use_labels:
											__lowerCamelCase					:	List[str]	       =				ids_tensor([self.batch_size, self.seq_length]						,	self.num_labels     )

								__lowerCamelCase					:	int	       =				self.get_config()

								return config, input_ids, input_mask, token_labels
					def     snake_case_				(    self     ):
								return GPTNeoXJapaneseConfig(
								    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_multiple_size=self.intermediate_multiple_size						,	hidden_act=self.hidden_act						,	hidden_dropout=self.hidden_dropout						,	attention_dropout=self.attention_dropout						,	weight_tying=self.weight_tying						,	max_position_embeddings=self.max_position_embeddings						,	type_vocab_size=self.type_vocab_size						,	is_decoder=__a						,	initializer_range=self.initializer_range						,	)
					def     snake_case_				(    self     ):
								__lowerCamelCase       ,      __lowerCamelCase       ,      __lowerCamelCase       ,      __lowerCamelCase					:	Union[str, Any]	       =				self.prepare_config_and_inputs()

								__lowerCamelCase					:	Tuple	       =				True

								return config, input_ids, input_mask, token_labels
					def     snake_case_				(    self						,	__a						,	__a						,	__a     ):
								__lowerCamelCase					:	Any	       =				GPTNeoXJapaneseModel(config=__a     )
								model.to(__a     )
								model.eval()
								__lowerCamelCase					:	int	       =				model(__a						,	attention_mask=__a     )
								__lowerCamelCase					:	Optional[Any]	       =				model(__a     )
								self.parent.assertEqual(result.last_hidden_state.shape						,	(self.batch_size, self.seq_length, self.hidden_size)     )
					def     snake_case_				(    self						,	__a						,	__a						,	__a     ):
								__lowerCamelCase					:	List[str]	       =				True
								__lowerCamelCase					:	Tuple	       =				GPTNeoXJapaneseModel(__a     )
								model.to(__a     )
								model.eval()
								__lowerCamelCase					:	Optional[Any]	       =				model(__a						,	attention_mask=__a     )
								self.parent.assertEqual(result.last_hidden_state.shape						,	(self.batch_size, self.seq_length, self.hidden_size)     )
					def     snake_case_				(    self						,	__a						,	__a						,	__a						,	__a     ):
								__lowerCamelCase					:	Optional[Any]	       =				GPTNeoXJapaneseForCausalLM(config=__a     )
								model.to(__a     )
								model.eval()
								__lowerCamelCase					:	List[str]	       =				model(__a						,	attention_mask=__a						,	labels=__a     )
								self.parent.assertEqual(result.logits.shape						,	(self.batch_size, self.seq_length, self.vocab_size)     )
					def     snake_case_				(    self						,	__a						,	__a						,	__a     ):
								__lowerCamelCase					:	List[Any]	       =				True
								__lowerCamelCase					:	str	       =				GPTNeoXJapaneseForCausalLM(config=__a     )
								model.to(__a     )
								model.eval()

								# first forward pass
								__lowerCamelCase					:	List[Any]	       =				model(__a						,	attention_mask=__a						,	use_cache=__a     )
								__lowerCamelCase					:	Any	       =				outputs.past_key_values

								# create hypothetical multiple next token and extent to next_input_ids
								__lowerCamelCase					:	int	       =				ids_tensor((self.batch_size, 3)						,	config.vocab_size     )
								__lowerCamelCase					:	Tuple	       =				ids_tensor((self.batch_size, 3)						,	vocab_size=2     )

								# append to next input_ids and
								__lowerCamelCase					:	Any	       =				torch.cat([input_ids, next_tokens]						,	dim=-1     )
								__lowerCamelCase					:	int	       =				torch.cat([input_mask, next_mask]						,	dim=-1     )

								__lowerCamelCase					:	int	       =				model(__a						,	attention_mask=__a						,	output_hidden_states=__a     )
								__lowerCamelCase					:	Dict	       =				output_from_no_past['hidden_states'][0]
								__lowerCamelCase					:	Any	       =				model(
								    __a						,	attention_mask=__a						,	past_key_values=__a						,	output_hidden_states=__a						,	)['hidden_states'][0]

								# select random slice
								__lowerCamelCase					:	Any	       =				ids_tensor((1,)						,	output_from_past.shape[-1]     ).item()
								__lowerCamelCase					:	Dict	       =				output_from_no_past[:, -3:, random_slice_idx].detach()
								__lowerCamelCase					:	Any	       =				output_from_past[:, :, random_slice_idx].detach()

								self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]     )

								# test that outputs are equal for slice
								self.parent.assertTrue(torch.allclose(__a						,	__a						,	atol=1E-3     )     )





					def     snake_case_				(    self     ):
								__lowerCamelCase					:	List[str]	       =				self.prepare_config_and_inputs()
								__lowerCamelCase       ,      __lowerCamelCase       ,      __lowerCamelCase       ,      __lowerCamelCase					:	Any	       =				config_and_inputs
								__lowerCamelCase					:	Tuple	       =				{'input_ids': input_ids, 'attention_mask': input_mask}
								return config, inputs_dict



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




					'''simple docstring'''


					__a						:						Dict     		= (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
					__a						:						str     		= (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
					__a						:						Optional[int]     		= (
					    {'feature-extraction': GPTNeoXJapaneseModel, 'text-generation': GPTNeoXJapaneseForCausalLM}
					    if is_torch_available()
					    else {}
					)
					__a						:						Union[str, Any]     		= False
					__a						:						Optional[Any]     		= False
					__a						:						List[Any]     		= False
					__a						:						List[Any]     		= False
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	List[Any]	       =				GPTNeoXJapaneseModelTester(self     )
								__lowerCamelCase					:	List[Any]	       =				ConfigTester(self						,	config_class=__a						,	hidden_size=37     )
					def     snake_case_				(    self     ):
								self.config_tester.run_common_tests()
					def     snake_case_				(    self     ):
								__lowerCamelCase       ,      __lowerCamelCase       ,      __lowerCamelCase       ,      __lowerCamelCase					:	Union[str, Any]	       =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(__a						,	__a						,	__a     )
					def     snake_case_				(    self     ):
								__lowerCamelCase       ,      __lowerCamelCase       ,      __lowerCamelCase       ,      __lowerCamelCase					:	Tuple	       =				self.model_tester.prepare_config_and_inputs_for_decoder()
								self.model_tester.create_and_check_model_as_decoder(__a						,	__a						,	__a     )
					def     snake_case_				(    self     ):
								# This regression test was failing with PyTorch < 1.3
								__lowerCamelCase       ,      __lowerCamelCase       ,      __lowerCamelCase       ,      __lowerCamelCase					:	str	       =				self.model_tester.prepare_config_and_inputs_for_decoder()

								__lowerCamelCase					:	Tuple	       =				None

								self.model_tester.create_and_check_model_as_decoder(__a						,	__a						,	__a     )
					def     snake_case_				(    self     ):
								__lowerCamelCase       ,      __lowerCamelCase       ,      __lowerCamelCase       ,      __lowerCamelCase					:	Tuple	       =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_decoder_model_past_large_inputs(__a						,	__a						,	__a     )
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	Optional[Any]	       =				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_causal_lm(*__a     )





					@slow
					def     snake_case_				(    self     ):
								__lowerCamelCase					:	List[Any]	       =				'abeja/gpt-neox-japanese-2.7b'

								__lowerCamelCase					:	Any	       =				['データサイエンティストとは、', '100年後に必要とされる会社は、', 'フルリモートの環境で働くために必要なことは、', '国境の長いトンネルを抜けると', '美味しい日本食といえば、']

								__lowerCamelCase					:	Union[str, Any]	       =				[
								    'データサイエンティストとは、データを分析し、ビジネスに役立つ知見を導き出す専門家のことです。',
								    '100年後に必要とされる会社は、「人」が中心の会社です。',
								    'フルリモートの環境で働くために必要なことは、「自分の時間をコントロールする」ことです。',
								    '国境の長いトンネルを抜けると、そこは雪国だった。',
								    '美味しい日本食といえば、やっぱりお寿司ですよね。',
								]

								__lowerCamelCase					:	Dict	       =				GPTNeoXJapaneseTokenizer.from_pretrained(__a     )
								__lowerCamelCase					:	str	       =				GPTNeoXJapaneseForCausalLM.from_pretrained(__a     )

								__lowerCamelCase					:	List[str]	       =				[]
								for prompt in prompts:
											__lowerCamelCase					:	List[Any]	       =				tokenizer(__a						,	return_tensors='pt'     ).input_ids
											__lowerCamelCase					:	List[Any]	       =				model.generate(__a						,	max_length=50     )
											__lowerCamelCase					:	str	       =				tokenizer.batch_decode(__a						,	skip_special_tokens=__a     )
											predicted_outputs += generated_string
								self.assertListEqual(__a						,	__a     )



