




"""simple docstring"""





import unittest

from cer import CER


_lowercase						   =     CER()

class    __lowercase							(      unittest.TestCase						):


				'''simple docstring'''


				def    _lowerCamelCase       ( self :  int   ):
										snake_case__							:       List[Any]				      =       ['''White House''']
										snake_case__							:       Any				      =       ['''white house''']
										# S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
										snake_case__							:       List[str]				      =       cer.compute(predictions=SCREAMING_SNAKE_CASE_   ,references=SCREAMING_SNAKE_CASE_   )
										self.assertTrue(abs(char_error_rate - 0.1818181818   ) < 1E-6   )

				def    _lowerCamelCase       ( self :  Any   ):
										snake_case__							:       str				      =       ['''were wolf''']
										snake_case__							:       Union[str, Any]				      =       ['''werewolf''']
										# S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
										snake_case__							:       Union[str, Any]				      =       cer.compute(predictions=SCREAMING_SNAKE_CASE_   ,references=SCREAMING_SNAKE_CASE_   )
										self.assertTrue(abs(char_error_rate - 0.1111111   ) < 1E-6   )

										snake_case__							:       Union[str, Any]				      =       ['''werewolf''']
										snake_case__							:       List[str]				      =       ['''weae     wolf''']
										# S = 1, D = 1, I = 0, N = 8, CER = 0.25
										snake_case__							:       Optional[int]				      =       cer.compute(predictions=SCREAMING_SNAKE_CASE_   ,references=SCREAMING_SNAKE_CASE_   )
										self.assertTrue(abs(char_error_rate - 0.25   ) < 1E-6   )

										# consecutive whitespaces case 1
										snake_case__							:       List[Any]				      =       ['''were wolf''']
										snake_case__							:       Dict				      =       ['''were               wolf''']
										# S = 0, D = 0, I = 0, N = 9, CER = 0
										snake_case__							:       Optional[Any]				      =       cer.compute(predictions=SCREAMING_SNAKE_CASE_   ,references=SCREAMING_SNAKE_CASE_   )
										self.assertTrue(abs(char_error_rate - 0.0   ) < 1E-6   )

										# consecutive whitespaces case 2
										snake_case__							:       List[Any]				      =       ['''were   wolf''']
										snake_case__							:       List[Any]				      =       ['''were               wolf''']
										# S = 0, D = 0, I = 0, N = 9, CER = 0
										snake_case__							:       List[str]				      =       cer.compute(predictions=SCREAMING_SNAKE_CASE_   ,references=SCREAMING_SNAKE_CASE_   )
										self.assertTrue(abs(char_error_rate - 0.0   ) < 1E-6   )

				def    _lowerCamelCase       ( self :  Tuple   ):
										snake_case__							:       Union[str, Any]				      =       ['''werewolf''']
										snake_case__							:       List[str]				      =       ['''weaewolf''']
										# S = 1, D = 0, I = 0, N = 8, CER = 0.125
										snake_case__							:       Tuple				      =       cer.compute(predictions=SCREAMING_SNAKE_CASE_   ,references=SCREAMING_SNAKE_CASE_   )
										self.assertTrue(abs(char_error_rate - 0.125   ) < 1E-6   )

				def    _lowerCamelCase       ( self :  str   ):
										snake_case__							:       Tuple				      =       ['''werewolf''']
										snake_case__							:       Optional[Any]				      =       ['''wereawolf''']
										# S = 0, D = 1, I = 0, N = 8, CER = 0.125
										snake_case__							:       Any				      =       cer.compute(predictions=SCREAMING_SNAKE_CASE_   ,references=SCREAMING_SNAKE_CASE_   )
										self.assertTrue(abs(char_error_rate - 0.125   ) < 1E-6   )

				def    _lowerCamelCase       ( self :  List[str]   ):
										snake_case__							:       List[Any]				      =       ['''werewolf''']
										snake_case__							:       Optional[int]				      =       ['''wereolf''']
										# S = 0, D = 0, I = 1, N = 8, CER = 0.125
										snake_case__							:       int				      =       cer.compute(predictions=SCREAMING_SNAKE_CASE_   ,references=SCREAMING_SNAKE_CASE_   )
										self.assertTrue(abs(char_error_rate - 0.125   ) < 1E-6   )

				def    _lowerCamelCase       ( self :  Optional[Any]   ):
										snake_case__							:       Any				      =       ['''werewolf''']
										snake_case__							:       int				      =       cer.compute(predictions=SCREAMING_SNAKE_CASE_   ,references=SCREAMING_SNAKE_CASE_   )
										self.assertEqual(SCREAMING_SNAKE_CASE_   ,0.0   )

				def    _lowerCamelCase       ( self :  Optional[Any]   ):
										snake_case__							:       int				      =       ['''werewolf''', '''I am your father''']
										snake_case__							:       Any				      =       cer.compute(predictions=SCREAMING_SNAKE_CASE_   ,references=SCREAMING_SNAKE_CASE_   )
										self.assertEqual(SCREAMING_SNAKE_CASE_   ,0.0   )

										snake_case__							:       Optional[Any]				      =       ['''werewolf''', '''I am your father''', '''doge''']
										snake_case__							:       Optional[Any]				      =       ['''werxwolf''', '''I       am your father''', '''doge''']
										# S = 1, D = 0, I = 0, N = 28, CER = 1 / 28
										snake_case__							:       Optional[Any]				      =       cer.compute(predictions=SCREAMING_SNAKE_CASE_   ,references=SCREAMING_SNAKE_CASE_   )
										self.assertTrue(abs(char_error_rate - 0.03571428   ) < 1E-6   )

				def    _lowerCamelCase       ( self :  int   ):
										snake_case__							:       Optional[int]				      =       ['''My hovercraft''', '''is full of eels''']
										snake_case__							:       List[str]				      =       ['''My hovercraft is full''', ''' of eels''']
										# S = 0, D = 0, I = 2, N = 28, CER = 2 / 28
										# whitespace at the front of " of eels" will be strip during preporcessing
										# so need to insert 2 whitespaces
										snake_case__							:       Optional[Any]				      =       cer.compute(predictions=SCREAMING_SNAKE_CASE_   ,references=SCREAMING_SNAKE_CASE_   ,concatenate_texts=SCREAMING_SNAKE_CASE_   )
										self.assertTrue(abs(char_error_rate - 0.071428   ) < 1E-6   )

				def    _lowerCamelCase       ( self :  Optional[int]   ):
										snake_case__							:       List[Any]				      =       ['''我能吞下玻璃而不伤身体''']
										snake_case__							:       int				      =       [''' 能吞虾玻璃而 不霜身体啦''']
										# S = 3, D = 2, I = 0, N = 11, CER = 5 / 11
										snake_case__							:       Any				      =       cer.compute(predictions=SCREAMING_SNAKE_CASE_   ,references=SCREAMING_SNAKE_CASE_   )
										self.assertTrue(abs(char_error_rate - 0.4545454545   ) < 1E-6   )

										snake_case__							:       Any				      =       ['''我能吞下玻璃''', '''而不伤身体''']
										snake_case__							:       str				      =       ['''我    能 吞 下 玻 璃''', '''而不伤身体''']
										# S = 0, D = 5, I = 0, N = 11, CER = 5 / 11
										snake_case__							:       int				      =       cer.compute(predictions=SCREAMING_SNAKE_CASE_   ,references=SCREAMING_SNAKE_CASE_   )
										self.assertTrue(abs(char_error_rate - 0.454545454545   ) < 1E-6   )

										snake_case__							:       List[str]				      =       ['''我能吞下玻璃而不伤身体''']
										snake_case__							:       Union[str, Any]				      =       cer.compute(predictions=SCREAMING_SNAKE_CASE_   ,references=SCREAMING_SNAKE_CASE_   )
										self.assertFalse(SCREAMING_SNAKE_CASE_   ,0.0   )





				def    _lowerCamelCase       ( self :  Any   ):
										snake_case__							:       int				      =       ['''''']
										snake_case__							:       Any				      =       ['''Hypothesis''']
										with self.assertRaises(SCREAMING_SNAKE_CASE_   ):
																cer.compute(predictions=SCREAMING_SNAKE_CASE_   ,references=SCREAMING_SNAKE_CASE_   )


if __name__ == "__main__":
							unittest.main()








"""simple docstring"""





import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


_lowercase						   =     logging.get_logger(__name__)

_lowercase						   =     {
    """facebook/musicgen-small""": """https://huggingface.co/facebook/musicgen-small/resolve/main/config.json""",
    # See all Musicgen models at https://huggingface.co/models?filter=musicgen
}

class    __lowercase							(      snake_case_						):


				'''simple docstring'''


				_lowerCAmelCase						      ="musicgen_decoder"
				_lowerCAmelCase						      =["past_key_values"]

				def __init__( self :  List[str]   ,SCREAMING_SNAKE_CASE_ :  Dict=2_0_4_8   ,SCREAMING_SNAKE_CASE_ :  str=2_0_4_8   ,SCREAMING_SNAKE_CASE_ :  List[str]=2_4   ,SCREAMING_SNAKE_CASE_ :  int=4_0_9_6   ,SCREAMING_SNAKE_CASE_ :  List[Any]=1_6   ,SCREAMING_SNAKE_CASE_ :  Dict=0.0   ,SCREAMING_SNAKE_CASE_ :  Any=True   ,SCREAMING_SNAKE_CASE_ :  Any="gelu"   ,SCREAMING_SNAKE_CASE_ :  Tuple=1_0_2_4   ,SCREAMING_SNAKE_CASE_ :  Any=0.1   ,SCREAMING_SNAKE_CASE_ :  List[str]=0.0   ,SCREAMING_SNAKE_CASE_ :  Optional[Any]=0.0   ,SCREAMING_SNAKE_CASE_ :  Tuple=0.02   ,SCREAMING_SNAKE_CASE_ :  Optional[Any]=False   ,SCREAMING_SNAKE_CASE_ :  List[str]=4   ,SCREAMING_SNAKE_CASE_ :  str=2_0_4_8   ,SCREAMING_SNAKE_CASE_ :  str=2_0_4_8   ,SCREAMING_SNAKE_CASE_ :  Optional[int]=None   ,SCREAMING_SNAKE_CASE_ :  Tuple=False   ,**SCREAMING_SNAKE_CASE_ :  Union[str, Any]   ,):
										snake_case__							:       List[str]				      =       vocab_size
										snake_case__							:       List[str]				      =       max_position_embeddings
										snake_case__							:       List[Any]				      =       hidden_size
										snake_case__							:       Dict				      =       ffn_dim
										snake_case__							:       Union[str, Any]				      =       num_hidden_layers
										snake_case__							:       List[str]				      =       num_attention_heads
										snake_case__							:       Optional[Any]				      =       dropout
										snake_case__							:       Dict				      =       attention_dropout
										snake_case__							:       Union[str, Any]				      =       activation_dropout
										snake_case__							:       Dict				      =       activation_function
										snake_case__							:       Union[str, Any]				      =       initializer_factor
										snake_case__							:       List[str]				      =       layerdrop
										snake_case__							:       Any				      =       use_cache
										snake_case__							:       Tuple				      =       scale_embedding  # scale factor will be sqrt(d_model) if True
										snake_case__							:       Any				      =       num_codebooks
										super().__init__(
										    pad_token_id=SCREAMING_SNAKE_CASE_   ,bos_token_id=SCREAMING_SNAKE_CASE_   ,eos_token_id=SCREAMING_SNAKE_CASE_   ,tie_word_embeddings=SCREAMING_SNAKE_CASE_   ,**SCREAMING_SNAKE_CASE_   ,)







class    __lowercase							(      snake_case_						):


				'''simple docstring'''


				_lowerCAmelCase						      ="musicgen"
				_lowerCAmelCase						      =True

				def __init__( self :  Optional[Any]   ,**SCREAMING_SNAKE_CASE_ :  Dict   ):
										super().__init__(**SCREAMING_SNAKE_CASE_   )
										if "text_encoder" not in kwargs or "audio_encoder" not in kwargs or "decoder" not in kwargs:
																raise ValueError('''Config has to be initialized with text_encoder, audio_encoder and decoder config'''   )

										snake_case__							:       Optional[int]				      =       kwargs.pop('''text_encoder'''   )
										snake_case__							:       Optional[Any]				      =       text_encoder_config.pop('''model_type'''   )

										snake_case__							:       Union[str, Any]				      =       kwargs.pop('''audio_encoder'''   )
										snake_case__							:       Dict				      =       audio_encoder_config.pop('''model_type'''   )

										snake_case__							:       Optional[Any]				      =       kwargs.pop('''decoder'''   )

										snake_case__							:       Union[str, Any]				      =       AutoConfig.for_model(SCREAMING_SNAKE_CASE_   ,**SCREAMING_SNAKE_CASE_   )
										snake_case__							:       List[Any]				      =       AutoConfig.for_model(SCREAMING_SNAKE_CASE_   ,**SCREAMING_SNAKE_CASE_   )
										snake_case__							:       List[str]				      =       MusicgenDecoderConfig(**SCREAMING_SNAKE_CASE_   )
										snake_case__							:       Union[str, Any]				      =       True

				@classmethod
				def    _lowerCamelCase       ( cls :  Dict   ,SCREAMING_SNAKE_CASE_ :  PretrainedConfig   ,SCREAMING_SNAKE_CASE_ :  PretrainedConfig   ,SCREAMING_SNAKE_CASE_ :  MusicgenDecoderConfig   ,**SCREAMING_SNAKE_CASE_ :  str   ,):
										return cls(
										    text_encoder=text_encoder_config.to_dict()   ,audio_encoder=audio_encoder_config.to_dict()   ,decoder=decoder_config.to_dict()   ,**SCREAMING_SNAKE_CASE_   ,)





				def    _lowerCamelCase       ( self :  Optional[Any]   ):
										snake_case__							:       Optional[Any]				      =       copy.deepcopy(self.__dict__   )
										snake_case__							:       Any				      =       self.text_encoder.to_dict()
										snake_case__							:       Dict				      =       self.audio_encoder.to_dict()
										snake_case__							:       Dict				      =       self.decoder.to_dict()
										snake_case__							:       Optional[int]				      =       self.__class__.model_type
										return output


