






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


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



class 						lowerCamelCase   (unittest.TestCase    ):







  '''simple docstring'''



  def       __UpperCAmelCase    (							self     )			->							Union[str, Any]:
       UpperCAmelCase_					:   Optional[Any]   =		torch.randn(5       ,	2     )
       UpperCAmelCase_					:   List[str]   =		torch.device('cuda'     ) if torch.cuda.is_available() else torch.device('cpu'     )

       UpperCAmelCase_					:   Optional[int]   =		send_to_device(_UpperCamelCase       ,	_UpperCamelCase     )
       self.assertTrue(torch.equal(resulta.cpu()       ,	_UpperCamelCase     )     )

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

       UpperCAmelCase_					:   Any   =		torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]     )
       self.assertEqual(listify(_UpperCamelCase     )       ,	[[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(_UpperCamelCase     )       ,	[[[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[int]:
       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     )			->							Optional[int]:
       UpperCAmelCase_					:   List[Any]   =		RegressionModel()
       UpperCAmelCase_					:   List[str]   =		model.forward
       UpperCAmelCase_					:   Optional[int]   =		convert_outputs_to_fpaa(model.forward     )
       UpperCAmelCase_					:   Tuple   =		extract_model_from_parallel(_UpperCamelCase       ,	keep_fpaa_wrapper=_UpperCamelCase     )
       UpperCAmelCase_					:   Optional[Any]   =		pickle.dumps(_UpperCamelCase     )



  @require_cuda
  def       __UpperCAmelCase    (							self     )			->							Dict:
       UpperCAmelCase_					:   int   =		RegressionModel()
       UpperCAmelCase_					:   Optional[int]   =		model.forward
       UpperCAmelCase_					:   int   =		torch.cuda.amp.autocast(dtype=torch.floataa     )(model.forward     )
       UpperCAmelCase_					:   Tuple   =		convert_outputs_to_fpaa(model.forward     )
       UpperCAmelCase_					:   List[Any]   =		extract_model_from_parallel(_UpperCamelCase       ,	keep_fpaa_wrapper=_UpperCamelCase     )
       UpperCAmelCase_					:   Tuple   =		pickle.dumps(_UpperCamelCase     )



  @require_cuda
  @require_torch_min_version(version='2.0'     )
  def       __UpperCAmelCase    (							self     )			->							Optional[int]:
       UpperCAmelCase_					:   Union[str, Any]   =		RegressionModel()
       UpperCAmelCase_					:   Any   =		model.forward
       UpperCAmelCase_					:   str   =		torch.cuda.amp.autocast(dtype=torch.floataa     )(model.forward     )
       UpperCAmelCase_					:   Any   =		convert_outputs_to_fpaa(model.forward     )
       UpperCAmelCase_					:   int   =		torch.compile(model.forward       ,	backend='inductor'     )
       UpperCAmelCase_					:   Tuple   =		torch.randn(4       ,	1_0     ).cuda()
       UpperCAmelCase_					:   Optional[int]   =		model(_UpperCamelCase     )



  def       __UpperCAmelCase    (							self     )			->							Tuple:
       UpperCAmelCase_					:   List[Any]   =		RegressionModel()
       # could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
       UpperCAmelCase_					:   Tuple   =		torch.nn.parallel.DataParallel(_UpperCamelCase     )
       UpperCAmelCase_					:   Tuple   =		extract_model_from_parallel(_UpperCamelCase     )

       self.assertEqual(_UpperCamelCase       ,	_UpperCamelCase     )



  @require_torch_min_version(version='2.0'     )
  def       __UpperCAmelCase    (							self     )			->							Optional[int]:
       UpperCAmelCase_					:   Tuple   =		RegressionModel()
       UpperCAmelCase_					:   int   =		torch.compile(_UpperCamelCase     )

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

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



  def       __UpperCAmelCase    (							self     )			->							str:
       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']     )     )














def   lowercase__   (  __snake_case					:							int  , __snake_case					:							int  , __snake_case					:							int  , __snake_case					:							int						):


					'''simple docstring'''


					return len_board * len_board_column * row + column




def   lowercase__   (  __snake_case					:							list[list[str]]  , __snake_case					:							str  , __snake_case					:							int  , __snake_case					:							int  , __snake_case					:							int  , __snake_case					:							set[int]  , ):


					'''simple docstring'''


					if board[row][column] != word[word_index]:
										return False

					if word_index == len(__snake_case						) - 1:
										return True

					UpperCAmelCase_					:   Dict   =		[(0, 1), (0, -1), (-1, 0), (1, 0)]
					UpperCAmelCase_					:   Union[str, Any]   =		len(__snake_case						)
					UpperCAmelCase_					:   List[Any]   =		len(board[0]						)
					for direction in traverts_directions:
										UpperCAmelCase_					:   Optional[int]   =		row + direction[0]
										UpperCAmelCase_					:   Optional[int]   =		column + direction[1]
										if not (0 <= next_i < len_board and 0 <= next_j < len_board_column):
															continue

										UpperCAmelCase_					:   Tuple   =		get_point_key(__snake_case  , __snake_case  , __snake_case  , __snake_case						)
										if key in visited_points_set:
															continue

										visited_points_set.add(__snake_case						)
										if exits_word(__snake_case  , __snake_case  , __snake_case  , __snake_case  , word_index + 1  , __snake_case						):
															return True

										visited_points_set.remove(__snake_case						)

					return False




def   lowercase__   (  __snake_case					:							list[list[str]]  , __snake_case					:							str						):


					'''simple docstring'''


					UpperCAmelCase_					:   Any   =		(
					    'The board should be a non empty matrix of single chars strings.'
					)

					UpperCAmelCase_					:   Optional[Any]   =		len(__snake_case						)
					if not isinstance(__snake_case  , __snake_case						) or len(__snake_case						) == 0:
										raise ValueError(__snake_case						)

					for row in board:
										if not isinstance(__snake_case  , __snake_case						) or len(__snake_case						) == 0:
															raise ValueError(__snake_case						)

										for item in row:
															if not isinstance(__snake_case  , __snake_case						) or len(__snake_case						) != 1:
																				raise ValueError(__snake_case						)

    # Validate word
					if not isinstance(__snake_case  , __snake_case						) or len(__snake_case						) == 0:
										raise ValueError(
										    'The word parameter should be a string of length greater than 0.'						)

					UpperCAmelCase_					:   Any   =		len(board[0]						)
					for i in range(__snake_case						):
										for j in range(__snake_case						):
															if exits_word(
															    __snake_case  , __snake_case  , __snake_case  , __snake_case  , 0  , {get_point_key(__snake_case  , __snake_case  , __snake_case  , __snake_case						)}						):
																				return True

					return False


if __name__ == "__main__":
		import doctest

		doctest.testmod()






