





'''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 					__snake_case  :






   '''simple docstring'''





   def __init__(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE=1_3	,		__SCREAMING_SNAKE_CASE=7	,		__SCREAMING_SNAKE_CASE=True	,		__SCREAMING_SNAKE_CASE=True	,		__SCREAMING_SNAKE_CASE=True	,		__SCREAMING_SNAKE_CASE=True	,		__SCREAMING_SNAKE_CASE=9_9	,		__SCREAMING_SNAKE_CASE=6_4	,		__SCREAMING_SNAKE_CASE=5	,		__SCREAMING_SNAKE_CASE=4	,		__SCREAMING_SNAKE_CASE=3_7	,		__SCREAMING_SNAKE_CASE="gelu"	,		__SCREAMING_SNAKE_CASE=0.1	,		__SCREAMING_SNAKE_CASE=0.1	,		__SCREAMING_SNAKE_CASE=5_1_2	,		__SCREAMING_SNAKE_CASE=1_6	,		__SCREAMING_SNAKE_CASE=2	,		__SCREAMING_SNAKE_CASE=0.02	,		__SCREAMING_SNAKE_CASE=3	,		__SCREAMING_SNAKE_CASE=4	,		__SCREAMING_SNAKE_CASE=None	,		):
    snake_case__     :					List[str]    =  parent
    snake_case__     :					Tuple    =  batch_size
    snake_case__     :					str    =  seq_length
    snake_case__     :					Optional[Any]    =  is_training
    snake_case__     :					str    =  use_input_mask
    snake_case__     :					List[str]    =  use_token_type_ids
    snake_case__     :					Optional[Any]    =  use_labels
    snake_case__     :					Any    =  vocab_size
    snake_case__     :					List[Any]    =  hidden_size
    snake_case__     :					List[Any]    =  num_hidden_layers
    snake_case__     :					Optional[int]    =  num_attention_heads
    snake_case__     :					Tuple    =  intermediate_size
    snake_case__     :					Dict    =  hidden_act
    snake_case__     :					Optional[Any]    =  hidden_dropout_prob
    snake_case__     :					int    =  attention_probs_dropout_prob
    snake_case__     :					Tuple    =  max_position_embeddings
    snake_case__     :					Union[str, Any]    =  type_vocab_size
    snake_case__     :					List[str]    =  type_sequence_label_size
    snake_case__     :					Any    =  initializer_range
    snake_case__     :					str    =  num_labels
    snake_case__     :					Optional[int]    =  num_choices
    snake_case__     :					int    =  scope
    snake_case__     :					Tuple    =  vocab_size - 1

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					Any    =  ids_tensor([self.batch_size, self.seq_length]	,		self.vocab_size       )

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

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

    snake_case__     :					List[str]    =  self.get_config()

    return config, input_ids, input_mask, token_labels

   def 		__UpperCamelCase						(							self       ):
    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=__SCREAMING_SNAKE_CASE	,		initializer_range=self.initializer_range	,		pad_token_id=self.pad_token_id	,		)

   def 		__UpperCamelCase						(							self       ):
    snake_case__  ,							snake_case__  ,							snake_case__  ,							snake_case__     :					Any    =  self.prepare_config_and_inputs()

    snake_case__     :					Dict    =  True

    return config, input_ids, input_mask, token_labels

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
    snake_case__     :					List[str]    =  GPTNeoXModel(config=__SCREAMING_SNAKE_CASE       )
    model.to(__SCREAMING_SNAKE_CASE       )
    model.eval()
    snake_case__     :					Dict    =  model(__SCREAMING_SNAKE_CASE	,		attention_mask=__SCREAMING_SNAKE_CASE       )
    snake_case__     :					str    =  model(__SCREAMING_SNAKE_CASE       )
    self.parent.assertEqual(result.last_hidden_state.shape	,		(self.batch_size, self.seq_length, self.hidden_size)       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
    snake_case__     :					List[Any]    =  True
    snake_case__     :					Tuple    =  GPTNeoXModel(__SCREAMING_SNAKE_CASE       )
    model.to(__SCREAMING_SNAKE_CASE       )
    model.eval()
    snake_case__     :					List[Any]    =  model(__SCREAMING_SNAKE_CASE	,		attention_mask=__SCREAMING_SNAKE_CASE       )
    self.parent.assertEqual(result.last_hidden_state.shape	,		(self.batch_size, self.seq_length, self.hidden_size)       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
    snake_case__     :					Optional[Any]    =  GPTNeoXForCausalLM(config=__SCREAMING_SNAKE_CASE       )
    model.to(__SCREAMING_SNAKE_CASE       )
    model.eval()
    snake_case__     :					List[str]    =  model(__SCREAMING_SNAKE_CASE	,		attention_mask=__SCREAMING_SNAKE_CASE	,		labels=__SCREAMING_SNAKE_CASE       )
    self.parent.assertEqual(result.logits.shape	,		(self.batch_size, self.seq_length, self.vocab_size)       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
    snake_case__     :					str    =  self.num_labels
    snake_case__     :					Any    =  GPTNeoXForQuestionAnswering(__SCREAMING_SNAKE_CASE       )
    model.to(__SCREAMING_SNAKE_CASE       )
    model.eval()
    snake_case__     :					Dict    =  model(__SCREAMING_SNAKE_CASE	,		attention_mask=__SCREAMING_SNAKE_CASE       )
    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	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
    snake_case__     :					int    =  self.num_labels
    snake_case__     :					str    =  GPTNeoXForSequenceClassification(__SCREAMING_SNAKE_CASE       )
    model.to(__SCREAMING_SNAKE_CASE       )
    model.eval()
    snake_case__     :					Any    =  ids_tensor([self.batch_size]	,		self.type_sequence_label_size       )
    snake_case__     :					str    =  model(__SCREAMING_SNAKE_CASE	,		attention_mask=__SCREAMING_SNAKE_CASE	,		labels=__SCREAMING_SNAKE_CASE       )
    self.parent.assertEqual(result.logits.shape	,		(self.batch_size, self.num_labels)       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
    snake_case__     :					Optional[int]    =  self.num_labels
    snake_case__     :					Union[str, Any]    =  GPTNeoXForTokenClassification(__SCREAMING_SNAKE_CASE       )
    model.to(__SCREAMING_SNAKE_CASE       )
    model.eval()
    snake_case__     :					List[str]    =  model(__SCREAMING_SNAKE_CASE	,		attention_mask=__SCREAMING_SNAKE_CASE	,		labels=__SCREAMING_SNAKE_CASE       )
    self.parent.assertEqual(result.logits.shape	,		(self.batch_size, self.seq_length, self.num_labels)       )

   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       ):
    snake_case__     :					str    =  True
    snake_case__     :					List[str]    =  GPTNeoXForCausalLM(config=__SCREAMING_SNAKE_CASE       )
    model.to(__SCREAMING_SNAKE_CASE       )
    model.eval()

    # first forward pass
    snake_case__     :					Dict    =  model(__SCREAMING_SNAKE_CASE	,		attention_mask=__SCREAMING_SNAKE_CASE	,		use_cache=__SCREAMING_SNAKE_CASE       )
    snake_case__     :					Tuple    =  outputs.past_key_values

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

    # append to next input_ids and
    snake_case__     :					Dict    =  torch.cat([input_ids, next_tokens]	,		dim=-1       )
    snake_case__     :					List[str]    =  torch.cat([input_mask, next_mask]	,		dim=-1       )

    snake_case__     :					Optional[Any]    =  model(__SCREAMING_SNAKE_CASE	,		attention_mask=__SCREAMING_SNAKE_CASE	,		output_hidden_states=__SCREAMING_SNAKE_CASE       )
    snake_case__     :					Any    =  output_from_no_past["""hidden_states"""][0]
    snake_case__     :					Union[str, Any]    =  model(
        __SCREAMING_SNAKE_CASE	,		attention_mask=__SCREAMING_SNAKE_CASE	,		past_key_values=__SCREAMING_SNAKE_CASE	,		output_hidden_states=__SCREAMING_SNAKE_CASE	,		)["""hidden_states"""][0]

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


   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					str    =  self.prepare_config_and_inputs()
    snake_case__  ,							snake_case__  ,							snake_case__  ,							snake_case__     :					Any    =  config_and_inputs
    snake_case__     :					Any    =  {"""input_ids""": input_ids, """attention_mask""": input_mask}
    return config, inputs_dict



@require_torch
class 					__snake_case  (					__SCREAMING_SNAKE_CASE		,				__SCREAMING_SNAKE_CASE		,				__SCREAMING_SNAKE_CASE		,				unittest.TestCase							):






   '''simple docstring'''





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

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					Dict    =  GPTNeoXModelTester(self       )
    snake_case__     :					Optional[int]    =  ConfigTester(self	,		config_class=__SCREAMING_SNAKE_CASE	,		hidden_size=6_4	,		num_attention_heads=8       )

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

   def 		__UpperCamelCase						(							self       ):
    snake_case__  ,							snake_case__  ,							snake_case__  ,							snake_case__     :					Any    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       )

   def 		__UpperCamelCase						(							self       ):
    snake_case__  ,							snake_case__  ,							snake_case__  ,							snake_case__     :					List[str]    =  self.model_tester.prepare_config_and_inputs_for_decoder()
    self.model_tester.create_and_check_model_as_decoder(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       )

   def 		__UpperCamelCase						(							self       ):
    # This regression test was failing with PyTorch < 1.3
    snake_case__  ,							snake_case__  ,							snake_case__  ,							snake_case__     :					Any    =  self.model_tester.prepare_config_and_inputs_for_decoder()

    snake_case__     :					Any    =  None

    self.model_tester.create_and_check_model_as_decoder(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       )

   def 		__UpperCamelCase						(							self       ):
    snake_case__  ,							snake_case__  ,							snake_case__  ,							snake_case__     :					List[str]    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_decoder_model_past_large_inputs(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       )

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					List[Any]    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_causal_lm(*__SCREAMING_SNAKE_CASE       )

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					Optional[Any]    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE       )

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					Dict    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_sequence_classification(*__SCREAMING_SNAKE_CASE       )

   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					str    =  self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE       )

   @unittest.skip(reason="""Feed forward chunking is not implemented"""       )
   def 		__UpperCamelCase						(							self       ):
    pass


   @parameterized.expand([("""linear""",), ("""dynamic""",)]       )
   def 		__UpperCamelCase						(							self	,		__SCREAMING_SNAKE_CASE       ):
    snake_case__  ,							snake_case__     :					Union[str, Any]    =  self.model_tester.prepare_config_and_inputs_for_common()
    snake_case__     :					Union[str, Any]    =  ids_tensor([1, 1_0]	,		config.vocab_size       )
    snake_case__     :					Union[str, Any]    =  ids_tensor([1, int(config.max_position_embeddings * 1.5       )]	,		config.vocab_size       )

    set_seed(4_2       )  # Fixed seed at init time so the two models get the same random weights
    snake_case__     :					Dict    =  GPTNeoXModel(__SCREAMING_SNAKE_CASE       )
    original_model.to(__SCREAMING_SNAKE_CASE       )
    original_model.eval()
    snake_case__     :					int    =  original_model(__SCREAMING_SNAKE_CASE       ).last_hidden_state
    snake_case__     :					str    =  original_model(__SCREAMING_SNAKE_CASE       ).last_hidden_state

    set_seed(4_2       )  # Fixed seed at init time so the two models get the same random weights
    snake_case__     :					Dict    =  {"""type""": scaling_type, """factor""": 10.0}
    snake_case__     :					Optional[Any]    =  GPTNeoXModel(__SCREAMING_SNAKE_CASE       )
    scaled_model.to(__SCREAMING_SNAKE_CASE       )
    scaled_model.eval()
    snake_case__     :					Any    =  scaled_model(__SCREAMING_SNAKE_CASE       ).last_hidden_state
    snake_case__     :					Optional[int]    =  scaled_model(__SCREAMING_SNAKE_CASE       ).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(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		atol=1e-5       )       )
    else:
     self.assertFalse(torch.allclose(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE	,		atol=1e-5       )       )

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



@require_torch
class 					__snake_case  (					unittest.TestCase							):






   '''simple docstring'''





   @slow
   def 		__UpperCamelCase						(							self       ):
    snake_case__     :					Any    =  AutoTokenizer.from_pretrained("""EleutherAI/pythia-410m-deduped"""       )
    for checkpointing in [True, False]:
     snake_case__     :					Tuple    =  GPTNeoXForCausalLM.from_pretrained("""EleutherAI/pythia-410m-deduped"""       )

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

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

     snake_case__     :					Union[str, Any]    =  model.generate(**__SCREAMING_SNAKE_CASE	,		do_sample=__SCREAMING_SNAKE_CASE	,		max_new_tokens=2_0       )
     snake_case__     :					Optional[int]    =  tokenizer.batch_decode(__SCREAMING_SNAKE_CASE       )[0]

     self.assertEqual(__SCREAMING_SNAKE_CASE	,		__SCREAMING_SNAKE_CASE       )








from unittest.mock import Mock, patch

from file_transfer.send_file import send_file
@patch("socket.socket"	)
@patch("builtins.open"	)
def 							UpperCamelCase_(					_A :Tuple					,  _A :str	)->		int:
			# ===== initialization =====
			UpperCamelCase__       =							Mock()
			UpperCamelCase__       =							conn, Mock()
			UpperCamelCase__       =							iter([1, None]	)
			UpperCamelCase__       =							lambda _A	: next(_A	)

			# ===== invoke =====
			send_file(filename="mytext.txt"					,  testing=_A	)

			# ===== ensurance =====
			sock.assert_called_once()
			sock.return_value.bind.assert_called_once()
			sock.return_value.listen.assert_called_once()
			sock.return_value.accept.assert_called_once()
			conn.recv.assert_called_once()

			file.return_value.__enter__.assert_called_once()
			file.return_value.__enter__.return_value.read.assert_called()

			conn.send.assert_called_once()
			conn.close.assert_called_once()
			sock.return_value.shutdown.assert_called_once()
			sock.return_value.close.assert_called_once()


