

# 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      lowerCamelCase_      :




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




	def        lowerCAmelCase_       (     self						:     Dict					,      *__A						:     int					,      **__A						:     List[Any]							):
		if not self.step_with_optimizer:
			# No link between scheduler and optimizer -> just step
			self.scheduler.step(*__A					,      **__A							)
			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(*__A					,      **__A							)
		else:
			# Otherwise the training dataloader batch size was multiplied by `num_processes`, so we need to do
			# num_processes steps per training step
			__A :			str		    =			AcceleratorState().num_processes
			for _ in range(__A							):
				# 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(*__A					,      **__A							)
				else:
					self.scheduler.step(*__A					,      **__A							)




	def        lowerCAmelCase_       (     self						:     Any							):
		return self.scheduler.get_last_lr()




	def        lowerCAmelCase_       (     self						:     Optional[Any]							):
		return self.scheduler.state_dict()




	def        lowerCAmelCase_       (     self						:     int					,      __A						:     int							):
		self.scheduler.load_state_dict(__A							)




	def        lowerCAmelCase_       (     self						:     Optional[int]							):
		return self.scheduler.get_lr()




	def        lowerCAmelCase_       (     self						:     List[Any]					,      *__A						:     Optional[Any]					,      **__A						:     List[str]							):
		return self.scheduler.print_lr(*__A					,      **__A							)










"""simple docstring"""







import contextlib
import importlib
import io
import unittest

import transformers

# Try to import everything from transformers to ensure every object can be loaded.
from transformers import *  # noqa F406
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch
from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available


if is_torch_available():
			from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification

if is_tf_available():
			from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification

if is_flax_available():
			from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification


A		=						DUMMY_UNKNOWN_IDENTIFIER
# An actual model hosted on huggingface.co

A		=						'main'
# Default branch name
A		=						'f2c752cfc5c0ab6f4bdec59acea69eefbee381c2'
# One particular commit (not the top of `main`)
A		=						'aaaaaaa'
# This commit does not exist, so we should 404.

A		=						'd9e9f15bc825e4b2c9249e9578f884bbcb5e3684'
# Sha-1 of config.json on the top of `main`, for checking purposes
A		=						'4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3'

@contextlib.contextmanager
def  __SCREAMING_SNAKE_CASE		(			):
						"""simple docstring"""






						print("Welcome!" )
						yield
						print("Bye!" )

@contextlib.contextmanager
def  __SCREAMING_SNAKE_CASE		(			):
						"""simple docstring"""






						print("Bonjour!" )
						yield
						print("Au revoir!" )

class       _a  ( unittest.TestCase):



			def 				__lowercase					(			self							:				Dict		)							->					Optional[int]:
									# If the spec is missing, importlib would not be able to import the module dynamically.
									assert transformers.__spec__ is not None
									assert importlib.util.find_spec("transformers"		) is not None



class       _a  ( unittest.TestCase):



			@unittest.mock.patch("sys.stdout"   ,	new_callable=io.StringIO		)
			def 				__lowercase					(			self							:				Union[str, Any]   ,	_lowercase							:				Optional[int]		)							->					Optional[Any]:
									with ContextManagers([]		):
															print("Transformers are awesome!"		)
									# The print statement adds a new line at the end of the output
									self.assertEqual(mock_stdout.getvalue()   ,	"Transformers are awesome!\n"		)



			@unittest.mock.patch("sys.stdout"   ,	new_callable=io.StringIO		)
			def 				__lowercase					(			self							:				Any   ,	_lowercase							:				Any		)							->					Dict:
									with ContextManagers([context_en()]		):
															print("Transformers are awesome!"		)
									# The output should be wrapped with an English welcome and goodbye
									self.assertEqual(mock_stdout.getvalue()   ,	"Welcome!\nTransformers are awesome!\nBye!\n"		)



			@unittest.mock.patch("sys.stdout"   ,	new_callable=io.StringIO		)
			def 				__lowercase					(			self							:				str   ,	_lowercase							:				List[Any]		)							->					Optional[int]:
									with ContextManagers([context_fr(), context_en()]		):
															print("Transformers are awesome!"		)
									# The output should be wrapped with an English and French welcome and goodbye
									self.assertEqual(mock_stdout.getvalue()   ,	"Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n"		)



			@require_torch
			def 				__lowercase					(			self							:				Any		)							->					Union[str, Any]:
									self.assertEqual(find_labels(_lowercase		)   ,	["labels"]		)
									self.assertEqual(find_labels(_lowercase		)   ,	["labels", "next_sentence_label"]		)
									self.assertEqual(find_labels(_lowercase		)   ,	["start_positions", "end_positions"]		)

									class       _a  ( SCREAMING_SNAKE_CASE__):
												pass



									self.assertEqual(find_labels(_lowercase		)   ,	["labels"]		)



			@require_tf
			def 				__lowercase					(			self							:				Optional[Any]		)							->					int:
									self.assertEqual(find_labels(_lowercase		)   ,	["labels"]		)
									self.assertEqual(find_labels(_lowercase		)   ,	["labels", "next_sentence_label"]		)
									self.assertEqual(find_labels(_lowercase		)   ,	["start_positions", "end_positions"]		)

									class       _a  ( SCREAMING_SNAKE_CASE__):
												pass



									self.assertEqual(find_labels(_lowercase		)   ,	["labels"]		)



			@require_flax
			def 				__lowercase					(			self							:				Tuple		)							->					List[Any]:
									# Flax models don't have labels
									self.assertEqual(find_labels(_lowercase		)   ,	[]		)
									self.assertEqual(find_labels(_lowercase		)   ,	[]		)
									self.assertEqual(find_labels(_lowercase		)   ,	[]		)

									class       _a  ( SCREAMING_SNAKE_CASE__):
												pass



									self.assertEqual(find_labels(_lowercase		)   ,	[]		)



