






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


    '''simple docstring'''

    def __init__(					self     :     Union[str, Any]				,     __lowerCamelCase     :     List[str]				,     __lowerCamelCase     :     Tuple=13				,     __lowerCamelCase     :     Tuple=7				,     __lowerCamelCase     :     Any=True				,     __lowerCamelCase     :     List[Any]=True				,     __lowerCamelCase     :     Union[str, Any]=True				,     __lowerCamelCase     :     Any=True				,     __lowerCamelCase     :     List[str]=99				,     __lowerCamelCase     :     Any=64				,     __lowerCamelCase     :     Tuple=5				,     __lowerCamelCase     :     Tuple=4				,     __lowerCamelCase     :     Optional[int]=37				,     __lowerCamelCase     :     Union[str, Any]="gelu"				,     __lowerCamelCase     :     str=0.1				,     __lowerCamelCase     :     int=0.1				,     __lowerCamelCase     :     Union[str, Any]=512				,     __lowerCamelCase     :     Optional[int]=16				,     __lowerCamelCase     :     Optional[int]=2				,     __lowerCamelCase     :     Tuple=0.0_2				,     __lowerCamelCase     :     int=3				,     __lowerCamelCase     :     int=4				,     __lowerCamelCase     :     Optional[Any]=None				,     )							->							str:

     '''simple docstring'''




     lowerCamelCase__  =     parent
     lowerCamelCase__  =     batch_size
     lowerCamelCase__  =     seq_length
     lowerCamelCase__  =     is_training
     lowerCamelCase__  =     use_input_mask
     lowerCamelCase__  =     use_token_type_ids
     lowerCamelCase__  =     use_labels
     lowerCamelCase__  =     vocab_size
     lowerCamelCase__  =     hidden_size
     lowerCamelCase__  =     num_hidden_layers
     lowerCamelCase__  =     num_attention_heads
     lowerCamelCase__  =     intermediate_size
     lowerCamelCase__  =     hidden_act
     lowerCamelCase__  =     hidden_dropout_prob
     lowerCamelCase__  =     attention_probs_dropout_prob
     lowerCamelCase__  =     max_position_embeddings
     lowerCamelCase__  =     type_vocab_size
     lowerCamelCase__  =     type_sequence_label_size
     lowerCamelCase__  =     initializer_range
     lowerCamelCase__  =     num_labels
     lowerCamelCase__  =     num_choices
     lowerCamelCase__  =     scope
     lowerCamelCase__  =     vocab_size - 1

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

     '''simple docstring'''




     lowerCamelCase__  =     ids_tensor([self.batch_size, self.seq_length]				,     self.vocab_size     )

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

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

     lowerCamelCase__  =     self.get_config()

     return config, input_ids, input_mask, token_labels

    def    a__				(					self     :     Any     )							->							Dict:

     '''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=__lowerCamelCase				,     initializer_range=self.initializer_range				,     pad_token_id=self.pad_token_id				,     )

    def    a__				(					self     :     int     )							->							Tuple:

     '''simple docstring'''




     lowerCamelCase__  ,	lowerCamelCase__  ,	lowerCamelCase__  ,	lowerCamelCase__  =     self.prepare_config_and_inputs()

     lowerCamelCase__  =     True

     return config, input_ids, input_mask, token_labels

    def    a__				(					self     :     int				,     __lowerCamelCase     :     List[str]				,     __lowerCamelCase     :     Union[str, Any]				,     __lowerCamelCase     :     str     )							->							List[str]:

     '''simple docstring'''




     lowerCamelCase__  =     GPTNeoXModel(config=__lowerCamelCase     )
     model.to(__lowerCamelCase     )
     model.eval()
     lowerCamelCase__  =     model(__lowerCamelCase				,     attention_mask=__lowerCamelCase     )
     lowerCamelCase__  =     model(__lowerCamelCase     )
     self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, self.seq_length, self.hidden_size)     )

    def    a__				(					self     :     Union[str, Any]				,     __lowerCamelCase     :     Dict				,     __lowerCamelCase     :     Optional[int]				,     __lowerCamelCase     :     int     )							->							int:

     '''simple docstring'''




     lowerCamelCase__  =     True
     lowerCamelCase__  =     GPTNeoXModel(__lowerCamelCase     )
     model.to(__lowerCamelCase     )
     model.eval()
     lowerCamelCase__  =     model(__lowerCamelCase				,     attention_mask=__lowerCamelCase     )
     self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, self.seq_length, self.hidden_size)     )

    def    a__				(					self     :     int				,     __lowerCamelCase     :     Union[str, Any]				,     __lowerCamelCase     :     List[Any]				,     __lowerCamelCase     :     Optional[Any]				,     __lowerCamelCase     :     int     )							->							List[Any]:

     '''simple docstring'''




     lowerCamelCase__  =     GPTNeoXForCausalLM(config=__lowerCamelCase     )
     model.to(__lowerCamelCase     )
     model.eval()
     lowerCamelCase__  =     model(__lowerCamelCase				,     attention_mask=__lowerCamelCase				,     labels=__lowerCamelCase     )
     self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.vocab_size)     )

    def    a__				(					self     :     Optional[int]				,     __lowerCamelCase     :     int				,     __lowerCamelCase     :     Union[str, Any]				,     __lowerCamelCase     :     Tuple				,     __lowerCamelCase     :     List[Any]     )							->							int:

     '''simple docstring'''




     lowerCamelCase__  =     self.num_labels
     lowerCamelCase__  =     GPTNeoXForQuestionAnswering(__lowerCamelCase     )
     model.to(__lowerCamelCase     )
     model.eval()
     lowerCamelCase__  =     model(__lowerCamelCase				,     attention_mask=__lowerCamelCase     )
     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    a__				(					self     :     List[str]				,     __lowerCamelCase     :     str				,     __lowerCamelCase     :     str				,     __lowerCamelCase     :     List[str]				,     __lowerCamelCase     :     List[Any]     )							->							Optional[int]:

     '''simple docstring'''




     lowerCamelCase__  =     self.num_labels
     lowerCamelCase__  =     GPTNeoXForSequenceClassification(__lowerCamelCase     )
     model.to(__lowerCamelCase     )
     model.eval()
     lowerCamelCase__  =     ids_tensor([self.batch_size]				,     self.type_sequence_label_size     )
     lowerCamelCase__  =     model(__lowerCamelCase				,     attention_mask=__lowerCamelCase				,     labels=__lowerCamelCase     )
     self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.num_labels)     )

    def    a__				(					self     :     str				,     __lowerCamelCase     :     List[Any]				,     __lowerCamelCase     :     str				,     __lowerCamelCase     :     int				,     __lowerCamelCase     :     Optional[int]     )							->							Tuple:

     '''simple docstring'''




     lowerCamelCase__  =     self.num_labels
     lowerCamelCase__  =     GPTNeoXForTokenClassification(__lowerCamelCase     )
     model.to(__lowerCamelCase     )
     model.eval()
     lowerCamelCase__  =     model(__lowerCamelCase				,     attention_mask=__lowerCamelCase				,     labels=__lowerCamelCase     )
     self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.num_labels)     )

    def    a__				(					self     :     Tuple				,     __lowerCamelCase     :     Union[str, Any]				,     __lowerCamelCase     :     Dict				,     __lowerCamelCase     :     List[str]     )							->							Tuple:

     '''simple docstring'''




     lowerCamelCase__  =     True
     lowerCamelCase__  =     GPTNeoXForCausalLM(config=__lowerCamelCase     )
     model.to(__lowerCamelCase     )
     model.eval()

     # first forward pass
     lowerCamelCase__  =     model(__lowerCamelCase				,     attention_mask=__lowerCamelCase				,     use_cache=__lowerCamelCase     )
     lowerCamelCase__  =     outputs.past_key_values

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

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

     lowerCamelCase__  =     model(__lowerCamelCase				,     attention_mask=__lowerCamelCase				,     output_hidden_states=__lowerCamelCase     )
     lowerCamelCase__  =     output_from_no_past["hidden_states"][0]
     lowerCamelCase__  =     model(
         __lowerCamelCase				,     attention_mask=__lowerCamelCase				,     past_key_values=__lowerCamelCase				,     output_hidden_states=__lowerCamelCase				,     )["hidden_states"][0]

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




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

     '''simple docstring'''




     lowerCamelCase__  =     self.prepare_config_and_inputs()
     lowerCamelCase__  ,	lowerCamelCase__  ,	lowerCamelCase__  ,	lowerCamelCase__  =     config_and_inputs
     lowerCamelCase__  =     {"input_ids": input_ids, "attention_mask": input_mask}
     return config, inputs_dict
@require_torch
class 				lowercase       (							_lowerCamelCase ,       _lowerCamelCase ,       _lowerCamelCase ,       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    a__				(					self     :     str     )							->							List[str]:

     '''simple docstring'''




     lowerCamelCase__  =     GPTNeoXModelTester(self     )
     lowerCamelCase__  =     ConfigTester(self				,     config_class=__lowerCamelCase				,     hidden_size=64				,     num_attention_heads=8     )

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

     '''simple docstring'''




     self.config_tester.run_common_tests()

    def    a__				(					self     :     Dict     )							->							Optional[Any]:

     '''simple docstring'''




     lowerCamelCase__  ,	lowerCamelCase__  ,	lowerCamelCase__  ,	lowerCamelCase__  =     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(__lowerCamelCase				,     __lowerCamelCase				,     __lowerCamelCase     )

    def    a__				(					self     :     Dict     )							->							List[str]:

     '''simple docstring'''




     lowerCamelCase__  ,	lowerCamelCase__  ,	lowerCamelCase__  ,	lowerCamelCase__  =     self.model_tester.prepare_config_and_inputs_for_decoder()
     self.model_tester.create_and_check_model_as_decoder(__lowerCamelCase				,     __lowerCamelCase				,     __lowerCamelCase     )

    def    a__				(					self     :     Any     )							->							int:

     '''simple docstring'''




     lowerCamelCase__  ,	lowerCamelCase__  ,	lowerCamelCase__  ,	lowerCamelCase__  =     self.model_tester.prepare_config_and_inputs_for_decoder()

     lowerCamelCase__  =     None

     self.model_tester.create_and_check_model_as_decoder(__lowerCamelCase				,     __lowerCamelCase				,     __lowerCamelCase     )

    def    a__				(					self     :     List[str]     )							->							Tuple:

     '''simple docstring'''




     lowerCamelCase__  ,	lowerCamelCase__  ,	lowerCamelCase__  ,	lowerCamelCase__  =     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_decoder_model_past_large_inputs(__lowerCamelCase				,     __lowerCamelCase				,     __lowerCamelCase     )

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

     '''simple docstring'''




     lowerCamelCase__  =     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_causal_lm(*__lowerCamelCase     )

    def    a__				(					self     :     List[Any]     )							->							str:

     '''simple docstring'''




     lowerCamelCase__  =     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_question_answering(*__lowerCamelCase     )

    def    a__				(					self     :     List[str]     )							->							Any:

     '''simple docstring'''




     lowerCamelCase__  =     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_sequence_classification(*__lowerCamelCase     )

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

     '''simple docstring'''




     lowerCamelCase__  =     self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_token_classification(*__lowerCamelCase     )

    @unittest.skip(reason="Feed forward chunking is not implemented"     )
    def    a__				(					self     :     int     )							->							Dict:

     '''simple docstring'''




     pass




    @parameterized.expand([("linear",), ("dynamic",)]     )
    def    a__				(					self     :     str				,     __lowerCamelCase     :     Optional[int]     )							->							Dict:

     '''simple docstring'''




     lowerCamelCase__  ,	lowerCamelCase__  =     self.model_tester.prepare_config_and_inputs_for_common()
     lowerCamelCase__  =     ids_tensor([1, 10]				,     config.vocab_size     )
     lowerCamelCase__  =     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
     lowerCamelCase__  =     GPTNeoXModel(__lowerCamelCase     )
     original_model.to(__lowerCamelCase     )
     original_model.eval()
     lowerCamelCase__  =     original_model(__lowerCamelCase     ).last_hidden_state
     lowerCamelCase__  =     original_model(__lowerCamelCase     ).last_hidden_state

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

     # The output should be different for long inputs
     self.assertFalse(torch.allclose(__lowerCamelCase				,     __lowerCamelCase				,     atol=1E-5     )     )
@require_torch
class 				lowercase       (							unittest.TestCase      ):


    '''simple docstring'''

    @slow
    def    a__				(					self     :     Tuple     )							->							Dict:

     '''simple docstring'''




     lowerCamelCase__  =     AutoTokenizer.from_pretrained("EleutherAI/pythia-410m-deduped"     )
     for checkpointing in [True, False]:
      lowerCamelCase__  =     GPTNeoXForCausalLM.from_pretrained("EleutherAI/pythia-410m-deduped"     )

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

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

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

      self.assertEqual(__lowerCamelCase				,     __lowerCamelCase     )









'''simple docstring'''





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


__A							:							Union[str, Any]  							=		{
    """configuration_layoutlmv2""": ["""LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP""", """LayoutLMv2Config"""],
    """processing_layoutlmv2""": ["""LayoutLMv2Processor"""],
    """tokenization_layoutlmv2""": ["""LayoutLMv2Tokenizer"""],
}

try:
 if not is_tokenizers_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __A							:							Optional[Any]  							=		["""LayoutLMv2TokenizerFast"""]

try:
 if not is_vision_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __A							:							List[Any]  							=		["""LayoutLMv2FeatureExtractor"""]
 __A							:							List[Any]  							=		["""LayoutLMv2ImageProcessor"""]

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 __A							:							Any  							=		[
     """LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST""",
     """LayoutLMv2ForQuestionAnswering""",
     """LayoutLMv2ForSequenceClassification""",
     """LayoutLMv2ForTokenClassification""",
     """LayoutLMv2Layer""",
     """LayoutLMv2Model""",
     """LayoutLMv2PreTrainedModel""",
 ]

if TYPE_CHECKING:
 from .configuration_layoutlmva import LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMvaConfig
 from .processing_layoutlmva import LayoutLMvaProcessor
 from .tokenization_layoutlmva import LayoutLMvaTokenizer

 try:
  if not is_tokenizers_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_layoutlmva_fast import LayoutLMvaTokenizerFast

 try:
  if not is_vision_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .feature_extraction_layoutlmva import LayoutLMvaFeatureExtractor, LayoutLMvaImageProcessor

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_layoutlmva import (
      LAYOUTLMV2_PRETRAINED_MODEL_ARCHIVE_LIST,
      LayoutLMvaForQuestionAnswering,
      LayoutLMvaForSequenceClassification,
      LayoutLMvaForTokenClassification,
      LayoutLMvaLayer,
      LayoutLMvaModel,
      LayoutLMvaPreTrainedModel,
  )
else:
 import sys

 __A							:							Tuple  							=		_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

