






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     _a							:






  """simple docstring"""





  def __init__(  self		,		_UpperCAmelCase		,		_UpperCAmelCase=13		,		_UpperCAmelCase=7		,		_UpperCAmelCase=True		,		_UpperCAmelCase=True		,		_UpperCAmelCase=True		,		_UpperCAmelCase=True		,		_UpperCAmelCase=99		,		_UpperCAmelCase=64		,		_UpperCAmelCase=5		,		_UpperCAmelCase=4		,		_UpperCAmelCase=37		,		_UpperCAmelCase="gelu"		,		_UpperCAmelCase=0.1		,		_UpperCAmelCase=0.1		,		_UpperCAmelCase=512		,		_UpperCAmelCase=16		,		_UpperCAmelCase=2		,		_UpperCAmelCase=0.0_2		,		_UpperCAmelCase=3		,		_UpperCAmelCase=4		,		_UpperCAmelCase=None		,		)      ->			List[str]:
     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_size
     UpperCamelCase_						=  hidden_act
     UpperCamelCase_						=  hidden_dropout_prob
     UpperCamelCase_						=  attention_probs_dropout_prob
     UpperCamelCase_						=  max_position_embeddings
     UpperCamelCase_						=  type_vocab_size
     UpperCamelCase_						=  type_sequence_label_size
     UpperCamelCase_						=  initializer_range
     UpperCamelCase_						=  num_labels
     UpperCamelCase_						=  num_choices
     UpperCamelCase_						=  scope
     UpperCamelCase_						=  vocab_size - 1




  def  _UpperCAmelCase  (  self			)      ->			Dict:
     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  _UpperCAmelCase  (  self			)      ->			List[Any]:
     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=_UpperCAmelCase		,		initializer_range=self.initializer_range		,		pad_token_id=self.pad_token_id		,		)




  def  _UpperCAmelCase  (  self			)      ->			Dict:
     UpperCamelCase_		,   UpperCamelCase_		,   UpperCamelCase_		,   UpperCamelCase_						=  self.prepare_config_and_inputs()

     UpperCamelCase_						=  True

     return config, input_ids, input_mask, token_labels




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase			)      ->			List[str]:
     UpperCamelCase_						=  GPTNeoXModel(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  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase			)      ->			int:
     UpperCamelCase_						=  True
     UpperCamelCase_						=  GPTNeoXModel(_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  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase			)      ->			Optional[Any]:
     UpperCamelCase_						=  GPTNeoXForCausalLM(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  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase			)      ->			Tuple:
     UpperCamelCase_						=  self.num_labels
     UpperCamelCase_						=  GPTNeoXForQuestionAnswering(_UpperCAmelCase			)
     model.to(_UpperCAmelCase			)
     model.eval()
     UpperCamelCase_						=  model(_UpperCAmelCase		,		attention_mask=_UpperCAmelCase			)
     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  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase			)      ->			Any:
     UpperCamelCase_						=  self.num_labels
     UpperCamelCase_						=  GPTNeoXForSequenceClassification(_UpperCAmelCase			)
     model.to(_UpperCAmelCase			)
     model.eval()
     UpperCamelCase_						=  ids_tensor([self.batch_size]		,		self.type_sequence_label_size			)
     UpperCamelCase_						=  model(_UpperCAmelCase		,		attention_mask=_UpperCAmelCase		,		labels=_UpperCAmelCase			)
     self.parent.assertEqual(result.logits.shape		,		(self.batch_size, self.num_labels)			)




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase			)      ->			List[Any]:
     UpperCamelCase_						=  self.num_labels
     UpperCamelCase_						=  GPTNeoXForTokenClassification(_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.num_labels)			)




  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase			)      ->			Optional[Any]:
     UpperCamelCase_						=  True
     UpperCamelCase_						=  GPTNeoXForCausalLM(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  _UpperCAmelCase  (  self			)      ->			Optional[Any]:
     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							(	UpperCAmelCase__    ,							UpperCAmelCase__    ,							UpperCAmelCase__    ,							unittest.TestCase     ):






  """simple docstring"""





  A_						       = (
      (
          GPTNeoXModel,
          GPTNeoXForCausalLM,
          GPTNeoXForQuestionAnswering,
          GPTNeoXForSequenceClassification,
          GPTNeoXForTokenClassification,
      )
      if is_torch_available()
      else ()
  )
  A_						       = (GPTNeoXForCausalLM,) if is_torch_available() else ()
  A_						       = (
      {
          """feature-extraction""": GPTNeoXModel,
          """question-answering""": GPTNeoXForQuestionAnswering,
          """text-classification""": GPTNeoXForSequenceClassification,
          """text-generation""": GPTNeoXForCausalLM,
          """token-classification""": GPTNeoXForTokenClassification,
          """zero-shot""": GPTNeoXForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  A_						       = False
  A_						       = False
  A_						       = False
  A_						       = False




  def  _UpperCAmelCase  (  self			)      ->			List[Any]:
     UpperCamelCase_						=  GPTNeoXModelTester(self			)
     UpperCamelCase_						=  ConfigTester(self		,		config_class=_UpperCAmelCase		,		hidden_size=64		,		num_attention_heads=8			)




  def  _UpperCAmelCase  (  self			)      ->			Any:
     self.config_tester.run_common_tests()




  def  _UpperCAmelCase  (  self			)      ->			str:
     UpperCamelCase_		,   UpperCamelCase_		,   UpperCamelCase_		,   UpperCamelCase_						=  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(_UpperCAmelCase		,		_UpperCAmelCase		,		_UpperCAmelCase			)




  def  _UpperCAmelCase  (  self			)      ->			Union[str, Any]:
     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  _UpperCAmelCase  (  self			)      ->			Dict:
     # 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  _UpperCAmelCase  (  self			)      ->			Optional[Any]:
     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  _UpperCAmelCase  (  self			)      ->			List[str]:
     UpperCamelCase_						=  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_causal_lm(*_UpperCAmelCase			)




  def  _UpperCAmelCase  (  self			)      ->			List[Any]:
     UpperCamelCase_						=  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase			)




  def  _UpperCAmelCase  (  self			)      ->			int:
     UpperCamelCase_						=  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase			)




  def  _UpperCAmelCase  (  self			)      ->			Any:
     UpperCamelCase_						=  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*_UpperCAmelCase			)




  @unittest.skip(reason='Feed forward chunking is not implemented'			)
  def  _UpperCAmelCase  (  self			)      ->			str:
     pass




  @parameterized.expand([('linear',), ('dynamic',)]			)
  def  _UpperCAmelCase  (  self		,		_UpperCAmelCase			)      ->			Tuple:
     UpperCamelCase_		,   UpperCamelCase_						=  self.model_tester.prepare_config_and_inputs_for_common()
     UpperCamelCase_						=  ids_tensor([1, 10]		,		config.vocab_size			)
     UpperCamelCase_						=  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
     UpperCamelCase_						=  GPTNeoXModel(_UpperCAmelCase			)
     original_model.to(_UpperCAmelCase			)
     original_model.eval()
     UpperCamelCase_						=  original_model(_UpperCAmelCase			).last_hidden_state
     UpperCamelCase_						=  original_model(_UpperCAmelCase			).last_hidden_state

     set_seed(42			)  # Fixed seed at init time so the two models get the same random weights
     UpperCamelCase_						=  {'type': scaling_type, 'factor': 1_0.0}
     UpperCamelCase_						=  GPTNeoXModel(_UpperCAmelCase			)
     scaled_model.to(_UpperCAmelCase			)
     scaled_model.eval()
     UpperCamelCase_						=  scaled_model(_UpperCAmelCase			).last_hidden_state
     UpperCamelCase_						=  scaled_model(_UpperCAmelCase			).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(_UpperCAmelCase		,		_UpperCAmelCase		,		atol=1e-5			)			)
     else:
        self.assertFalse(torch.allclose(_UpperCAmelCase		,		_UpperCAmelCase		,		atol=1e-5			)			)

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




@require_torch
class     _a							(	unittest.TestCase     ):






  """simple docstring"""





  @slow
  def  _UpperCAmelCase  (  self			)      ->			Union[str, Any]:
     UpperCamelCase_						=  AutoTokenizer.from_pretrained('EleutherAI/pythia-410m-deduped'			)
     for checkpointing in [True, False]:
        UpperCamelCase_						=  GPTNeoXForCausalLM.from_pretrained('EleutherAI/pythia-410m-deduped'			)

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

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

        UpperCamelCase_						=  model.generate(**_UpperCAmelCase		,		do_sample=_UpperCAmelCase		,		max_new_tokens=20			)
        UpperCamelCase_						=  tokenizer.batch_decode(_UpperCAmelCase			)[0]

        self.assertEqual(_UpperCAmelCase		,		_UpperCAmelCase			)













import requests






def      _snake_case     (__lowercase     ,   __lowercase):
   UpperCamelCase_						=  {'Content-Type': 'application/json'}
   UpperCamelCase_						=  requests.post(__lowercase     ,   json={'text': message_body}     ,   headers=__lowercase)
   if response.status_code != 200:
      UpperCamelCase_						=  (
          'Request to slack returned an error '
          f"""{response.status_code}, the response is:\n{response.text}"""
      )
      raise ValueError(__lowercase)


if __name__ == "__main__":
 # Set the slack url to the one provided by Slack when you create the webhook at
 # https://my.slack.com/services/new/incoming-webhook/
 send_slack_message("""<YOUR MESSAGE BODY>""", """<SLACK CHANNEL URL>""")





