


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


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

class  lowercase_  ( unittest.TestCase						):







      def 				_lowerCAmelCase       (      self :							Any    ):
        lowerCAmelCase__    :    List[Any]								=					torch.randn(5		,  2    )
        lowerCAmelCase__    :    Union[str, Any]								=					torch.device("cuda"    ) if torch.cuda.is_available() else torch.device("cpu"    )

        lowerCAmelCase__    :    str								=					send_to_device(_lowercase		,  _lowercase    )
        self.assertTrue(torch.equal(resulta.cpu()		,  _lowercase    )    )

        lowerCAmelCase__    :    List[Any]								=					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    )

        lowerCAmelCase__    :    List[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    )

        lowerCAmelCase__    :    str								=					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    )

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

        lowerCAmelCase__    :    Any								=					torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]    )
        self.assertEqual(listify(_lowercase    )		,  [[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]    )

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







      @require_cuda
      def 				_lowerCAmelCase       (      self :							Dict    ):
        lowerCAmelCase__    :    Optional[Any]								=					RegressionModel()
        lowerCAmelCase__    :    Optional[int]								=					model.forward
        lowerCAmelCase__    :    Dict								=					torch.cuda.amp.autocast(dtype=torch.floataa    )(model.forward    )
        lowerCAmelCase__    :    List[str]								=					convert_outputs_to_fpaa(model.forward    )
        lowerCAmelCase__    :    Dict								=					extract_model_from_parallel(_lowercase		,  keep_fpaa_wrapper=_lowercase    )
        lowerCAmelCase__    :    int								=					pickle.dumps(_lowercase    )







      @require_cuda
      @require_torch_min_version(version="2.0"    )
      def 				_lowerCAmelCase       (      self :							int    ):
        lowerCAmelCase__    :    Dict								=					RegressionModel()
        lowerCAmelCase__    :    int								=					model.forward
        lowerCAmelCase__    :    Union[str, Any]								=					torch.cuda.amp.autocast(dtype=torch.floataa    )(model.forward    )
        lowerCAmelCase__    :    List[str]								=					convert_outputs_to_fpaa(model.forward    )
        lowerCAmelCase__    :    int								=					torch.compile(model.forward		,  backend="inductor"    )
        lowerCAmelCase__    :    List[Any]								=					torch.randn(4		,  1_0    ).cuda()
        lowerCAmelCase__    :    Tuple								=					model(_lowercase    )







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

        self.assertEqual(_lowercase		,  _lowercase    )







      @require_torch_min_version(version="2.0"    )
      def 				_lowerCAmelCase       (      self :							List[Any]    ):
        lowerCAmelCase__    :    int								=					RegressionModel()
        lowerCAmelCase__    :    Union[str, Any]								=					torch.compile(_lowercase    )

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

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







      def 				_lowerCAmelCase       (      self :							List[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"]    )    )




"""simple docstring"""





from math import factorial





def     lowercase__	(  lowerCamelCase     :  Dict				,	lowerCamelCase     :  int				)					->     Tuple:
  return factorial(lowerCamelCase				) / (factorial(lowerCamelCase				) * factorial(n - r				))





def     lowercase__	(  )					->     Optional[Any]:
  lowerCAmelCase__    :    List[Any]								=					0

  for i in range(1				,	1_0_1				):
    for j in range(1				,	i + 1				):
      if combinations(lowerCamelCase				,	lowerCamelCase				) > 1e6:
        total += 1
  return total


if __name__ == "__main__":
   print(solution())
