'''simple docstring'''


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,
)


snake_case__	:     Optional[int]      =    namedtuple('''ExampleNamedTuple''', '''a b c''')






class 	__SCREAMING_SNAKE_CASE		(			unittest.TestCase			):







  '''simple docstring'''






  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  List[Any]  = torch.randn(5 ,						2		)
       UpperCAmelCase_				:  Optional[Any]  = torch.device('cuda'		) if torch.cuda.is_available() else torch.device('cpu'		)

       UpperCAmelCase_				:  Dict  = send_to_device(snake_case_ ,						snake_case_		)
       self.assertTrue(torch.equal(resulta.cpu() ,						snake_case_		)		)

       UpperCAmelCase_				:  Tuple  = send_to_device((tensor, [tensor, tensor], 1) ,						snake_case_		)
       self.assertIsInstance(snake_case_ ,						snake_case_		)
       self.assertTrue(torch.equal(resulta[0].cpu() ,						snake_case_		)		)
       self.assertIsInstance(resulta[1] ,						snake_case_		)
       self.assertTrue(torch.equal(resulta[1][0].cpu() ,						snake_case_		)		)
       self.assertTrue(torch.equal(resulta[1][1].cpu() ,						snake_case_		)		)
       self.assertEqual(resulta[2] ,						1		)

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

       UpperCAmelCase_				:  Tuple  = send_to_device(ExampleNamedTuple(a=snake_case_ ,						b=[tensor, tensor] ,						c=1		) ,						snake_case_		)
       self.assertIsInstance(snake_case_ ,						snake_case_		)
       self.assertTrue(torch.equal(resulta.a.cpu() ,						snake_case_		)		)
       self.assertIsInstance(resulta.b ,						snake_case_		)
       self.assertTrue(torch.equal(resulta.b[0].cpu() ,						snake_case_		)		)
       self.assertTrue(torch.equal(resulta.b[1].cpu() ,						snake_case_		)		)
       self.assertEqual(resulta.c ,						1		)

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


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       with self.assertRaises(snake_case_		) as cm:
            UpperCAmelCase_				:  Optional[Any]  = recursively_apply(torch.tensor ,						(torch.tensor(1		), 1) ,						error_on_other_type=snake_case_		)
       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_				:  Dict  = torch.tensor([1, 2, 3, 4, 5]		)
       self.assertEqual(listify(snake_case_		) ,						[1, 2, 3, 4, 5]		)

       UpperCAmelCase_				:  List[str]  = torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]		)
       self.assertEqual(listify(snake_case_		) ,						[[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]		)

       UpperCAmelCase_				:  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(snake_case_		) ,						[[[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		):




       '''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_				:  Optional[int]  = RegressionModel()
       UpperCAmelCase_				:  Optional[Any]  = model.forward
       UpperCAmelCase_				:  List[Any]  = convert_outputs_to_fpaa(model.forward		)
       UpperCAmelCase_				:  Any  = extract_model_from_parallel(snake_case_ ,						keep_fpaa_wrapper=snake_case_		)
       UpperCAmelCase_				:  Optional[Any]  = pickle.dumps(snake_case_		)


  @require_cuda
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  Tuple  = RegressionModel()
       UpperCAmelCase_				:  Tuple  = model.forward
       UpperCAmelCase_				:  Optional[Any]  = torch.cuda.amp.autocast(dtype=torch.floataa		)(model.forward		)
       UpperCAmelCase_				:  int  = convert_outputs_to_fpaa(model.forward		)
       UpperCAmelCase_				:  Optional[Any]  = extract_model_from_parallel(snake_case_ ,						keep_fpaa_wrapper=snake_case_		)
       UpperCAmelCase_				:  List[Any]  = pickle.dumps(snake_case_		)


  @require_cuda
  @require_torch_min_version(version='2.0'		)
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  Tuple  = RegressionModel()
       UpperCAmelCase_				:  str  = model.forward
       UpperCAmelCase_				:  str  = torch.cuda.amp.autocast(dtype=torch.floataa		)(model.forward		)
       UpperCAmelCase_				:  List[str]  = convert_outputs_to_fpaa(model.forward		)
       UpperCAmelCase_				:  List[Any]  = torch.compile(model.forward ,						backend='inductor'		)
       UpperCAmelCase_				:  List[Any]  = torch.randn(4 ,						1_0		).cuda()
       UpperCAmelCase_				:  List[str]  = model(snake_case_		)


  def _UpperCamelCase	(					self		):




       '''simple docstring'''

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

       self.assertEqual(snake_case_ ,						snake_case_		)


  @require_torch_min_version(version='2.0'		)
  def _UpperCamelCase	(					self		):




       '''simple docstring'''

       UpperCAmelCase_				:  Optional[Any]  = RegressionModel()
       UpperCAmelCase_				:  List[str]  = torch.compile(snake_case_		)

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

       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']		)		)












import os
from pathlib import Path
from typing import Dict, List

import fire
import torch

from transformers import AutoModelForSeqaSeqLM, AutoTokenizer
from transformers.utils.logging import get_logger


__lowerCamelCase :    Any      						=      get_logger(__name__)







def 		lowercase__ (		__A:						str				,__A:						str					):





					'''simple docstring'''






					if text.startswith(__A					):
										return text[len(__A					) :]
					return text  # or whatever







def 		lowercase__ (		__A:						List[str]					):





					'''simple docstring'''






					return {remove_prefix(__A				,'''model.'''					): v for k, v in sd.items()}







def 		lowercase__ (		__A:						List[Dict[str, torch.Tensor]]					):





					'''simple docstring'''






					__magic_name__				:						List[Any]     				=				{}
					for k in state_dicts[0].keys():
										__magic_name__				:						str     				=				[sd[k] for sd in state_dicts]
										__magic_name__				:						List[Any]     				=				sum(__A					) / len(__A					)
										assert isinstance(__A				,torch.Tensor					)
										__magic_name__				:						Any     				=				new_t
					return new_sd







def 		lowercase__ (		__A:						str				,__A:						str				,__A:						str					):





					'''simple docstring'''






					__magic_name__				:						List[Any]     				=				AutoModelForSeqaSeqLM.from_pretrained(__A					)
					if os.path.isfile(__A					):
										__magic_name__				:						str     				=				[pl_ckpt_path]
					else:
										assert os.path.isdir(__A					)
										__magic_name__				:						List[str]     				=				list(Path(__A					).glob('''*.ckpt'''					)					)
										assert ckpt_files, F'''could not find any ckpt files inside the {pl_ckpt_path} directory'''

					if len(__A					) > 1:
										logger.info(F'''averaging the weights of {ckpt_files}'''					)

					__magic_name__				:						Union[str, Any]     				=				[sanitize(torch.load(__A				,map_location='''cpu'''					)['''state_dict''']					) for x in ckpt_files]
					__magic_name__				:						Dict     				=				average_state_dicts(__A					)

					__magic_name__				,     __magic_name__				:						Union[str, Any]     				=				hf_model.load_state_dict(__A				,strict=__A					)
					assert not missing, F'''missing keys: {missing}'''
					hf_model.save_pretrained(__A					)
					try:
										__magic_name__				:						Optional[Any]     				=				AutoTokenizer.from_pretrained(__A					)
										tok.save_pretrained(__A					)
					except Exception:
										pass
										# dont copy tokenizer if cant


if __name__ == "__main__":
	fire.Fire(convert_pl_to_hf)



