

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 					_UpperCAmelCase       (  ):





    """simple docstring"""


    raise RuntimeError('CUDA out of memory.'       )



class       __snake_case  (    nn.Module       ):

  def __init__(					self):
      super().__init__()
      SCREAMING_SNAKE_CASE_									=						nn.Linear(3						,		4)
      SCREAMING_SNAKE_CASE_									=						nn.BatchNormad(4)
      SCREAMING_SNAKE_CASE_									=						nn.Linear(4						,		5)


  def 						lowerCAmelCase__     (					self						,		_A):
      return self.lineara(self.batchnorm(self.lineara(_SCREAMING_SNAKE_CASE)))






class       __snake_case  (    unittest.TestCase       ):

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						[]

      @find_executable_batch_size(starting_batch_size=128)
      def mock_training_loop_function(_A):
          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 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						[]

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

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

  def 						lowerCAmelCase__     (					self):
      @find_executable_batch_size(starting_batch_size=0)
      def mock_training_loop_function(_A):
          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 						lowerCAmelCase__     (					self):
      @find_executable_batch_size(starting_batch_size=16)
      def mock_training_loop_function(_A):
          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 						lowerCAmelCase__     (					self):
      @find_executable_batch_size(starting_batch_size=128)
      def mock_training_loop_function(_A						,		_A						,		_A):
          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 						lowerCAmelCase__     (					self):
      @find_executable_batch_size(starting_batch_size=16)
      def mock_training_loop_function(_A):
          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 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						torch.cuda.memory_allocated()
      SCREAMING_SNAKE_CASE_									=						ModelForTest()
      model.cuda()
      self.assertGreater(torch.cuda.memory_allocated()						,		_SCREAMING_SNAKE_CASE)
      SCREAMING_SNAKE_CASE_									=						release_memory(_SCREAMING_SNAKE_CASE)
      self.assertEqual(torch.cuda.memory_allocated()						,		_SCREAMING_SNAKE_CASE)






import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin


UpperCamelCase__  :      int			=    "\nHugging Face was founded in 2016 by French entrepreneurs Clément Delangue, Julien Chaumond, and Thomas Wolf originally as a company that developed a chatbot app targeted at teenagers.[2] After open-sourcing the model behind the chatbot, the company pivoted to focus on being a platform for machine learning.\n\nIn March 2021, Hugging Face raised $40 million in a Series B funding round.[3]\n\nOn April 28, 2021, the company launched the BigScience Research Workshop in collaboration with several other research groups to release an open large language model.[4] In 2022, the workshop concluded with the announcement of BLOOM, a multilingual large language model with 176 billion parameters.[5]\n"



class       __snake_case  (    unittest.TestCase     , lowerCAmelCase__       ):

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						load_tool('text-question-answering')
      self.tool.setup()
      SCREAMING_SNAKE_CASE_									=						load_tool('text-question-answering'						,		remote=_A)

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.tool(_A						,		'What did Hugging Face do in April 2021?')
      self.assertEqual(_A						,		'launched the BigScience Research Workshop')

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.remote_tool(_A						,		'What did Hugging Face do in April 2021?')
      self.assertEqual(_A						,		'launched the BigScience Research Workshop')

  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.tool(text=_A						,		question='What did Hugging Face do in April 2021?')
      self.assertEqual(_A						,		'launched the BigScience Research Workshop')


  def 						lowerCAmelCase__     (					self):
      SCREAMING_SNAKE_CASE_									=						self.remote_tool(text=_A						,		question='What did Hugging Face do in April 2021?')
      self.assertEqual(_A						,		'launched the BigScience Research Workshop')


