






import copy
import json
import os
import tempfile

from transformers import is_torch_available

from .test_configuration_utils import config_common_kwargs





class 					A__     (     __snake_case ):

     '''simple docstring'''






     def __init__( self      :    Dict   ,						_SCREAMING_SNAKE_CASE      :    Dict   ,						_SCREAMING_SNAKE_CASE      :    List[Any]=None   ,						_SCREAMING_SNAKE_CASE      :    Optional[Any]=True   ,						_SCREAMING_SNAKE_CASE      :    Any=None   ,						**_SCREAMING_SNAKE_CASE      :    Any ):






           """simple docstring"""




           UpperCamelCase					       =  parent
           UpperCamelCase					       =  config_class
           UpperCamelCase					       =  has_text_modality
           UpperCamelCase					       =  kwargs
           UpperCamelCase					       =  common_properties







     def   _SCREAMING_SNAKE_CASE     ( self      :    List[Any] ):






           """simple docstring"""




           UpperCamelCase					       =  self.config_class(**self.inputs_dict )
           UpperCamelCase					       =  (
               ['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(_SCREAMING_SNAKE_CASE   ,						_SCREAMING_SNAKE_CASE )   ,						msg=f'`{prop}` does not exist' )

           # Test that config has the common properties as setter
           for idx, name in enumerate(_SCREAMING_SNAKE_CASE ):
                 try:
                       setattr(_SCREAMING_SNAKE_CASE   ,						_SCREAMING_SNAKE_CASE   ,						_SCREAMING_SNAKE_CASE )
                       self.parent.assertEqual(
                           getattr(_SCREAMING_SNAKE_CASE   ,						_SCREAMING_SNAKE_CASE )   ,						_SCREAMING_SNAKE_CASE   ,						msg=f'`{name} value {idx} expected, but was {getattr(_SCREAMING_SNAKE_CASE   ,						_SCREAMING_SNAKE_CASE )}' )
                 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(_SCREAMING_SNAKE_CASE ):
                 try:
                       UpperCamelCase					       =  self.config_class(**{name: idx} )
                       self.parent.assertEqual(
                           getattr(_SCREAMING_SNAKE_CASE   ,						_SCREAMING_SNAKE_CASE )   ,						_SCREAMING_SNAKE_CASE   ,						msg=f'`{name} value {idx} expected, but was {getattr(_SCREAMING_SNAKE_CASE   ,						_SCREAMING_SNAKE_CASE )}' )
                 except NotImplementedError:
                       # Some models might not be able to implement setters for common_properties
                       # In that case, a NotImplementedError is raised
                       pass







     def   _SCREAMING_SNAKE_CASE     ( self      :    Dict ):






           """simple docstring"""




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







     def   _SCREAMING_SNAKE_CASE     ( self      :    str ):






           """simple docstring"""




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

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

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







     def   _SCREAMING_SNAKE_CASE     ( self      :    int ):






           """simple docstring"""




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

           with tempfile.TemporaryDirectory() as tmpdirname:
                 config_first.save_pretrained(_SCREAMING_SNAKE_CASE )
                 UpperCamelCase					       =  self.config_class.from_pretrained(_SCREAMING_SNAKE_CASE )

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







     def   _SCREAMING_SNAKE_CASE     ( self      :    List[str] ):






           """simple docstring"""




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

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

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







     def   _SCREAMING_SNAKE_CASE     ( self      :    List[Any] ):






           """simple docstring"""




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

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







     def   _SCREAMING_SNAKE_CASE     ( self      :    Tuple ):






           """simple docstring"""




           if self.config_class.is_composition:
                 return
           UpperCamelCase					       =  self.config_class()
           self.parent.assertIsNotNone(_SCREAMING_SNAKE_CASE )







     def   _SCREAMING_SNAKE_CASE     ( self      :    List[str] ):






           """simple docstring"""




           UpperCamelCase					       =  copy.deepcopy(_SCREAMING_SNAKE_CASE )
           UpperCamelCase					       =  self.config_class(**_SCREAMING_SNAKE_CASE )
           UpperCamelCase					       =  []
           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(_SCREAMING_SNAKE_CASE   ,						_SCREAMING_SNAKE_CASE ) != value:
                       wrong_values.append((key, getattr(_SCREAMING_SNAKE_CASE   ,						_SCREAMING_SNAKE_CASE ), value) )

           if len(_SCREAMING_SNAKE_CASE ) > 0:
                 UpperCamelCase					       =  '\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   _SCREAMING_SNAKE_CASE     ( self      :    Dict ):






           """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()











import re

import jax.numpy as jnp
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.random import PRNGKey

from ..utils import logging


__magic_name__   :  Optional[int]        =							logging.get_logger(__name__)
def 		lowercase__  (				_UpperCamelCase) ->			Dict:
      """simple docstring"""

      UpperCamelCase					       =  r'\w+[.]\d+'
      UpperCamelCase					       =  re.findall(_UpperCamelCase       ,							_UpperCamelCase)
      for pat in pats:
            UpperCamelCase					       =  key.replace(_UpperCamelCase       ,							'_'.join(pat.split('.')))
      return key
def 		lowercase__  (				_UpperCamelCase       ,							_UpperCamelCase       ,							_UpperCamelCase) ->			Tuple:
      """simple docstring"""

      UpperCamelCase					       =  pt_tuple_key[:-1] + ('scale',)
      if (
          any('norm' in str_ for str_ in pt_tuple_key)
          and (pt_tuple_key[-1] == "bias")
          and (pt_tuple_key[:-1] + ("bias",) not in random_flax_state_dict)
          and (pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict)
      ):
            UpperCamelCase					       =  pt_tuple_key[:-1] + ('scale',)
            return renamed_pt_tuple_key, pt_tensor
      elif pt_tuple_key[-1] in ["weight", "gamma"] and pt_tuple_key[:-1] + ("scale",) in random_flax_state_dict:
            UpperCamelCase					       =  pt_tuple_key[:-1] + ('scale',)
            return renamed_pt_tuple_key, pt_tensor

      # embedding
      if pt_tuple_key[-1] == "weight" and pt_tuple_key[:-1] + ("embedding",) in random_flax_state_dict:
            UpperCamelCase					       =  pt_tuple_key[:-1] + ('embedding',)
            return renamed_pt_tuple_key, pt_tensor

      # conv layer
      UpperCamelCase					       =  pt_tuple_key[:-1] + ('kernel',)
      if pt_tuple_key[-1] == "weight" and pt_tensor.ndim == 4:
            UpperCamelCase					       =  pt_tensor.transpose(2       ,							3       ,							1       ,							0)
            return renamed_pt_tuple_key, pt_tensor

      # linear layer
      UpperCamelCase					       =  pt_tuple_key[:-1] + ('kernel',)
      if pt_tuple_key[-1] == "weight":
            UpperCamelCase					       =  pt_tensor.T
            return renamed_pt_tuple_key, pt_tensor

      # old PyTorch layer norm weight
      UpperCamelCase					       =  pt_tuple_key[:-1] + ('weight',)
      if pt_tuple_key[-1] == "gamma":
            return renamed_pt_tuple_key, pt_tensor

      # old PyTorch layer norm bias
      UpperCamelCase					       =  pt_tuple_key[:-1] + ('bias',)
      if pt_tuple_key[-1] == "beta":
            return renamed_pt_tuple_key, pt_tensor

      return pt_tuple_key, pt_tensor
def 		lowercase__  (				_UpperCamelCase       ,							_UpperCamelCase       ,							_UpperCamelCase=42) ->			Optional[Any]:
      """simple docstring"""

      UpperCamelCase					       =  {k: v.numpy() for k, v in pt_state_dict.items()}

      # Step 2: Since the model is stateless, get random Flax params
      UpperCamelCase					       =  flax_model.init_weights(PRNGKey(_UpperCamelCase))

      UpperCamelCase					       =  flatten_dict(_UpperCamelCase)
      UpperCamelCase					       =  {}

      # Need to change some parameters name to match Flax names
      for pt_key, pt_tensor in pt_state_dict.items():
            UpperCamelCase					       =  rename_key(_UpperCamelCase)
            UpperCamelCase					       =  tuple(renamed_pt_key.split('.'))

            # Correctly rename weight parameters
            UpperCamelCase       ,  UpperCamelCase					       =  rename_key_and_reshape_tensor(_UpperCamelCase       ,							_UpperCamelCase       ,							_UpperCamelCase)

            if flax_key in random_flax_state_dict:
                  if flax_tensor.shape != random_flax_state_dict[flax_key].shape:
                        raise ValueError(
                            F'PyTorch checkpoint seems to be incorrect. Weight {pt_key} was expected to be of shape '
                            F'{random_flax_state_dict[flax_key].shape}, but is {flax_tensor.shape}.')

        # also add unexpected weight so that warning is thrown
            UpperCamelCase					       =  jnp.asarray(_UpperCamelCase)

      return unflatten_dict(_UpperCamelCase)



