


'''simple docstring'''
import os
import tempfile
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
		import torch
		from torch import nn

		from transformers import (
		    Adafactor,
		    AdamW,
		    get_constant_schedule,
		    get_constant_schedule_with_warmup,
		    get_cosine_schedule_with_warmup,
		    get_cosine_with_hard_restarts_schedule_with_warmup,
		    get_inverse_sqrt_schedule,
		    get_linear_schedule_with_warmup,
		    get_polynomial_decay_schedule_with_warmup,
		)






def       a				(      __a			, __a=10       )  ->     Tuple:



					'''simple docstring'''






					UpperCamelCase__       :Dict							 =			[]
					for _ in range(__a       ):
										lrs.append(scheduler.get_lr()[0]       )
										scheduler.step()
					return lrs






def       a				(      __a			, __a=10       )  ->     List[Any]:



					'''simple docstring'''






					UpperCamelCase__       :Union[str, Any]							 =			[]
					for step in range(__a       ):
										lrs.append(scheduler.get_lr()[0]       )
										scheduler.step()
										if step == num_steps // 2:
															with tempfile.TemporaryDirectory() as tmpdirname:
																				UpperCamelCase__       :List[str]							 =			os.path.join(__a			, '''schedule.bin'''       )
																				torch.save(scheduler.state_dict()			, __a       )

																				UpperCamelCase__       :Any							 =			torch.load(__a       )
																				scheduler.load_state_dict(__a       )
					return lrs
@require_torch
class        lowercase     (							unittest.TestCase ):

		"""simple docstring"""

		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_							):

							'''simple docstring'''


							self.assertEqual(len(UpperCamelCase_							)   ,       len(UpperCamelCase_							)							)
							for a, b in zip(UpperCamelCase_   ,       UpperCamelCase_							):
												self.assertAlmostEqual(UpperCamelCase_   ,       UpperCamelCase_   ,       delta=UpperCamelCase_							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Dict							 =			torch.tensor([0.1, -0.2, -0.1]   ,       requires_grad=UpperCamelCase_							)
							UpperCamelCase__       :int							 =			torch.tensor([0.4, 0.2, -0.5]							)
							UpperCamelCase__       :Optional[int]							 =			nn.MSELoss()
							# No warmup, constant schedule, no gradient clipping
							UpperCamelCase__       :Union[str, Any]							 =			AdamW(params=[w]   ,       lr=2e-1   ,       weight_decay=0.0							)
							for _ in range(100							):
												UpperCamelCase__       :Optional[Any]							 =			criterion(UpperCamelCase_   ,       UpperCamelCase_							)
												loss.backward()
												optimizer.step()
												w.grad.detach_()  # No zero_grad() function on simple tensors. we do it ourselves.
												w.grad.zero_()
							self.assertListAlmostEqual(w.tolist()   ,       [0.4, 0.2, -0.5]   ,       tol=1e-2							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Dict							 =			torch.tensor([0.1, -0.2, -0.1]   ,       requires_grad=UpperCamelCase_							)
							UpperCamelCase__       :Any							 =			torch.tensor([0.4, 0.2, -0.5]							)
							UpperCamelCase__       :Any							 =			nn.MSELoss()
							# No warmup, constant schedule, no gradient clipping
							UpperCamelCase__       :Union[str, Any]							 =			Adafactor(
							    params=[w]   ,       lr=1e-2   ,       eps=(1e-30, 1e-3)   ,       clip_threshold=1.0   ,       decay_rate=-0.8   ,       betaa=UpperCamelCase_   ,       weight_decay=0.0   ,       relative_step=UpperCamelCase_   ,       scale_parameter=UpperCamelCase_   ,       warmup_init=UpperCamelCase_   ,       )
							for _ in range(1000							):
												UpperCamelCase__       :Dict							 =			criterion(UpperCamelCase_   ,       UpperCamelCase_							)
												loss.backward()
												optimizer.step()
												w.grad.detach_()  # No zero_grad() function on simple tensors. we do it ourselves.
												w.grad.zero_()
							self.assertListAlmostEqual(w.tolist()   ,       [0.4, 0.2, -0.5]   ,       tol=1e-2							)



@require_torch
class        lowercase     (							unittest.TestCase ):

		"""simple docstring"""

		_a   			=    nn.Linear(50     ,		50 ) if is_torch_available() else None
		_a   			=    AdamW(m.parameters()     ,		lr=10.0 ) if is_torch_available() else None
		_a   			=    10




		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_   ,       UpperCamelCase_=None							):

							'''simple docstring'''


							self.assertEqual(len(UpperCamelCase_							)   ,       len(UpperCamelCase_							)							)
							for a, b in zip(UpperCamelCase_   ,       UpperCamelCase_							):
												self.assertAlmostEqual(UpperCamelCase_   ,       UpperCamelCase_   ,       delta=UpperCamelCase_   ,       msg=UpperCamelCase_							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Optional[Any]							 =			{'''num_warmup_steps''': 2, '''num_training_steps''': 10}
							# schedulers doct format
							# function: (sched_args_dict, expected_learning_rates)
							UpperCamelCase__       :str							 =			{
							    get_constant_schedule: ({}, [10.0] * self.num_steps),
							    get_constant_schedule_with_warmup: (
							        {'''num_warmup_steps''': 4},
							        [0.0, 2.5, 5.0, 7.5, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0],
							    ),
							    get_linear_schedule_with_warmup: (
							        {**common_kwargs},
							        [0.0, 5.0, 10.0, 8.75, 7.5, 6.25, 5.0, 3.75, 2.5, 1.25],
							    ),
							    get_cosine_schedule_with_warmup: (
							        {**common_kwargs},
							        [0.0, 5.0, 10.0, 9.61, 8.53, 6.91, 5.0, 3.08, 1.46, 0.38],
							    ),
							    get_cosine_with_hard_restarts_schedule_with_warmup: (
							        {**common_kwargs, '''num_cycles''': 2},
							        [0.0, 5.0, 10.0, 8.53, 5.0, 1.46, 10.0, 8.53, 5.0, 1.46],
							    ),
							    get_polynomial_decay_schedule_with_warmup: (
							        {**common_kwargs, '''power''': 2.0, '''lr_end''': 1e-7},
							        [0.0, 5.0, 10.0, 7.656, 5.625, 3.906, 2.5, 1.406, 0.625, 0.156],
							    ),
							    get_inverse_sqrt_schedule: (
							        {'''num_warmup_steps''': 2},
							        [0.0, 5.0, 10.0, 8.165, 7.071, 6.325, 5.774, 5.345, 5.0, 4.714],
							    ),
							}

							for scheduler_func, data in scheds.items():
												UpperCamelCase__       :Tuple							 =			data

												UpperCamelCase__       :Tuple							 =			scheduler_func(self.optimizer   ,       **UpperCamelCase_							)
												self.assertEqual(len([scheduler.get_lr()[0]]							)   ,       1							)
												UpperCamelCase__       :List[Any]							 =			unwrap_schedule(UpperCamelCase_   ,       self.num_steps							)
												self.assertListAlmostEqual(
												    UpperCamelCase_   ,       UpperCamelCase_   ,       tol=1e-2   ,       msg=F'''failed for {scheduler_func} in normal scheduler'''   ,       )

												UpperCamelCase__       :Dict							 =			scheduler_func(self.optimizer   ,       **UpperCamelCase_							)
												if scheduler_func.__name__ != "get_constant_schedule":
																	LambdaScheduleWrapper.wrap_scheduler(UpperCamelCase_							)  # wrap to test picklability of the schedule
												UpperCamelCase__       :List[str]							 =			unwrap_and_save_reload_schedule(UpperCamelCase_   ,       self.num_steps							)
												self.assertListEqual(UpperCamelCase_   ,       UpperCamelCase_   ,       msg=F'''failed for {scheduler_func} in save and reload'''							)



class        lowercase     :

		"""simple docstring"""

		def __init__(		self   ,       UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__       :int							 =			fn




		def __call__(		self   ,       *UpperCamelCase_   ,       **UpperCamelCase_							):

							'''simple docstring'''


							return self.fn(*UpperCamelCase_   ,       **UpperCamelCase_							)




		@classmethod
		def    lowerCAmelCase__    (		self   ,       UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__       :Optional[Any]							 =			list(map(self   ,       scheduler.lr_lambdas							)							)



'''simple docstring'''
import os
import unittest

from huggingface_hub.utils import are_progress_bars_disabled

import transformers.models.bart.tokenization_bart
from transformers import logging
from transformers.testing_utils import CaptureLogger, mockenv, mockenv_context
from transformers.utils.logging import disable_progress_bar, enable_progress_bar
class        lowercase     (							unittest.TestCase ):

		"""simple docstring"""

		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :int							 =			logging.get_logger()

							# the current default level is logging.WARNING
							UpperCamelCase__       :List[Any]							 =			logging.get_verbosity()

							logging.set_verbosity_error()
							self.assertEqual(logger.getEffectiveLevel()   ,       logging.get_verbosity()							)

							logging.set_verbosity_warning()
							self.assertEqual(logger.getEffectiveLevel()   ,       logging.get_verbosity()							)

							logging.set_verbosity_info()
							self.assertEqual(logger.getEffectiveLevel()   ,       logging.get_verbosity()							)

							logging.set_verbosity_debug()
							self.assertEqual(logger.getEffectiveLevel()   ,       logging.get_verbosity()							)

							# restore to the original level
							logging.set_verbosity(UpperCamelCase_							)




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							UpperCamelCase__       :Union[str, Any]							 =			logging.get_verbosity()

							UpperCamelCase__       :Union[str, Any]							 =			logging.get_logger('''transformers.models.bart.tokenization_bart'''							)
							UpperCamelCase__       :Optional[Any]							 =			'''Testing 1, 2, 3'''

							# should be able to log warnings (if default settings weren't overridden by `pytest --log-level-all`)
							if level_origin <= logging.WARNING:
												with CaptureLogger(UpperCamelCase_							) as cl:
																	logger.warning(UpperCamelCase_							)
												self.assertEqual(cl.out   ,       msg + '''\n'''							)

							# this is setting the level for all of `transformers.*` loggers
							logging.set_verbosity_error()

							# should not be able to log warnings
							with CaptureLogger(UpperCamelCase_							) as cl:
												logger.warning(UpperCamelCase_							)
							self.assertEqual(cl.out   ,       ''''''							)

							# should be able to log warnings again
							logging.set_verbosity_warning()
							with CaptureLogger(UpperCamelCase_							) as cl:
												logger.warning(UpperCamelCase_							)
							self.assertEqual(cl.out   ,       msg + '''\n'''							)

							# restore to the original level
							logging.set_verbosity(UpperCamelCase_							)




		@mockenv(TRANSFORMERS_VERBOSITY='''error'''							)
		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							transformers.utils.logging._reset_library_root_logger()
							# this action activates the env var
							UpperCamelCase__       :Tuple							 =			logging.get_logger('''transformers.models.bart.tokenization_bart'''							)

							UpperCamelCase__       :Any							 =			os.getenv('''TRANSFORMERS_VERBOSITY'''   ,       UpperCamelCase_							)
							UpperCamelCase__       :Optional[Any]							 =			logging.log_levels[env_level_str]

							UpperCamelCase__       :int							 =			logging.get_verbosity()
							self.assertEqual(
							    UpperCamelCase_   ,       UpperCamelCase_   ,       F'''TRANSFORMERS_VERBOSITY={env_level_str}/{env_level}, but internal verbosity is {current_level}'''   ,       )

							# restore to the original level
							UpperCamelCase__       :Union[str, Any]							 =			''''''
							transformers.utils.logging._reset_library_root_logger()




		@mockenv(TRANSFORMERS_VERBOSITY='''super-error'''							)
		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							transformers.utils.logging._reset_library_root_logger()
							UpperCamelCase__       :Dict							 =			logging.logging.getLogger()
							with CaptureLogger(UpperCamelCase_							) as cl:
												# this action activates the env var
												logging.get_logger('''transformers.models.bart.tokenization_bart'''							)
							self.assertIn('''Unknown option TRANSFORMERS_VERBOSITY=super-error'''   ,       cl.out							)

							# no need to restore as nothing was changed




		def    lowerCAmelCase__    (		self							):

							'''simple docstring'''


							transformers.utils.logging._reset_library_root_logger()

							UpperCamelCase__       :Optional[int]							 =			logging.get_logger('''transformers.models.bart.tokenization_bart'''							)
							UpperCamelCase__       :int							 =			'''Testing 1, 2, 3'''

							with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS='''1'''							):
												# nothing should be logged as env var disables this method
												with CaptureLogger(UpperCamelCase_							) as cl:
																	logger.warning_advice(UpperCamelCase_							)
												self.assertEqual(cl.out   ,       ''''''							)

							with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS=''''''							):
												# should log normally as TRANSFORMERS_NO_ADVISORY_WARNINGS is unset
												with CaptureLogger(UpperCamelCase_							) as cl:
																	logger.warning_advice(UpperCamelCase_							)
												self.assertEqual(cl.out   ,       msg + '''\n'''							)



def       a				(      )  ->     str:



					'''simple docstring'''






					disable_progress_bar()
					assert are_progress_bars_disabled()

					enable_progress_bar()
					assert not are_progress_bars_disabled()