






"""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      a						(					nn.Module		):






    def __init__(       self     :							Any					)     ->		Tuple:
      super().__init__()
      lowerCamelCase_         =						nn.Linear(3       ,						4					)
      lowerCamelCase_         =						nn.BatchNormad(4					)
      lowerCamelCase_         =						nn.Linear(4       ,						5					)






    def    UpperCamelCase       (       self     :							List[str]       ,						__SCREAMING_SNAKE_CASE     :							List[str]					)     ->		Optional[int]:
      return self.lineara(self.batchnorm(self.lineara(__SCREAMING_SNAKE_CASE					)					)					)






class      a						(					unittest.TestCase		):






    def    UpperCamelCase       (       self     :							List[str]					)     ->		Any:
      lowerCamelCase_         =						[]

      @find_executable_batch_size(starting_batch_size=128					)
      def mock_training_loop_function(__SCREAMING_SNAKE_CASE     :							List[str]					):
        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    UpperCamelCase       (       self     :							Optional[Any]					)     ->		List[str]:
      lowerCamelCase_         =						[]

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

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






    def    UpperCamelCase       (       self     :							Union[str, Any]					)     ->		Optional[Any]:
      @find_executable_batch_size(starting_batch_size=0					)
      def mock_training_loop_function(__SCREAMING_SNAKE_CASE     :							Union[str, Any]					):
        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    UpperCamelCase       (       self     :							Tuple					)     ->		List[str]:
      @find_executable_batch_size(starting_batch_size=16					)
      def mock_training_loop_function(__SCREAMING_SNAKE_CASE     :							Dict					):
        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    UpperCamelCase       (       self     :							Tuple					)     ->		Optional[Any]:
      @find_executable_batch_size(starting_batch_size=128					)
      def mock_training_loop_function(__SCREAMING_SNAKE_CASE     :							Optional[int]       ,						__SCREAMING_SNAKE_CASE     :							Tuple       ,						__SCREAMING_SNAKE_CASE     :							str					):
        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    UpperCamelCase       (       self     :							Dict					)     ->		int:
      @find_executable_batch_size(starting_batch_size=16					)
      def mock_training_loop_function(__SCREAMING_SNAKE_CASE     :							int					):
        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    UpperCamelCase       (       self     :							Any					)     ->		Dict:
      lowerCamelCase_         =						torch.cuda.memory_allocated()
      lowerCamelCase_         =						ModelForTest()
      model.cuda()
      self.assertGreater(torch.cuda.memory_allocated()       ,						__SCREAMING_SNAKE_CASE					)
      lowerCamelCase_         =						release_memory(__SCREAMING_SNAKE_CASE					)
      self.assertEqual(torch.cuda.memory_allocated()       ,						__SCREAMING_SNAKE_CASE					)












"""simple docstring"""





from cva import destroyAllWindows, imread, imshow, waitKey







def      lowerCamelCase__       (							_lowerCamelCase       :		Tuple )      ->				Dict:
  # getting number of pixels in the image
  lowerCamelCase_     ,	lowerCamelCase_         =						img.shape[0], img.shape[1]

  # converting each pixel's color to its negative
  for i in range(_lowerCamelCase ):
    for j in range(_lowerCamelCase ):
      lowerCamelCase_         =						[255, 255, 255] - img[i][j]

  return img


if __name__ == "__main__":
  # read original image
  _SCREAMING_SNAKE_CASE					:				List[Any]					    =						imread('''image_data/lena.jpg''', 1)

  # convert to its negative
  _SCREAMING_SNAKE_CASE					:				List[Any]					    =						convert_to_negative(img)

  # show result image
  imshow('''negative of original image''', img)
  waitKey(0)
  destroyAllWindows()




