


'''simple docstring'''
import unittest

from parameterized import parameterized

from transformers import AutoTokenizer, GPTNeoXConfig, is_torch_available, set_seed
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 (
     GPTNeoXForCausalLM,
     GPTNeoXForQuestionAnswering,
     GPTNeoXForSequenceClassification,
     GPTNeoXForTokenClassification,
     GPTNeoXModel,
 )

class     _lowerCamelCase       :




 '''simple docstring'''


 def __init__(   self   ,   __lowercase   ,   __lowercase=13   ,   __lowercase=7   ,   __lowercase=True   ,   __lowercase=True   ,   __lowercase=True   ,   __lowercase=True   ,   __lowercase=99   ,   __lowercase=64   ,   __lowercase=5   ,   __lowercase=4   ,   __lowercase=37   ,   __lowercase="gelu"   ,   __lowercase=0.1   ,   __lowercase=0.1   ,   __lowercase=512   ,   __lowercase=16   ,   __lowercase=2   ,   __lowercase=0.0_2   ,   __lowercase=3   ,   __lowercase=4   ,   __lowercase=None   ,   ):

  """simple docstring"""







  __A	:		List[Any]	  =      parent
  __A	:		Dict	  =      batch_size
  __A	:		Optional[int]	  =      seq_length
  __A	:		str	  =      is_training
  __A	:		Tuple	  =      use_input_mask
  __A	:		Dict	  =      use_token_type_ids
  __A	:		Optional[int]	  =      use_labels
  __A	:		Dict	  =      vocab_size
  __A	:		Optional[int]	  =      hidden_size
  __A	:		List[str]	  =      num_hidden_layers
  __A	:		Tuple	  =      num_attention_heads
  __A	:		Optional[Any]	  =      intermediate_size
  __A	:		Dict	  =      hidden_act
  __A	:		Optional[int]	  =      hidden_dropout_prob
  __A	:		str	  =      attention_probs_dropout_prob
  __A	:		Any	  =      max_position_embeddings
  __A	:		Dict	  =      type_vocab_size
  __A	:		Any	  =      type_sequence_label_size
  __A	:		int	  =      initializer_range
  __A	:		Dict	  =      num_labels
  __A	:		Any	  =      num_choices
  __A	:		Tuple	  =      scope
  __A	:		Union[str, Any]	  =      vocab_size - 1


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		int	  =      ids_tensor([self.batch_size, self.seq_length]   ,   self.vocab_size    )

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

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

  __A	:		str	  =      self.get_config()

  return config, input_ids, input_mask, token_labels


 def 			snake_case__ (   self    ):

  """simple docstring"""







  return GPTNeoXConfig(
      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   ,   hidden_dropout_prob=self.hidden_dropout_prob   ,   attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,   max_position_embeddings=self.max_position_embeddings   ,   type_vocab_size=self.type_vocab_size   ,   is_decoder=__lowercase   ,   initializer_range=self.initializer_range   ,   pad_token_id=self.pad_token_id   ,   )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A  ,__A  ,__A  ,__A	:		Optional[Any]	  =      self.prepare_config_and_inputs()

  __A	:		List[str]	  =      True

  return config, input_ids, input_mask, token_labels


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase   ,   __lowercase    ):

  """simple docstring"""







  __A	:		int	  =      GPTNeoXModel(config=__lowercase    )
  model.to(__lowercase    )
  model.eval()
  __A	:		List[str]	  =      model(__lowercase   ,   attention_mask=__lowercase    )
  __A	:		str	  =      model(__lowercase    )
  self.parent.assertEqual(result.last_hidden_state.shape   ,   (self.batch_size, self.seq_length, self.hidden_size)    )


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase   ,   __lowercase    ):

  """simple docstring"""







  __A	:		Union[str, Any]	  =      True
  __A	:		Optional[int]	  =      GPTNeoXModel(__lowercase    )
  model.to(__lowercase    )
  model.eval()
  __A	:		Tuple	  =      model(__lowercase   ,   attention_mask=__lowercase    )
  self.parent.assertEqual(result.last_hidden_state.shape   ,   (self.batch_size, self.seq_length, self.hidden_size)    )


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase   ,   __lowercase   ,   __lowercase    ):

  """simple docstring"""







  __A	:		Tuple	  =      GPTNeoXForCausalLM(config=__lowercase    )
  model.to(__lowercase    )
  model.eval()
  __A	:		Any	  =      model(__lowercase   ,   attention_mask=__lowercase   ,   labels=__lowercase    )
  self.parent.assertEqual(result.logits.shape   ,   (self.batch_size, self.seq_length, self.vocab_size)    )


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase   ,   __lowercase   ,   __lowercase    ):

  """simple docstring"""







  __A	:		Union[str, Any]	  =      self.num_labels
  __A	:		List[Any]	  =      GPTNeoXForQuestionAnswering(__lowercase    )
  model.to(__lowercase    )
  model.eval()
  __A	:		List[str]	  =      model(__lowercase   ,   attention_mask=__lowercase    )
  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 			snake_case__ (   self   ,   __lowercase   ,   __lowercase   ,   __lowercase   ,   __lowercase    ):

  """simple docstring"""







  __A	:		Optional[Any]	  =      self.num_labels
  __A	:		List[Any]	  =      GPTNeoXForSequenceClassification(__lowercase    )
  model.to(__lowercase    )
  model.eval()
  __A	:		int	  =      ids_tensor([self.batch_size]   ,   self.type_sequence_label_size    )
  __A	:		Any	  =      model(__lowercase   ,   attention_mask=__lowercase   ,   labels=__lowercase    )
  self.parent.assertEqual(result.logits.shape   ,   (self.batch_size, self.num_labels)    )


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase   ,   __lowercase   ,   __lowercase    ):

  """simple docstring"""







  __A	:		Optional[int]	  =      self.num_labels
  __A	:		Dict	  =      GPTNeoXForTokenClassification(__lowercase    )
  model.to(__lowercase    )
  model.eval()
  __A	:		int	  =      model(__lowercase   ,   attention_mask=__lowercase   ,   labels=__lowercase    )
  self.parent.assertEqual(result.logits.shape   ,   (self.batch_size, self.seq_length, self.num_labels)    )


 def 			snake_case__ (   self   ,   __lowercase   ,   __lowercase   ,   __lowercase    ):

  """simple docstring"""







  __A	:		str	  =      True
  __A	:		List[Any]	  =      GPTNeoXForCausalLM(config=__lowercase    )
  model.to(__lowercase    )
  model.eval()

  # first forward pass
  __A	:		Optional[int]	  =      model(__lowercase   ,   attention_mask=__lowercase   ,   use_cache=__lowercase    )
  __A	:		Any	  =      outputs.past_key_values

  # create hypothetical multiple next token and extent to next_input_ids
  __A	:		List[Any]	  =      ids_tensor((self.batch_size, 3)   ,   config.vocab_size    )
  __A	:		List[Any]	  =      ids_tensor((self.batch_size, 3)   ,   vocab_size=2    )

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

  __A	:		Tuple	  =      model(__lowercase   ,   attention_mask=__lowercase   ,   output_hidden_states=__lowercase    )
  __A	:		str	  =      output_from_no_past['hidden_states'][0]
  __A	:		Optional[int]	  =      model(
      __lowercase   ,   attention_mask=__lowercase   ,   past_key_values=__lowercase   ,   output_hidden_states=__lowercase   ,   )['hidden_states'][0]

  # select random slice
  __A	:		Any	  =      ids_tensor((1,)   ,   output_from_past.shape[-1]    ).item()
  __A	:		Tuple	  =      output_from_no_past[:, -3:, random_slice_idx].detach()
  __A	:		List[str]	  =      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(__lowercase   ,   __lowercase   ,   atol=1E-3    )    )







 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Dict	  =      self.prepare_config_and_inputs()
  __A  ,__A  ,__A  ,__A	:		Union[str, Any]	  =      config_and_inputs
  __A	:		Optional[Any]	  =      {'input_ids': input_ids, 'attention_mask': input_mask}
  return config, inputs_dict


@require_torch
class     _lowerCamelCase       (     snake_case_     ,		snake_case_     ,		snake_case_     ,		unittest.TestCase					):




 '''simple docstring'''
 __lowercase					:		Optional[int]     				=    (
     (
         GPTNeoXModel,
         GPTNeoXForCausalLM,
         GPTNeoXForQuestionAnswering,
         GPTNeoXForSequenceClassification,
         GPTNeoXForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 __lowercase					:		Union[str, Any]     				=    (GPTNeoXForCausalLM,) if is_torch_available() else ()
 __lowercase					:		Dict     				=    (
     {
         '''feature-extraction''': GPTNeoXModel,
         '''question-answering''': GPTNeoXForQuestionAnswering,
         '''text-classification''': GPTNeoXForSequenceClassification,
         '''text-generation''': GPTNeoXForCausalLM,
         '''token-classification''': GPTNeoXForTokenClassification,
         '''zero-shot''': GPTNeoXForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 __lowercase					:		List[Any]     				=    False
 __lowercase					:		List[str]     				=    False
 __lowercase					:		List[str]     				=    False
 __lowercase					:		List[Any]     				=    False


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Tuple	  =      GPTNeoXModelTester(self    )
  __A	:		str	  =      ConfigTester(self   ,   config_class=__lowercase   ,   hidden_size=64   ,   num_attention_heads=8    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  self.config_tester.run_common_tests()


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A  ,__A  ,__A  ,__A	:		int	  =      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_model(__lowercase   ,   __lowercase   ,   __lowercase    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A  ,__A  ,__A  ,__A	:		Dict	  =      self.model_tester.prepare_config_and_inputs_for_decoder()
  self.model_tester.create_and_check_model_as_decoder(__lowercase   ,   __lowercase   ,   __lowercase    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A  ,__A  ,__A  ,__A	:		int	  =      self.model_tester.prepare_config_and_inputs_for_decoder()

  __A	:		Any	  =      None

  self.model_tester.create_and_check_model_as_decoder(__lowercase   ,   __lowercase   ,   __lowercase    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A  ,__A  ,__A  ,__A	:		Optional[Any]	  =      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_decoder_model_past_large_inputs(__lowercase   ,   __lowercase   ,   __lowercase    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Optional[int]	  =      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_causal_lm(*__lowercase    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Dict	  =      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_question_answering(*__lowercase    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Any	  =      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_sequence_classification(*__lowercase    )


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Optional[Any]	  =      self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_token_classification(*__lowercase    )


 @unittest.skip(reason='Feed forward chunking is not implemented'    )
 def 			snake_case__ (   self    ):

  """simple docstring"""







  pass







 @parameterized.expand([('linear',), ('dynamic',)]    )
 def 			snake_case__ (   self   ,   __lowercase    ):

  """simple docstring"""







  __A  ,__A	:		Union[str, Any]	  =      self.model_tester.prepare_config_and_inputs_for_common()
  __A	:		Any	  =      ids_tensor([1, 10]   ,   config.vocab_size    )
  __A	:		Any	  =      ids_tensor([1, int(config.max_position_embeddings * 1.5    )]   ,   config.vocab_size    )

  set_seed(42    )  # Fixed seed at init time so the two models get the same random weights
  __A	:		str	  =      GPTNeoXModel(__lowercase    )
  original_model.to(__lowercase    )
  original_model.eval()
  __A	:		Optional[Any]	  =      original_model(__lowercase    ).last_hidden_state
  __A	:		Dict	  =      original_model(__lowercase    ).last_hidden_state

  set_seed(42    )  # Fixed seed at init time so the two models get the same random weights
  __A	:		Optional[Any]	  =      {'type': scaling_type, 'factor': 1_0.0}
  __A	:		Optional[int]	  =      GPTNeoXModel(__lowercase    )
  scaled_model.to(__lowercase    )
  scaled_model.eval()
  __A	:		Any	  =      scaled_model(__lowercase    ).last_hidden_state
  __A	:		Optional[int]	  =      scaled_model(__lowercase    ).last_hidden_state

  # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original
  # maximum sequence length, so the outputs for the short input should match.
  if scaling_type == "dynamic":
   self.assertTrue(torch.allclose(__lowercase   ,   __lowercase   ,   atol=1E-5    )    )
  else:
   self.assertFalse(torch.allclose(__lowercase   ,   __lowercase   ,   atol=1E-5    )    )

  # The output should be different for long inputs
  self.assertFalse(torch.allclose(__lowercase   ,   __lowercase   ,   atol=1E-5    )    )


@require_torch
class     _lowerCamelCase       (     unittest.TestCase					):




 '''simple docstring'''


 @slow
 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		str	  =      AutoTokenizer.from_pretrained('EleutherAI/pythia-410m-deduped'    )
  for checkpointing in [True, False]:
   __A	:		Any	  =      GPTNeoXForCausalLM.from_pretrained('EleutherAI/pythia-410m-deduped'    )

   if checkpointing:
    model.gradient_checkpointing_enable()
   else:
    model.gradient_checkpointing_disable()
   model.to(__lowercase    )

   __A	:		Any	  =      tokenizer('My favorite food is'   ,   return_tensors='pt'    ).to(__lowercase    )
   # The hub repo. is updated on 2023-04-04, resulting in poor outputs.
   # See: https://github.com/huggingface/transformers/pull/24193
   __A	:		Dict	  =      'My favorite food is a good old-fashioned, old-fashioned, old-fashioned.\n\nI\'m not sure'

   __A	:		List[str]	  =      model.generate(**__lowercase   ,   do_sample=__lowercase   ,   max_new_tokens=20    )
   __A	:		str	  =      tokenizer.batch_decode(__lowercase    )[0]

   self.assertEqual(__lowercase   ,   __lowercase    )










'''simple docstring'''
def       _lowercase							(					UpperCamelCase__							:       int,					UpperCamelCase__							:       int      ):
 return 1 if input_a == input_a else 0


def       _lowercase							(					):
 assert xnor_gate(0,					0      ) == 1
 assert xnor_gate(0,					1      ) == 0
 assert xnor_gate(1,					0      ) == 0
 assert xnor_gate(1,					1      ) == 1


if __name__ == "__main__":
 print(xnor_gate(0, 0))
 print(xnor_gate(0, 1))
 print(xnor_gate(1, 0))
 print(xnor_gate(1, 1))






