



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__  :  str       = namedtuple('''ExampleNamedTuple''', '''a b c''')



class     __snake_case						(					unittest.TestCase						):




							def        UpperCAmelCase__      (	self					:	Optional[Any]):
									lowerCAmelCase_      :					int     =			torch.randn(5    ,    2)
									lowerCAmelCase_      :					List[str]     =			torch.device('''cuda''') if torch.cuda.is_available() else torch.device('''cpu''')

									lowerCAmelCase_      :					List[Any]     =			send_to_device(A_    ,    A_)
									self.assertTrue(torch.equal(resulta.cpu()    ,    A_))

									lowerCAmelCase_      :					Dict     =			send_to_device((tensor, [tensor, tensor], 1)    ,    A_)
									self.assertIsInstance(A_    ,    A_)
									self.assertTrue(torch.equal(resulta[0].cpu()    ,    A_))
									self.assertIsInstance(resulta[1]    ,    A_)
									self.assertTrue(torch.equal(resulta[1][0].cpu()    ,    A_))
									self.assertTrue(torch.equal(resulta[1][1].cpu()    ,    A_))
									self.assertEqual(resulta[2]    ,    1)

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

									lowerCAmelCase_      :					Union[str, Any]     =			send_to_device(ExampleNamedTuple(a=A_    ,    b=[tensor, tensor]    ,    c=1)    ,    A_)
									self.assertIsInstance(A_    ,    A_)
									self.assertTrue(torch.equal(resulta.a.cpu()    ,    A_))
									self.assertIsInstance(resulta.b    ,    A_)
									self.assertTrue(torch.equal(resulta.b[0].cpu()    ,    A_))
									self.assertTrue(torch.equal(resulta.b[1].cpu()    ,    A_))
									self.assertEqual(resulta.c    ,    1)

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




							def        UpperCAmelCase__      (	self					:	List[str]):
									with self.assertRaises(A_) as cm:
											lowerCAmelCase_      :					List[str]     =			recursively_apply(torch.tensor    ,    (torch.tensor(1), 1)    ,    error_on_other_type=A_)
									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					:	Union[str, Any]):
									lowerCAmelCase_      :					int     =			torch.tensor([1, 2, 3, 4, 5])
									self.assertEqual(listify(A_)    ,    [1, 2, 3, 4, 5])

									lowerCAmelCase_      :					Union[str, Any]     =			torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]])
									self.assertEqual(listify(A_)    ,    [[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]])

									lowerCAmelCase_      :					int     =			torch.tensor([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]], [[1_1, 1_2, 1_3, 1_4, 1_5], [1_6, 1_7, 1_8, 1_9, 2_0]]])
									self.assertEqual(
									    listify(A_)    ,    [[[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]], [[1_1, 1_2, 1_3, 1_4, 1_5], [1_6, 1_7, 1_8, 1_9, 2_0]]])




							def        UpperCAmelCase__      (	self					:	Optional[Any]):
									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					:	Dict):
									lowerCAmelCase_      :					Optional[Any]     =			RegressionModel()
									lowerCAmelCase_      :					List[str]     =			model.forward
									lowerCAmelCase_      :					Tuple     =			convert_outputs_to_fpaa(model.forward)
									lowerCAmelCase_      :					int     =			extract_model_from_parallel(A_    ,    keep_fpaa_wrapper=A_)
									lowerCAmelCase_      :					List[str]     =			pickle.dumps(A_)




							@require_cuda
							def        UpperCAmelCase__      (	self					:	List[Any]):
									lowerCAmelCase_      :					List[str]     =			RegressionModel()
									lowerCAmelCase_      :					int     =			model.forward
									lowerCAmelCase_      :					Tuple     =			torch.cuda.amp.autocast(dtype=torch.floataa)(model.forward)
									lowerCAmelCase_      :					Dict     =			convert_outputs_to_fpaa(model.forward)
									lowerCAmelCase_      :					Any     =			extract_model_from_parallel(A_    ,    keep_fpaa_wrapper=A_)
									lowerCAmelCase_      :					Dict     =			pickle.dumps(A_)




							@require_cuda
							@require_torch_min_version(version='''2.0''')
							def        UpperCAmelCase__      (	self					:	Tuple):
									lowerCAmelCase_      :					str     =			RegressionModel()
									lowerCAmelCase_      :					Optional[int]     =			model.forward
									lowerCAmelCase_      :					List[str]     =			torch.cuda.amp.autocast(dtype=torch.floataa)(model.forward)
									lowerCAmelCase_      :					Tuple     =			convert_outputs_to_fpaa(model.forward)
									lowerCAmelCase_      :					Union[str, Any]     =			torch.compile(model.forward    ,    backend='''inductor''')
									lowerCAmelCase_      :					Dict     =			torch.randn(4    ,    1_0).cuda()
									lowerCAmelCase_      :					Tuple     =			model(A_)




							def        UpperCAmelCase__      (	self					:	Union[str, Any]):
									lowerCAmelCase_      :					Optional[int]     =			RegressionModel()
									# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
									lowerCAmelCase_      :					str     =			torch.nn.parallel.DataParallel(A_)
									lowerCAmelCase_      :					Optional[Any]     =			extract_model_from_parallel(A_)

									self.assertEqual(A_    ,    A_)




							@require_torch_min_version(version='''2.0''')
							def        UpperCAmelCase__      (	self					:	Union[str, Any]):
									lowerCAmelCase_      :					List[str]     =			RegressionModel()
									lowerCAmelCase_      :					Tuple     =			torch.compile(A_)

									# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
									lowerCAmelCase_      :					Any     =			torch.nn.parallel.DataParallel(A_)
									lowerCAmelCase_      :					List[str]     =			torch.compile(A_)
									lowerCAmelCase_      :					Tuple     =			extract_model_from_parallel(A_)

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







							def        UpperCAmelCase__      (	self					:	Any):
									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 ...configuration_utils import PretrainedConfig
from ...utils import logging


A__  :  Tuple       = logging.get_logger(__name__)

A__  :  Tuple       = {
    '''facebook/opt-125m''': '''https://huggingface.co/facebook/opt-125m/blob/main/config.json''',
    '''facebook/opt-350m''': '''https://huggingface.co/facebook/opt-350m/blob/main/config.json''',
    '''facebook/opt-1.3b''': '''https://huggingface.co/facebook/opt-1.3b/blob/main/config.json''',
    '''facebook/opt-2.7b''': '''https://huggingface.co/facebook/opt-2.7b/blob/main/config.json''',
    '''facebook/opt-6.7b''': '''https://huggingface.co/facebook/opt-6.7b/blob/main/config.json''',
    '''facebook/opt-13b''': '''https://huggingface.co/facebook/opt-13b/blob/main/config.json''',
}



class     __snake_case						(					UpperCamelCase_						):
							_a      				=			'''opt'''
							_a      				=			['''past_key_values''']




							def __init__(	self					:	Optional[int]    ,    A_					:	Any=5_0_2_7_2    ,    A_					:	Tuple=7_6_8    ,    A_					:	Optional[Any]=1_2    ,    A_					:	str=3_0_7_2    ,    A_					:	Dict=2_0_4_8    ,    A_					:	List[Any]=True    ,    A_					:	Optional[int]=False    ,    A_					:	Optional[int]=None    ,    A_					:	Dict=0.1    ,    A_					:	str=0.0    ,    A_					:	Union[str, Any]=1_2    ,    A_					:	List[str]="relu"    ,    A_					:	Dict=0.0    ,    A_					:	Union[str, Any]=0.02    ,    A_					:	Optional[Any]=True    ,    A_					:	Optional[Any]=1    ,    A_					:	List[Any]=2    ,    A_					:	Dict=2    ,    A_					:	Optional[int]=True    ,    A_					:	Optional[int]=True    ,    **A_					:	List[Any]    ,    ):
									super().__init__(
									    pad_token_id=A_    ,    bos_token_id=A_    ,    eos_token_id=A_    ,    **A_    ,    )
									lowerCAmelCase_      :					List[Any]     =			vocab_size
									lowerCAmelCase_      :					Union[str, Any]     =			max_position_embeddings
									lowerCAmelCase_      :					Optional[int]     =			num_attention_heads
									lowerCAmelCase_      :					int     =			word_embed_proj_dim if word_embed_proj_dim is not None else hidden_size
									lowerCAmelCase_      :					List[Any]     =			ffn_dim
									lowerCAmelCase_      :					Any     =			hidden_size
									lowerCAmelCase_      :					Union[str, Any]     =			num_hidden_layers
									lowerCAmelCase_      :					str     =			dropout
									lowerCAmelCase_      :					List[str]     =			attention_dropout
									lowerCAmelCase_      :					Any     =			activation_function
									lowerCAmelCase_      :					Union[str, Any]     =			init_std
									lowerCAmelCase_      :					int     =			layerdrop
									lowerCAmelCase_      :					Optional[int]     =			use_cache
									lowerCAmelCase_      :					int     =			do_layer_norm_before
									# We keep these variables at `True` for backward compatibility.
									lowerCAmelCase_      :					Optional[Any]     =			enable_bias
									lowerCAmelCase_      :					Optional[int]     =			layer_norm_elementwise_affine

									# Note that the only purpose of `_remove_final_layer_norm` is to keep backward compatibility
									# with checkpoints that have been fine-tuned before transformers v4.20.1
									# see https://github.com/facebookresearch/metaseq/pull/164
									lowerCAmelCase_      :					Optional[Any]     =			_remove_final_layer_norm





