


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       : str			     =       """0.12"""  # assumed parallelism: 8

@require_flax
@is_staging_test
class      lowerCAmelCase							(      unittest.TestCase    ):
 @classmethod
 def   A_			(   cls						:   Dict			)    ->			Optional[Any]:
     lowerCamelCase__					:   int      				=	TOKEN
     HfFolder.save_token(__A			)
 @classmethod
 def   A_			(   cls						:   str			)    ->			List[Any]:



     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   A_			(   self						:   List[Any]			)    ->			Tuple:
     lowerCamelCase__					:   Optional[Any]      				=	BertConfig(
         vocab_size=99    ,  hidden_size=32    ,  num_hidden_layers=5    ,  num_attention_heads=4    ,  intermediate_size=37			)
     lowerCamelCase__					:   List[Any]      				=	FlaxBertModel(__A			)
     model.push_to_hub('test-model-flax'    ,  use_auth_token=self._token			)

     lowerCamelCase__					:   Union[str, Any]      				=	FlaxBertModel.from_pretrained(F"""{USER}/test-model-flax"""			)

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

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

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

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

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







 def   A_			(   self						:   Optional[Any]			)    ->			str:
     lowerCamelCase__					:   int      				=	BertConfig(
         vocab_size=99    ,  hidden_size=32    ,  num_hidden_layers=5    ,  num_attention_heads=4    ,  intermediate_size=37			)
     lowerCamelCase__					:   Optional[int]      				=	FlaxBertModel(__A			)
     model.push_to_hub('valid_org/test-model-flax-org'    ,  use_auth_token=self._token			)

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

     lowerCamelCase__					:   Optional[int]      				=	flatten_dict(unfreeze(model.params			)			)
     lowerCamelCase__					:   str      				=	flatten_dict(unfreeze(new_model.params			)			)

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

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

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

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


def  SCREAMING_SNAKE_CASE   ( _UpperCAmelCase		,			_UpperCAmelCase				)    ->  int:
    lowerCamelCase__					:   Union[str, Any]      				=	True
    lowerCamelCase__					:   Optional[int]      				=	flatten_dict(modela.params				)
    lowerCamelCase__					:   Optional[int]      				=	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:
            lowerCamelCase__					:   Tuple      				=	False

    return models_are_equal


@require_flax
class      lowerCAmelCase							(      unittest.TestCase    ):
 def   A_			(   self						:   List[str]			)    ->			Optional[int]:
     lowerCamelCase__					:   Tuple      				=	BertConfig.from_pretrained('hf-internal-testing/tiny-bert-flax-only'			)
     lowerCamelCase__					:   Tuple      				=	FlaxBertModel(__A			)

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

         with self.assertRaises(__A			):
             lowerCamelCase__					:   Dict      				=	FlaxBertModel.from_pretrained(__A			)

         lowerCamelCase__					:   Optional[int]      				=	FlaxBertModel.from_pretrained(__A    ,  subfolder=__A			)

     self.assertTrue(check_models_equal(__A    ,  __A			)			)
 def   A_			(   self						:   Union[str, Any]			)    ->			Dict:
     lowerCamelCase__					:   Optional[int]      				=	BertConfig.from_pretrained('hf-internal-testing/tiny-bert-flax-only'			)
     lowerCamelCase__					:   Union[str, Any]      				=	FlaxBertModel(__A			)

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

         with self.assertRaises(__A			):
             lowerCamelCase__					:   Dict      				=	FlaxBertModel.from_pretrained(__A			)

         lowerCamelCase__					:   Optional[int]      				=	FlaxBertModel.from_pretrained(__A    ,  subfolder=__A			)

     self.assertTrue(check_models_equal(__A    ,  __A			)			)
 def   A_			(   self						:   List[Any]			)    ->			str:
     lowerCamelCase__					:   Dict      				=	'bert'
     lowerCamelCase__					:   Dict      				=	'hf-internal-testing/tiny-random-bert-subfolder'

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

     lowerCamelCase__					:   Optional[Any]      				=	FlaxBertModel.from_pretrained(__A    ,  subfolder=__A			)

     self.assertIsNotNone(__A			)







 def   A_			(   self						:   List[str]			)    ->			Union[str, Any]:
     lowerCamelCase__					:   Optional[int]      				=	'bert'
     lowerCamelCase__					:   List[str]      				=	'hf-internal-testing/tiny-random-bert-sharded-subfolder'
     with self.assertRaises(__A			):
         lowerCamelCase__					:   int      				=	FlaxBertModel.from_pretrained(__A			)

     lowerCamelCase__					:   List[Any]      				=	FlaxBertModel.from_pretrained(__A    ,  subfolder=__A			)

     self.assertIsNotNone(__A			)












import shutil
import tempfile
import unittest

from transformers import ClapFeatureExtractor, ClapProcessor, RobertaTokenizer, RobertaTokenizerFast
from transformers.testing_utils import require_sentencepiece, require_torchaudio

from .test_feature_extraction_clap import floats_list

@require_torchaudio
@require_sentencepiece
class      lowerCAmelCase							(      unittest.TestCase    ):
 def   A_			(   self						:   str			)    ->			Union[str, Any]:
     lowerCamelCase__					:   Optional[int]      				=	'laion/clap-htsat-unfused'
     lowerCamelCase__					:   List[Any]      				=	tempfile.mkdtemp()
 def   A_			(   self						:   Optional[int]    ,  **UpperCAmelCase						:   int			)    ->			Optional[Any]:
     return RobertaTokenizer.from_pretrained(self.checkpoint    ,  **UpperCAmelCase			)
 def   A_			(   self						:   Union[str, Any]    ,  **UpperCAmelCase						:   Any			)    ->			List[str]:
     return ClapFeatureExtractor.from_pretrained(self.checkpoint    ,  **UpperCAmelCase			)
 def   A_			(   self						:   List[str]			)    ->			Dict:
     shutil.rmtree(self.tmpdirname			)
 def   A_			(   self						:   Optional[Any]			)    ->			Optional[Any]:
     lowerCamelCase__					:   Tuple      				=	self.get_tokenizer()
     lowerCamelCase__					:   Dict      				=	self.get_feature_extractor()

     lowerCamelCase__					:   Dict      				=	ClapProcessor(tokenizer=UpperCAmelCase    ,  feature_extractor=UpperCAmelCase			)

     processor.save_pretrained(self.tmpdirname			)
     lowerCamelCase__					:   List[Any]      				=	ClapProcessor.from_pretrained(self.tmpdirname			)

     self.assertEqual(processor.tokenizer.get_vocab()    ,  tokenizer.get_vocab()			)
     self.assertIsInstance(processor.tokenizer    ,  UpperCAmelCase			)

     self.assertEqual(processor.feature_extractor.to_json_string()    ,  feature_extractor.to_json_string()			)
     self.assertIsInstance(processor.feature_extractor    ,  UpperCAmelCase			)
 def   A_			(   self						:   str			)    ->			int:
     lowerCamelCase__					:   Optional[Any]      				=	ClapProcessor(tokenizer=self.get_tokenizer()    ,  feature_extractor=self.get_feature_extractor()			)
     processor.save_pretrained(self.tmpdirname			)

     lowerCamelCase__					:   Dict      				=	self.get_tokenizer(bos_token='(BOS)'    ,  eos_token='(EOS)'			)
     lowerCamelCase__					:   Dict      				=	self.get_feature_extractor(do_normalize=UpperCAmelCase    ,  padding_value=1.0			)

     lowerCamelCase__					:   str      				=	ClapProcessor.from_pretrained(
         self.tmpdirname    ,  bos_token='(BOS)'    ,  eos_token='(EOS)'    ,  do_normalize=UpperCAmelCase    ,  padding_value=1.0			)

     self.assertEqual(processor.tokenizer.get_vocab()    ,  tokenizer_add_kwargs.get_vocab()			)
     self.assertIsInstance(processor.tokenizer    ,  UpperCAmelCase			)

     self.assertEqual(processor.feature_extractor.to_json_string()    ,  feature_extractor_add_kwargs.to_json_string()			)
     self.assertIsInstance(processor.feature_extractor    ,  UpperCAmelCase			)
 def   A_			(   self						:   List[Any]			)    ->			int:
     lowerCamelCase__					:   Any      				=	self.get_feature_extractor()
     lowerCamelCase__					:   str      				=	self.get_tokenizer()

     lowerCamelCase__					:   Optional[Any]      				=	ClapProcessor(tokenizer=UpperCAmelCase    ,  feature_extractor=UpperCAmelCase			)

     lowerCamelCase__					:   Optional[Any]      				=	floats_list((3, 1000)			)

     lowerCamelCase__					:   Tuple      				=	feature_extractor(UpperCAmelCase    ,  return_tensors='np'			)
     lowerCamelCase__					:   Tuple      				=	processor(audios=UpperCAmelCase    ,  return_tensors='np'			)

     for key in input_feat_extract.keys():
         self.assertAlmostEqual(input_feat_extract[key].sum()    ,  input_processor[key].sum()    ,  delta=1e-2			)
 def   A_			(   self						:   List[str]			)    ->			Tuple:
     lowerCamelCase__					:   Dict      				=	self.get_feature_extractor()
     lowerCamelCase__					:   List[Any]      				=	self.get_tokenizer()

     lowerCamelCase__					:   Any      				=	ClapProcessor(tokenizer=UpperCAmelCase    ,  feature_extractor=UpperCAmelCase			)

     lowerCamelCase__					:   Union[str, Any]      				=	'This is a test string'

     lowerCamelCase__					:   Union[str, Any]      				=	processor(text=UpperCAmelCase			)

     lowerCamelCase__					:   Any      				=	tokenizer(UpperCAmelCase			)

     for key in encoded_tok.keys():
         self.assertListEqual(encoded_tok[key]    ,  encoded_processor[key]			)
 def   A_			(   self						:   Optional[int]			)    ->			Tuple:
     lowerCamelCase__					:   Tuple      				=	self.get_feature_extractor()
     lowerCamelCase__					:   List[str]      				=	self.get_tokenizer()

     lowerCamelCase__					:   Optional[int]      				=	ClapProcessor(tokenizer=UpperCAmelCase    ,  feature_extractor=UpperCAmelCase			)

     lowerCamelCase__					:   int      				=	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

     lowerCamelCase__					:   List[Any]      				=	processor.batch_decode(UpperCAmelCase			)
     lowerCamelCase__					:   Tuple      				=	tokenizer.batch_decode(UpperCAmelCase			)

     self.assertListEqual(UpperCAmelCase    ,  UpperCAmelCase			)







 def   A_			(   self						:   Optional[Any]			)    ->			int:
     lowerCamelCase__					:   str      				=	self.get_feature_extractor()
     lowerCamelCase__					:   str      				=	self.get_tokenizer()

     lowerCamelCase__					:   Dict      				=	ClapProcessor(tokenizer=UpperCAmelCase    ,  feature_extractor=UpperCAmelCase			)

     self.assertListEqual(
         processor.model_input_names[2:]    ,  feature_extractor.model_input_names    ,  msg='`processor` and `feature_extractor` model input names do not match'    ,  )





