


'''simple docstring'''




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

 UpperCAmelCase_      :		Optional[int]          =		"0.12"  # assumed parallelism: 8







@require_flax
@is_staging_test
class 					a			(				unittest.TestCase  ):
 '''simple docstring'''




 @classmethod
 def   __UpperCamelCase   (    cls      )      ->    Dict:
    _a					:  str						=  TOKEN
    HfFolder.save_token(lowerCamelCase_      )




 @classmethod
 def   __UpperCamelCase   (    cls      )      ->    Tuple:
    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      )      ->    Any:
    _a					:  List[str]						=  BertConfig(
        vocab_size=9_9		,      hidden_size=3_2		,      num_hidden_layers=5		,      num_attention_heads=4		,      intermediate_size=3_7      )
    _a					:  int						=  FlaxBertModel(lowerCamelCase_      )
    model.push_to_hub('test-model-flax'		,      use_auth_token=self._token      )

    _a					:  Tuple						=  FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax'''      )

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

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

    _a					:  Any						=  FlaxBertModel.from_pretrained(F'''{USER}/test-model-flax'''      )

    _a					:  Tuple						=  flatten_dict(unfreeze(model.params      )      )
    _a					:  str						=  flatten_dict(unfreeze(new_model.params      )      )

    for key in base_params.keys():
       _a					:  Optional[Any]						=  (base_params[key] - new_params[key]).sum().item()
       self.assertLessEqual(lowerCamelCase_		,      1e-3		,      msg=F'''{key} not identical'''      )




 def   __UpperCamelCase   (    self      )      ->    int:
    _a					:  Optional[int]						=  BertConfig(
        vocab_size=9_9		,      hidden_size=3_2		,      num_hidden_layers=5		,      num_attention_heads=4		,      intermediate_size=3_7      )
    _a					:  Optional[int]						=  FlaxBertModel(lowerCamelCase_      )
    model.push_to_hub('valid_org/test-model-flax-org'		,      use_auth_token=self._token      )

    _a					:  List[str]						=  FlaxBertModel.from_pretrained('valid_org/test-model-flax-org'      )

    _a					:  str						=  flatten_dict(unfreeze(model.params      )      )
    _a					:  int						=  flatten_dict(unfreeze(new_model.params      )      )

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

    _a					:  Tuple						=  FlaxBertModel.from_pretrained('valid_org/test-model-flax-org'      )

    _a					:  Dict						=  flatten_dict(unfreeze(model.params      )      )
    _a					:  Dict						=  flatten_dict(unfreeze(new_model.params      )      )

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





def    UpperCAmelCase_  (       A		,	A      ):
   '''simple docstring'''





   _a					:  List[Any]						=  True
   _a					:  Tuple						=  flatten_dict(modela.params      )
   _a					:  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:
         _a					:  int						=  False

   return models_are_equal







@require_flax
class 					a			(				unittest.TestCase  ):
 '''simple docstring'''




 def   __UpperCamelCase   (    self      )      ->    int:
    _a					:  Dict						=  BertConfig.from_pretrained('hf-internal-testing/tiny-bert-flax-only'      )
    _a					:  List[Any]						=  FlaxBertModel(lowerCamelCase_      )

    _a					:  Optional[int]						=  'bert'
    with tempfile.TemporaryDirectory() as tmp_dir:
       model.save_pretrained(os.path.join(lowerCamelCase_		,      lowerCamelCase_      )      )

       with self.assertRaises(lowerCamelCase_      ):
          _a					:  Any						=  FlaxBertModel.from_pretrained(lowerCamelCase_      )

       _a					:  Dict						=  FlaxBertModel.from_pretrained(lowerCamelCase_		,      subfolder=lowerCamelCase_      )

    self.assertTrue(check_models_equal(lowerCamelCase_		,      lowerCamelCase_      )      )




 def   __UpperCamelCase   (    self      )      ->    int:
    _a					:  Tuple						=  BertConfig.from_pretrained('hf-internal-testing/tiny-bert-flax-only'      )
    _a					:  str						=  FlaxBertModel(lowerCamelCase_      )

    _a					:  List[Any]						=  'bert'
    with tempfile.TemporaryDirectory() as tmp_dir:
       model.save_pretrained(os.path.join(lowerCamelCase_		,      lowerCamelCase_      )		,      max_shard_size='10KB'      )

       with self.assertRaises(lowerCamelCase_      ):
          _a					:  Tuple						=  FlaxBertModel.from_pretrained(lowerCamelCase_      )

       _a					:  int						=  FlaxBertModel.from_pretrained(lowerCamelCase_		,      subfolder=lowerCamelCase_      )

    self.assertTrue(check_models_equal(lowerCamelCase_		,      lowerCamelCase_      )      )




 def   __UpperCamelCase   (    self      )      ->    Dict:
    _a					:  Union[str, Any]						=  'bert'
    _a					:  Dict						=  'hf-internal-testing/tiny-random-bert-subfolder'

    with self.assertRaises(lowerCamelCase_      ):
       _a					:  Union[str, Any]						=  FlaxBertModel.from_pretrained(lowerCamelCase_      )

    _a					:  Tuple						=  FlaxBertModel.from_pretrained(lowerCamelCase_		,      subfolder=lowerCamelCase_      )

    self.assertIsNotNone(lowerCamelCase_      )




 def   __UpperCamelCase   (    self      )      ->    Any:
    _a					:  str						=  'bert'
    _a					:  Dict						=  'hf-internal-testing/tiny-random-bert-sharded-subfolder'
    with self.assertRaises(lowerCamelCase_      ):
       _a					:  str						=  FlaxBertModel.from_pretrained(lowerCamelCase_      )

    _a					:  Tuple						=  FlaxBertModel.from_pretrained(lowerCamelCase_		,      subfolder=lowerCamelCase_      )

    self.assertIsNotNone(lowerCamelCase_      )






'''simple docstring'''




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


UpperCAmelCase_      :		Optional[int]          =		logging.get_logger(__name__)

UpperCAmelCase_      :		List[str]          =		{
    "google/vivit-b-16x2-kinetics400": (
        "https://huggingface.co/google/vivit-b-16x2-kinetics400/resolve/main/config.json"
    ),
    # See all Vivit models at https://huggingface.co/models?filter=vivit
}







class 					a			(				snake_case__  ):
 '''simple docstring'''




 __lowerCAmelCase						: str							  =		"""vivit"""




 def __init__(    self		,      lowerCamelCase_=2_2_4		,      lowerCamelCase_=3_2		,      lowerCamelCase_=[2, 1_6, 1_6]		,      lowerCamelCase_=3		,      lowerCamelCase_=7_6_8		,      lowerCamelCase_=1_2		,      lowerCamelCase_=1_2		,      lowerCamelCase_=3_0_7_2		,      lowerCamelCase_="gelu_fast"		,      lowerCamelCase_=0.0		,      lowerCamelCase_=0.0		,      lowerCamelCase_=0.02		,      lowerCamelCase_=1e-06		,      lowerCamelCase_=True		,      **lowerCamelCase_		,      )      ->    int:
    _a					:  Tuple						=  hidden_size
    _a					:  Dict						=  num_hidden_layers
    _a					:  List[str]						=  num_attention_heads
    _a					:  int						=  intermediate_size
    _a					:  Optional[int]						=  hidden_act
    _a					:  Dict						=  hidden_dropout_prob
    _a					:  List[str]						=  attention_probs_dropout_prob
    _a					:  List[str]						=  initializer_range
    _a					:  List[str]						=  layer_norm_eps

    _a					:  Any						=  image_size
    _a					:  Optional[Any]						=  num_frames
    _a					:  Dict						=  tubelet_size
    _a					:  Union[str, Any]						=  num_channels
    _a					:  Optional[int]						=  qkv_bias

    super().__init__(**lowerCamelCase_      )


