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


a_           =    namedtuple("ExampleNamedTuple", "a b c")






class  UpperCAmelCase_					(						unittest.TestCase       ):





 def 							snake_case__		(     self):
     snake_case_			:     str        =    torch.randn(5			,							2)
     snake_case_			:     str        =    torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")

     snake_case_			:     Union[str, Any]        =    send_to_device(lowercase_			,							lowercase_)
     self.assertTrue(torch.equal(resulta.cpu()			,							lowercase_))

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

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

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

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





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

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

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





 def 							snake_case__		(     self):
     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 							snake_case__		(     self):
     snake_case_			:     Tuple        =    RegressionModel()
     snake_case_			:     Optional[Any]        =    model.forward
     snake_case_			:     int        =    convert_outputs_to_fpaa(model.forward)
     snake_case_			:     Optional[int]        =    extract_model_from_parallel(lowercase_			,							keep_fpaa_wrapper=lowercase_)
     snake_case_			:     List[Any]        =    pickle.dumps(lowercase_)





 @require_cuda
 def 							snake_case__		(     self):
     snake_case_			:     int        =    RegressionModel()
     snake_case_			:     str        =    model.forward
     snake_case_			:     Tuple        =    torch.cuda.amp.autocast(dtype=torch.floataa)(model.forward)
     snake_case_			:     List[Any]        =    convert_outputs_to_fpaa(model.forward)
     snake_case_			:     Optional[Any]        =    extract_model_from_parallel(lowercase_			,							keep_fpaa_wrapper=lowercase_)
     snake_case_			:     str        =    pickle.dumps(lowercase_)





 @require_cuda
 @require_torch_min_version(version="2.0")
 def 							snake_case__		(     self):
     snake_case_			:     int        =    RegressionModel()
     snake_case_			:     Union[str, Any]        =    model.forward
     snake_case_			:     Dict        =    torch.cuda.amp.autocast(dtype=torch.floataa)(model.forward)
     snake_case_			:     Optional[int]        =    convert_outputs_to_fpaa(model.forward)
     snake_case_			:     int        =    torch.compile(model.forward			,							backend="inductor")
     snake_case_			:     Union[str, Any]        =    torch.randn(4			,							10).cuda()
     snake_case_			:     Union[str, Any]        =    model(lowercase_)





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

     self.assertEqual(lowercase_			,							lowercase_)





 @require_torch_min_version(version="2.0")
 def 							snake_case__		(     self):
     snake_case_			:     Optional[Any]        =    RegressionModel()
     snake_case_			:     Any        =    torch.compile(lowercase_)

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

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





 def 							snake_case__		(     self):
     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'''
def     UpperCamelCase_  (						__SCREAMING_SNAKE_CASE							):
    """simple docstring"""






    if number != int(__SCREAMING_SNAKE_CASE							):
        raise ValueError("factorial() only accepts integral values"							)
    if number < 0:
        raise ValueError("factorial() not defined for negative values"							)
    snake_case_			:     int        =    1
    for i in range(1,		number + 1							):
        value *= i
    return value

def     UpperCamelCase_  (						__SCREAMING_SNAKE_CASE							):
    """simple docstring"""






    if not isinstance(__SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE							):
        raise ValueError("factorial() only accepts integral values"							)
    if n < 0:
        raise ValueError("factorial() not defined for negative values"							)
    return 1 if n == 0 or n == 1 else n * factorial(n - 1							)


if __name__ == "__main__":
  import doctest

  doctest.testmod()

  a_           =    int(input("Enter a positive integer: ").strip() or 0)
  print(f"""factorial{n} is {factorial(n)}""")




