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









"""simple docstring"""




from __future__ import annotations

import math
from itertools import permutations



def 						UpperCAmelCase__ (					A__ )   ->   bool:




   """simple docstring"""

   if 1 < number < 4:
      # 2 and 3 are primes
      return True
   elif number < 2 or number % 2 == 0 or number % 3 == 0:
      # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
      return False

   # All primes number are in format of 6k +/- 1
   for i in range(5						,     int(math.sqrt(A__ ) + 1 )						,     6 ):
      if number % i == 0 or number % (i + 2) == 0:
         return False
   return True



def 						UpperCAmelCase__ (					A__ = 7 )   ->   int:




   """simple docstring"""
   lowerCamelCase__						       =      "".join(str(A__ ) for i in range(1						,     n + 1 ) )
   lowerCamelCase__						       =      [int("".join(A__ ) ) for i in permutations(A__						,     A__ )]
   lowerCamelCase__						       =      [num for num in perm_list if is_prime(A__ )]
   return max(A__ ) if pandigitals else 0


if __name__ == "__main__":
     print(F'''{solution() = }''')




