
'''simple docstring'''







from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


__SCREAMING_SNAKE_CASE		:  List[str]        =      logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE		:  Optional[int]        =      {
    'facebook/m2m100_418M': 'https://huggingface.co/facebook/m2m100_418M/resolve/main/config.json',
    # See all M2M100 models at https://huggingface.co/models?filter=m2m_100
}
class  SCREAMING_SNAKE_CASE__       (  __UpperCamelCase   ):
	lowercase__	    =							"m2m_100"
	lowercase__	    =							["past_key_values"]
	lowercase__	    =							{"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}
	def __init__(				self  ,    __UpperCamelCase=12_8112  ,    __UpperCamelCase=1024  ,    __UpperCamelCase=12  ,    __UpperCamelCase=4096  ,    __UpperCamelCase=16  ,    __UpperCamelCase=12  ,    __UpperCamelCase=4096  ,    __UpperCamelCase=16  ,    __UpperCamelCase=0.0_5  ,    __UpperCamelCase=0.0_5  ,    __UpperCamelCase=True  ,    __UpperCamelCase=True  ,    __UpperCamelCase="relu"  ,    __UpperCamelCase=1024  ,    __UpperCamelCase=0.1  ,    __UpperCamelCase=0.1  ,    __UpperCamelCase=0.0  ,    __UpperCamelCase=0.0_2  ,    __UpperCamelCase=2  ,    __UpperCamelCase=True  ,    __UpperCamelCase=1  ,    __UpperCamelCase=0  ,    __UpperCamelCase=2  ,    **__UpperCamelCase  ,    ):



					'''simple docstring'''






					__a       :     Any								=	vocab_size
					__a       :     Optional[int]								=	max_position_embeddings
					__a       :     str								=	d_model
					__a       :     List[Any]								=	encoder_ffn_dim
					__a       :     str								=	encoder_layers
					__a       :     Any								=	encoder_attention_heads
					__a       :     Optional[Any]								=	decoder_ffn_dim
					__a       :     List[str]								=	decoder_layers
					__a       :     str								=	decoder_attention_heads
					__a       :     List[Any]								=	dropout
					__a       :     List[str]								=	attention_dropout
					__a       :     Optional[int]								=	activation_dropout
					__a       :     Union[str, Any]								=	activation_function
					__a       :     Any								=	init_std
					__a       :     int								=	encoder_layerdrop
					__a       :     Tuple								=	decoder_layerdrop
					__a       :     List[Any]								=	use_cache
					__a       :     List[str]								=	encoder_layers
					__a       :     Optional[Any]								=	scale_embedding  # scale factor will be sqrt(d_model) if True

					super().__init__(
					    pad_token_id=__UpperCamelCase  ,    bos_token_id=__UpperCamelCase  ,    eos_token_id=__UpperCamelCase  ,    is_encoder_decoder=__UpperCamelCase  ,    decoder_start_token_id=__UpperCamelCase  ,    **__UpperCamelCase  ,    )







class  SCREAMING_SNAKE_CASE__       (  __UpperCamelCase   ):
	@property
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     Tuple								=	OrderedDict(
					    [
					        ("""input_ids""", {0: """batch""", 1: """encoder_sequence"""}),
					        ("""attention_mask""", {0: """batch""", 1: """encoder_sequence"""}),
					    ]			)

					if self.use_past:
									__a       :     Optional[int]								=	{0: """batch"""}
									__a       :     Optional[int]								=	{0: """batch""", 1: """past_decoder_sequence + sequence"""}
					else:
									__a       :     int								=	{0: """batch""", 1: """decoder_sequence"""}
									__a       :     Optional[int]								=	{0: """batch""", 1: """decoder_sequence"""}

					if self.use_past:
									self.fill_with_past_key_values_(__UpperCamelCase  ,    direction="""inputs"""			)
					return common_inputs
	def __lowerCamelCase    (				self  ,    __UpperCamelCase  ,    __UpperCamelCase = -1  ,    __UpperCamelCase = -1  ,    __UpperCamelCase = False  ,    __UpperCamelCase = None  ,    ):



					'''simple docstring'''






					__a       :     Union[str, Any]								=	compute_effective_axis_dimension(
					    __UpperCamelCase  ,    fixed_dimension=OnnxConfig.default_fixed_batch  ,    num_token_to_add=0			)

					# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
					__a       :     Optional[int]								=	tokenizer.num_special_tokens_to_add(__UpperCamelCase			)
					__a       :     Optional[int]								=	compute_effective_axis_dimension(
					    __UpperCamelCase  ,    fixed_dimension=OnnxConfig.default_fixed_sequence  ,    num_token_to_add=__UpperCamelCase			)

					# Generate dummy inputs according to compute batch and sequence
					__a       :     List[Any]								=	[""" """.join([tokenizer.unk_token]			) * seq_length] * batch_size
					__a       :     Tuple								=	dict(tokenizer(__UpperCamelCase  ,    return_tensors=__UpperCamelCase			)			)
					return common_inputs
	def __lowerCamelCase    (				self  ,    __UpperCamelCase  ,    __UpperCamelCase = -1  ,    __UpperCamelCase = -1  ,    __UpperCamelCase = False  ,    __UpperCamelCase = None  ,    ):



					'''simple docstring'''






					__a       :     Any								=	self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
					    __UpperCamelCase  ,    __UpperCamelCase  ,    __UpperCamelCase  ,    __UpperCamelCase  ,    __UpperCamelCase			)

					# Generate decoder inputs
					__a       :     Optional[Any]								=	seq_length if not self.use_past else 1
					__a       :     List[Any]								=	self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
					    __UpperCamelCase  ,    __UpperCamelCase  ,    __UpperCamelCase  ,    __UpperCamelCase  ,    __UpperCamelCase			)
					__a       :     Union[str, Any]								=	{f"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
					__a       :     Optional[int]								=	dict(**__UpperCamelCase  ,    **__UpperCamelCase			)

					if self.use_past:
									if not is_torch_available():
													raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""			)
									else:
													import torch
									__a							,	__a       :     Any								=	common_inputs["""input_ids"""].shape
									__a       :     Optional[int]								=	common_inputs["""decoder_input_ids"""].shape[1]
									__a							,	__a       :     Optional[int]								=	self.num_attention_heads
									__a       :     Tuple								=	(
									    batch,
									    num_encoder_attention_heads,
									    encoder_seq_length,
									    self._config.hidden_size // num_encoder_attention_heads,
									)
									__a       :     Optional[Any]								=	decoder_seq_length + 3
									__a       :     Dict								=	(
									    batch,
									    num_decoder_attention_heads,
									    decoder_past_length,
									    self._config.hidden_size // num_decoder_attention_heads,
									)

									__a       :     List[str]								=	torch.cat(
									    [common_inputs["""decoder_attention_mask"""], torch.ones(__UpperCamelCase  ,    __UpperCamelCase			)]  ,    dim=1			)

									__a       :     Any								=	[]
									# If the number of encoder and decoder layers are present in the model configuration, both are considered
									__a							,	__a       :     str								=	self.num_layers
									__a       :     Any								=	min(__UpperCamelCase  ,    __UpperCamelCase			)
									__a       :     Optional[int]								=	max(__UpperCamelCase  ,    __UpperCamelCase			) - min_num_layers
									__a       :     Union[str, Any]								=	"""encoder""" if num_encoder_layers > num_decoder_layers else """decoder"""

									for _ in range(__UpperCamelCase			):
													common_inputs["past_key_values"].append(
													    (
													        torch.zeros(__UpperCamelCase			),
													        torch.zeros(__UpperCamelCase			),
													        torch.zeros(__UpperCamelCase			),
													        torch.zeros(__UpperCamelCase			),
													    )			)
									# TODO: test this.
									__a       :     Optional[int]								=	encoder_shape if remaining_side_name == """encoder""" else decoder_shape
									for _ in range(__UpperCamelCase  ,    __UpperCamelCase			):
													common_inputs["past_key_values"].append((torch.zeros(__UpperCamelCase			), torch.zeros(__UpperCamelCase			))			)
					return common_inputs

	lowercase__	    =							_generate_dummy_inputs_for_default_and_seqaseq_lm

'''simple docstring'''







import unittest

from cer import CER


__SCREAMING_SNAKE_CASE		:  List[str]        =      CER()
class  SCREAMING_SNAKE_CASE__       (  unittest.TestCase   ):
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     Optional[Any]								=	["""White House"""]
					__a       :     Tuple								=	["""white house"""]
					# S = 2, D = 0, I = 0, N = 11, CER = 2 / 11
					__a       :     Any								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.1_8_1_8_1_8_1_8_1_8			) < 1E-6			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     Optional[int]								=	["""were wolf"""]
					__a       :     Tuple								=	["""werewolf"""]
					# S = 0, D = 0, I = 1, N = 9, CER = 1 / 9
					__a       :     Any								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.1_1_1_1_1_1_1			) < 1E-6			)

					__a       :     str								=	["""werewolf"""]
					__a       :     List[Any]								=	["""weae     wolf"""]
					# S = 1, D = 1, I = 0, N = 8, CER = 0.25
					__a       :     Tuple								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.2_5			) < 1E-6			)

					# consecutive whitespaces case 1
					__a       :     int								=	["""were wolf"""]
					__a       :     Dict								=	["""were               wolf"""]
					# S = 0, D = 0, I = 0, N = 9, CER = 0
					__a       :     str								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.0			) < 1E-6			)

					# consecutive whitespaces case 2
					__a       :     List[str]								=	["""were   wolf"""]
					__a       :     Optional[int]								=	["""were               wolf"""]
					# S = 0, D = 0, I = 0, N = 9, CER = 0
					__a       :     Optional[Any]								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.0			) < 1E-6			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     Dict								=	["""werewolf"""]
					__a       :     str								=	["""weaewolf"""]
					# S = 1, D = 0, I = 0, N = 8, CER = 0.125
					__a       :     Optional[int]								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.1_2_5			) < 1E-6			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     List[str]								=	["""werewolf"""]
					__a       :     Tuple								=	["""wereawolf"""]
					# S = 0, D = 1, I = 0, N = 8, CER = 0.125
					__a       :     Optional[Any]								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.1_2_5			) < 1E-6			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     List[str]								=	["""werewolf"""]
					__a       :     Optional[Any]								=	["""wereolf"""]
					# S = 0, D = 0, I = 1, N = 8, CER = 0.125
					__a       :     int								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.1_2_5			) < 1E-6			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     Dict								=	["""werewolf"""]
					__a       :     Tuple								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertEqual(__UpperCamelCase  ,    0.0			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     List[str]								=	["""werewolf""", """I am your father"""]
					__a       :     int								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertEqual(__UpperCamelCase  ,    0.0			)

					__a       :     Dict								=	["""werewolf""", """I am your father""", """doge"""]
					__a       :     Optional[Any]								=	["""werxwolf""", """I       am your father""", """doge"""]
					# S = 1, D = 0, I = 0, N = 28, CER = 1 / 28
					__a       :     Union[str, Any]								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.0_3_5_7_1_4_2_8			) < 1E-6			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     List[str]								=	["""My hovercraft""", """is full of eels"""]
					__a       :     Union[str, Any]								=	["""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
					__a       :     str								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase  ,    concatenate_texts=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.0_7_1_4_2_8			) < 1E-6			)
	def __lowerCamelCase    (				self			):



					'''simple docstring'''






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

					__a       :     Union[str, Any]								=	["""我能吞下玻璃""", """而不伤身体"""]
					__a       :     Optional[Any]								=	["""我    能 吞 下 玻 璃""", """而不伤身体"""]
					# S = 0, D = 5, I = 0, N = 11, CER = 5 / 11
					__a       :     List[str]								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertTrue(abs(char_error_rate - 0.4_5_4_5_4_5_4_5_4_5_4_5			) < 1E-6			)

					__a       :     Union[str, Any]								=	["""我能吞下玻璃而不伤身体"""]
					__a       :     Dict								=	cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)
					self.assertFalse(__UpperCamelCase  ,    0.0			)

	def __lowerCamelCase    (				self			):



					'''simple docstring'''






					__a       :     List[Any]								=	[""""""]
					__a       :     Dict								=	["""Hypothesis"""]
					with self.assertRaises(__UpperCamelCase			):
									cer.compute(predictions=__UpperCamelCase  ,    references=__UpperCamelCase			)


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