

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


SCREAMING_SNAKE_CASE_  :      List[Any]							=      namedtuple('''ExampleNamedTuple''', '''a b c''')


class 		_A							(      unittest.TestCase       ):





      def        _lowerCamelCase	(	self		) ->       List[Any]:
         lowerCamelCase__						       =      torch.randn(5     ,    2		)
         lowerCamelCase__						       =      torch.device("cuda"		) if torch.cuda.is_available() else torch.device("cpu"		)

         lowerCamelCase__						       =      send_to_device(SCREAMING_SNAKE_CASE__     ,    SCREAMING_SNAKE_CASE__		)
         self.assertTrue(torch.equal(resulta.cpu()     ,    SCREAMING_SNAKE_CASE__		)		)

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

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

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

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





      def        _lowerCamelCase	(	self		) ->       Optional[Any]:
         with self.assertRaises(SCREAMING_SNAKE_CASE__		) as cm:
            lowerCamelCase__						       =      recursively_apply(torch.tensor     ,    (torch.tensor(1		), 1)     ,    error_on_other_type=SCREAMING_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        _lowerCamelCase	(	self		) ->       Tuple:
         lowerCamelCase__						       =      torch.tensor([1, 2, 3, 4, 5]		)
         self.assertEqual(listify(SCREAMING_SNAKE_CASE__		)     ,    [1, 2, 3, 4, 5]		)

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

         lowerCamelCase__						       =      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(SCREAMING_SNAKE_CASE__		)     ,    [[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]], [[11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]]		)





      def        _lowerCamelCase	(	self		) ->       str:
         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        _lowerCamelCase	(	self		) ->       Optional[Any]:
         lowerCamelCase__						       =      RegressionModel()
         lowerCamelCase__						       =      model.forward
         lowerCamelCase__						       =      convert_outputs_to_fpaa(model.forward		)
         lowerCamelCase__						       =      extract_model_from_parallel(SCREAMING_SNAKE_CASE__     ,    keep_fpaa_wrapper=SCREAMING_SNAKE_CASE__		)
         lowerCamelCase__						       =      pickle.dumps(SCREAMING_SNAKE_CASE__		)





      @require_cuda
      def        _lowerCamelCase	(	self		) ->       Optional[int]:
         lowerCamelCase__						       =      RegressionModel()
         lowerCamelCase__						       =      model.forward
         lowerCamelCase__						       =      torch.cuda.amp.autocast(dtype=torch.floataa		)(model.forward		)
         lowerCamelCase__						       =      convert_outputs_to_fpaa(model.forward		)
         lowerCamelCase__						       =      extract_model_from_parallel(SCREAMING_SNAKE_CASE__     ,    keep_fpaa_wrapper=SCREAMING_SNAKE_CASE__		)
         lowerCamelCase__						       =      pickle.dumps(SCREAMING_SNAKE_CASE__		)





      @require_cuda
      @require_torch_min_version(version="2.0"		)
      def        _lowerCamelCase	(	self		) ->       Optional[int]:
         lowerCamelCase__						       =      RegressionModel()
         lowerCamelCase__						       =      model.forward
         lowerCamelCase__						       =      torch.cuda.amp.autocast(dtype=torch.floataa		)(model.forward		)
         lowerCamelCase__						       =      convert_outputs_to_fpaa(model.forward		)
         lowerCamelCase__						       =      torch.compile(model.forward     ,    backend="inductor"		)
         lowerCamelCase__						       =      torch.randn(4     ,    10		).cuda()
         lowerCamelCase__						       =      model(SCREAMING_SNAKE_CASE__		)





      def        _lowerCamelCase	(	self		) ->       Union[str, Any]:
         lowerCamelCase__						       =      RegressionModel()
         # could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
         lowerCamelCase__						       =      torch.nn.parallel.DataParallel(SCREAMING_SNAKE_CASE__		)
         lowerCamelCase__						       =      extract_model_from_parallel(SCREAMING_SNAKE_CASE__		)

         self.assertEqual(SCREAMING_SNAKE_CASE__     ,    SCREAMING_SNAKE_CASE__		)





      @require_torch_min_version(version="2.0"		)
      def        _lowerCamelCase	(	self		) ->       Tuple:
         lowerCamelCase__						       =      RegressionModel()
         lowerCamelCase__						       =      torch.compile(SCREAMING_SNAKE_CASE__		)

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

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





      def        _lowerCamelCase	(	self		) ->       int:
         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() = }''')




