






from abc import ABC, abstractmethod
from typing import List, Optional




class 							_lowerCAmelCase(			UpperCAmelCase_   ):





       """simple docstring"""







       def __init__(   self				):
         # test for the above condition
         self.test()





       def      _a       (   self				):
         UpperCamelCase_:   List[str]        =							0
         UpperCamelCase_:   List[Any]        =							False
         while not completed:
           if counter == 1:
             self.reset()
           UpperCamelCase_:   Union[str, Any]        =							self.advance()
           if not self.does_advance(_lowerCamelCase				):
             raise Exception(
                 'Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true.'				)

           UpperCamelCase_ ,UpperCamelCase_ ,UpperCamelCase_:   int        =							self.update(_lowerCamelCase				)
           counter += 1

           if counter > 1_0_0_0_0:
             raise Exception('update() does not fulfill the constraint.'				)

         if self.remaining() != 0:
           raise Exception('Custom Constraint is not defined correctly.'				)





       @abstractmethod
       def      _a       (   self				):
         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''				)





       @abstractmethod
       def      _a       (   self    ,     _lowerCamelCase				):
         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''				)





       @abstractmethod
       def      _a       (   self    ,     _lowerCamelCase				):
         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''				)





       @abstractmethod
       def      _a       (   self				):
         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''				)





       @abstractmethod
       def      _a       (   self				):
         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''				)





       @abstractmethod
       def      _a       (   self    ,     _lowerCamelCase=False				):
         raise NotImplementedError(
             f'''{self.__class__} is an abstract class. Only classes inheriting this class can be called.'''				)







class 							_lowerCAmelCase(			UpperCAmelCase_   ):





       """simple docstring"""







       def __init__(   self    ,     _lowerCamelCase				):
         super(_lowerCamelCase    ,     self				).__init__()

         if not isinstance(_lowerCamelCase    ,     _lowerCamelCase				) or len(_lowerCamelCase				) == 0:
           raise ValueError(f'''`token_ids` has to be a non-empty list, but is {token_ids}.'''				)
         if any((not isinstance(_lowerCamelCase    ,     _lowerCamelCase				) or token_id < 0) for token_id in token_ids				):
           raise ValueError(f'''Each list in `token_ids` has to be a list of positive integers, but is {token_ids}.'''				)

         UpperCamelCase_:   Optional[Any]        =							token_ids

         UpperCamelCase_:   Optional[int]        =							len(self.token_ids				)
         UpperCamelCase_:   Union[str, Any]        =							-1  # the index of the currently fulfilled step
         UpperCamelCase_:   List[str]        =							False





       def      _a       (   self				):
         if self.completed:
           return None
         return self.token_ids[self.fulfilled_idx + 1]





       def      _a       (   self    ,     _lowerCamelCase				):
         if not isinstance(_lowerCamelCase    ,     _lowerCamelCase				):
           raise ValueError(f'''`token_id` has to be an `int`, but is {token_id} of type {type(_lowerCamelCase				)}'''				)

         if self.completed:
           return False

         return token_id == self.token_ids[self.fulfilled_idx + 1]





       def      _a       (   self    ,     _lowerCamelCase				):
         if not isinstance(_lowerCamelCase    ,     _lowerCamelCase				):
           raise ValueError(f'''`token_id` has to be an `int`, but is {token_id} of type {type(_lowerCamelCase				)}'''				)

         UpperCamelCase_:   Dict        =							False
         UpperCamelCase_:   Optional[Any]        =							False
         UpperCamelCase_:   int        =							False

         if self.does_advance(_lowerCamelCase				):
           self.fulfilled_idx += 1
           UpperCamelCase_:   Tuple        =							True
           if self.fulfilled_idx == (self.seqlen - 1):
             UpperCamelCase_:   Tuple        =							True
           UpperCamelCase_:   List[Any]        =							completed
         else:
           # failed to make progress.
           UpperCamelCase_:   Tuple        =							True
           self.reset()
         return stepped, completed, reset





       def      _a       (   self				):
         UpperCamelCase_:   str        =							False
         UpperCamelCase_:   List[Any]        =							0





       def      _a       (   self				):
         return self.seqlen - (self.fulfilled_idx + 1)





       def      _a       (   self    ,     _lowerCamelCase=False				):
         UpperCamelCase_:   Union[str, Any]        =							PhrasalConstraint(self.token_ids				)

         if stateful:
           UpperCamelCase_:   Any        =							self.seqlen
           UpperCamelCase_:   Any        =							self.fulfilled_idx
           UpperCamelCase_:   List[Any]        =							self.completed

         return new_constraint







class 							_lowerCAmelCase:





       """simple docstring"""







       def __init__(   self    ,     _lowerCamelCase    ,     _lowerCamelCase=True				):
         UpperCamelCase_:   Dict        =							max([len(_lowerCamelCase				) for one in nested_token_ids]				)

         UpperCamelCase_:   Dict        =							{}
         for token_ids in nested_token_ids:
           UpperCamelCase_:   str        =							root
           for tidx, token_id in enumerate(_lowerCamelCase				):
             if token_id not in level:
               UpperCamelCase_:   Optional[Any]        =							{}

             UpperCamelCase_:   Dict        =							level[token_id]

         if no_subsets and self.has_subsets(_lowerCamelCase    ,     _lowerCamelCase				):
           raise ValueError(
               'Each list in `nested_token_ids` can\'t be a complete subset of another list, but is'
               f''' {nested_token_ids}.'''				)

         UpperCamelCase_:   int        =							root





       def      _a       (   self    ,     _lowerCamelCase				):
         UpperCamelCase_:   Union[str, Any]        =							self.trie

         for current_token in current_seq:
           UpperCamelCase_:   Optional[Any]        =							start[current_token]

         UpperCamelCase_:   Dict        =							list(start.keys()				)

         return next_tokens





       def      _a       (   self    ,     _lowerCamelCase				):
         UpperCamelCase_:   Tuple        =							self.next_tokens(_lowerCamelCase				)

         return len(_lowerCamelCase				) == 0





       def      _a       (   self    ,     _lowerCamelCase				):
         UpperCamelCase_:   Union[str, Any]        =							list(root.values()				)
         if len(_lowerCamelCase				) == 0:
           return 1
         else:
           return sum([self.count_leaves(_lowerCamelCase				) for nn in next_nodes]				)





       def      _a       (   self    ,     _lowerCamelCase    ,     _lowerCamelCase				):
         UpperCamelCase_:   str        =							self.count_leaves(_lowerCamelCase				)
         return len(_lowerCamelCase				) != leaf_count







class 							_lowerCAmelCase(			UpperCAmelCase_   ):





       """simple docstring"""







       def __init__(   self    ,     _lowerCamelCase				):
         super(_lowerCamelCase    ,     self				).__init__()

         if not isinstance(_lowerCamelCase    ,     _lowerCamelCase				) or len(_lowerCamelCase				) == 0:
           raise ValueError(f'''`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}.'''				)
         if any(not isinstance(_lowerCamelCase    ,     _lowerCamelCase				) for token_ids in nested_token_ids				):
           raise ValueError(f'''`nested_token_ids` has to be a list of lists, but is {nested_token_ids}.'''				)
         if any(
             any((not isinstance(_lowerCamelCase    ,     _lowerCamelCase				) or token_id < 0) for token_id in token_ids				)
             for token_ids in nested_token_ids				):
           raise ValueError(
               f'''Each list in `nested_token_ids` has to be a list of positive integers, but is {nested_token_ids}.'''				)

         UpperCamelCase_:   Any        =							DisjunctiveTrie(_lowerCamelCase				)
         UpperCamelCase_:   Dict        =							nested_token_ids

         UpperCamelCase_:   Union[str, Any]        =							self.trie.max_height
         UpperCamelCase_:   Any        =							[]
         UpperCamelCase_:   Optional[Any]        =							False





       def      _a       (   self				):
         UpperCamelCase_:   Optional[int]        =							self.trie.next_tokens(self.current_seq				)

         if len(_lowerCamelCase				) == 0:
           return None
         else:
           return token_list





       def      _a       (   self    ,     _lowerCamelCase				):
         if not isinstance(_lowerCamelCase    ,     _lowerCamelCase				):
           raise ValueError(f'''`token_id` is supposed to be type `int`, but is {token_id} of type {type(_lowerCamelCase				)}'''				)

         UpperCamelCase_:   Union[str, Any]        =							self.trie.next_tokens(self.current_seq				)

         return token_id in next_tokens





       def      _a       (   self    ,     _lowerCamelCase				):
         if not isinstance(_lowerCamelCase    ,     _lowerCamelCase				):
           raise ValueError(f'''`token_id` is supposed to be type `int`, but is {token_id} of type {type(_lowerCamelCase				)}'''				)

         UpperCamelCase_:   int        =							False
         UpperCamelCase_:   Dict        =							False
         UpperCamelCase_:   int        =							False

         if self.does_advance(_lowerCamelCase				):
           self.current_seq.append(_lowerCamelCase				)
           UpperCamelCase_:   List[str]        =							True
         else:
           UpperCamelCase_:   Tuple        =							True
           self.reset()

         UpperCamelCase_:   Any        =							self.trie.reached_leaf(self.current_seq				)
         UpperCamelCase_:   Union[str, Any]        =							completed

         return stepped, completed, reset





       def      _a       (   self				):
         UpperCamelCase_:   Dict        =							False
         UpperCamelCase_:   List[str]        =							[]





       def      _a       (   self				):
         if self.completed:
           # since this can be completed without reaching max height
           return 0
         else:
           return self.seqlen - len(self.current_seq				)





       def      _a       (   self    ,     _lowerCamelCase=False				):
         UpperCamelCase_:   Optional[int]        =							DisjunctiveConstraint(self.token_ids				)

         if stateful:
           UpperCamelCase_:   str        =							self.seqlen
           UpperCamelCase_:   Any        =							self.current_seq
           UpperCamelCase_:   Any        =							self.completed

         return new_constraint







class 							_lowerCAmelCase:





       """simple docstring"""







       def __init__(   self    ,     _lowerCamelCase				):
         UpperCamelCase_:   Tuple        =							constraints

         # max # of steps required to fulfill a given constraint
         UpperCamelCase_:   List[Any]        =							max([c.seqlen for c in constraints]				)
         UpperCamelCase_:   Optional[int]        =							len(_lowerCamelCase				)
         UpperCamelCase_:   Optional[Any]        =							False

         self.init_state()





       def      _a       (   self				):
         UpperCamelCase_:   str        =							[]
         UpperCamelCase_:   Union[str, Any]        =							None
         UpperCamelCase_:   Tuple        =							[constraint.copy(stateful=_lowerCamelCase				) for constraint in self.constraints]





       def      _a       (   self				):
         UpperCamelCase_:   Tuple        =							0
         if self.inprogress_constraint:
           # extra points for having a constraint mid-fulfilled
           add += self.max_seqlen - self.inprogress_constraint.remaining()

         return (len(self.complete_constraints				) * self.max_seqlen) + add





       def      _a       (   self				):
         UpperCamelCase_:   List[str]        =							[]
         if self.inprogress_constraint is None:
           for constraint in self.pending_constraints:  # "pending" == "unfulfilled yet"
             UpperCamelCase_:   Tuple        =							constraint.advance()
             if isinstance(_lowerCamelCase    ,     _lowerCamelCase				):
               token_list.append(_lowerCamelCase				)
             elif isinstance(_lowerCamelCase    ,     _lowerCamelCase				):
               token_list.extend(_lowerCamelCase				)
         else:
           UpperCamelCase_:   Dict        =							self.inprogress_constraint.advance()
           if isinstance(_lowerCamelCase    ,     _lowerCamelCase				):
             token_list.append(_lowerCamelCase				)
           elif isinstance(_lowerCamelCase    ,     _lowerCamelCase				):
             token_list.extend(_lowerCamelCase				)

         if len(_lowerCamelCase				) == 0:
           return None
         else:
           return token_list





       def      _a       (   self    ,     _lowerCamelCase				):
         self.init_state()

         if token_ids is not None:
           for token in token_ids:
             # completes or steps **one** constraint
             UpperCamelCase_ ,UpperCamelCase_:   Any        =							self.add(_lowerCamelCase				)

             # the entire list of constraints are fulfilled
             if self.completed:
               break





       def      _a       (   self    ,     _lowerCamelCase				):
         if not isinstance(_lowerCamelCase    ,     _lowerCamelCase				):
           raise ValueError(f'''`token_id` should be an `int`, but is `{token_id}`.'''				)

         UpperCamelCase_ ,UpperCamelCase_:   int        =							False, False

         if self.completed:
           UpperCamelCase_:   List[Any]        =							True
           UpperCamelCase_:   List[Any]        =							False
           return complete, stepped

         if self.inprogress_constraint is not None:
           # In the middle of fulfilling a constraint. If the `token_id` *does* makes an incremental progress to current
           # job, simply update the state

           UpperCamelCase_ ,UpperCamelCase_ ,UpperCamelCase_:   Optional[Any]        =							self.inprogress_constraint.update(_lowerCamelCase				)
           if reset:
             # 1. If the next token breaks the progress, then we must restart.
             #     e.g. constraint = "I love pies" and sequence so far is "I love" but `token_id` == "books".

             #     But that doesn't mean we self.init_state(), since we only reset the state for this particular
             #     constraint, not the full list of constraints.

             self.pending_constraints.append(self.inprogress_constraint.copy(stateful=_lowerCamelCase				)				)
             UpperCamelCase_:   str        =							None

           if complete:
             # 2. If the next token completes the constraint, move it to completed list, set
             #     inprogress to None. If there are no pending constraints either, then this full list of constraints
             #     is complete.

             self.complete_constraints.append(self.inprogress_constraint				)
             UpperCamelCase_:   List[Any]        =							None

             if len(self.pending_constraints				) == 0:
               # we're done!
               UpperCamelCase_:   Any        =							True

         else:
           # Not in the middle of fulfilling a constraint. So does this `token_id` helps us step towards any of our list
           # of constraints?

           for cidx, pending_constraint in enumerate(self.pending_constraints				):
             if pending_constraint.does_advance(_lowerCamelCase				):
               UpperCamelCase_ ,UpperCamelCase_ ,UpperCamelCase_:   int        =							pending_constraint.update(_lowerCamelCase				)

               if not stepped:
                 raise Exception(
                     '`constraint.update(token_id)` is not yielding incremental progress, '
                     'even though `constraint.does_advance(token_id)` is true.'				)

               if complete:
                 self.complete_constraints.append(_lowerCamelCase				)
                 UpperCamelCase_:   Union[str, Any]        =							None

               if not complete and stepped:
                 UpperCamelCase_:   List[str]        =							pending_constraint

               if complete or stepped:
                 # If we made any progress at all, then it's at least not a "pending constraint".

                 UpperCamelCase_:   List[Any]        =							(
                     self.pending_constraints[:cidx] + self.pending_constraints[cidx + 1 :]
                 )

                 if len(self.pending_constraints				) == 0 and self.inprogress_constraint is None:
                   # If there's no longer any pending after this and no inprogress either, then we must be
                   # complete.

                   UpperCamelCase_:   str        =							True

                 break  # prevent accidentally stepping through multiple constraints with just one token.

         return complete, stepped





       def      _a       (   self    ,     _lowerCamelCase=True				):
         UpperCamelCase_:   List[Any]        =							ConstraintListState(self.constraints				)  # we actually never though self.constraints objects
         # throughout this process. So it's at initialization state.

         if stateful:
           UpperCamelCase_:   Optional[int]        =							[
               constraint.copy(stateful=_lowerCamelCase				) for constraint in self.complete_constraints
           ]
           if self.inprogress_constraint is not None:
             UpperCamelCase_:   Tuple        =							self.inprogress_constraint.copy(stateful=_lowerCamelCase				)
           UpperCamelCase_:   Optional[int]        =							[constraint.copy() for constraint in self.pending_constraints]

         return new_state

"""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			:				Optional[int]							,   UpperCAmelCase_			:				Any							,   UpperCAmelCase_			:				str=13							,   UpperCAmelCase_			:				Optional[int]=7							,   UpperCAmelCase_			:				Optional[int]=True							,   UpperCAmelCase_			:				Optional[Any]=True							,   UpperCAmelCase_			:				Optional[int]=True							,   UpperCAmelCase_			:				int=True							,   UpperCAmelCase_			:				Dict=99							,   UpperCAmelCase_			:				str=32							,   UpperCAmelCase_			:				List[Any]=5							,   UpperCAmelCase_			:				str=4							,   UpperCAmelCase_			:				Union[str, Any]=4							,   UpperCAmelCase_			:				Optional[int]="gelu"							,   UpperCAmelCase_			:				Tuple=0.0							,   UpperCAmelCase_			:				Any=0.1							,   UpperCAmelCase_			:				int=True							,   UpperCAmelCase_			:				Union[str, Any]=512							,   UpperCAmelCase_			:				int=16							,   UpperCAmelCase_			:				Optional[Any]=2							,   UpperCAmelCase_			:				str=0.02							,   UpperCAmelCase_			:				Any=3							,   UpperCAmelCase_			:				Any=4							,   UpperCAmelCase_			:				Optional[Any]=None							,   )->	Tuple:







              """simple docstring"""
              UpperCamelCase       			=		parent
              UpperCamelCase       			=		batch_size
              UpperCamelCase       			=		seq_length
              UpperCamelCase       			=		is_training
              UpperCamelCase       			=		use_input_mask
              UpperCamelCase       			=		use_token_type_ids
              UpperCamelCase       			=		use_labels
              UpperCamelCase       			=		vocab_size
              UpperCamelCase       			=		hidden_size
              UpperCamelCase       			=		num_hidden_layers
              UpperCamelCase       			=		num_attention_heads
              UpperCamelCase       			=		intermediate_multiple_size
              UpperCamelCase       			=		hidden_act
              UpperCamelCase       			=		hidden_dropout
              UpperCamelCase       			=		attention_dropout
              UpperCamelCase       			=		weight_tying
              UpperCamelCase       			=		max_position_embeddings
              UpperCamelCase       			=		type_vocab_size
              UpperCamelCase       			=		type_sequence_label_size
              UpperCamelCase       			=		initializer_range
              UpperCamelCase       			=		num_labels
              UpperCamelCase       			=		num_choices
              UpperCamelCase       			=		scope






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Optional[int]      )->	Union[str, Any]:







              """simple docstring"""
              UpperCamelCase       			=		ids_tensor([self.batch_size, self.seq_length]							,   self.vocab_size      )

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

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

              UpperCamelCase       			=		self.get_config()

              return config, input_ids, input_mask, token_labels






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Dict      )->	Dict:







              """simple docstring"""
              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=UpperCAmelCase_							,   initializer_range=self.initializer_range							,   )






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







              """simple docstring"""
              UpperCamelCase			,							UpperCamelCase			,							UpperCamelCase			,							UpperCamelCase       			=		self.prepare_config_and_inputs()

              UpperCamelCase       			=		True

              return config, input_ids, input_mask, token_labels






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Union[str, Any]							,   UpperCAmelCase_			:				List[Any]							,   UpperCAmelCase_			:				Union[str, Any]							,   UpperCAmelCase_			:				List[Any]      )->	List[str]:







              """simple docstring"""
              UpperCamelCase       			=		GPTNeoXJapaneseModel(config=UpperCAmelCase_      )
              model.to(UpperCAmelCase_      )
              model.eval()
              UpperCamelCase       			=		model(UpperCAmelCase_							,   attention_mask=UpperCAmelCase_      )
              UpperCamelCase       			=		model(UpperCAmelCase_      )
              self.parent.assertEqual(result.last_hidden_state.shape							,   (self.batch_size, self.seq_length, self.hidden_size)      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Any							,   UpperCAmelCase_			:				List[str]							,   UpperCAmelCase_			:				List[Any]							,   UpperCAmelCase_			:				List[str]      )->	List[str]:







              """simple docstring"""
              UpperCamelCase       			=		True
              UpperCamelCase       			=		GPTNeoXJapaneseModel(UpperCAmelCase_      )
              model.to(UpperCAmelCase_      )
              model.eval()
              UpperCamelCase       			=		model(UpperCAmelCase_							,   attention_mask=UpperCAmelCase_      )
              self.parent.assertEqual(result.last_hidden_state.shape							,   (self.batch_size, self.seq_length, self.hidden_size)      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				List[str]							,   UpperCAmelCase_			:				Tuple							,   UpperCAmelCase_			:				Dict							,   UpperCAmelCase_			:				List[Any]							,   UpperCAmelCase_			:				Optional[Any]      )->	Any:







              """simple docstring"""
              UpperCamelCase       			=		GPTNeoXJapaneseForCausalLM(config=UpperCAmelCase_      )
              model.to(UpperCAmelCase_      )
              model.eval()
              UpperCamelCase       			=		model(UpperCAmelCase_							,   attention_mask=UpperCAmelCase_							,   labels=UpperCAmelCase_      )
              self.parent.assertEqual(result.logits.shape							,   (self.batch_size, self.seq_length, self.vocab_size)      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Dict							,   UpperCAmelCase_			:				int							,   UpperCAmelCase_			:				Optional[int]							,   UpperCAmelCase_			:				Optional[Any]      )->	Any:







              """simple docstring"""
              UpperCamelCase       			=		True
              UpperCamelCase       			=		GPTNeoXJapaneseForCausalLM(config=UpperCAmelCase_      )
              model.to(UpperCAmelCase_      )
              model.eval()

              # first forward pass
              UpperCamelCase       			=		model(UpperCAmelCase_							,   attention_mask=UpperCAmelCase_							,   use_cache=UpperCAmelCase_      )
              UpperCamelCase       			=		outputs.past_key_values

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

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

              UpperCamelCase       			=		model(UpperCAmelCase_							,   attention_mask=UpperCAmelCase_							,   output_hidden_states=UpperCAmelCase_      )
              UpperCamelCase       			=		output_from_no_past["hidden_states"][0]
              UpperCamelCase       			=		model(
                  UpperCAmelCase_							,   attention_mask=UpperCAmelCase_							,   past_key_values=UpperCAmelCase_							,   output_hidden_states=UpperCAmelCase_							,   )["hidden_states"][0]

              # select random slice
              UpperCamelCase       			=		ids_tensor((1,)							,   output_from_past.shape[-1]      ).item()
              UpperCamelCase       			=		output_from_no_past[:, -3:, random_slice_idx].detach()
              UpperCamelCase       			=		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(UpperCAmelCase_							,   UpperCAmelCase_							,   atol=1e-3      )      )






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







              """simple docstring"""
              UpperCamelCase       			=		self.prepare_config_and_inputs()
              UpperCamelCase			,							UpperCamelCase			,							UpperCamelCase			,							UpperCamelCase       			=		config_and_inputs
              UpperCamelCase       			=		{"input_ids": input_ids, "attention_mask": input_mask}
              return config, inputs_dict





@require_torch
class 	__a	(   _lowerCAmelCase     ,  _lowerCAmelCase     ,  unittest.TestCase						):
       UpperCamelCase_		:       Union[str, Any]				=      (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
       UpperCamelCase_		:       Optional[int]				=      (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
       UpperCamelCase_		:       List[str]				=      (
           {'''feature-extraction''': GPTNeoXJapaneseModel, '''text-generation''': GPTNeoXJapaneseForCausalLM}
           if is_torch_available()
           else {}
       )
       UpperCamelCase_		:       Optional[Any]				=      False
       UpperCamelCase_		:       Tuple				=      False
       UpperCamelCase_		:       Optional[int]				=      False
       UpperCamelCase_		:       List[Any]				=      False






       def 						_SCREAMING_SNAKE_CASE							(	self			:				List[str]      )->	List[str]:







              """simple docstring"""
              UpperCamelCase       			=		GPTNeoXJapaneseModelTester(self      )
              UpperCamelCase       			=		ConfigTester(self							,   config_class=UpperCAmelCase_							,   hidden_size=37      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				str      )->	Any:







              """simple docstring"""
              self.config_tester.run_common_tests()






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Optional[int]      )->	Dict:







              """simple docstring"""
              UpperCamelCase			,							UpperCamelCase			,							UpperCamelCase			,							UpperCamelCase       			=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(UpperCAmelCase_							,   UpperCAmelCase_							,   UpperCAmelCase_      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				List[Any]      )->	Optional[Any]:







              """simple docstring"""
              UpperCamelCase			,							UpperCamelCase			,							UpperCamelCase			,							UpperCamelCase       			=		self.model_tester.prepare_config_and_inputs_for_decoder()
              self.model_tester.create_and_check_model_as_decoder(UpperCAmelCase_							,   UpperCAmelCase_							,   UpperCAmelCase_      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Dict      )->	List[Any]:







              """simple docstring"""
              # This regression test was failing with PyTorch < 1.3
              UpperCamelCase			,							UpperCamelCase			,							UpperCamelCase			,							UpperCamelCase       			=		self.model_tester.prepare_config_and_inputs_for_decoder()

              UpperCamelCase       			=		None

              self.model_tester.create_and_check_model_as_decoder(UpperCAmelCase_							,   UpperCAmelCase_							,   UpperCAmelCase_      )






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







              """simple docstring"""
              UpperCamelCase			,							UpperCamelCase			,							UpperCamelCase			,							UpperCamelCase       			=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_decoder_model_past_large_inputs(UpperCAmelCase_							,   UpperCAmelCase_							,   UpperCAmelCase_      )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				List[Any]      )->	Optional[int]:







              """simple docstring"""
              UpperCamelCase       			=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_causal_lm(*UpperCAmelCase_      )






       @slow
       def 						_SCREAMING_SNAKE_CASE							(	self			:				Dict      )->	str:







              """simple docstring"""
              UpperCamelCase       			=		"abeja/gpt-neox-japanese-2.7b"

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

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

              UpperCamelCase       			=		GPTNeoXJapaneseTokenizer.from_pretrained(UpperCAmelCase_      )
              UpperCamelCase       			=		GPTNeoXJapaneseForCausalLM.from_pretrained(UpperCAmelCase_      )

              UpperCamelCase       			=		[]
              for prompt in prompts:
                     UpperCamelCase       			=		tokenizer(UpperCAmelCase_							,   return_tensors="pt"      ).input_ids
                     UpperCamelCase       			=		model.generate(UpperCAmelCase_							,   max_length=50      )
                     UpperCamelCase       			=		tokenizer.batch_decode(UpperCAmelCase_							,   skip_special_tokens=UpperCAmelCase_      )
                     predicted_outputs += generated_string
              self.assertListEqual(UpperCAmelCase_							,   UpperCAmelCase_      )





