






import json
import os
from dataclasses import dataclass
from functools import partial
from typing import Callable

import flax.linen as nn
import jax
import jax.numpy as jnp
import joblib
import optax
import wandb
from flax import jax_utils, struct, traverse_util
from flax.serialization import from_bytes, to_bytes
from flax.training import train_state
from flax.training.common_utils import shard
from tqdm.auto import tqdm

from transformers import BigBirdConfig, FlaxBigBirdForQuestionAnswering
from transformers.models.big_bird.modeling_flax_big_bird import FlaxBigBirdForQuestionAnsweringModule
class 							A_    (						__lowerCamelCase			):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			int =					4_2
 _SCREAMING_SNAKE_CASE							:			Dict =					jnp.floataa
 _SCREAMING_SNAKE_CASE							:			List[str] =					True


 def  snake_case__				(	self) ->		List[str]:

     """simple docstring"""



     super().setup()
     _UpperCAmelCase				:      int			  =				nn.Dense(5							,  dtype=self.dtype)


 def __call__(	self							,  *_A							,  **_A) ->		Optional[int]:

     """simple docstring"""



     _UpperCAmelCase				:      str			  =				super().__call__(*UpperCAmelCase_							,  **UpperCAmelCase_)
     _UpperCAmelCase				:      str			  =				self.cls(outputs[2])
     return outputs[:2] + (cls_out,)



class 							A_    (						__lowerCamelCase			):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			List[str] =					FlaxBigBirdForNaturalQuestionsModule



def      _lowerCamelCase	(						__A    :							Tuple		,		__A    :							List[str]		,		__A    :							Any		,		__A    :							Union[str, Any]		,		__A    :							Optional[int]		,		__A    :							Dict )      ->					Any:
    def cross_entropy(__A    :							int		,		__A    :							Dict		,		__A    :							List[Any]=None ):
        _UpperCAmelCase				:      Tuple			  =				logits.shape[-1]
        _UpperCAmelCase				:      Any			  =				(labels[..., None] == jnp.arange(lowerCamelCase_ )[None]).astype('''f4''' )
        _UpperCAmelCase				:      Tuple			  =				jax.nn.log_softmax(lowerCamelCase_		,		axis=-1 )
        _UpperCAmelCase				:      List[str]			  =				-jnp.sum(labels * logits		,		axis=-1 )
        if reduction is not None:
            _UpperCAmelCase				:      List[str]			  =				reduction(lowerCamelCase_ )
        return loss

    _UpperCAmelCase				:      int			  =				partial(lowerCamelCase_		,		reduction=jnp.mean )
    _UpperCAmelCase				:      int			  =				cross_entropy(lowerCamelCase_		,		lowerCamelCase_ )
    _UpperCAmelCase				:      str			  =				cross_entropy(lowerCamelCase_		,		lowerCamelCase_ )
    _UpperCAmelCase				:      Dict			  =				cross_entropy(lowerCamelCase_		,		lowerCamelCase_ )
    return (start_loss + end_loss + pooled_loss) / 3
@dataclass
class 							A_    :






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			Union[str, Any] =					"google/bigbird-roberta-base"
 _SCREAMING_SNAKE_CASE							:			Optional[Any] =					3_0_0_0
 _SCREAMING_SNAKE_CASE							:			Dict =					1_0_5_0_0

 _SCREAMING_SNAKE_CASE							:			Union[str, Any] =					1_2_8
 _SCREAMING_SNAKE_CASE							:			int =					3

 _SCREAMING_SNAKE_CASE							:			List[str] =					1
 _SCREAMING_SNAKE_CASE							:			List[str] =					5

 # tx_args
 _SCREAMING_SNAKE_CASE							:			Optional[Any] =					3e-5
 _SCREAMING_SNAKE_CASE							:			Any =					0.0
 _SCREAMING_SNAKE_CASE							:			Any =					2_0_0_0_0
 _SCREAMING_SNAKE_CASE							:			int =					0.00_95

 _SCREAMING_SNAKE_CASE							:			int =					"bigbird-roberta-natural-questions"
 _SCREAMING_SNAKE_CASE							:			int =					"training-expt"
 _SCREAMING_SNAKE_CASE							:			Optional[Any] =					"data/nq-training.jsonl"
 _SCREAMING_SNAKE_CASE							:			int =					"data/nq-validation.jsonl"


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

     """simple docstring"""



     os.makedirs(self.base_dir							,  exist_ok=UpperCAmelCase_)
     _UpperCAmelCase				:      str			  =				os.path.join(self.base_dir							,  self.save_dir)
     _UpperCAmelCase				:      Optional[int]			  =				self.batch_size_per_device * jax.device_count()



@dataclass
class 							A_    :






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			List[Any] =					4_2
 _SCREAMING_SNAKE_CASE							:			Any =					4_0_9_6  # no dynamic padding on TPUs


 def __call__(	self							,  _A) ->		Any:

     """simple docstring"""



     _UpperCAmelCase				:      Optional[Any]			  =				self.collate_fn(UpperCAmelCase_)
     _UpperCAmelCase				:      Tuple			  =				jax.tree_util.tree_map(UpperCAmelCase_							,  UpperCAmelCase_)
     return batch


 def  snake_case__				(	self							,  _A) ->		Optional[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      List[Any]			  =				self.fetch_inputs(features['''input_ids'''])
     _UpperCAmelCase				:      Union[str, Any]			  =				{
         'input_ids': jnp.array(UpperCAmelCase_							,  dtype=jnp.intaa),
         'attention_mask': jnp.array(UpperCAmelCase_							,  dtype=jnp.intaa),
         'start_labels': jnp.array(features['''start_token''']							,  dtype=jnp.intaa),
         'end_labels': jnp.array(features['''end_token''']							,  dtype=jnp.intaa),
         'pooled_labels': jnp.array(features['''category''']							,  dtype=jnp.intaa),
     }
     return batch


 def  snake_case__				(	self							,  _A) ->		Optional[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      int			  =				[self._fetch_inputs(UpperCAmelCase_) for ids in input_ids]
     return zip(*UpperCAmelCase_)


 def  snake_case__				(	self							,  _A) ->		Optional[int]:

     """simple docstring"""



     _UpperCAmelCase				:      Dict			  =				[1 for _ in range(len(UpperCAmelCase_))]
     while len(UpperCAmelCase_) < self.max_length:
         input_ids.append(self.pad_id)
         attention_mask.append(0)
     return input_ids, attention_mask



def      _lowerCamelCase	(						__A    :							Tuple		,		__A    :							Optional[int]		,		__A    :							Optional[Any]=None )      ->					Any:
    if seed is not None:
        _UpperCAmelCase				:      str			  =				dataset.shuffle(seed=lowerCamelCase_ )
    for i in range(len(lowerCamelCase_ ) // batch_size ):
        _UpperCAmelCase				:      Any			  =				dataset[i * batch_size : (i + 1) * batch_size]
        yield dict(lowerCamelCase_ )

@partial(jax.pmap		,		axis_name='''batch''' )
def      _lowerCamelCase	(						__A    :							List[str]		,		__A    :							List[Any]		,		**__A    :							Any )      ->					Optional[Any]:
    def loss_fn(__A    :							List[Any] ):
        _UpperCAmelCase				:      List[Any]			  =				model_inputs.pop('''start_labels''' )
        _UpperCAmelCase				:      Any			  =				model_inputs.pop('''end_labels''' )
        _UpperCAmelCase				:      Optional[Any]			  =				model_inputs.pop('''pooled_labels''' )

        _UpperCAmelCase				:      int			  =				state.apply_fn(**lowerCamelCase_		,		params=lowerCamelCase_		,		dropout_rng=lowerCamelCase_		,		train=lowerCamelCase_ )
        _UpperCAmelCase				:      Any			  =				outputs

        return state.loss_fn(
            lowerCamelCase_		,		lowerCamelCase_		,		lowerCamelCase_		,		lowerCamelCase_		,		lowerCamelCase_		,		lowerCamelCase_		,		)

    _UpperCAmelCase				:      str			  =				jax.random.split(lowerCamelCase_ )
    _UpperCAmelCase				:      str			  =				jax.value_and_grad(lowerCamelCase_ )
    _UpperCAmelCase				:      Any			  =				grad_fn(state.params )
    _UpperCAmelCase				:      Tuple			  =				jax.lax.pmean({'''loss''': loss}		,		axis_name='''batch''' )
    _UpperCAmelCase				:      Optional[Any]			  =				jax.lax.pmean(lowerCamelCase_		,		'''batch''' )

    _UpperCAmelCase				:      List[Any]			  =				state.apply_gradients(grads=lowerCamelCase_ )
    return state, metrics, new_drp_rng

@partial(jax.pmap		,		axis_name='''batch''' )
def      _lowerCamelCase	(						__A    :							int		,		**__A    :							Optional[int] )      ->					Dict:
    _UpperCAmelCase				:      Tuple			  =				model_inputs.pop('''start_labels''' )
    _UpperCAmelCase				:      Dict			  =				model_inputs.pop('''end_labels''' )
    _UpperCAmelCase				:      Any			  =				model_inputs.pop('''pooled_labels''' )

    _UpperCAmelCase				:      Optional[Any]			  =				state.apply_fn(**lowerCamelCase_		,		params=state.params		,		train=lowerCamelCase_ )
    _UpperCAmelCase				:      str			  =				outputs

    _UpperCAmelCase				:      str			  =				state.loss_fn(lowerCamelCase_		,		lowerCamelCase_		,		lowerCamelCase_		,		lowerCamelCase_		,		lowerCamelCase_		,		lowerCamelCase_ )
    _UpperCAmelCase				:      Union[str, Any]			  =				jax.lax.pmean({'''loss''': loss}		,		axis_name='''batch''' )
    return metrics
class 							A_    (						train_state.TrainState			):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			Tuple =					struct.field(pytree_node=__lowerCamelCase			)



@dataclass
class 							A_    :






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:			Optional[Any] =					4_2
 _SCREAMING_SNAKE_CASE							:			Optional[Any] =					4_2
 _SCREAMING_SNAKE_CASE							:			Dict =					4_2
 _SCREAMING_SNAKE_CASE							:			Union[str, Any] =					4_2
 _SCREAMING_SNAKE_CASE							:			Union[str, Any] =					4_2
 _SCREAMING_SNAKE_CASE							:			Union[str, Any] =					4_2
 _SCREAMING_SNAKE_CASE							:			Optional[Any] =					None


 def  snake_case__				(	self							,  _A							,  _A							,  _A							,  _A=None) ->		str:

     """simple docstring"""



     _UpperCAmelCase				:      Any			  =				model.params
     _UpperCAmelCase				:      str			  =				TrainState.create(
         apply_fn=model.__call__							,  params=UpperCAmelCase_							,  tx=UpperCAmelCase_							,  loss_fn=UpperCAmelCase_							,  )
     if ckpt_dir is not None:
         _UpperCAmelCase				:      Optional[int]			  =				restore_checkpoint(UpperCAmelCase_							,  UpperCAmelCase_)
         _UpperCAmelCase				:      Tuple			  =				{
             'lr': args.lr,
             'init_lr': args.init_lr,
             'warmup_steps': args.warmup_steps,
             'num_train_steps': num_train_steps,
             'weight_decay': args.weight_decay,
         }
         _UpperCAmelCase				:      Optional[Any]			  =				build_tx(**UpperCAmelCase_)
         _UpperCAmelCase				:      List[Any]			  =				train_state.TrainState(
             step=UpperCAmelCase_							,  apply_fn=model.__call__							,  params=UpperCAmelCase_							,  tx=UpperCAmelCase_							,  opt_state=UpperCAmelCase_							,  )
         _UpperCAmelCase				:      Tuple			  =				args
         _UpperCAmelCase				:      Optional[Any]			  =				data_collator
         _UpperCAmelCase				:      str			  =				lr
         _UpperCAmelCase				:      Any			  =				params
     _UpperCAmelCase				:      Any			  =				jax_utils.replicate(UpperCAmelCase_)
     return state


 def  snake_case__				(	self							,  _A							,  _A							,  _A) ->		List[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      int			  =				self.args
     _UpperCAmelCase				:      str			  =				len(UpperCAmelCase_) // args.batch_size

     _UpperCAmelCase				:      Tuple			  =				jax.random.PRNGKey(0)
     _UpperCAmelCase				:      Optional[Any]			  =				jax.random.split(UpperCAmelCase_							,  jax.device_count())
     for epoch in range(args.max_epochs):
         _UpperCAmelCase				:      Union[str, Any]			  =				jnp.array(0							,  dtype=jnp.floataa)
         _UpperCAmelCase				:      Optional[int]			  =				get_batched_dataset(UpperCAmelCase_							,  args.batch_size							,  seed=UpperCAmelCase_)
         _UpperCAmelCase				:      Dict			  =				0
         for batch in tqdm(UpperCAmelCase_							,  total=UpperCAmelCase_							,  desc=f'''Running EPOCH-{epoch}'''):
             _UpperCAmelCase				:      str			  =				self.data_collator(UpperCAmelCase_)
             _UpperCAmelCase				:      Dict			  =				self.train_step_fn(UpperCAmelCase_							,  UpperCAmelCase_							,  **UpperCAmelCase_)
             running_loss += jax_utils.unreplicate(metrics['''loss'''])
             i += 1
             if i % args.logging_steps == 0:
                 _UpperCAmelCase				:      Optional[Any]			  =				jax_utils.unreplicate(state.step)
                 _UpperCAmelCase				:      List[Any]			  =				running_loss.item() / i
                 _UpperCAmelCase				:      Optional[Any]			  =				self.scheduler_fn(state_step - 1)

                 _UpperCAmelCase				:      List[str]			  =				self.evaluate(UpperCAmelCase_							,  UpperCAmelCase_)
                 _UpperCAmelCase				:      Dict			  =				{
                     'step': state_step.item(),
                     'eval_loss': eval_loss.item(),
                     'tr_loss': tr_loss,
                     'lr': lr.item(),
                 }
                 tqdm.write(str(UpperCAmelCase_))
                 self.logger.log(UpperCAmelCase_							,  commit=UpperCAmelCase_)

             if i % args.save_steps == 0:
                 self.save_checkpoint(args.save_dir + f'''-e{epoch}-s{i}'''							,  state=UpperCAmelCase_)


 def  snake_case__				(	self							,  _A							,  _A) ->		int:

     """simple docstring"""



     _UpperCAmelCase				:      str			  =				get_batched_dataset(UpperCAmelCase_							,  self.args.batch_size)
     _UpperCAmelCase				:      Any			  =				len(UpperCAmelCase_) // self.args.batch_size
     _UpperCAmelCase				:      Optional[Any]			  =				jnp.array(0							,  dtype=jnp.floataa)
     _UpperCAmelCase				:      Optional[Any]			  =				0
     for batch in tqdm(UpperCAmelCase_							,  total=UpperCAmelCase_							,  desc='''Evaluating ... '''):
         _UpperCAmelCase				:      Optional[Any]			  =				self.data_collator(UpperCAmelCase_)
         _UpperCAmelCase				:      str			  =				self.val_step_fn(UpperCAmelCase_							,  **UpperCAmelCase_)
         running_loss += jax_utils.unreplicate(metrics['''loss'''])
         i += 1
     return running_loss / i


 def  snake_case__				(	self							,  _A							,  _A) ->		List[Any]:

     """simple docstring"""



     _UpperCAmelCase				:      Union[str, Any]			  =				jax_utils.unreplicate(UpperCAmelCase_)
     print(f'''SAVING CHECKPOINT IN {save_dir}'''							,  end=''' ... ''')
     self.model_save_fn(UpperCAmelCase_							,  params=state.params)
     with open(os.path.join(UpperCAmelCase_							,  '''opt_state.msgpack''')							,  '''wb''') as f:
         f.write(to_bytes(state.opt_state))
     joblib.dump(self.args							,  os.path.join(UpperCAmelCase_							,  '''args.joblib'''))
     joblib.dump(self.data_collator							,  os.path.join(UpperCAmelCase_							,  '''data_collator.joblib'''))
     with open(os.path.join(UpperCAmelCase_							,  '''training_state.json''')							,  '''w''') as f:
         json.dump({'''step''': state.step.item()}							,  UpperCAmelCase_)
     print('''DONE''')



def      _lowerCamelCase	(						__A    :							List[Any]		,		__A    :							List[Any] )      ->					Dict:
    print(f'''RESTORING CHECKPOINT FROM {save_dir}'''		,		end=''' ... ''' )
    with open(os.path.join(lowerCamelCase_		,		'''flax_model.msgpack''' )		,		'''rb''' ) as f:
        _UpperCAmelCase				:      Optional[Any]			  =				from_bytes(state.params		,		f.read() )

    with open(os.path.join(lowerCamelCase_		,		'''opt_state.msgpack''' )		,		'''rb''' ) as f:
        _UpperCAmelCase				:      Any			  =				from_bytes(state.opt_state		,		f.read() )

    _UpperCAmelCase				:      Union[str, Any]			  =				joblib.load(os.path.join(lowerCamelCase_		,		'''args.joblib''' ) )
    _UpperCAmelCase				:      int			  =				joblib.load(os.path.join(lowerCamelCase_		,		'''data_collator.joblib''' ) )

    with open(os.path.join(lowerCamelCase_		,		'''training_state.json''' )		,		'''r''' ) as f:
        _UpperCAmelCase				:      str			  =				json.load(lowerCamelCase_ )
    _UpperCAmelCase				:      Optional[int]			  =				training_state['step']

    print('''DONE''' )
    return params, opt_state, step, args, data_collator

def      _lowerCamelCase	(						__A    :							Union[str, Any]		,		__A    :							str		,		__A    :							str		,		__A    :							str )      ->					Optional[Any]:
    _UpperCAmelCase				:      List[str]			  =				num_train_steps - warmup_steps
    _UpperCAmelCase				:      List[Any]			  =				optax.linear_schedule(init_value=lowerCamelCase_		,		end_value=lowerCamelCase_		,		transition_steps=lowerCamelCase_ )
    _UpperCAmelCase				:      Dict			  =				optax.linear_schedule(init_value=lowerCamelCase_		,		end_value=1e-7		,		transition_steps=lowerCamelCase_ )
    _UpperCAmelCase				:      Optional[int]			  =				optax.join_schedules(schedules=[warmup_fn, decay_fn]		,		boundaries=[warmup_steps] )
    return lr


def      _lowerCamelCase	(						__A    :							Tuple		,		__A    :							Optional[int]		,		__A    :							Optional[Any]		,		__A    :							Union[str, Any]		,		__A    :							Union[str, Any] )      ->					List[Any]:
    def weight_decay_mask(__A    :							str ):
        _UpperCAmelCase				:      int			  =				traverse_util.flatten_dict(lowerCamelCase_ )
        _UpperCAmelCase				:      Dict			  =				{k: (v[-1] != 'bias' and v[-2:] != ('LayerNorm', 'scale')) for k, v in params.items()}
        return traverse_util.unflatten_dict(lowerCamelCase_ )

    _UpperCAmelCase				:      str			  =				scheduler_fn(lowerCamelCase_		,		lowerCamelCase_		,		lowerCamelCase_		,		lowerCamelCase_ )

    _UpperCAmelCase				:      str			  =				optax.adamw(learning_rate=lowerCamelCase_		,		weight_decay=lowerCamelCase_		,		mask=lowerCamelCase_ )
    return tx, lr







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 							=						get_tests_dir('fixtures/dummy-config.json')
class 							A_    (						unittest.TestCase			):






 '''simple docstring'''




 def  snake_case__				(	self) ->		Optional[int]:

     """simple docstring"""



     _UpperCAmelCase				:      Union[str, Any]			  =				0


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

     """simple docstring"""



     self.assertIsNotNone(transformers.models.auto.__spec__)
     self.assertIsNotNone(importlib.util.find_spec('''transformers.models.auto'''))


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

     """simple docstring"""



     _UpperCAmelCase				:      List[str]			  =				AutoConfig.from_pretrained('''bert-base-uncased''')
     self.assertIsInstance(_A							,  _A)


 def  snake_case__				(	self) ->		Dict:

     """simple docstring"""



     _UpperCAmelCase				:      Tuple			  =				AutoConfig.from_pretrained(_A)
     self.assertIsInstance(_A							,  _A)


 def  snake_case__				(	self) ->		Optional[int]:

     """simple docstring"""



     _UpperCAmelCase				:      Optional[Any]			  =				AutoConfig.from_pretrained(_A)
     self.assertIsInstance(_A							,  _A)


 def  snake_case__				(	self) ->		Any:

     """simple docstring"""



     _UpperCAmelCase				:      str			  =				AutoConfig.for_model('''roberta''')
     self.assertIsInstance(_A							,  _A)


 def  snake_case__				(	self) ->		int:

     """simple docstring"""



     with tempfile.TemporaryDirectory() as tmp_dir:
         # This model name contains bert and roberta, but roberta ends up being picked.
         _UpperCAmelCase				:      str			  =				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				:      Any			  =				AutoConfig.from_pretrained(_A)
         self.assertEqual(type(_A)							,  _A)


 def  snake_case__				(	self) ->		List[Any]:

     """simple docstring"""



     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				:      Tuple			  =				CustomConfig()
         with tempfile.TemporaryDirectory() as tmp_dir:
             config.save_pretrained(_A)
             _UpperCAmelCase				:      Tuple			  =				AutoConfig.from_pretrained(_A)
             self.assertIsInstance(_A							,  _A)

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


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

     """simple docstring"""



     with self.assertRaisesRegex(
         _A							,  '''bert-base is not a local folder and is not a valid model identifier'''):
         _UpperCAmelCase				:      Tuple			  =				AutoConfig.from_pretrained('''bert-base''')


 def  snake_case__				(	self) ->		int:

     """simple docstring"""



     with self.assertRaisesRegex(
         _A							,  R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''):
         _UpperCAmelCase				:      List[Any]			  =				AutoConfig.from_pretrained(_A							,  revision='''aaaaaa''')


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

     """simple docstring"""



     with self.assertRaisesRegex(
         _A							,  '''hf-internal-testing/no-config-test-repo does not appear to have a file named config.json.'''							,  ):
         _UpperCAmelCase				:      List[str]			  =				AutoConfig.from_pretrained('''hf-internal-testing/no-config-test-repo''')


 def  snake_case__				(	self) ->		List[str]:

     """simple docstring"""



     with self.assertRaises(_A):
         _UpperCAmelCase				:      Dict			  =				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				:      Optional[int]			  =				AutoConfig.from_pretrained('''hf-internal-testing/test_dynamic_model'''							,  trust_remote_code=_A)

     _UpperCAmelCase				:      int			  =				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				:      Any			  =				AutoConfig.from_pretrained(_A							,  trust_remote_code=_A)
     self.assertEqual(reloaded_config.__class__.__name__							,  '''NewModelConfig''')


 def  snake_case__				(	self) ->		Tuple:

     """simple docstring"""



     class 							A_    (						__lowercase			):






      '''simple docstring'''




      _SCREAMING_SNAKE_CASE							:			Union[str, Any] =					"new-model"

     try:
         AutoConfig.register('''new-model'''							,  _A)
         # If remote code is not set, the default is to use local
         _UpperCAmelCase				:      List[str]			  =				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				:      List[Any]			  =				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				:      Dict			  =				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"]




