
import os
import pickle
import unittest
from collections import UserDict, namedtuple

import torch

from accelerate.test_utils.testing import require_cuda, require_torch_min_version
from accelerate.test_utils.training import RegressionModel
from accelerate.utils import (
    convert_outputs_to_fpaa,
    extract_model_from_parallel,
    find_device,
    listify,
    patch_environment,
    recursively_apply,
    send_to_device,
)


_a   			=						namedtuple('''ExampleNamedTuple''', '''a b c''')





class __lowerCamelCase				(   unittest.TestCase):





	"""simple docstring"""






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







							_UpperCAmelCase             =    torch.randn(5 ,					2					)
							_UpperCAmelCase             =    torch.device('cuda'					) if torch.cuda.is_available() else torch.device('cpu'					)

							_UpperCAmelCase             =    send_to_device(UpperCAmelCase ,					UpperCAmelCase					)
							self.assertTrue(torch.equal(resulta.cpu() ,					UpperCAmelCase					)					)

							_UpperCAmelCase             =    send_to_device((tensor, [tensor, tensor], 1) ,					UpperCAmelCase					)
							self.assertIsInstance(UpperCAmelCase ,					UpperCAmelCase					)
							self.assertTrue(torch.equal(resulta[0].cpu() ,					UpperCAmelCase					)					)
							self.assertIsInstance(resulta[1] ,					UpperCAmelCase					)
							self.assertTrue(torch.equal(resulta[1][0].cpu() ,					UpperCAmelCase					)					)
							self.assertTrue(torch.equal(resulta[1][1].cpu() ,					UpperCAmelCase					)					)
							self.assertEqual(resulta[2] ,					1					)

							_UpperCAmelCase             =    send_to_device({'a': tensor, 'b': [tensor, tensor], 'c': 1} ,					UpperCAmelCase					)
							self.assertIsInstance(UpperCAmelCase ,					UpperCAmelCase					)
							self.assertTrue(torch.equal(resulta['a'].cpu() ,					UpperCAmelCase					)					)
							self.assertIsInstance(resulta['b'] ,					UpperCAmelCase					)
							self.assertTrue(torch.equal(resulta['b'][0].cpu() ,					UpperCAmelCase					)					)
							self.assertTrue(torch.equal(resulta['b'][1].cpu() ,					UpperCAmelCase					)					)
							self.assertEqual(resulta['c'] ,					1					)

							_UpperCAmelCase             =    send_to_device(ExampleNamedTuple(a=UpperCAmelCase ,					b=[tensor, tensor] ,					c=1					) ,					UpperCAmelCase					)
							self.assertIsInstance(UpperCAmelCase ,					UpperCAmelCase					)
							self.assertTrue(torch.equal(resulta.a.cpu() ,					UpperCAmelCase					)					)
							self.assertIsInstance(resulta.b ,					UpperCAmelCase					)
							self.assertTrue(torch.equal(resulta.b[0].cpu() ,					UpperCAmelCase					)					)
							self.assertTrue(torch.equal(resulta.b[1].cpu() ,					UpperCAmelCase					)					)
							self.assertEqual(resulta.c ,					1					)

							_UpperCAmelCase             =    send_to_device(UserDict({'a': tensor, 'b': [tensor, tensor], 'c': 1}					) ,					UpperCAmelCase					)
							self.assertIsInstance(UpperCAmelCase ,					UpperCAmelCase					)
							self.assertTrue(torch.equal(resulta['a'].cpu() ,					UpperCAmelCase					)					)
							self.assertIsInstance(resulta['b'] ,					UpperCAmelCase					)
							self.assertTrue(torch.equal(resulta['b'][0].cpu() ,					UpperCAmelCase					)					)
							self.assertTrue(torch.equal(resulta['b'][1].cpu() ,					UpperCAmelCase					)					)
							self.assertEqual(resulta['c'] ,					1					)

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







							with self.assertRaises(UpperCAmelCase					) as cm:
													_UpperCAmelCase             =    recursively_apply(torch.tensor ,					(torch.tensor(1					), 1) ,					error_on_other_type=UpperCAmelCase					)
							self.assertEqual(
							    str(cm.exception					) ,					'Unsupported types (<class \'int\'>) passed to `tensor`. Only nested list/tuple/dicts of objects that are valid for `is_torch_tensor` should be passed.' ,					)

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







							_UpperCAmelCase             =    torch.tensor([1, 2, 3, 4, 5]					)
							self.assertEqual(listify(UpperCAmelCase					) ,					[1, 2, 3, 4, 5]					)

							_UpperCAmelCase             =    torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]					)
							self.assertEqual(listify(UpperCAmelCase					) ,					[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]					)

							_UpperCAmelCase             =    torch.tensor([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]					)
							self.assertEqual(
							    listify(UpperCAmelCase					) ,					[[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]					)

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







							with patch_environment(aa=1 ,					BB=2					):
													self.assertEqual(os.environ.get('AA'					) ,					'1'					)
													self.assertEqual(os.environ.get('BB'					) ,					'2'					)

							self.assertNotIn('AA' ,					os.environ					)
							self.assertNotIn('BB' ,					os.environ					)

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







							_UpperCAmelCase             =    RegressionModel()
							_UpperCAmelCase             =    model.forward
							_UpperCAmelCase             =    convert_outputs_to_fpaa(model.forward					)
							_UpperCAmelCase             =    extract_model_from_parallel(UpperCAmelCase ,					keep_fpaa_wrapper=UpperCAmelCase					)
							_UpperCAmelCase             =    pickle.dumps(UpperCAmelCase					)

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







							_UpperCAmelCase             =    RegressionModel()
							_UpperCAmelCase             =    model.forward
							_UpperCAmelCase             =    torch.cuda.amp.autocast(dtype=torch.floataa					)(model.forward					)
							_UpperCAmelCase             =    convert_outputs_to_fpaa(model.forward					)
							_UpperCAmelCase             =    extract_model_from_parallel(UpperCAmelCase ,					keep_fpaa_wrapper=UpperCAmelCase					)
							_UpperCAmelCase             =    pickle.dumps(UpperCAmelCase					)

	@require_cuda
	@require_torch_min_version(version='2.0'					)
	def  UpperCamelCase					(							self					):
							"""simple docstring"""







							_UpperCAmelCase             =    RegressionModel()
							_UpperCAmelCase             =    model.forward
							_UpperCAmelCase             =    torch.cuda.amp.autocast(dtype=torch.floataa					)(model.forward					)
							_UpperCAmelCase             =    convert_outputs_to_fpaa(model.forward					)
							_UpperCAmelCase             =    torch.compile(model.forward ,					backend='inductor'					)
							_UpperCAmelCase             =    torch.randn(4 ,					10					).cuda()
							_UpperCAmelCase             =    model(UpperCAmelCase					)

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







							_UpperCAmelCase             =    RegressionModel()
							# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
							_UpperCAmelCase             =    torch.nn.parallel.DataParallel(UpperCAmelCase					)
							_UpperCAmelCase             =    extract_model_from_parallel(UpperCAmelCase					)

							self.assertEqual(UpperCAmelCase ,					UpperCAmelCase					)

	@require_torch_min_version(version='2.0'					)
	def  UpperCamelCase					(							self					):
							"""simple docstring"""







							_UpperCAmelCase             =    RegressionModel()
							_UpperCAmelCase             =    torch.compile(UpperCAmelCase					)

							# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
							_UpperCAmelCase             =    torch.nn.parallel.DataParallel(UpperCAmelCase					)
							_UpperCAmelCase             =    torch.compile(UpperCAmelCase					)
							_UpperCAmelCase             =    extract_model_from_parallel(UpperCAmelCase					)

							self.assertEqual(compiled_model._orig_mod ,					compiled_model_unwrapped._orig_mod					)

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







							self.assertEqual(find_device([1, 'a', torch.tensor([1, 2, 3]					)]					) ,					torch.device('cpu'					)					)
							self.assertEqual(find_device({'a': 1, 'b': torch.tensor([1, 2, 3]					)}					) ,					torch.device('cpu'					)					)
							self.assertIsNone(find_device([1, 'a']					)					)








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
