'''simple docstring'''





import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / 'utils'))

from test_module.custom_configuration import CustomConfig  # noqa E402


SCREAMING_SNAKE_CASE_:     Dict  =get_tests_dir('fixtures/dummy-config.json')




class        __A      ( unittest.TestCase	):



    def 					_lowercase						(self    :    List[str] ):
         UpperCAmelCase_           =   0



    def 					_lowercase						(self    :    Union[str, Any] ):
         self.assertIsNotNone(transformers.models.auto.__spec__ )
         self.assertIsNotNone(importlib.util.find_spec("transformers.models.auto" ) )



    def 					_lowercase						(self    :    int ):
         UpperCAmelCase_           =   AutoConfig.from_pretrained("bert-base-uncased" )
         self.assertIsInstance(__a   ,			__a )



    def 					_lowercase						(self    :    Optional[int] ):
         UpperCAmelCase_           =   AutoConfig.from_pretrained(__a )
         self.assertIsInstance(__a   ,			__a )



    def 					_lowercase						(self    :    Optional[Any] ):
         UpperCAmelCase_           =   AutoConfig.from_pretrained(__a )
         self.assertIsInstance(__a   ,			__a )



    def 					_lowercase						(self    :    Tuple ):
         UpperCAmelCase_           =   AutoConfig.for_model("roberta" )
         self.assertIsInstance(__a   ,			__a )



    def 					_lowercase						(self    :    List[str] ):
         with tempfile.TemporaryDirectory() as tmp_dir:
              # This model name contains bert and roberta, but roberta ends up being picked.
              UpperCAmelCase_           =   os.path.join(__a   ,			"fake-roberta" )
              os.makedirs(__a   ,			exist_ok=__a )
              with open(os.path.join(__a   ,			"config.json" )   ,			"w" ) as f:
                   f.write(json.dumps({} ) )
              UpperCAmelCase_           =   AutoConfig.from_pretrained(__a )
              self.assertEqual(type(__a )   ,			__a )



    def 					_lowercase						(self    :    str ):
         try:
              AutoConfig.register("custom"   ,			__a )
              # Wrong model type will raise an error
              with self.assertRaises(__a ):
                   AutoConfig.register("model"   ,			__a )
              # Trying to register something existing in the Transformers library will raise an error
              with self.assertRaises(__a ):
                   AutoConfig.register("bert"   ,			__a )

              # Now that the config is registered, it can be used as any other config with the auto-API
              UpperCAmelCase_           =   CustomConfig()
              with tempfile.TemporaryDirectory() as tmp_dir:
                   config.save_pretrained(__a )
                   UpperCAmelCase_           =   AutoConfig.from_pretrained(__a )
                   self.assertIsInstance(__a   ,			__a )

         finally:
              if "custom" in CONFIG_MAPPING._extra_content:
                   del CONFIG_MAPPING._extra_content["custom"]



    def 					_lowercase						(self    :    Union[str, Any] ):
         with self.assertRaisesRegex(
             __a   ,			"bert-base is not a local folder and is not a valid model identifier" ):
              UpperCAmelCase_           =   AutoConfig.from_pretrained("bert-base" )



    def 					_lowercase						(self    :    Optional[Any] ):
         with self.assertRaisesRegex(
             __a   ,			r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)" ):
              UpperCAmelCase_           =   AutoConfig.from_pretrained(__a   ,			revision="aaaaaa" )



    def 					_lowercase						(self    :    Tuple ):
         with self.assertRaisesRegex(
             __a   ,			"hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."   ,			):
              UpperCAmelCase_           =   AutoConfig.from_pretrained("hf-internal-testing/no-config-test-repo" )



    def 					_lowercase						(self    :    Any ):
         # If remote code is not set, we will time out when asking whether to load the model.
         with self.assertRaises(__a ):
              UpperCAmelCase_           =   AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model" )
         # If remote code is disabled, we can't load this config.
         with self.assertRaises(__a ):
              UpperCAmelCase_           =   AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"   ,			trust_remote_code=__a )

         UpperCAmelCase_           =   AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"   ,			trust_remote_code=__a )
         self.assertEqual(config.__class__.__name__   ,			"NewModelConfig" )

         # Test config can be reloaded.
         with tempfile.TemporaryDirectory() as tmp_dir:
              config.save_pretrained(__a )
              UpperCAmelCase_           =   AutoConfig.from_pretrained(__a   ,			trust_remote_code=__a )
         self.assertEqual(reloaded_config.__class__.__name__   ,			"NewModelConfig" )






    def 					_lowercase						(self    :    Tuple ):




         class        __A      ( UpperCamelCase__	):
             a__       :						str          =  """new-model"""

         try:
              AutoConfig.register("new-model"   ,			__a )
              # If remote code is not set, the default is to use local
              UpperCAmelCase_           =   AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model" )
              self.assertEqual(config.__class__.__name__   ,			"NewModelConfigLocal" )

              # If remote code is disabled, we load the local one.
              UpperCAmelCase_           =   AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"   ,			trust_remote_code=__a )
              self.assertEqual(config.__class__.__name__   ,			"NewModelConfigLocal" )

              # If remote is enabled, we load from the Hub
              UpperCAmelCase_           =   AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"   ,			trust_remote_code=__a )
              self.assertEqual(config.__class__.__name__   ,			"NewModelConfig" )

         finally:
              if "new-model" in CONFIG_MAPPING._extra_content:
                   del CONFIG_MAPPING._extra_content["new-model"]





from ...configuration_utils import PretrainedConfig
from ...utils import logging


__UpperCamelCase		:       Any									=     logging.get_logger(__name__)

__UpperCamelCase		:       Optional[int]									=     {
    'facebook/nllb-moe-54B': 'https://huggingface.co/facebook/nllb-moe-54b/resolve/main/config.json',
}





class 					lowercase__						(   UpperCamelCase_):
						UpperCamelCase_     =		"""nllb-moe"""
						UpperCamelCase_     =		["""past_key_values"""]
						UpperCamelCase_     =		{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}


						def __init__(       self	:				List[str]    ,		UpperCamelCase__	:				List[str]=12_8112    ,		UpperCamelCase__	:				str=1024    ,		UpperCamelCase__	:				Optional[int]=12    ,		UpperCamelCase__	:				Union[str, Any]=4096    ,		UpperCamelCase__	:				Optional[Any]=16    ,		UpperCamelCase__	:				Optional[Any]=12    ,		UpperCamelCase__	:				str=4096    ,		UpperCamelCase__	:				Dict=16    ,		UpperCamelCase__	:				Any=0.05    ,		UpperCamelCase__	:				Any=0.05    ,		UpperCamelCase__	:				Optional[Any]=True    ,		UpperCamelCase__	:				Optional[Any]=True    ,		UpperCamelCase__	:				List[Any]="relu"    ,		UpperCamelCase__	:				Union[str, Any]=1024    ,		UpperCamelCase__	:				Optional[int]=0.1    ,		UpperCamelCase__	:				Any=0.1    ,		UpperCamelCase__	:				Optional[Any]=0.0    ,		UpperCamelCase__	:				str=0.02    ,		UpperCamelCase__	:				Tuple=2    ,		UpperCamelCase__	:				List[str]=True    ,		UpperCamelCase__	:				Dict=False    ,		UpperCamelCase__	:				Any="float32"    ,		UpperCamelCase__	:				List[Any]=False    ,		UpperCamelCase__	:				Tuple=128    ,		UpperCamelCase__	:				Tuple=64    ,		UpperCamelCase__	:				List[str]=4    ,		UpperCamelCase__	:				Union[str, Any]=4    ,		UpperCamelCase__	:				Dict=0.001    ,		UpperCamelCase__	:				Optional[Any]=0.001    ,		UpperCamelCase__	:				Optional[Any]="all"    ,		UpperCamelCase__	:				Tuple=False    ,		UpperCamelCase__	:				List[Any]=False    ,		UpperCamelCase__	:				List[Any]=1.0    ,		UpperCamelCase__	:				str=0.2    ,		UpperCamelCase__	:				List[Any]=1    ,		UpperCamelCase__	:				List[Any]=0    ,		UpperCamelCase__	:				Optional[int]=2    ,		UpperCamelCase__	:				Tuple=False    ,		**UpperCamelCase__	:				Union[str, Any]    ,		):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[str]							=							vocab_size
												SCREAMING_SNAKE_CASE							:    Dict							=							max_position_embeddings
												SCREAMING_SNAKE_CASE							:    int							=							d_model
												SCREAMING_SNAKE_CASE							:    Any							=							encoder_ffn_dim
												SCREAMING_SNAKE_CASE							:    List[Any]							=							encoder_layers
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							encoder_attention_heads
												SCREAMING_SNAKE_CASE							:    List[str]							=							decoder_ffn_dim
												SCREAMING_SNAKE_CASE							:    Dict							=							decoder_layers
												SCREAMING_SNAKE_CASE							:    List[Any]							=							decoder_attention_heads
												SCREAMING_SNAKE_CASE							:    List[str]							=							dropout
												SCREAMING_SNAKE_CASE							:    Any							=							attention_dropout
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							activation_dropout
												SCREAMING_SNAKE_CASE							:    List[Any]							=							activation_function
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							init_std
												SCREAMING_SNAKE_CASE							:    int							=							encoder_layerdrop
												SCREAMING_SNAKE_CASE							:    List[Any]							=							decoder_layerdrop
												SCREAMING_SNAKE_CASE							:    Any							=							use_cache
												SCREAMING_SNAKE_CASE							:    str							=							encoder_layers
												SCREAMING_SNAKE_CASE							:    List[Any]							=							scale_embedding  # scale factor will be sqrt(d_model) if True
												SCREAMING_SNAKE_CASE							:    List[str]							=							router_z_loss_coef
												SCREAMING_SNAKE_CASE							:    List[str]							=							router_aux_loss_coef
												SCREAMING_SNAKE_CASE							:    int							=							decoder_sparse_step
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							encoder_sparse_step
												SCREAMING_SNAKE_CASE							:    List[str]							=							num_experts
												SCREAMING_SNAKE_CASE							:    int							=							expert_capacity
												SCREAMING_SNAKE_CASE							:    Any							=							router_bias
												if router_dtype not in ["float32", "float16", "bfloat16"]:
																		raise ValueError(f"""`router_dtype` must be one of 'float32', 'float16' or 'bfloat16', got {router_dtype}"""						)
												SCREAMING_SNAKE_CASE							:    str							=							router_dtype

												SCREAMING_SNAKE_CASE							:    List[Any]							=							router_ignore_padding_tokens
												SCREAMING_SNAKE_CASE							:    int							=							batch_prioritized_routing
												SCREAMING_SNAKE_CASE							:    str							=							second_expert_policy
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							normalize_router_prob_before_dropping
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							moe_eval_capacity_token_fraction
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							moe_token_dropout
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							output_router_logits
												super().__init__(
												    pad_token_id=UpperCamelCase__    ,		bos_token_id=UpperCamelCase__    ,		eos_token_id=UpperCamelCase__    ,		is_encoder_decoder=UpperCamelCase__    ,		decoder_start_token_id=UpperCamelCase__    ,		**UpperCamelCase__    ,		)





