






import unittest

from diffusers import __version__
from diffusers.utils import deprecate





class __lowerCamelCase				(   unittest.TestCase):





	"""simple docstring"""






	UpperCamelCase__			=   ".".join([str(int(__version__.split(".")[0]) + 1)] + __version__.split(".")[1:])
	UpperCamelCase__			=   "0.0.1"

	def  UpperCamelCase					(							self					):
							"""simple docstring"""







							_UpperCAmelCase             =    {'deprecated_arg': 4}

							with self.assertWarns(UpperCAmelCase					) as warning:
													_UpperCAmelCase             =    deprecate('deprecated_arg' ,					self.higher_version ,					'message' ,					take_from=UpperCAmelCase					)

							assert output == 4
							assert (
							    str(warning.warning					)
							    == F"""The `deprecated_arg` argument is deprecated and will be removed in version {self.higher_version}."""
							    " message"
							)

	def  UpperCamelCase					(							self					):
							"""simple docstring"""







							_UpperCAmelCase             =    {'deprecated_arg': 4}

							with self.assertWarns(UpperCAmelCase					) as warning:
													_UpperCAmelCase             =    deprecate(('deprecated_arg', self.higher_version, 'message') ,					take_from=UpperCAmelCase					)

							assert output == 4
							assert (
							    str(warning.warning					)
							    == F"""The `deprecated_arg` argument is deprecated and will be removed in version {self.higher_version}."""
							    " message"
							)

	def  UpperCamelCase					(							self					):
							"""simple docstring"""







							_UpperCAmelCase             =    {'deprecated_arg_1': 4, 'deprecated_arg_2': 8}
							with self.assertWarns(UpperCAmelCase					) as warning:
													_UpperCAmelCase      ,				_UpperCAmelCase             =    deprecate(
													    ('deprecated_arg_1', self.higher_version, 'Hey') ,					('deprecated_arg_2', self.higher_version, 'Hey') ,					take_from=UpperCAmelCase ,					)
							assert output_a == 4
							assert output_a == 8
							assert (
							    str(warning.warnings[0].message					)
							    == "The `deprecated_arg_1` argument is deprecated and will be removed in version"
							    F""" {self.higher_version}. Hey"""
							)
							assert (
							    str(warning.warnings[1].message					)
							    == "The `deprecated_arg_2` argument is deprecated and will be removed in version"
							    F""" {self.higher_version}. Hey"""
							)

	def  UpperCamelCase					(							self					):
							"""simple docstring"""







							_UpperCAmelCase             =    {'deprecated_arg': 4}

							with self.assertRaises(UpperCAmelCase					) as error:
													deprecate(('wrong_arg', self.higher_version, 'message') ,					take_from=UpperCAmelCase					)

							assert "test_deprecate_function_incorrect_arg in" in str(error.exception					)
							assert "line" in str(error.exception					)
							assert "got an unexpected keyword argument `deprecated_arg`" in str(error.exception					)

	def  UpperCamelCase					(							self					):
							"""simple docstring"""







							with self.assertWarns(UpperCAmelCase					) as warning:
													deprecate(('deprecated_arg', self.higher_version, 'message')					)

							assert (
							    str(warning.warning					)
							    == F"""`deprecated_arg` is deprecated and will be removed in version {self.higher_version}. message"""
							)

	def  UpperCamelCase					(							self					):
							"""simple docstring"""







							with self.assertWarns(UpperCAmelCase					) as warning:
													deprecate(
													    ('deprecated_arg_1', self.higher_version, 'Hey') ,					('deprecated_arg_2', self.higher_version, 'Hey') ,					)
							assert (
							    str(warning.warnings[0].message					)
							    == F"""`deprecated_arg_1` is deprecated and will be removed in version {self.higher_version}. Hey"""
							)
							assert (
							    str(warning.warnings[1].message					)
							    == F"""`deprecated_arg_2` is deprecated and will be removed in version {self.higher_version}. Hey"""
							)

	def  UpperCamelCase					(							self					):
							"""simple docstring"""







							class __lowerCamelCase				:





								"""simple docstring"""






								UpperCamelCase__			=   5

							with self.assertWarns(UpperCAmelCase					) as warning:
													_UpperCAmelCase             =    deprecate(('arg', self.higher_version, 'message') ,					take_from=Args()					)

							assert arg == 5
							assert (
							    str(warning.warning					)
							    == F"""The `arg` attribute is deprecated and will be removed in version {self.higher_version}. message"""
							)

	def  UpperCamelCase					(							self					):
							"""simple docstring"""







							class __lowerCamelCase				:





								"""simple docstring"""






								UpperCamelCase__			=   5
								UpperCamelCase__			=   7

							with self.assertWarns(UpperCAmelCase					) as warning:
													_UpperCAmelCase      ,				_UpperCAmelCase             =    deprecate(
													    ('arg', self.higher_version, 'message') ,					('foo', self.higher_version, 'message') ,					('does not exist', self.higher_version, 'message') ,					take_from=Args() ,					)

							assert arg_a == 5
							assert arg_a == 7
							assert (
							    str(warning.warning					)
							    == F"""The `arg` attribute is deprecated and will be removed in version {self.higher_version}. message"""
							)
							assert (
							    str(warning.warnings[0].message					)
							    == F"""The `arg` attribute is deprecated and will be removed in version {self.higher_version}. message"""
							)
							assert (
							    str(warning.warnings[1].message					)
							    == F"""The `foo` attribute is deprecated and will be removed in version {self.higher_version}. message"""
							)

	def  UpperCamelCase					(							self					):
							"""simple docstring"""







							_UpperCAmelCase             =    {'deprecated_arg': 4}

							with self.assertRaises(UpperCAmelCase					) as error:
													deprecate(('wrong_arg', self.lower_version, 'message') ,					take_from=UpperCAmelCase					)

							assert (
							    str(error.exception					)
							    == "The deprecation tuple ('wrong_arg', '0.0.1', 'message') should be removed since diffusers' version"
							    F""" {__version__} is >= {self.lower_version}"""
							)

	def  UpperCamelCase					(							self					):
							"""simple docstring"""







							with self.assertWarns(UpperCAmelCase					) as warning:
													deprecate(('deprecated_arg', self.higher_version, 'This message is better!!!') ,					standard_warn=UpperCAmelCase					)

							assert str(warning.warning					) == "This message is better!!!"

	def  UpperCamelCase					(							self					):
							"""simple docstring"""







							with self.assertWarns(UpperCAmelCase					) as warning:
													deprecate(('deprecated_arg', self.higher_version, 'This message is better!!!') ,					standard_warn=UpperCAmelCase					)
							assert str(warning.warning					) == "This message is better!!!"
							assert "diffusers/tests/others/test_utils.py" in warning.filename








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


_a   			=						logging.get_logger(__name__)

_a   			=						{
    '''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 __lowerCamelCase				(   snake_case__):





	"""simple docstring"""






	UpperCamelCase__			=   "m2m_100"
	UpperCamelCase__			=   ["past_key_values"]
	UpperCamelCase__			=   {"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.05 ,					UpperCAmelCase=0.05 ,					UpperCAmelCase=True ,					UpperCAmelCase=True ,					UpperCAmelCase="relu" ,					UpperCAmelCase=1024 ,					UpperCAmelCase=0.1 ,					UpperCAmelCase=0.1 ,					UpperCAmelCase=0.0 ,					UpperCAmelCase=0.02 ,					UpperCAmelCase=2 ,					UpperCAmelCase=True ,					UpperCAmelCase=1 ,					UpperCAmelCase=0 ,					UpperCAmelCase=2 ,					**UpperCAmelCase ,					):
							"""simple docstring"""







							_UpperCAmelCase             =    vocab_size
							_UpperCAmelCase             =    max_position_embeddings
							_UpperCAmelCase             =    d_model
							_UpperCAmelCase             =    encoder_ffn_dim
							_UpperCAmelCase             =    encoder_layers
							_UpperCAmelCase             =    encoder_attention_heads
							_UpperCAmelCase             =    decoder_ffn_dim
							_UpperCAmelCase             =    decoder_layers
							_UpperCAmelCase             =    decoder_attention_heads
							_UpperCAmelCase             =    dropout
							_UpperCAmelCase             =    attention_dropout
							_UpperCAmelCase             =    activation_dropout
							_UpperCAmelCase             =    activation_function
							_UpperCAmelCase             =    init_std
							_UpperCAmelCase             =    encoder_layerdrop
							_UpperCAmelCase             =    decoder_layerdrop
							_UpperCAmelCase             =    use_cache
							_UpperCAmelCase             =    encoder_layers
							_UpperCAmelCase             =    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 __lowerCamelCase				(   snake_case__):





	"""simple docstring"""






	@property
	def  UpperCamelCase					(							self					):
							"""simple docstring"""







							_UpperCAmelCase             =    OrderedDict(
							    [
							        ('input_ids', {0: 'batch', 1: 'encoder_sequence'}),
							        ('attention_mask', {0: 'batch', 1: 'encoder_sequence'}),
							    ]					)

							if self.use_past:
													_UpperCAmelCase             =    {0: 'batch'}
													_UpperCAmelCase             =    {0: 'batch', 1: 'past_decoder_sequence + sequence'}
							else:
													_UpperCAmelCase             =    {0: 'batch', 1: 'decoder_sequence'}
													_UpperCAmelCase             =    {0: 'batch', 1: 'decoder_sequence'}

							if self.use_past:
													self.fill_with_past_key_values_(UpperCAmelCase ,					direction='inputs'					)
							return common_inputs

	def  UpperCamelCase					(							self ,					UpperCAmelCase ,					UpperCAmelCase = -1 ,					UpperCAmelCase = -1 ,					UpperCAmelCase = False ,					UpperCAmelCase = None ,					):
							"""simple docstring"""







							_UpperCAmelCase             =    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
							_UpperCAmelCase             =    tokenizer.num_special_tokens_to_add(UpperCAmelCase					)
							_UpperCAmelCase             =    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
							_UpperCAmelCase             =    [' '.join([tokenizer.unk_token]					) * seq_length] * batch_size
							_UpperCAmelCase             =    dict(tokenizer(UpperCAmelCase ,					return_tensors=UpperCAmelCase					)					)
							return common_inputs

	def  UpperCamelCase					(							self ,					UpperCAmelCase ,					UpperCAmelCase = -1 ,					UpperCAmelCase = -1 ,					UpperCAmelCase = False ,					UpperCAmelCase = None ,					):
							"""simple docstring"""







							_UpperCAmelCase             =    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
							    UpperCAmelCase ,					UpperCAmelCase ,					UpperCAmelCase ,					UpperCAmelCase ,					UpperCAmelCase					)

							# Generate decoder inputs
							_UpperCAmelCase             =    seq_length if not self.use_past else 1
							_UpperCAmelCase             =    self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
							    UpperCAmelCase ,					UpperCAmelCase ,					UpperCAmelCase ,					UpperCAmelCase ,					UpperCAmelCase					)
							_UpperCAmelCase             =    {F"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
							_UpperCAmelCase             =    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
													_UpperCAmelCase      ,				_UpperCAmelCase             =    common_inputs['input_ids'].shape
													_UpperCAmelCase             =    common_inputs['decoder_input_ids'].shape[1]
													_UpperCAmelCase      ,				_UpperCAmelCase             =    self.num_attention_heads
													_UpperCAmelCase             =    (
													    batch,
													    num_encoder_attention_heads,
													    encoder_seq_length,
													    self._config.hidden_size // num_encoder_attention_heads,
													)
													_UpperCAmelCase             =    decoder_seq_length + 3
													_UpperCAmelCase             =    (
													    batch,
													    num_decoder_attention_heads,
													    decoder_past_length,
													    self._config.hidden_size // num_decoder_attention_heads,
													)

													_UpperCAmelCase             =    torch.cat(
													    [common_inputs['decoder_attention_mask'], torch.ones(UpperCAmelCase ,					UpperCAmelCase					)] ,					dim=1					)

													_UpperCAmelCase             =    []
													# If the number of encoder and decoder layers are present in the model configuration, both are considered
													_UpperCAmelCase      ,				_UpperCAmelCase             =    self.num_layers
													_UpperCAmelCase             =    min(UpperCAmelCase ,					UpperCAmelCase					)
													_UpperCAmelCase             =    max(UpperCAmelCase ,					UpperCAmelCase					) - min_num_layers
													_UpperCAmelCase             =    '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.
													_UpperCAmelCase             =    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

	UpperCamelCase__			=   _generate_dummy_inputs_for_default_and_seqaseq_lm
