





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

      lowerCAmelCase_						   =    """0.12"""  # assumed parallelism: 8



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


  '''simple docstring'''







  @classmethod
  def UpperCamelCase_ (      cls      :					Dict							):


    '''simple docstring'''





    _snake_case   :				Union[str, Any]														=	TOKEN
    HfFolder.save_token(UpperCamelCase							)







  @classmethod
  def UpperCamelCase_ (      cls      :					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 UpperCamelCase_ (      self      :					int							):


    '''simple docstring'''





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

    _snake_case   :				str														=	FlaxBertModel.from_pretrained(f"""{USER}/test-model-flax"""							)

    _snake_case   :				List[str]														=	flatten_dict(unfreeze(model.params							)							)
    _snake_case   :				List[Any]														=	flatten_dict(unfreeze(new_model.params							)							)

    for key in base_params.keys():
      _snake_case   :				int														=	(base_params[key] - new_params[key]).sum().item()
      self.assertLessEqual(UpperCamelCase      ,				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(UpperCamelCase      ,				repo_id='test-model-flax'      ,				push_to_hub=UpperCamelCase      ,				use_auth_token=self._token							)

    _snake_case   :				str														=	FlaxBertModel.from_pretrained(f"""{USER}/test-model-flax"""							)

    _snake_case   :				Any														=	flatten_dict(unfreeze(model.params							)							)
    _snake_case   :				Optional[int]														=	flatten_dict(unfreeze(new_model.params							)							)

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







  def UpperCamelCase_ (      self      :					List[str]							):


    '''simple docstring'''





    _snake_case   :				Optional[Any]														=	BertConfig(
        vocab_size=99      ,				hidden_size=32      ,				num_hidden_layers=5      ,				num_attention_heads=4      ,				intermediate_size=37							)
    _snake_case   :				Union[str, Any]														=	FlaxBertModel(UpperCamelCase							)
    model.push_to_hub('valid_org/test-model-flax-org'      ,				use_auth_token=self._token							)

    _snake_case   :				int														=	FlaxBertModel.from_pretrained('valid_org/test-model-flax-org'							)

    _snake_case   :				Dict														=	flatten_dict(unfreeze(model.params							)							)
    _snake_case   :				List[str]														=	flatten_dict(unfreeze(new_model.params							)							)

    for key in base_params.keys():
      _snake_case   :				Any														=	(base_params[key] - new_params[key]).sum().item()
      self.assertLessEqual(UpperCamelCase      ,				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(
          UpperCamelCase      ,				repo_id='valid_org/test-model-flax-org'      ,				push_to_hub=UpperCamelCase      ,				use_auth_token=self._token							)

    _snake_case   :				Union[str, Any]														=	FlaxBertModel.from_pretrained('valid_org/test-model-flax-org'							)

    _snake_case   :				List[str]														=	flatten_dict(unfreeze(model.params							)							)
    _snake_case   :				Any														=	flatten_dict(unfreeze(new_model.params							)							)

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




def     lowerCamelCase_	(      lowerCAmelCase:	int    ,							lowerCAmelCase:	Optional[Any]				)->		Optional[int]:
  _snake_case   :				List[Any]														=	True
  _snake_case   :				Union[str, Any]														=	flatten_dict(modela.params				)
  _snake_case   :				str														=	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:
      _snake_case   :				int														=	False

  return models_are_equal







@require_flax
class        _lowerCAmelCase    (			unittest.TestCase							):


  '''simple docstring'''







  def UpperCamelCase_ (      self      :					Tuple							):


    '''simple docstring'''





    _snake_case   :				int														=	BertConfig.from_pretrained('hf-internal-testing/tiny-bert-flax-only'							)
    _snake_case   :				Tuple														=	FlaxBertModel(UpperCamelCase							)

    _snake_case   :				List[Any]														=	'bert'
    with tempfile.TemporaryDirectory() as tmp_dir:
      model.save_pretrained(os.path.join(UpperCamelCase      ,				UpperCamelCase							)							)

      with self.assertRaises(UpperCamelCase							):
        _snake_case   :				Any														=	FlaxBertModel.from_pretrained(UpperCamelCase							)

      _snake_case   :				List[str]														=	FlaxBertModel.from_pretrained(UpperCamelCase      ,				subfolder=UpperCamelCase							)

    self.assertTrue(check_models_equal(UpperCamelCase      ,				UpperCamelCase							)							)







  def UpperCamelCase_ (      self      :					Dict							):


    '''simple docstring'''





    _snake_case   :				List[str]														=	BertConfig.from_pretrained('hf-internal-testing/tiny-bert-flax-only'							)
    _snake_case   :				int														=	FlaxBertModel(UpperCamelCase							)

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

      with self.assertRaises(UpperCamelCase							):
        _snake_case   :				List[Any]														=	FlaxBertModel.from_pretrained(UpperCamelCase							)

      _snake_case   :				Any														=	FlaxBertModel.from_pretrained(UpperCamelCase      ,				subfolder=UpperCamelCase							)

    self.assertTrue(check_models_equal(UpperCamelCase      ,				UpperCamelCase							)							)







  def UpperCamelCase_ (      self      :					List[str]							):


    '''simple docstring'''





    _snake_case   :				Any														=	'bert'
    _snake_case   :				Any														=	'hf-internal-testing/tiny-random-bert-subfolder'

    with self.assertRaises(UpperCamelCase							):
      _snake_case   :				List[Any]														=	FlaxBertModel.from_pretrained(UpperCamelCase							)

    _snake_case   :				Optional[Any]														=	FlaxBertModel.from_pretrained(UpperCamelCase      ,				subfolder=UpperCamelCase							)

    self.assertIsNotNone(UpperCamelCase							)







  def UpperCamelCase_ (      self      :					int							):


    '''simple docstring'''





    _snake_case   :				List[Any]														=	'bert'
    _snake_case   :				Tuple														=	'hf-internal-testing/tiny-random-bert-sharded-subfolder'
    with self.assertRaises(UpperCamelCase							):
      _snake_case   :				List[str]														=	FlaxBertModel.from_pretrained(UpperCamelCase							)

    _snake_case   :				Tuple														=	FlaxBertModel.from_pretrained(UpperCamelCase      ,				subfolder=UpperCamelCase							)

    self.assertIsNotNone(UpperCamelCase							)











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


lowerCAmelCase_						   =    logging.get_logger(__name__)

lowerCAmelCase_						   =    {
    """MIT/ast-finetuned-audioset-10-10-0.4593""": (
        """https://huggingface.co/MIT/ast-finetuned-audioset-10-10-0.4593/resolve/main/config.json"""
    ),
}



class        _lowerCAmelCase    (			UpperCAmelCase_							):


  '''simple docstring'''
  a_							:       Tuple      					="""audio-spectrogram-transformer"""







  def __init__(      self      :					List[Any]      ,				UpperCamelCase      :					Union[str, Any]=7_68      ,				UpperCamelCase      :					int=12      ,				UpperCamelCase      :					str=12      ,				UpperCamelCase      :					Tuple=30_72      ,				UpperCamelCase      :					Optional[Any]="gelu"      ,				UpperCamelCase      :					Any=0.0      ,				UpperCamelCase      :					Dict=0.0      ,				UpperCamelCase      :					List[Any]=0.02      ,				UpperCamelCase      :					Dict=1e-1_2      ,				UpperCamelCase      :					str=16      ,				UpperCamelCase      :					List[Any]=True      ,				UpperCamelCase      :					Any=10      ,				UpperCamelCase      :					Optional[int]=10      ,				UpperCamelCase      :					int=10_24      ,				UpperCamelCase      :					Optional[Any]=1_28      ,				**UpperCamelCase      :					Optional[Any]      ,				):


    '''simple docstring'''





    super().__init__(**UpperCamelCase							)

    _snake_case   :				Tuple														=	hidden_size
    _snake_case   :				str														=	num_hidden_layers
    _snake_case   :				Optional[Any]														=	num_attention_heads
    _snake_case   :				Optional[Any]														=	intermediate_size
    _snake_case   :				Optional[Any]														=	hidden_act
    _snake_case   :				List[str]														=	hidden_dropout_prob
    _snake_case   :				Union[str, Any]														=	attention_probs_dropout_prob
    _snake_case   :				Any														=	initializer_range
    _snake_case   :				List[str]														=	layer_norm_eps
    _snake_case   :				int														=	patch_size
    _snake_case   :				List[str]														=	qkv_bias
    _snake_case   :				int														=	frequency_stride
    _snake_case   :				List[Any]														=	time_stride
    _snake_case   :				List[Any]														=	max_length
    _snake_case   :				List[str]														=	num_mel_bins




