




import tempfile
import unittest

import numpy as np
from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import BertConfig, is_flax_available
from transformers.testing_utils import TOKEN, USER, is_staging_test, require_flax


if is_flax_available():
    import os

    from flax.core.frozen_dict import unfreeze
    from flax.traverse_util import flatten_dict

    from transformers import FlaxBertModel

    snake_case_ : int    		=       "0.12"  # assumed parallelism: 8







@require_flax
@is_staging_test
class __snake_case			(			unittest.TestCase		):







   @classmethod
   def lowerCamelCase						(      cls		:						Union[str, Any]):

        """simple docstring"""


        UpperCAmelCase_          =							TOKEN
        HfFolder.save_token(_snake_case)







   @classmethod
   def lowerCamelCase						(      cls		:						Union[str, Any]):

        """simple docstring"""


        try:
             delete_repo(token=cls._token					,						repo_id='''test-model-flax''')
        except HTTPError:
             pass

        try:
             delete_repo(token=cls._token					,						repo_id='''valid_org/test-model-flax-org''')
        except HTTPError:
             pass







   def lowerCamelCase						(      self		:						List[str]):

        """simple docstring"""


        UpperCAmelCase_          =							BertConfig(
            vocab_size=99					,						hidden_size=32					,						num_hidden_layers=5					,						num_attention_heads=4					,						intermediate_size=37)
        UpperCAmelCase_          =							FlaxBertModel(_snake_case)
        model.push_to_hub('''test-model-flax'''					,						use_auth_token=self._token)

        UpperCAmelCase_          =							FlaxBertModel.from_pretrained(F"""{USER}/test-model-flax""")

        UpperCAmelCase_          =							flatten_dict(unfreeze(model.params))
        UpperCAmelCase_          =							flatten_dict(unfreeze(new_model.params))

        for key in base_params.keys():
             UpperCAmelCase_          =							(base_params[key] - new_params[key]).sum().item()
             self.assertLessEqual(_snake_case					,						1e-3					,						msg=F"""{key} not identical""")

        # Reset repo
        delete_repo(token=self._token					,						repo_id='''test-model-flax''')

        # Push to hub via save_pretrained
        with tempfile.TemporaryDirectory() as tmp_dir:
             model.save_pretrained(_snake_case					,						repo_id='''test-model-flax'''					,						push_to_hub=_snake_case					,						use_auth_token=self._token)

        UpperCAmelCase_          =							FlaxBertModel.from_pretrained(F"""{USER}/test-model-flax""")

        UpperCAmelCase_          =							flatten_dict(unfreeze(model.params))
        UpperCAmelCase_          =							flatten_dict(unfreeze(new_model.params))

        for key in base_params.keys():
             UpperCAmelCase_          =							(base_params[key] - new_params[key]).sum().item()
             self.assertLessEqual(_snake_case					,						1e-3					,						msg=F"""{key} not identical""")







   def lowerCamelCase						(      self		:						Union[str, Any]):

        """simple docstring"""


        UpperCAmelCase_          =							BertConfig(
            vocab_size=99					,						hidden_size=32					,						num_hidden_layers=5					,						num_attention_heads=4					,						intermediate_size=37)
        UpperCAmelCase_          =							FlaxBertModel(_snake_case)
        model.push_to_hub('''valid_org/test-model-flax-org'''					,						use_auth_token=self._token)

        UpperCAmelCase_          =							FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''')

        UpperCAmelCase_          =							flatten_dict(unfreeze(model.params))
        UpperCAmelCase_          =							flatten_dict(unfreeze(new_model.params))

        for key in base_params.keys():
             UpperCAmelCase_          =							(base_params[key] - new_params[key]).sum().item()
             self.assertLessEqual(_snake_case					,						1e-3					,						msg=F"""{key} not identical""")

        # Reset repo
        delete_repo(token=self._token					,						repo_id='''valid_org/test-model-flax-org''')

        # Push to hub via save_pretrained
        with tempfile.TemporaryDirectory() as tmp_dir:
             model.save_pretrained(
                 _snake_case					,						repo_id='''valid_org/test-model-flax-org'''					,						push_to_hub=_snake_case					,						use_auth_token=self._token)

        UpperCAmelCase_          =							FlaxBertModel.from_pretrained('''valid_org/test-model-flax-org''')

        UpperCAmelCase_          =							flatten_dict(unfreeze(model.params))
        UpperCAmelCase_          =							flatten_dict(unfreeze(new_model.params))

        for key in base_params.keys():
             UpperCAmelCase_          =							(base_params[key] - new_params[key]).sum().item()
             self.assertLessEqual(_snake_case					,						1e-3					,						msg=F"""{key} not identical""")







def    A						(__A   :    Dict   ,				__A   :    Dict       )   ->      int:







     """simple docstring"""


     UpperCAmelCase_          =							True
     UpperCAmelCase_          =							flatten_dict(modela.params       )
     UpperCAmelCase_          =							flatten_dict(modela.params       )
     for key in flat_params_a.keys():
          if np.sum(np.abs(flat_params_a[key] - flat_params_a[key]       )       ) > 1E-4:
               UpperCAmelCase_          =							False

     return models_are_equal







@require_flax
class __snake_case			(			unittest.TestCase		):







   def lowerCamelCase						(      self		:						Optional[int]):

        """simple docstring"""


        UpperCAmelCase_          =							BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''')
        UpperCAmelCase_          =							FlaxBertModel(_snake_case)

        UpperCAmelCase_          =							'''bert'''
        with tempfile.TemporaryDirectory() as tmp_dir:
             model.save_pretrained(os.path.join(_snake_case					,						_snake_case))

             with self.assertRaises(_snake_case):
                  UpperCAmelCase_          =							FlaxBertModel.from_pretrained(_snake_case)

             UpperCAmelCase_          =							FlaxBertModel.from_pretrained(_snake_case					,						subfolder=_snake_case)

        self.assertTrue(check_models_equal(_snake_case					,						_snake_case))







   def lowerCamelCase						(      self		:						int):

        """simple docstring"""


        UpperCAmelCase_          =							BertConfig.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''')
        UpperCAmelCase_          =							FlaxBertModel(_snake_case)

        UpperCAmelCase_          =							'''bert'''
        with tempfile.TemporaryDirectory() as tmp_dir:
             model.save_pretrained(os.path.join(_snake_case					,						_snake_case)					,						max_shard_size='''10KB''')

             with self.assertRaises(_snake_case):
                  UpperCAmelCase_          =							FlaxBertModel.from_pretrained(_snake_case)

             UpperCAmelCase_          =							FlaxBertModel.from_pretrained(_snake_case					,						subfolder=_snake_case)

        self.assertTrue(check_models_equal(_snake_case					,						_snake_case))







   def lowerCamelCase						(      self		:						List[Any]):

        """simple docstring"""


        UpperCAmelCase_          =							'''bert'''
        UpperCAmelCase_          =							'''hf-internal-testing/tiny-random-bert-subfolder'''

        with self.assertRaises(_snake_case):
             UpperCAmelCase_          =							FlaxBertModel.from_pretrained(_snake_case)

        UpperCAmelCase_          =							FlaxBertModel.from_pretrained(_snake_case					,						subfolder=_snake_case)

        self.assertIsNotNone(_snake_case)







   def lowerCamelCase						(      self		:						Optional[int]):

        """simple docstring"""


        UpperCAmelCase_          =							'''bert'''
        UpperCAmelCase_          =							'''hf-internal-testing/tiny-random-bert-sharded-subfolder'''
        with self.assertRaises(_snake_case):
             UpperCAmelCase_          =							FlaxBertModel.from_pretrained(_snake_case)

        UpperCAmelCase_          =							FlaxBertModel.from_pretrained(_snake_case					,						subfolder=_snake_case)

        self.assertIsNotNone(_snake_case)











import functools
import operator

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


_snake_case		=  logging.get_logger(__name__)

_snake_case		=  {
    "facebook/wav2vec2-base-960h": "https://huggingface.co/facebook/wav2vec2-base-960h/resolve/main/config.json",
    # See all Wav2Vec2 models at https://huggingface.co/models?filter=wav2vec2
}



class   UpperCAmelCase_    (       a):
							lowerCamelCase__   			=		'wav2vec2'







							def __init__(   self,			__a=32,			__a=768,			__a=12,			__a=12,			__a=3072,			__a="gelu",			__a=0.1,			__a=0.1,			__a=0.1,			__a=0.0,			__a=0.0,			__a=0.1,			__a=0.1,			__a=0.02,			__a=1E-5,			__a="group",			__a="gelu",			__a=(512, 512, 512, 512, 512, 512, 512),			__a=(5, 2, 2, 2, 2, 2, 2),			__a=(10, 3, 3, 3, 3, 2, 2),			__a=False,			__a=128,			__a=16,			__a=False,			__a=True,			__a=0.05,			__a=10,			__a=2,			__a=0.0,			__a=10,			__a=0,			__a=320,			__a=2,			__a=0.1,			__a=100,			__a=256,			__a=256,			__a=0.1,			__a="sum",			__a=False,			__a=False,			__a=256,			__a=(512, 512, 512, 512, 1500),			__a=(5, 3, 3, 1, 1),			__a=(1, 2, 3, 1, 1),			__a=512,			__a=0,			__a=1,			__a=2,			__a=False,			__a=3,			__a=2,			__a=3,			__a=None,			__a=None,			**__a,			):

														'''simple docstring'''

														super().__init__(**__a,			pad_token_id=__a,			bos_token_id=__a,			eos_token_id=__a)
														_lowerCAmelCase     :							str    =    hidden_size
														_lowerCAmelCase     :							Optional[int]    =    feat_extract_norm
														_lowerCAmelCase     :							Union[str, Any]    =    feat_extract_activation
														_lowerCAmelCase     :							Optional[Any]    =    list(__a)
														_lowerCAmelCase     :							List[str]    =    list(__a)
														_lowerCAmelCase     :							str    =    list(__a)
														_lowerCAmelCase     :							List[str]    =    conv_bias
														_lowerCAmelCase     :							str    =    num_conv_pos_embeddings
														_lowerCAmelCase     :							List[Any]    =    num_conv_pos_embedding_groups
														_lowerCAmelCase     :							str    =    len(self.conv_dim)
														_lowerCAmelCase     :							List[str]    =    num_hidden_layers
														_lowerCAmelCase     :							str    =    intermediate_size
														_lowerCAmelCase     :							Any    =    hidden_act
														_lowerCAmelCase     :							int    =    num_attention_heads
														_lowerCAmelCase     :							Optional[Any]    =    hidden_dropout
														_lowerCAmelCase     :							List[str]    =    attention_dropout
														_lowerCAmelCase     :							Tuple    =    activation_dropout
														_lowerCAmelCase     :							int    =    feat_proj_dropout
														_lowerCAmelCase     :							List[str]    =    final_dropout
														_lowerCAmelCase     :							int    =    layerdrop
														_lowerCAmelCase     :							int    =    layer_norm_eps
														_lowerCAmelCase     :							Union[str, Any]    =    initializer_range
														_lowerCAmelCase     :							str    =    vocab_size
														_lowerCAmelCase     :							Optional[Any]    =    do_stable_layer_norm
														_lowerCAmelCase     :							Any    =    use_weighted_layer_sum

														if (
														    (len(self.conv_stride) != self.num_feat_extract_layers)
														    or (len(self.conv_kernel) != self.num_feat_extract_layers)
														    or (len(self.conv_dim) != self.num_feat_extract_layers)
														):
																					raise ValueError(
																					    "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="
																					    " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="
																					    f" {len(self.conv_dim)}`, `len(config.conv_stride) = {len(self.conv_stride)}`,"
																					    f" `len(config.conv_kernel) = {len(self.conv_kernel)}`.")

														# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
														_lowerCAmelCase     :							str    =    apply_spec_augment
														_lowerCAmelCase     :							Optional[Any]    =    mask_time_prob
														_lowerCAmelCase     :							Optional[int]    =    mask_time_length
														_lowerCAmelCase     :							List[str]    =    mask_time_min_masks
														_lowerCAmelCase     :							Optional[int]    =    mask_feature_prob
														_lowerCAmelCase     :							Optional[int]    =    mask_feature_length
														_lowerCAmelCase     :							List[str]    =    mask_feature_min_masks

														# parameters for pretraining with codevector quantized representations
														_lowerCAmelCase     :							Union[str, Any]    =    num_codevectors_per_group
														_lowerCAmelCase     :							str    =    num_codevector_groups
														_lowerCAmelCase     :							Optional[int]    =    contrastive_logits_temperature
														_lowerCAmelCase     :							Optional[int]    =    feat_quantizer_dropout
														_lowerCAmelCase     :							Optional[int]    =    num_negatives
														_lowerCAmelCase     :							Union[str, Any]    =    codevector_dim
														_lowerCAmelCase     :							Any    =    proj_codevector_dim
														_lowerCAmelCase     :							Optional[int]    =    diversity_loss_weight

														# ctc loss
														_lowerCAmelCase     :							Tuple    =    ctc_loss_reduction
														_lowerCAmelCase     :							Tuple    =    ctc_zero_infinity

														# adapter
														_lowerCAmelCase     :							List[Any]    =    add_adapter
														_lowerCAmelCase     :							List[str]    =    adapter_kernel_size
														_lowerCAmelCase     :							str    =    adapter_stride
														_lowerCAmelCase     :							List[str]    =    num_adapter_layers
														_lowerCAmelCase     :							str    =    output_hidden_size or hidden_size
														_lowerCAmelCase     :							Tuple    =    adapter_attn_dim

														# SequenceClassification-specific parameter. Feel free to ignore for other classes.
														_lowerCAmelCase     :							str    =    classifier_proj_size

														# XVector-specific parameters. Feel free to ignore for other classes.
														_lowerCAmelCase     :							str    =    list(__a)
														_lowerCAmelCase     :							Union[str, Any]    =    list(__a)
														_lowerCAmelCase     :							List[str]    =    list(__a)
														_lowerCAmelCase     :							Tuple    =    xvector_output_dim







							@property
							def    snake_case__      (   self):

														'''simple docstring'''

														return functools.reduce(operator.mul,			self.conv_stride,			1)
