"""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 		__A							:






     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.0_2       ,  a__=3       ,  a__=4       ,  a__=None       ,  ):
       _lowerCAmelCase		: Union[str, Any]						    =  parent
       _lowerCAmelCase		: str						    =  batch_size
       _lowerCAmelCase		: List[str]						    =  seq_length
       _lowerCAmelCase		: int						    =  is_training
       _lowerCAmelCase		: Union[str, Any]						    =  use_input_mask
       _lowerCAmelCase		: Optional[Any]						    =  use_token_type_ids
       _lowerCAmelCase		: List[Any]						    =  use_labels
       _lowerCAmelCase		: List[Any]						    =  vocab_size
       _lowerCAmelCase		: List[str]						    =  hidden_size
       _lowerCAmelCase		: List[Any]						    =  num_hidden_layers
       _lowerCAmelCase		: int						    =  num_attention_heads
       _lowerCAmelCase		: Any						    =  intermediate_multiple_size
       _lowerCAmelCase		: Dict						    =  hidden_act
       _lowerCAmelCase		: Any						    =  hidden_dropout
       _lowerCAmelCase		: str						    =  attention_dropout
       _lowerCAmelCase		: List[str]						    =  weight_tying
       _lowerCAmelCase		: int						    =  max_position_embeddings
       _lowerCAmelCase		: Tuple						    =  type_vocab_size
       _lowerCAmelCase		: Optional[Any]						    =  type_sequence_label_size
       _lowerCAmelCase		: Optional[Any]						    =  initializer_range
       _lowerCAmelCase		: str						    =  num_labels
       _lowerCAmelCase		: Union[str, Any]						    =  num_choices
       _lowerCAmelCase		: Any						    =  scope






     def 					__A						(	self						):
       _lowerCAmelCase		: Tuple						    =  ids_tensor([self.batch_size, self.seq_length]       ,  self.vocab_size						)

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

       _lowerCAmelCase		: Optional[int]						    =  None
       if self.use_labels:
         _lowerCAmelCase		: Optional[Any]						    =  ids_tensor([self.batch_size, self.seq_length]       ,  self.num_labels						)

       _lowerCAmelCase		: Union[str, Any]						    =  self.get_config()

       return config, input_ids, input_mask, token_labels






     def 					__A						(	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 					__A						(	self						):
       _lowerCAmelCase    ,			_lowerCAmelCase    ,			_lowerCAmelCase    ,			_lowerCAmelCase		: Union[str, Any]						    =  self.prepare_config_and_inputs()

       _lowerCAmelCase		: Any						    =  True

       return config, input_ids, input_mask, token_labels






     def 					__A						(	self       ,  a__       ,  a__       ,  a__						):
       _lowerCAmelCase		: Optional[Any]						    =  GPTNeoXJapaneseModel(config=a__						)
       model.to(a__						)
       model.eval()
       _lowerCAmelCase		: List[str]						    =  model(a__       ,  attention_mask=a__						)
       _lowerCAmelCase		: str						    =  model(a__						)
       self.parent.assertEqual(result.last_hidden_state.shape       ,  (self.batch_size, self.seq_length, self.hidden_size)						)






     def 					__A						(	self       ,  a__       ,  a__       ,  a__						):
       _lowerCAmelCase		: List[str]						    =  True
       _lowerCAmelCase		: Tuple						    =  GPTNeoXJapaneseModel(a__						)
       model.to(a__						)
       model.eval()
       _lowerCAmelCase		: Optional[int]						    =  model(a__       ,  attention_mask=a__						)
       self.parent.assertEqual(result.last_hidden_state.shape       ,  (self.batch_size, self.seq_length, self.hidden_size)						)






     def 					__A						(	self       ,  a__       ,  a__       ,  a__       ,  a__						):
       _lowerCAmelCase		: int						    =  GPTNeoXJapaneseForCausalLM(config=a__						)
       model.to(a__						)
       model.eval()
       _lowerCAmelCase		: int						    =  model(a__       ,  attention_mask=a__       ,  labels=a__						)
       self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.vocab_size)						)






     def 					__A						(	self       ,  a__       ,  a__       ,  a__						):
       _lowerCAmelCase		: int						    =  True
       _lowerCAmelCase		: Dict						    =  GPTNeoXJapaneseForCausalLM(config=a__						)
       model.to(a__						)
       model.eval()

       # first forward pass
       _lowerCAmelCase		: Optional[int]						    =  model(a__       ,  attention_mask=a__       ,  use_cache=a__						)
       _lowerCAmelCase		: str						    =  outputs.past_key_values

       # create hypothetical multiple next token and extent to next_input_ids
       _lowerCAmelCase		: str						    =  ids_tensor((self.batch_size, 3)       ,  config.vocab_size						)
       _lowerCAmelCase		: Dict						    =  ids_tensor((self.batch_size, 3)       ,  vocab_size=2						)

       # append to next input_ids and
       _lowerCAmelCase		: Union[str, Any]						    =  torch.cat([input_ids, next_tokens]       ,  dim=-1						)
       _lowerCAmelCase		: Optional[Any]						    =  torch.cat([input_mask, next_mask]       ,  dim=-1						)

       _lowerCAmelCase		: List[str]						    =  model(a__       ,  attention_mask=a__       ,  output_hidden_states=a__						)
       _lowerCAmelCase		: Dict						    =  output_from_no_past["""hidden_states"""][0]
       _lowerCAmelCase		: Tuple						    =  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		: int						    =  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 					__A						(	self						):
       _lowerCAmelCase		: Any						    =  self.prepare_config_and_inputs()
       _lowerCAmelCase    ,			_lowerCAmelCase    ,			_lowerCAmelCase    ,			_lowerCAmelCase		: Any						    =  config_and_inputs
       _lowerCAmelCase		: List[Any]						    =  {"""input_ids""": input_ids, """attention_mask""": input_mask}
       return config, inputs_dict







@require_torch
class 		__A							(				SCREAMING_SNAKE_CASE_					,			SCREAMING_SNAKE_CASE_					,			unittest.TestCase	):
     _UpperCamelCase							:    Optional[int]	      =	(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
     _UpperCamelCase							:    str	      =	(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
     _UpperCamelCase							:    Union[str, Any]	      =	(
         {"feature-extraction": GPTNeoXJapaneseModel, "text-generation": GPTNeoXJapaneseForCausalLM}
         if is_torch_available()
         else {}
     )
     _UpperCamelCase							:    Optional[int]	      =	False
     _UpperCamelCase							:    int	      =	False
     _UpperCamelCase							:    int	      =	False
     _UpperCamelCase							:    int	      =	False






     def 					__A						(	self						):
       _lowerCAmelCase		: List[str]						    =  GPTNeoXJapaneseModelTester(self						)
       _lowerCAmelCase		: int						    =  ConfigTester(self       ,  config_class=a__       ,  hidden_size=37						)






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






     def 					__A						(	self						):
       _lowerCAmelCase    ,			_lowerCAmelCase    ,			_lowerCAmelCase    ,			_lowerCAmelCase		: Tuple						    =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(a__       ,  a__       ,  a__						)






     def 					__A						(	self						):
       _lowerCAmelCase    ,			_lowerCAmelCase    ,			_lowerCAmelCase    ,			_lowerCAmelCase		: Union[str, Any]						    =  self.model_tester.prepare_config_and_inputs_for_decoder()
       self.model_tester.create_and_check_model_as_decoder(a__       ,  a__       ,  a__						)






     def 					__A						(	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		: Optional[Any]						    =  None

       self.model_tester.create_and_check_model_as_decoder(a__       ,  a__       ,  a__						)






     def 					__A						(	self						):
       _lowerCAmelCase    ,			_lowerCAmelCase    ,			_lowerCAmelCase    ,			_lowerCAmelCase		: Union[str, Any]						    =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_decoder_model_past_large_inputs(a__       ,  a__       ,  a__						)






     def 					__A						(	self						):
       _lowerCAmelCase		: Tuple						    =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_causal_lm(*a__						)






     @slow
     def 					__A						(	self						):
       _lowerCAmelCase		: int						    =  """abeja/gpt-neox-japanese-2.7b"""

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

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

       _lowerCAmelCase		: Tuple						    =  GPTNeoXJapaneseTokenizer.from_pretrained(a__						)
       _lowerCAmelCase		: Tuple						    =  GPTNeoXJapaneseForCausalLM.from_pretrained(a__						)

       _lowerCAmelCase		: Dict						    =  []
       for prompt in prompts:
         _lowerCAmelCase		: str						    =  tokenizer(a__       ,  return_tensors="""pt"""						).input_ids
         _lowerCAmelCase		: Optional[int]						    =  model.generate(a__       ,  max_length=50						)
         _lowerCAmelCase		: Optional[int]						    =  tokenizer.batch_decode(a__       ,  skip_special_tokens=a__						)
         predicted_outputs += generated_string
       self.assertListEqual(a__       ,  a__						)





"""simple docstring"""




from random import shuffle

import tensorflow as tf
from numpy import array





def 							SCREAMING_SNAKE_CASE  (    _lowerCamelCase		: Union[str, Any]    ,_lowerCamelCase		: Tuple			) ->   Dict:
  _lowerCAmelCase		: List[str]						    =  int(_lowerCamelCase			)
  assert noofclusters < len(_lowerCamelCase			)

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

  # Will help select random centroids from among the available vectors
  _lowerCAmelCase		: Any						    =  list(range(len(_lowerCamelCase			)			)			)
  shuffle(_lowerCamelCase			)

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

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

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

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

    # Initialize all variables
    sess.run(_lowerCamelCase			)

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

        # Return centroids and assignments
    _lowerCAmelCase		: Optional[int]						    =  sess.run(_lowerCamelCase			)
    _lowerCAmelCase		: List[Any]						    =  sess.run(_lowerCamelCase			)
    return centroids, assignments




