

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


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



class        lowercase    (	unittest.TestCase			):


							def     _snake_case				(      self)				->					str:
								UpperCAmelCase_		:   str													=       torch.randn(5     ,      2)
								UpperCAmelCase_		:   Union[str, Any]													=       torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')

								UpperCAmelCase_		:   str													=       send_to_device(_snake_case     ,      _snake_case)
								self.assertTrue(torch.equal(resulta.cpu()     ,      _snake_case))

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

								UpperCAmelCase_		:   Dict													=       torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])
								self.assertEqual(listify(_snake_case)     ,      [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])

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


							def     _snake_case				(      self)				->					Dict:
								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)				->					Union[str, Any]:
								UpperCAmelCase_		:   Dict													=       RegressionModel()
								UpperCAmelCase_		:   List[str]													=       model.forward
								UpperCAmelCase_		:   Optional[Any]													=       convert_outputs_to_fpaa(model.forward)
								UpperCAmelCase_		:   List[str]													=       extract_model_from_parallel(_snake_case     ,      keep_fpaa_wrapper=_snake_case)
								UpperCAmelCase_		:   Optional[int]													=       pickle.dumps(_snake_case)


							@require_cuda
							def     _snake_case				(      self)				->					Optional[int]:
								UpperCAmelCase_		:   Union[str, Any]													=       RegressionModel()
								UpperCAmelCase_		:   Union[str, Any]													=       model.forward
								UpperCAmelCase_		:   Optional[int]													=       torch.cuda.amp.autocast(dtype=torch.floataa)(model.forward)
								UpperCAmelCase_		:   Tuple													=       convert_outputs_to_fpaa(model.forward)
								UpperCAmelCase_		:   Optional[int]													=       extract_model_from_parallel(_snake_case     ,      keep_fpaa_wrapper=_snake_case)
								UpperCAmelCase_		:   Dict													=       pickle.dumps(_snake_case)


							@require_cuda
							@require_torch_min_version(version='2.0')
							def     _snake_case				(      self)				->					Any:
								UpperCAmelCase_		:   Optional[int]													=       RegressionModel()
								UpperCAmelCase_		:   Dict													=       model.forward
								UpperCAmelCase_		:   Optional[int]													=       torch.cuda.amp.autocast(dtype=torch.floataa)(model.forward)
								UpperCAmelCase_		:   Optional[Any]													=       convert_outputs_to_fpaa(model.forward)
								UpperCAmelCase_		:   Optional[Any]													=       torch.compile(model.forward     ,      backend='inductor')
								UpperCAmelCase_		:   Optional[Any]													=       torch.randn(4     ,      10).cuda()
								UpperCAmelCase_		:   Optional[int]													=       model(_snake_case)


							def     _snake_case				(      self)				->					Optional[int]:
								UpperCAmelCase_		:   List[Any]													=       RegressionModel()
								# 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_		:   Union[str, Any]													=       extract_model_from_parallel(_snake_case)

								self.assertEqual(_snake_case     ,      _snake_case)


							@require_torch_min_version(version='2.0')
							def     _snake_case				(      self)				->					List[Any]:
								UpperCAmelCase_		:   Union[str, Any]													=       RegressionModel()
								UpperCAmelCase_		:   Optional[Any]													=       torch.compile(_snake_case)

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

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





							def     _snake_case				(      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







def     SCREAMING_SNAKE_CASE(	UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,UpperCamelCase	)     ->     int:

	# Base Case
	if counter == 0 or capacity == 0:
		return 0

	# If weight of the nth item is more than Knapsack of capacity,
	#   then this item cannot be included in the optimal solution,
	# else return the maximum of two cases:
	#   (1) nth item included
	#   (2) not included
	if weights[counter - 1] > capacity:
		return knapsack(UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,counter - 1	)
	else:
		UpperCAmelCase_		:   int													=       capacity - weights[counter - 1]
		UpperCAmelCase_		:   Optional[int]													=       values[counter - 1] + knapsack(
		    UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,counter - 1	)
		UpperCAmelCase_		:   Any													=       knapsack(UpperCamelCase ,UpperCamelCase ,UpperCamelCase ,counter - 1	)
		return max(UpperCamelCase ,UpperCamelCase	)


if __name__ == "__main__":
						import doctest

						doctest.testmod()






