

import copy
import json
import os
import tempfile

from transformers import is_torch_available

from .test_configuration_utils import config_common_kwargs





class 					__lowerCAmelCase	(   SCREAMING_SNAKE_CASE				):




  def __init__(							self				,  lowerCAmelCase				,  lowerCAmelCase=None				,  lowerCAmelCase=True				,  lowerCAmelCase=None				,  **lowerCAmelCase   )    ->					int:
        '''simple docstring'''






        _lowercase      			=parent
        _lowercase      			=config_class
        _lowercase      			=has_text_modality
        _lowercase      			=kwargs
        _lowercase      			=common_properties




  def     A__	(							self   )    ->					List[str]:
        '''simple docstring'''






        _lowercase      			=self.config_class(**self.inputs_dict   )
        _lowercase      			=(
            ['hidden_size', 'num_attention_heads', 'num_hidden_layers']
            if self.common_properties is None
            else self.common_properties
        )

        # Add common fields for text models
        if self.has_text_modality:
              common_properties.extend(['vocab_size']   )

        # Test that config has the common properties as getters
        for prop in common_properties:
              self.parent.assertTrue(hasattr(lowerCAmelCase				,  lowerCAmelCase   )				,  msg=F'''`{prop}` does not exist'''   )

        # Test that config has the common properties as setter
        for idx, name in enumerate(lowerCAmelCase   ):
              try:
                    setattr(lowerCAmelCase				,  lowerCAmelCase				,  lowerCAmelCase   )
                    self.parent.assertEqual(
                        getattr(lowerCAmelCase				,  lowerCAmelCase   )				,  lowerCAmelCase				,  msg=F'''`{name} value {idx} expected, but was {getattr(lowerCAmelCase				,  lowerCAmelCase   )}'''   )
              except NotImplementedError:
                    # Some models might not be able to implement setters for common_properties
                    # In that case, a NotImplementedError is raised
                    pass

        # Test if config class can be called with Config(prop_name=..)
        for idx, name in enumerate(lowerCAmelCase   ):
              try:
                    _lowercase      			=self.config_class(**{name: idx}   )
                    self.parent.assertEqual(
                        getattr(lowerCAmelCase				,  lowerCAmelCase   )				,  lowerCAmelCase				,  msg=F'''`{name} value {idx} expected, but was {getattr(lowerCAmelCase				,  lowerCAmelCase   )}'''   )
              except NotImplementedError:
                    # Some models might not be able to implement setters for common_properties
                    # In that case, a NotImplementedError is raised
                    pass




  def     A__	(							self   )    ->					List[str]:
        '''simple docstring'''






        _lowercase      			=self.config_class(**self.inputs_dict   )
        _lowercase      			=json.loads(config.to_json_string()   )
        for key, value in self.inputs_dict.items():
              self.parent.assertEqual(obj[key]				,  lowerCAmelCase   )




  def     A__	(							self   )    ->					str:
        '''simple docstring'''






        _lowercase      			=self.config_class(**self.inputs_dict   )

        with tempfile.TemporaryDirectory() as tmpdirname:
              _lowercase      			=os.path.join(lowerCAmelCase				,  'config.json'   )
              config_first.to_json_file(lowerCAmelCase   )
              _lowercase      			=self.config_class.from_json_file(lowerCAmelCase   )

        self.parent.assertEqual(config_second.to_dict()				,  config_first.to_dict()   )




  def     A__	(							self   )    ->					List[Any]:
        '''simple docstring'''






        _lowercase      			=self.config_class(**self.inputs_dict   )

        with tempfile.TemporaryDirectory() as tmpdirname:
              config_first.save_pretrained(lowerCAmelCase   )
              _lowercase      			=self.config_class.from_pretrained(lowerCAmelCase   )

        self.parent.assertEqual(config_second.to_dict()				,  config_first.to_dict()   )




  def     A__	(							self   )    ->					Optional[Any]:
        '''simple docstring'''






        _lowercase      			=self.config_class(**self.inputs_dict   )

        _lowercase      			='test'
        with tempfile.TemporaryDirectory() as tmpdirname:
              _lowercase      			=os.path.join(lowerCAmelCase				,  lowerCAmelCase   )
              config_first.save_pretrained(lowerCAmelCase   )
              _lowercase      			=self.config_class.from_pretrained(lowerCAmelCase				,  subfolder=lowerCAmelCase   )

        self.parent.assertEqual(config_second.to_dict()				,  config_first.to_dict()   )




  def     A__	(							self   )    ->					Union[str, Any]:
        '''simple docstring'''






        _lowercase      			=self.config_class(**self.inputs_dict				,  num_labels=5   )
        self.parent.assertEqual(len(config.idalabel   )				,  5   )
        self.parent.assertEqual(len(config.labelaid   )				,  5   )

        _lowercase      			=3
        self.parent.assertEqual(len(config.idalabel   )				,  3   )
        self.parent.assertEqual(len(config.labelaid   )				,  3   )




  def     A__	(							self   )    ->					Any:
        '''simple docstring'''






        if self.config_class.is_composition:
              return
        _lowercase      			=self.config_class()
        self.parent.assertIsNotNone(lowerCAmelCase   )




  def     A__	(							self   )    ->					List[Any]:
        '''simple docstring'''






        _lowercase      			=copy.deepcopy(lowerCAmelCase   )
        _lowercase      			=self.config_class(**lowerCAmelCase   )
        _lowercase      			=[]
        for key, value in config_common_kwargs.items():
              if key == "torch_dtype":
                    if not is_torch_available():
                          continue
                    else:
                          import torch

                          if config.torch_dtype != torch.floataa:
                                wrong_values.append(('torch_dtype', config.torch_dtype, torch.floataa)   )
              elif getattr(lowerCAmelCase				,  lowerCAmelCase   ) != value:
                    wrong_values.append((key, getattr(lowerCAmelCase				,  lowerCAmelCase   ), value)   )

        if len(lowerCAmelCase   ) > 0:
              _lowercase      			='\n'.join([F'''- {v[0]}: got {v[1]} instead of {v[2]}''' for v in wrong_values]   )
              raise ValueError(F'''The following keys were not properly set in the config:\n{errors}'''   )




  def     A__	(							self   )    ->					int:
        '''simple docstring'''






        self.create_and_test_config_common_properties()
        self.create_and_test_config_to_json_string()
        self.create_and_test_config_to_json_file()
        self.create_and_test_config_from_and_save_pretrained()
        self.create_and_test_config_from_and_save_pretrained_subfolder()
        self.create_and_test_config_with_num_labels()
        self.check_config_can_be_init_without_params()
        self.check_config_arguments_init()





from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


lowercase_   							=  logging.get_logger(__name__)

lowercase_   							=  {
    'EleutherAI/gpt-j-6B': 'https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json',
    # See all GPT-J models at https://huggingface.co/models?filter=gpt_j
}





class 					__lowerCAmelCase	(   SCREAMING_SNAKE_CASE				):
  _a		     =    """gptj"""
  _a		     =    {
      """max_position_embeddings""": """n_positions""",
      """hidden_size""": """n_embd""",
      """num_attention_heads""": """n_head""",
      """num_hidden_layers""": """n_layer""",
  }




  def __init__(							self				,  lowerCAmelCase=50_400				,  lowerCAmelCase=2_048				,  lowerCAmelCase=4_096				,  lowerCAmelCase=28				,  lowerCAmelCase=16				,  lowerCAmelCase=64				,  lowerCAmelCase=None				,  lowerCAmelCase="gelu_new"				,  lowerCAmelCase=0.0				,  lowerCAmelCase=0.0				,  lowerCAmelCase=0.0				,  lowerCAmelCase=1e-5				,  lowerCAmelCase=0.02				,  lowerCAmelCase=True				,  lowerCAmelCase=50_256				,  lowerCAmelCase=50_256				,  lowerCAmelCase=False				,  **lowerCAmelCase				,  )    ->					Optional[Any]:
        '''simple docstring'''






        _lowercase      			=vocab_size
        _lowercase      			=n_positions
        _lowercase      			=n_embd
        _lowercase      			=n_layer
        _lowercase      			=n_head
        _lowercase      			=n_inner
        _lowercase      			=rotary_dim
        _lowercase      			=activation_function
        _lowercase      			=resid_pdrop
        _lowercase      			=embd_pdrop
        _lowercase      			=attn_pdrop
        _lowercase      			=layer_norm_epsilon
        _lowercase      			=initializer_range
        _lowercase      			=use_cache

        _lowercase      			=bos_token_id
        _lowercase      			=eos_token_id

        super().__init__(
            bos_token_id=lowerCAmelCase				,  eos_token_id=lowerCAmelCase				,  tie_word_embeddings=lowerCAmelCase				,  **lowerCAmelCase   )





class 					__lowerCAmelCase	(   SCREAMING_SNAKE_CASE				):




  def __init__(							self				,  lowerCAmelCase				,  lowerCAmelCase = "default"				,  lowerCAmelCase = None				,  lowerCAmelCase = False				,  )    ->					Optional[int]:
        '''simple docstring'''






        super().__init__(lowerCAmelCase				,  task=lowerCAmelCase				,  patching_specs=lowerCAmelCase				,  use_past=lowerCAmelCase   )
        if not getattr(self._config				,  'pad_token_id'				,  lowerCAmelCase   ):
              # TODO: how to do that better?
              _lowercase      			=0




  @property
  def     A__	(							self   )    ->					Mapping[str, Mapping[int, str]]:
        '''simple docstring'''






        _lowercase      			=OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}}   )
        if self.use_past:
              self.fill_with_past_key_values_(lowerCAmelCase				,  direction='inputs'   )
              _lowercase      			={0: 'batch', 1: 'past_sequence + sequence'}
        else:
              _lowercase      			={0: 'batch', 1: 'sequence'}

        return common_inputs




  @property
  def     A__	(							self   )    ->					int:
        '''simple docstring'''






        return self._config.n_layer




  @property
  def     A__	(							self   )    ->					int:
        '''simple docstring'''






        return self._config.n_head




  def     A__	(							self				,  lowerCAmelCase				,  lowerCAmelCase = -1				,  lowerCAmelCase = -1				,  lowerCAmelCase = False				,  lowerCAmelCase = None				,  )    ->					Mapping[str, Any]:
        '''simple docstring'''






        _lowercase      			=super(lowerCAmelCase				,  self   ).generate_dummy_inputs(
            lowerCAmelCase				,  batch_size=lowerCAmelCase				,  seq_length=lowerCAmelCase				,  is_pair=lowerCAmelCase				,  framework=lowerCAmelCase   )

        # We need to order the input in the way they appears in the forward()
        _lowercase      			=OrderedDict({'input_ids': common_inputs['input_ids']}   )

        # Need to add the past_keys
        if self.use_past:
              if not is_torch_available():
                    raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'   )
              else:
                    import torch

                    _lowercase			,			_lowercase      			=common_inputs['input_ids'].shape
                    # Not using the same length for past_key_values
                    _lowercase      			=seqlen + 2
                    _lowercase      			=(
                        batch,
                        self.num_attention_heads,
                        past_key_values_length,
                        self._config.hidden_size // self.num_attention_heads,
                    )
                    _lowercase      			=[
                        (torch.zeros(lowerCAmelCase   ), torch.zeros(lowerCAmelCase   )) for _ in range(self.num_layers   )
                    ]

        _lowercase      			=common_inputs['attention_mask']
        if self.use_past:
              _lowercase      			=ordered_inputs['attention_mask'].dtype
              _lowercase      			=torch.cat(
                  [ordered_inputs['attention_mask'], torch.ones(lowerCAmelCase				,  lowerCAmelCase				,  dtype=lowerCAmelCase   )]				,  dim=1   )

        return ordered_inputs




  @property
  def     A__	(							self   )    ->					int:
        '''simple docstring'''






        return 13


