





'''simple docstring'''






# We ignore warnings about stepping the scheduler since we step it ourselves during gradient accumulation

import warnings

from .state import AcceleratorState, GradientState


warnings.filterwarnings('ignore', category=UserWarning, module='torch.optim.lr_scheduler')







class 		UpperCAmelCase :


  """simple docstring"""







  def __init__(					self     :       Optional[Any]				,						UpperCamelCase__     :       Optional[int]				,						UpperCamelCase__     :       List[str]				,						UpperCamelCase__     :       Optional[int] = True				,						UpperCamelCase__     :       Dict = False	)  ->    str:
    _UpperCamelCase							      =scheduler
    _UpperCamelCase							      =optimizers if isinstance(UpperCamelCase__				,						(list, tuple)	) else [optimizers]
    _UpperCamelCase							      =split_batches
    _UpperCamelCase							      =step_with_optimizer
    _UpperCamelCase							      =GradientState()


  def 	UpperCamelCase__   (					self     :       List[Any]				,						*UpperCamelCase__     :       List[str]				,						**UpperCamelCase__     :       Dict	)  ->    List[Any]:






    if not self.step_with_optimizer:
      # No link between scheduler and optimizer -> just step
      self.scheduler.step(*UpperCamelCase__				,						**UpperCamelCase__	)
      return

    # Otherwise, first make sure the optimizer was stepped.
    if not self.gradient_state.sync_gradients:
      if self.gradient_state.adjust_scheduler:
        self.scheduler._step_count += 1
      return

    for opt in self.optimizers:
      if opt.step_was_skipped:
        return
    if self.split_batches:
      # Split batches -> the training dataloader batch size is not changed so one step per training step
      self.scheduler.step(*UpperCamelCase__				,						**UpperCamelCase__	)
    else:
      # Otherwise the training dataloader batch size was multiplied by `num_processes`, so we need to do
      # num_processes steps per training step
      _UpperCamelCase							      =AcceleratorState().num_processes
      for _ in range(UpperCamelCase__	):
        # Special case when using OneCycle and `drop_last` was not used
        if hasattr(self.scheduler				,						'''total_steps'''	):
          if self.scheduler._step_count <= self.scheduler.total_steps:
            self.scheduler.step(*UpperCamelCase__				,						**UpperCamelCase__	)
        else:
          self.scheduler.step(*UpperCamelCase__				,						**UpperCamelCase__	)


  def 	UpperCamelCase__   (					self     :       Dict	)  ->    str:
    return self.scheduler.get_last_lr()


  def 	UpperCamelCase__   (					self     :       Optional[int]	)  ->    Tuple:
    return self.scheduler.state_dict()


  def 	UpperCamelCase__   (					self     :       str				,						UpperCamelCase__     :       Any	)  ->    str:
    self.scheduler.load_state_dict(UpperCamelCase__	)


  def 	UpperCamelCase__   (					self     :       Optional[int]	)  ->    int:
    return self.scheduler.get_lr()




  def 	UpperCamelCase__   (					self     :       str				,						*UpperCamelCase__     :       str				,						**UpperCamelCase__     :       Union[str, Any]	)  ->    Optional[int]:
    return self.scheduler.print_lr(*UpperCamelCase__				,						**UpperCamelCase__	)









import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin


lowercase_   							=  '\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 					__lowerCAmelCase	(   unittest.TestCase     ,     SCREAMING_SNAKE_CASE				):




  def     A__	(							self   )    ->					Union[str, Any]:
        '''simple docstring'''






        _lowercase      			=load_tool('text-question-answering'   )
        self.tool.setup()
        _lowercase      			=load_tool('text-question-answering'				,  remote=lowerCAmelCase   )




  def     A__	(							self   )    ->					Dict:
        '''simple docstring'''






        _lowercase      			=self.tool(lowerCAmelCase				,  'What did Hugging Face do in April 2021?'   )
        self.assertEqual(lowerCAmelCase				,  'launched the BigScience Research Workshop'   )




  def     A__	(							self   )    ->					Optional[Any]:
        '''simple docstring'''






        _lowercase      			=self.remote_tool(lowerCAmelCase				,  'What did Hugging Face do in April 2021?'   )
        self.assertEqual(lowerCAmelCase				,  'launched the BigScience Research Workshop'   )




  def     A__	(							self   )    ->					Any:
        '''simple docstring'''






        _lowercase      			=self.tool(text=lowerCAmelCase				,  question='What did Hugging Face do in April 2021?'   )
        self.assertEqual(lowerCAmelCase				,  'launched the BigScience Research Workshop'   )




  def     A__	(							self   )    ->					Optional[int]:
        '''simple docstring'''






        _lowercase      			=self.remote_tool(text=lowerCAmelCase				,  question='What did Hugging Face do in April 2021?'   )
        self.assertEqual(lowerCAmelCase				,  'launched the BigScience Research Workshop'   )


