





'''simple docstring'''




import unittest

import torch
from torch import nn

from accelerate.test_utils import require_cuda
from accelerate.utils.memory import find_executable_batch_size, release_memory






def        _lowerCAmelCase			(  )     ->     Dict:
     raise RuntimeError('CUDA out of memory.'			)


class  SCREAMING_SNAKE_CASE    (nn.Module							):


     def __init__(						self):

          '''simple docstring'''






          super().__init__()
          __A						: Any      =       nn.Linear(3						,	4)
          __A						: Tuple      =       nn.BatchNormad(4)
          __A						: Union[str, Any]      =       nn.Linear(4						,	5)


     def 							SCREAMING_SNAKE_CASE  (						self						,	_UpperCAmelCase):

          '''simple docstring'''






          return self.lineara(self.batchnorm(self.lineara(__SCREAMING_SNAKE_CASE)))




class  SCREAMING_SNAKE_CASE    (unittest.TestCase							):


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Tuple      =       []

          @find_executable_batch_size(starting_batch_size=128)
          def mock_training_loop_function(_UpperCAmelCase):
               nonlocal batch_sizes
               batch_sizes.append(__SCREAMING_SNAKE_CASE)
               if batch_size != 8:
                    raise_fake_out_of_memory()

          mock_training_loop_function()
          self.assertListEqual(__SCREAMING_SNAKE_CASE						,	[128, 64, 32, 16, 8])


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Tuple      =       []

          @find_executable_batch_size(starting_batch_size=128)
          def mock_training_loop_function(_UpperCAmelCase						,	_UpperCAmelCase):
               nonlocal batch_sizes
               batch_sizes.append(__SCREAMING_SNAKE_CASE)
               if batch_size != 8:
                    raise_fake_out_of_memory()
               return batch_size, arga

          __A ,__A						: Optional[Any]      =       mock_training_loop_function('hello')
          self.assertListEqual(__SCREAMING_SNAKE_CASE						,	[128, 64, 32, 16, 8])
          self.assertListEqual([bs, arga]						,	[8, 'hello'])


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          @find_executable_batch_size(starting_batch_size=0)
          def mock_training_loop_function(_UpperCAmelCase):
               pass

          with self.assertRaises(__SCREAMING_SNAKE_CASE) as cm:
               mock_training_loop_function()
               self.assertIn('No executable batch size found, reached zero.'						,	cm.exception.args[0])


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          @find_executable_batch_size(starting_batch_size=16)
          def mock_training_loop_function(_UpperCAmelCase):
               if batch_size > 0:
                    raise_fake_out_of_memory()
               pass

          with self.assertRaises(__SCREAMING_SNAKE_CASE) as cm:
               mock_training_loop_function()
               self.assertIn('No executable batch size found, reached zero.'						,	cm.exception.args[0])


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          @find_executable_batch_size(starting_batch_size=128)
          def mock_training_loop_function(_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase):
               if batch_size != 8:
                    raise raise_fake_out_of_memory()

          with self.assertRaises(__SCREAMING_SNAKE_CASE) as cm:
               mock_training_loop_function(128						,	'hello'						,	'world')
               self.assertIn('Batch size was passed into `f`'						,	cm.exception.args[0])
               self.assertIn('`f(arg1=\'hello\', arg2=\'world\')'						,	cm.exception.args[0])


     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          @find_executable_batch_size(starting_batch_size=16)
          def mock_training_loop_function(_UpperCAmelCase):
               raise ValueError('Oops, we had an error!')

          with self.assertRaises(__SCREAMING_SNAKE_CASE) as cm:
               mock_training_loop_function()
               self.assertIn('Oops, we had an error!'						,	cm.exception.args[0])


     @require_cuda
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: str      =       torch.cuda.memory_allocated()
          __A						: List[str]      =       ModelForTest()
          model.cuda()
          self.assertGreater(torch.cuda.memory_allocated()						,	__SCREAMING_SNAKE_CASE)
          __A						: Any      =       release_memory(__SCREAMING_SNAKE_CASE)
          self.assertEqual(torch.cuda.memory_allocated()						,	__SCREAMING_SNAKE_CASE)







"""simple docstring"""





def      lowerCamelCase__       (							_lowerCamelCase       :		int		,				_lowerCamelCase       :		int )      ->				int:
  lowerCamelCase_         =						1  # To kept the Calculated Value
  # Since C(n, k) = C(n, n-k)
  if k > (n - k):
    lowerCamelCase_         =						n - k
  # Calculate C(n,k)
  for i in range(_lowerCamelCase ):
    result *= n - i
    result //= i + 1
  return result







def      lowerCamelCase__       (							_lowerCamelCase       :		int )      ->				int:
  return binomial_coefficient(2 * node_count		,				_lowerCamelCase ) // (node_count + 1)







def      lowerCamelCase__       (							_lowerCamelCase       :		int )      ->				int:
  if n < 0:
    raise ValueError('factorial() not defined for negative values' )
  lowerCamelCase_         =						1
  for i in range(1		,				n + 1 ):
    result *= i
  return result







def      lowerCamelCase__       (							_lowerCamelCase       :		int )      ->				int:
  return catalan_number(_lowerCamelCase ) * factorial(_lowerCamelCase )


if __name__ == "__main__":
  _SCREAMING_SNAKE_CASE					:				int					    =						int(input('''Enter the number of nodes: ''').strip() or 0)
  if node_count <= 0:
    raise ValueError('''We need some nodes to work with.''')
  print(
      F'''Given {node_count} nodes, there are {binary_tree_count(node_count)} '''
      F'''binary trees and {catalan_number(node_count)} binary search trees.'''
  )




