






"""simple docstring"""







import random
import shutil
import tempfile
import unittest

import numpy as np

from transformers import TaTokenizer, TaTokenizerFast
from transformers.testing_utils import require_sentencepiece, require_torch
from transformers.utils.import_utils import is_speech_available, is_torch_available


if is_torch_available():
 pass

if is_speech_available():
 from transformers import EncodecFeatureExtractor, MusicgenProcessor


lowerCAmelCase_		:			Any				   =					random.Random()






def 					_lowerCAmelCase				(					lowerCAmelCase		,					lowerCAmelCase=1.0		,					lowerCAmelCase=None		,					lowerCAmelCase=None						):







       '''simple docstring'''
       if rng is None:
              UpperCAmelCase					=	global_rng

       UpperCAmelCase					=	[]
       for batch_idx in range(shape[0]						):
              values.append([]						)
              for _ in range(shape[1]						):
                     values[-1].append(rng.random() * scale						)

       return values





@require_torch
@require_sentencepiece
class      UpperCamelCase_       (				unittest.TestCase     ):



  def 						UpperCamelCase_ (					self )	->	str:



         """simple docstring"""





         UpperCAmelCase					=	"""facebook/musicgen-small"""
         UpperCAmelCase					=	tempfile.mkdtemp()



  def 						UpperCamelCase_ (					self       ,						**snake_case__ )	->	Optional[int]:



         """simple docstring"""





         return TaTokenizer.from_pretrained(self.checkpoint       ,						**snake_case__ )



  def 						UpperCamelCase_ (					self       ,						**snake_case__ )	->	Optional[int]:



         """simple docstring"""





         return EncodecFeatureExtractor.from_pretrained(self.checkpoint       ,						**snake_case__ )



  def 						UpperCamelCase_ (					self )	->	int:



         """simple docstring"""





         shutil.rmtree(self.tmpdirname )



  def 						UpperCamelCase_ (					self )	->	Any:



         """simple docstring"""





         UpperCAmelCase					=	self.get_tokenizer()
         UpperCAmelCase					=	self.get_feature_extractor()

         UpperCAmelCase					=	MusicgenProcessor(tokenizer=snake_case__       ,						feature_extractor=snake_case__ )

         processor.save_pretrained(self.tmpdirname )
         UpperCAmelCase					=	MusicgenProcessor.from_pretrained(self.tmpdirname )

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

         self.assertEqual(processor.feature_extractor.to_json_string()       ,						feature_extractor.to_json_string() )
         self.assertIsInstance(processor.feature_extractor       ,						snake_case__ )



  def 						UpperCamelCase_ (					self )	->	List[Any]:



         """simple docstring"""





         UpperCAmelCase					=	MusicgenProcessor(tokenizer=self.get_tokenizer()       ,						feature_extractor=self.get_feature_extractor() )
         processor.save_pretrained(self.tmpdirname )

         UpperCAmelCase					=	self.get_tokenizer(bos_token="""(BOS)"""       ,						eos_token="""(EOS)""" )
         UpperCAmelCase					=	self.get_feature_extractor(do_normalize=snake_case__       ,						padding_value=1.0 )

         UpperCAmelCase					=	MusicgenProcessor.from_pretrained(
             self.tmpdirname       ,						bos_token="""(BOS)"""       ,						eos_token="""(EOS)"""       ,						do_normalize=snake_case__       ,						padding_value=1.0 )

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

         self.assertEqual(processor.feature_extractor.to_json_string()       ,						feature_extractor_add_kwargs.to_json_string() )
         self.assertIsInstance(processor.feature_extractor       ,						snake_case__ )



  def 						UpperCamelCase_ (					self )	->	Any:



         """simple docstring"""





         UpperCAmelCase					=	self.get_feature_extractor()
         UpperCAmelCase					=	self.get_tokenizer()

         UpperCAmelCase					=	MusicgenProcessor(tokenizer=snake_case__       ,						feature_extractor=snake_case__ )

         UpperCAmelCase					=	floats_list((3, 10_00) )

         UpperCAmelCase					=	feature_extractor(snake_case__       ,						return_tensors="""np""" )
         UpperCAmelCase					=	processor(snake_case__       ,						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 						UpperCamelCase_ (					self )	->	Any:



         """simple docstring"""





         UpperCAmelCase					=	self.get_feature_extractor()
         UpperCAmelCase					=	self.get_tokenizer()

         UpperCAmelCase					=	MusicgenProcessor(tokenizer=snake_case__       ,						feature_extractor=snake_case__ )

         UpperCAmelCase					=	"""This is a test string"""

         UpperCAmelCase					=	processor(text=snake_case__ )

         UpperCAmelCase					=	tokenizer(snake_case__ )

         for key in encoded_tok.keys():
                self.assertListEqual(encoded_tok[key]       ,						encoded_processor[key] )



  def 						UpperCamelCase_ (					self )	->	Optional[int]:



         """simple docstring"""





         UpperCAmelCase					=	self.get_feature_extractor()
         UpperCAmelCase					=	self.get_tokenizer()

         UpperCAmelCase					=	MusicgenProcessor(tokenizer=snake_case__       ,						feature_extractor=snake_case__ )

         UpperCAmelCase					=	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

         UpperCAmelCase					=	processor.batch_decode(sequences=snake_case__ )
         UpperCAmelCase					=	tokenizer.batch_decode(snake_case__ )

         self.assertListEqual(snake_case__       ,						snake_case__ )



  def 						UpperCamelCase_ (					self )	->	str:



         """simple docstring"""





         UpperCAmelCase					=	self.get_feature_extractor()
         UpperCAmelCase					=	self.get_tokenizer()

         UpperCAmelCase					=	MusicgenProcessor(tokenizer=snake_case__       ,						feature_extractor=snake_case__ )

         self.assertListEqual(
             processor.model_input_names       ,						feature_extractor.model_input_names       ,						msg="""`processor` and `feature_extractor` model input names do not match"""       ,						)



  def 						UpperCamelCase_ (					self )	->	List[Any]:



         """simple docstring"""





         UpperCAmelCase					=	self.get_feature_extractor(padding_side="""left""" )
         UpperCAmelCase					=	self.get_tokenizer()

         UpperCAmelCase					=	MusicgenProcessor(tokenizer=snake_case__       ,						feature_extractor=snake_case__ )

         UpperCAmelCase					=	[floats_list((1, x) )[0] for x in range(5       ,						20       ,						5 )]
         UpperCAmelCase					=	processor(snake_case__ ).padding_mask

         UpperCAmelCase					=	np.asarray(floats_list((3, 20) ) )[:, None, :]
         UpperCAmelCase					=	processor.batch_decode(snake_case__       ,						padding_mask=snake_case__ )

         self.assertIsInstance(snake_case__       ,						snake_case__ )

         for audio in decoded_audios:
                self.assertIsInstance(snake_case__       ,						np.ndarray )

         self.assertTrue(decoded_audios[0].shape == (1, 10) )
         self.assertTrue(decoded_audios[1].shape == (1, 15) )
         self.assertTrue(decoded_audios[2].shape == (1, 20) )








"""simple docstring"""







import unittest

import numpy as np

from transformers import ElectraConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
 from transformers.models.electra.modeling_flax_electra import (
     FlaxElectraForCausalLM,
     FlaxElectraForMaskedLM,
     FlaxElectraForMultipleChoice,
     FlaxElectraForPreTraining,
     FlaxElectraForQuestionAnswering,
     FlaxElectraForSequenceClassification,
     FlaxElectraForTokenClassification,
     FlaxElectraModel,
 )





class      UpperCamelCase_       (				unittest.TestCase     ):



  def __init__(					self       ,						snake_case__       ,						snake_case__=13       ,						snake_case__=7       ,						snake_case__=True       ,						snake_case__=True       ,						snake_case__=True       ,						snake_case__=True       ,						snake_case__=99       ,						snake_case__=24       ,						snake_case__=32       ,						snake_case__=5       ,						snake_case__=4       ,						snake_case__=37       ,						snake_case__="gelu"       ,						snake_case__=0.1       ,						snake_case__=0.1       ,						snake_case__=5_12       ,						snake_case__=16       ,						snake_case__=2       ,						snake_case__=0.02       ,						snake_case__=4       ,						)	->	int:



         """simple docstring"""





         UpperCAmelCase					=	parent
         UpperCAmelCase					=	batch_size
         UpperCAmelCase					=	seq_length
         UpperCAmelCase					=	is_training
         UpperCAmelCase					=	use_attention_mask
         UpperCAmelCase					=	use_token_type_ids
         UpperCAmelCase					=	use_labels
         UpperCAmelCase					=	vocab_size
         UpperCAmelCase					=	hidden_size
         UpperCAmelCase					=	embedding_size
         UpperCAmelCase					=	num_hidden_layers
         UpperCAmelCase					=	num_attention_heads
         UpperCAmelCase					=	intermediate_size
         UpperCAmelCase					=	hidden_act
         UpperCAmelCase					=	hidden_dropout_prob
         UpperCAmelCase					=	attention_probs_dropout_prob
         UpperCAmelCase					=	max_position_embeddings
         UpperCAmelCase					=	type_vocab_size
         UpperCAmelCase					=	type_sequence_label_size
         UpperCAmelCase					=	initializer_range
         UpperCAmelCase					=	num_choices



  def 						UpperCamelCase_ (					self )	->	Optional[Any]:



         """simple docstring"""





         UpperCAmelCase					=	ids_tensor([self.batch_size, self.seq_length]       ,						self.vocab_size )

         UpperCAmelCase					=	None
         if self.use_attention_mask:
                UpperCAmelCase					=	random_attention_mask([self.batch_size, self.seq_length] )

         UpperCAmelCase					=	None
         if self.use_token_type_ids:
                UpperCAmelCase					=	ids_tensor([self.batch_size, self.seq_length]       ,						self.type_vocab_size )

         UpperCAmelCase					=	ElectraConfig(
             vocab_size=self.vocab_size       ,						hidden_size=self.hidden_size       ,						embedding_size=self.embedding_size       ,						num_hidden_layers=self.num_hidden_layers       ,						num_attention_heads=self.num_attention_heads       ,						intermediate_size=self.intermediate_size       ,						hidden_act=self.hidden_act       ,						hidden_dropout_prob=self.hidden_dropout_prob       ,						attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,						max_position_embeddings=self.max_position_embeddings       ,						type_vocab_size=self.type_vocab_size       ,						initializer_range=self.initializer_range       ,						)

         return config, input_ids, token_type_ids, attention_mask



  def 						UpperCamelCase_ (					self )	->	List[str]:



         """simple docstring"""





         UpperCAmelCase					=	self.prepare_config_and_inputs()
         UpperCAmelCase			,				UpperCAmelCase			,				UpperCAmelCase			,				UpperCAmelCase					=	config_and_inputs
         UpperCAmelCase					=	{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": attention_mask}
         return config, inputs_dict





@require_flax
class      UpperCamelCase_       (				a_      ,    unittest.TestCase     ):
  _A				:    str =		True

  _A				:    List[Any] =		(
      (
          FlaxElectraModel,
          FlaxElectraForCausalLM,
          FlaxElectraForMaskedLM,
          FlaxElectraForPreTraining,
          FlaxElectraForTokenClassification,
          FlaxElectraForQuestionAnswering,
          FlaxElectraForMultipleChoice,
          FlaxElectraForSequenceClassification,
      )
      if is_flax_available()
      else ()
  )



  def 						UpperCamelCase_ (					self )	->	str:



         """simple docstring"""





         UpperCAmelCase					=	FlaxElectraModelTester(self )



  @slow
  def 						UpperCamelCase_ (					self )	->	Tuple:



         """simple docstring"""





         for model_class_name in self.all_model_classes:
                if model_class_name == FlaxElectraForMaskedLM:
                       UpperCAmelCase					=	model_class_name.from_pretrained("""google/electra-small-generator""" )
                else:
                       UpperCAmelCase					=	model_class_name.from_pretrained("""google/electra-small-discriminator""" )
                UpperCAmelCase					=	model(np.ones((1, 1) ) )
                self.assertIsNotNone(snake_case__ )
