
"""simple docstring"""





import math
from enum import Enum
from typing import Optional, Union

from torch.optim import Optimizer
from torch.optim.lr_scheduler import LambdaLR

from .utils import logging


UpperCamelCase_					:						Optional[int]				= logging.get_logger(__name__)



class __lowerCAmelCase				(					_snake_case      ):




   """simple docstring"""







   snake_case	      =				'linear'
   snake_case	      =				'cosine'
   snake_case	      =				'cosine_with_restarts'
   snake_case	      =				'polynomial'
   snake_case	      =				'constant'
   snake_case	      =				'constant_with_warmup'
   snake_case	      =				'piecewise_constant'





def 							A_     (__a				,   __a = -1				):

     '''simple docstring'''





     return LambdaLR(__UpperCAmelCase				,   lambda __a				: 1				,   last_epoch=__UpperCAmelCase				)
def 							A_     (__a				,   __a				,   __a = -1				):

     '''simple docstring'''





     def lr_lambda(__a				):
          if current_step < num_warmup_steps:
               return float(__UpperCAmelCase				) / float(max(1.0				,   __UpperCAmelCase				)				)
          return 1.0

     return LambdaLR(__UpperCAmelCase				,   __UpperCAmelCase				,   last_epoch=__UpperCAmelCase				)
def 							A_     (__a				,   __a				,   __a = -1				):

     '''simple docstring'''





     A_				 =						{}
     A_				 =						step_rules.split(","				)
     for rule_str in rule_list[:-1]:
          A_ ,    A_				 =						rule_str.split(":"				)
          A_				 =						int(__UpperCAmelCase				)
          A_				 =						float(__UpperCAmelCase				)
          A_				 =						value
     A_				 =						float(rule_list[-1]				)

     def create_rules_function(__a				,   __a				):
          def rule_func(__a				) -> float:
               A_				 =						sorted(rules_dict.keys()				)
               for i, sorted_step in enumerate(__UpperCAmelCase				):
                    if steps < sorted_step:
                         return rules_dict[sorted_steps[i]]
               return last_lr_multiple

          return rule_func

     A_				 =						create_rules_function(__UpperCAmelCase				,   __UpperCAmelCase				)

     return LambdaLR(__UpperCAmelCase				,   __UpperCAmelCase				,   last_epoch=__UpperCAmelCase				)
def 							A_     (__a				,   __a				,   __a				,   __a=-1				):

     '''simple docstring'''





     def lr_lambda(__a				):
          if current_step < num_warmup_steps:
               return float(__UpperCAmelCase				) / float(max(1				,   __UpperCAmelCase				)				)
          return max(
              0.0				,   float(num_training_steps - current_step				) / float(max(1				,   num_training_steps - num_warmup_steps				)				)				)

     return LambdaLR(__UpperCAmelCase				,   __UpperCAmelCase				,   __UpperCAmelCase				)
def 							A_     (__a				,   __a				,   __a				,   __a = 0.5				,   __a = -1				):

     '''simple docstring'''





     def lr_lambda(__a				):
          if current_step < num_warmup_steps:
               return float(__UpperCAmelCase				) / float(max(1				,   __UpperCAmelCase				)				)
          A_				 =						float(current_step - num_warmup_steps				) / float(max(1				,   num_training_steps - num_warmup_steps				)				)
          return max(0.0				,   0.5 * (1.0 + math.cos(math.pi * float(__UpperCAmelCase				) * 2.0 * progress				))				)

     return LambdaLR(__UpperCAmelCase				,   __UpperCAmelCase				,   __UpperCAmelCase				)
def 							A_     (__a				,   __a				,   __a				,   __a = 1				,   __a = -1				):

     '''simple docstring'''





     def lr_lambda(__a				):
          if current_step < num_warmup_steps:
               return float(__UpperCAmelCase				) / float(max(1				,   __UpperCAmelCase				)				)
          A_				 =						float(current_step - num_warmup_steps				) / float(max(1				,   num_training_steps - num_warmup_steps				)				)
          if progress >= 1.0:
               return 0.0
          return max(0.0				,   0.5 * (1.0 + math.cos(math.pi * ((float(__UpperCAmelCase				) * progress) % 1.0)				))				)

     return LambdaLR(__UpperCAmelCase				,   __UpperCAmelCase				,   __UpperCAmelCase				)
def 							A_     (__a				,   __a				,   __a				,   __a=1e-7				,   __a=1.0				,   __a=-1				):

     '''simple docstring'''





     A_				 =						optimizer.defaults["lr"]
     if not (lr_init > lr_end):
          raise ValueError(f'lr_end ({lr_end}) must be be smaller than initial lr ({lr_init})'				)

     def lr_lambda(__a				):
          if current_step < num_warmup_steps:
               return float(__UpperCAmelCase				) / float(max(1				,   __UpperCAmelCase				)				)
          elif current_step > num_training_steps:
               return lr_end / lr_init  # as LambdaLR multiplies by lr_init
          else:
               A_				 =						lr_init - lr_end
               A_				 =						num_training_steps - num_warmup_steps
               A_				 =						1 - (current_step - num_warmup_steps) / decay_steps
               A_				 =						lr_range * pct_remaining**power + lr_end
               return decay / lr_init  # as LambdaLR multiplies by lr_init

     return LambdaLR(__UpperCAmelCase				,   __UpperCAmelCase				,   __UpperCAmelCase				)


UpperCamelCase_					:						Optional[int]				= {
    SchedulerType.LINEAR: get_linear_schedule_with_warmup,
    SchedulerType.COSINE: get_cosine_schedule_with_warmup,
    SchedulerType.COSINE_WITH_RESTARTS: get_cosine_with_hard_restarts_schedule_with_warmup,
    SchedulerType.POLYNOMIAL: get_polynomial_decay_schedule_with_warmup,
    SchedulerType.CONSTANT: get_constant_schedule,
    SchedulerType.CONSTANT_WITH_WARMUP: get_constant_schedule_with_warmup,
    SchedulerType.PIECEWISE_CONSTANT: get_piecewise_constant_schedule,
}




def 							A_     (__a				,   __a				,   __a = None				,   __a = None				,   __a = None				,   __a = 1				,   __a = 1.0				,   __a = -1				,   ):

     '''simple docstring'''





     A_				 =						SchedulerType(__UpperCAmelCase				)
     A_				 =						TYPE_TO_SCHEDULER_FUNCTION[name]
     if name == SchedulerType.CONSTANT:
          return schedule_func(__UpperCAmelCase				,   last_epoch=__UpperCAmelCase				)

     if name == SchedulerType.PIECEWISE_CONSTANT:
          return schedule_func(__UpperCAmelCase				,   step_rules=__UpperCAmelCase				,   last_epoch=__UpperCAmelCase				)

     # All other schedulers require `num_warmup_steps`
     if num_warmup_steps is None:
          raise ValueError(f'{name} requires `num_warmup_steps`, please provide that argument.'				)

     if name == SchedulerType.CONSTANT_WITH_WARMUP:
          return schedule_func(__UpperCAmelCase				,   num_warmup_steps=__UpperCAmelCase				,   last_epoch=__UpperCAmelCase				)

     # All other schedulers require `num_training_steps`
     if num_training_steps is None:
          raise ValueError(f'{name} requires `num_training_steps`, please provide that argument.'				)

     if name == SchedulerType.COSINE_WITH_RESTARTS:
          return schedule_func(
              __UpperCAmelCase				,   num_warmup_steps=__UpperCAmelCase				,   num_training_steps=__UpperCAmelCase				,   num_cycles=__UpperCAmelCase				,   last_epoch=__UpperCAmelCase				,   )

     if name == SchedulerType.POLYNOMIAL:
          return schedule_func(
              __UpperCAmelCase				,   num_warmup_steps=__UpperCAmelCase				,   num_training_steps=__UpperCAmelCase				,   power=__UpperCAmelCase				,   last_epoch=__UpperCAmelCase				,   )

     return schedule_func(
         __UpperCAmelCase				,   num_warmup_steps=__UpperCAmelCase				,   num_training_steps=__UpperCAmelCase				,   last_epoch=__UpperCAmelCase				)






"""simple docstring"""





import unittest

from transformers import load_tool

from .test_tools_common import ToolTesterMixin


UpperCamelCase_					:						int				= '''
Hugging 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.

In March 2021, Hugging Face raised $40 million in a Series B funding round.[3]

On 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]
'''



class __lowerCAmelCase				(					unittest.TestCase     ,				_lowercase      ):




			"""simple docstring"""







			def      lowerCamelCase__     (				self						:					str	)      -> Dict:







								"""simple docstring"""
								A_				 =						load_tool("text-question-answering"	)
								self.tool.setup()
								A_				 =						load_tool("text-question-answering"       ,   remote=_snake_case	)




			def      lowerCamelCase__     (				self						:					Union[str, Any]	)      -> Any:







								"""simple docstring"""
								A_				 =						self.tool(_snake_case       ,   "What did Hugging Face do in April 2021?"	)
								self.assertEqual(_snake_case       ,   "launched the BigScience Research Workshop"	)




			def      lowerCamelCase__     (				self						:					Union[str, Any]	)      -> int:







								"""simple docstring"""
								A_				 =						self.remote_tool(_snake_case       ,   "What did Hugging Face do in April 2021?"	)
								self.assertEqual(_snake_case       ,   "launched the BigScience Research Workshop"	)




			def      lowerCamelCase__     (				self						:					Optional[Any]	)      -> Optional[Any]:







								"""simple docstring"""
								A_				 =						self.tool(text=_snake_case       ,   question="What did Hugging Face do in April 2021?"	)
								self.assertEqual(_snake_case       ,   "launched the BigScience Research Workshop"	)






			def      lowerCamelCase__     (				self						:					str	)      -> Optional[Any]:







								"""simple docstring"""
								A_				 =						self.remote_tool(text=_snake_case       ,   question="What did Hugging Face do in April 2021?"	)
								self.assertEqual(_snake_case       ,   "launched the BigScience Research Workshop"	)




