





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_          =		{
    """Salesforce/codegen-350M-nl""": """https://huggingface.co/Salesforce/codegen-350M-nl/resolve/main/config.json""",
    """Salesforce/codegen-350M-multi""": """https://huggingface.co/Salesforce/codegen-350M-multi/resolve/main/config.json""",
    """Salesforce/codegen-350M-mono""": """https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/config.json""",
    """Salesforce/codegen-2B-nl""": """https://huggingface.co/Salesforce/codegen-2B-nl/resolve/main/config.json""",
    """Salesforce/codegen-2B-multi""": """https://huggingface.co/Salesforce/codegen-2B-multi/resolve/main/config.json""",
    """Salesforce/codegen-2B-mono""": """https://huggingface.co/Salesforce/codegen-2B-mono/resolve/main/config.json""",
    """Salesforce/codegen-6B-nl""": """https://huggingface.co/Salesforce/codegen-6B-nl/resolve/main/config.json""",
    """Salesforce/codegen-6B-multi""": """https://huggingface.co/Salesforce/codegen-6B-multi/resolve/main/config.json""",
    """Salesforce/codegen-6B-mono""": """https://huggingface.co/Salesforce/codegen-6B-mono/resolve/main/config.json""",
    """Salesforce/codegen-16B-nl""": """https://huggingface.co/Salesforce/codegen-16B-nl/resolve/main/config.json""",
    """Salesforce/codegen-16B-multi""": """https://huggingface.co/Salesforce/codegen-16B-multi/resolve/main/config.json""",
    """Salesforce/codegen-16B-mono""": """https://huggingface.co/Salesforce/codegen-16B-mono/resolve/main/config.json""",
}
class     SCREAMING_SNAKE_CASE       (UpperCAmelCase				):
   _UpperCamelCase   :		Union[str, Any]			 =	'codegen'
   _UpperCamelCase   :		Tuple			 =	{
       'max_position_embeddings': 'n_positions',
       'hidden_size': 'n_embd',
       'num_attention_heads': 'n_head',
       'num_hidden_layers': 'n_layer',
   }


   def __init__(   self      :    str      ,					a      :    List[str]=50_400      ,					a      :    Union[str, Any]=2_048      ,					a      :    str=2_048      ,					a      :    Dict=4_096      ,					a      :    Optional[Any]=28      ,					a      :    List[str]=16      ,					a      :    str=64      ,					a      :    str=None      ,					a      :    Union[str, Any]="gelu_new"      ,					a      :    Dict=0.0      ,					a      :    List[str]=0.0      ,					a      :    Optional[int]=0.0      ,					a      :    List[str]=1E-5      ,					a      :    Optional[Any]=0.02      ,					a      :    List[Any]=True      ,					a      :    Tuple=50_256      ,					a      :    Optional[int]=50_256      ,					a      :    Dict=False      ,					**a      :    Any      ,					)->       Tuple:





     """simple docstring"""







     lowercase__           =  vocab_size
     lowercase__           =  n_ctx
     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=a      ,					eos_token_id=a      ,					tie_word_embeddings=a      ,					**a	)






class     SCREAMING_SNAKE_CASE       (UpperCAmelCase				):


   def __init__(   self      :    Union[str, Any]      ,					a      :    PretrainedConfig      ,					a      :    str = "default"      ,					a      :    List[PatchingSpec] = None      ,					a      :    bool = False      ,					)->       List[Any]:





     """simple docstring"""







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


   @property
   def 	SCREAMING_SNAKE_CASE_							(   self      :    List[Any]	)->       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_(a      ,					direction='inputs'	)
       lowercase__           =  {0: 'batch', 1: 'past_sequence + sequence'}
     else:
       lowercase__           =  {0: 'batch', 1: 'sequence'}

     return common_inputs


   @property
   def 	SCREAMING_SNAKE_CASE_							(   self      :    List[str]	)->       int:





     """simple docstring"""







     return self._config.n_layer


   @property
   def 	SCREAMING_SNAKE_CASE_							(   self      :    Tuple	)->       int:





     """simple docstring"""







     return self._config.n_head


   def 	SCREAMING_SNAKE_CASE_							(   self      :    Dict      ,					a      :    PreTrainedTokenizer      ,					a      :    int = -1      ,					a      :    int = -1      ,					a      :    bool = False      ,					a      :    Optional[TensorType] = None      ,					)->       Mapping[str, Any]:





     """simple docstring"""







     lowercase__           =  super(a      ,					self	).generate_dummy_inputs(
         a      ,					batch_size=a      ,					seq_length=a      ,					is_pair=a      ,					framework=a	)

     # 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(a	), torch.zeros(a	)) 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(a      ,					a      ,					dtype=a	)]      ,					dim=1	)

     return ordered_inputs


   @property
   def 	SCREAMING_SNAKE_CASE_							(   self      :    Optional[Any]	)->       int:





     """simple docstring"""







     return 13







from datasets.utils.patching import _PatchedModuleObj, patch_submodule

from . import _test_patching




def       __UpperCamelCase  ()    ->       str:
  import os as original_os
  from os import path as original_path
  from os import rename as original_rename
  from os.path import dirname as original_dirname
  from os.path import join as original_join

  assert _test_patching.os is original_os
  assert _test_patching.path is original_path
  assert _test_patching.join is original_join

  assert _test_patching.renamed_os is original_os
  assert _test_patching.renamed_path is original_path
  assert _test_patching.renamed_join is original_join

  lowercase__           =  '__test_patch_submodule_mock__'
  with patch_submodule(_test_patching		,						'os.path.join'		,						_SCREAMING_SNAKE_CASE		):
    # Every way to access os.path.join must be patched, and the rest must stay untouched

    # check os.path.join
    assert isinstance(_test_patching.os		,						_PatchedModuleObj		)
    assert isinstance(_test_patching.os.path		,						_PatchedModuleObj		)
    assert _test_patching.os.path.join is mock

    # check path.join
    assert isinstance(_test_patching.path		,						_PatchedModuleObj		)
    assert _test_patching.path.join is mock

    # check join
    assert _test_patching.join is mock

    # check that the other attributes are untouched
    assert _test_patching.os.rename is original_rename
    assert _test_patching.path.dirname is original_dirname
    assert _test_patching.os.path.dirname is original_dirname

    # Even renamed modules or objects must be patched

    # check renamed_os.path.join
    assert isinstance(_test_patching.renamed_os		,						_PatchedModuleObj		)
    assert isinstance(_test_patching.renamed_os.path		,						_PatchedModuleObj		)
    assert _test_patching.renamed_os.path.join is mock

    # check renamed_path.join
    assert isinstance(_test_patching.renamed_path		,						_PatchedModuleObj		)
    assert _test_patching.renamed_path.join is mock

    # check renamed_join
    assert _test_patching.renamed_join is mock

    # check that the other attributes are untouched
    assert _test_patching.renamed_os.rename is original_rename
    assert _test_patching.renamed_path.dirname is original_dirname
    assert _test_patching.renamed_os.path.dirname is original_dirname

  # check that everthing is back to normal when the patch is over

  assert _test_patching.os is original_os
  assert _test_patching.path is original_path
  assert _test_patching.join is original_join

  assert _test_patching.renamed_os is original_os
  assert _test_patching.renamed_path is original_path
  assert _test_patching.renamed_join is original_join




def       __UpperCamelCase  ()    ->       Any:
  assert _test_patching.open is open

  lowercase__           =  '__test_patch_submodule_builtin_mock__'
  # _test_patching has "open" in its globals
  assert _test_patching.open is open
  with patch_submodule(_test_patching		,						'open'		,						_SCREAMING_SNAKE_CASE		):
    assert _test_patching.open is mock

  # check that everthing is back to normal when the patch is over

  assert _test_patching.open is open




def       __UpperCamelCase  ()    ->       List[str]:
  # pandas.read_csv is not present in _test_patching
  lowercase__           =  '__test_patch_submodule_missing_mock__'
  with patch_submodule(_test_patching		,						'pandas.read_csv'		,						_SCREAMING_SNAKE_CASE		):
    pass




def       __UpperCamelCase  ()    ->       List[str]:
  # builtin should always be mocked even if they're not in the globals
  # in case they're loaded at one point
  lowercase__           =  '__test_patch_submodule_missing_builtin_mock__'
  # _test_patching doesn't have "len" in its globals
  assert getattr(_test_patching		,						'len'		,						_SCREAMING_SNAKE_CASE		) is None
  with patch_submodule(_test_patching		,						'len'		,						_SCREAMING_SNAKE_CASE		):
    assert _test_patching.len is mock
  assert _test_patching.len is len




def       __UpperCamelCase  ()    ->       List[str]:
  lowercase__           =  '__test_patch_submodule_start_and_stop_mock__'
  lowercase__           =  patch_submodule(_test_patching		,						'open'		,						_SCREAMING_SNAKE_CASE		)
  assert _test_patching.open is open
  patch.start()
  assert _test_patching.open is mock
  patch.stop()
  assert _test_patching.open is open




def       __UpperCamelCase  ()    ->       Optional[int]:
  from os import rename as original_rename
  from os.path import dirname as original_dirname
  from os.path import join as original_join

  lowercase__           =  '__test_patch_submodule_successive_join__'
  lowercase__           =  '__test_patch_submodule_successive_dirname__'
  lowercase__           =  '__test_patch_submodule_successive_rename__'
  assert _test_patching.os.path.join is original_join
  assert _test_patching.os.path.dirname is original_dirname
  assert _test_patching.os.rename is original_rename

  with patch_submodule(_test_patching		,						'os.path.join'		,						_SCREAMING_SNAKE_CASE		):
    with patch_submodule(_test_patching		,						'os.rename'		,						_SCREAMING_SNAKE_CASE		):
      with patch_submodule(_test_patching		,						'os.path.dirname'		,						_SCREAMING_SNAKE_CASE		):
        assert _test_patching.os.path.join is mock_join
        assert _test_patching.os.path.dirname is mock_dirname
        assert _test_patching.os.rename is mock_rename

    # try another order
  with patch_submodule(_test_patching		,						'os.rename'		,						_SCREAMING_SNAKE_CASE		):
    with patch_submodule(_test_patching		,						'os.path.join'		,						_SCREAMING_SNAKE_CASE		):
      with patch_submodule(_test_patching		,						'os.path.dirname'		,						_SCREAMING_SNAKE_CASE		):
        assert _test_patching.os.path.join is mock_join
        assert _test_patching.os.path.dirname is mock_dirname
        assert _test_patching.os.rename is mock_rename

  assert _test_patching.os.path.join is original_join
  assert _test_patching.os.path.dirname is original_dirname
  assert _test_patching.os.rename is original_rename




def       __UpperCamelCase  ()    ->       Optional[Any]:
  lowercase__           =  '__test_patch_submodule_doesnt_exist_mock__'
  with patch_submodule(_test_patching		,						'__module_that_doesn_exist__.__attribute_that_doesn_exist__'		,						_SCREAMING_SNAKE_CASE		):
    pass
  with patch_submodule(_test_patching		,						'os.__attribute_that_doesn_exist__'		,						_SCREAMING_SNAKE_CASE		):
    pass


