




from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase__    :					Any			 =						logging.get_logger(__name__)

lowercase__    :					str			 =						{
    "google/pegasus-x-base": "https://huggingface.co/google/pegasus-x-base/resolve/main/config.json",
    "google/pegasus-x-large": "https://huggingface.co/google/pegasus-x-large/resolve/main/config.json",
    # See all PEGASUS-X models at https://huggingface.co/models?filter=pegasus-x
}
class 		UpperCAmelCase       (    lowercase_      ):







						'''simple docstring'''





						lowerCAmelCase_							=       '''pegasus_x'''
						lowerCAmelCase_							=       ['''past_key_values''']
						lowerCAmelCase_							=       {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}






						def __init__(						self							:					Tuple	,     __lowercase							:					Dict=9_61_03	,     __lowercase							:					Optional[int]=1_63_84	,     __lowercase							:					List[str]=16	,     __lowercase							:					Optional[Any]=40_96	,     __lowercase							:					int=16	,     __lowercase							:					List[str]=16	,     __lowercase							:					Tuple=40_96	,     __lowercase							:					List[Any]=16	,     __lowercase							:					List[str]=0.0	,     __lowercase							:					str=0.0	,     __lowercase							:					Optional[int]=True	,     __lowercase							:					int=True	,     __lowercase							:					str="gelu"	,     __lowercase							:					List[Any]=10_24	,     __lowercase							:					Dict=0.1	,     __lowercase							:					List[Any]=0.0	,     __lowercase							:					Tuple=0.0	,     __lowercase							:					Any=0.02	,     __lowercase							:					Optional[Any]=0	,     __lowercase							:					Optional[int]=True	,     __lowercase							:					Optional[Any]=0	,     __lowercase							:					Dict=1	,     __lowercase							:					Optional[Any]=1	,     __lowercase							:					str=32	,     __lowercase							:					Union[str, Any]=5_12	,     __lowercase							:					Optional[Any]=True	,     **__lowercase							:					Any	,     ):






							"""simple docstring"""






							snake_case_		   =					vocab_size
							snake_case_		   =					max_position_embeddings
							snake_case_		   =					d_model
							snake_case_		   =					encoder_ffn_dim
							snake_case_		   =					encoder_layers
							snake_case_		   =					encoder_attention_heads
							snake_case_		   =					decoder_ffn_dim
							snake_case_		   =					decoder_layers
							snake_case_		   =					decoder_attention_heads
							snake_case_		   =					dropout
							snake_case_		   =					attention_dropout
							snake_case_		   =					activation_dropout
							snake_case_		   =					activation_function
							snake_case_		   =					init_std
							snake_case_		   =					encoder_layerdrop
							snake_case_		   =					decoder_layerdrop
							snake_case_		   =					use_cache
							snake_case_		   =					encoder_layers
							snake_case_		   =					scale_embedding  # scale factor will be sqrt(d_model) if True

							snake_case_		   =					num_global_tokens
							snake_case_		   =					block_size
							snake_case_		   =					stagger_local_blocks

							super().__init__(
							    pad_token_id=UpperCamelCase_	,     eos_token_id=UpperCamelCase_	,     is_encoder_decoder=UpperCamelCase_	,     decoder_start_token_id=UpperCamelCase_	,     forced_eos_token_id=UpperCamelCase_	,     **UpperCamelCase_	,     )






						@property
						def 	snake_case__						(						self							:					Optional[int]						):






							"""simple docstring"""






							return self.encoder_attention_heads






						@property
						def 	snake_case__						(						self							:					List[Any]						):






							"""simple docstring"""






							return self.d_model






import unittest
from functools import partial

import torch

from accelerate import Accelerator, debug_launcher
from accelerate.state import AcceleratorState, GradientState
from accelerate.test_utils import require_cpu, require_huggingface_suite
from accelerate.utils import GradientAccumulationPlugin


def 						lowerCamelCase__   (		_A=2					,     _A=True					,     _A=False							):







	'''simple docstring'''





	snake_case_		   =					Accelerator(step_scheduler_with_optimizer=_A					,     split_batches=_A							)
	snake_case_		   =					torch.nn.Linear(2					,     4							)
	snake_case_		   =					torch.optim.AdamW(model.parameters()					,     lr=1.0							)
	snake_case_		   =					torch.optim.lr_scheduler.OneCycleLR(_A					,     max_lr=0.01					,     steps_per_epoch=2					,     epochs=1							)
	snake_case_   ,				snake_case_   ,				snake_case_		   =					accelerator.prepare(_A					,     _A					,     _A							)

	# Optimizer has stepped
	scheduler.step()
	if step_scheduler_with_optimizer or (num_processes == 1):
		assert (
		    scheduler.scheduler.last_epoch == num_processes
		), f"Last Epoch ({scheduler.scheduler.last_epoch}) != Num Processes ({num_processes})"
	else:
		assert (
		    scheduler.scheduler.last_epoch != num_processes
		), f"Last Epoch ({scheduler.scheduler.last_epoch}) == Num Processes ({num_processes})"


def 						lowerCamelCase__   (		_A=2					,     _A=True					,     _A=False							):







	'''simple docstring'''





	snake_case_		   =					Accelerator(step_scheduler_with_optimizer=_A					,     split_batches=_A							)
	snake_case_		   =					torch.nn.Linear(2					,     4							)
	snake_case_		   =					torch.optim.AdamW(model.parameters()					,     lr=1.0							)
	snake_case_		   =					torch.optim.lr_scheduler.LambdaLR(_A					,     lr_lambda=lambda _A							: 1 - n / 10							)
	snake_case_   ,				snake_case_   ,				snake_case_		   =					accelerator.prepare(_A					,     _A					,     _A							)

	# Optimizer has stepped
	snake_case_		   =					False
	scheduler.step()
	snake_case_		   =					1 - (num_processes if (step_scheduler_with_optimizer and not split_batches) else 1) / 10
	assert (
	    scheduler.get_last_lr()[0] == expected_lr
	), f"Wrong lr found at first step, expected {expected_lr}, got {scheduler.get_last_lr()[0]}"

	# Optimizer has not stepped
	snake_case_		   =					True
	scheduler.step()
	if not step_scheduler_with_optimizer:
		snake_case_		   =					1 - 2 / 10
	assert (
	    scheduler.get_last_lr()[0] == expected_lr
	), f"Wrong lr found at second step, expected {expected_lr}, got {scheduler.get_last_lr()[0]}"


def 						lowerCamelCase__   (		_A = 2							):







	'''simple docstring'''





	from transformers import get_linear_schedule_with_warmup

	snake_case_		   =					[1, 2, 4]
	for num_steps in steps:
		snake_case_		   =					GradientAccumulationPlugin(num_steps=_A					,     adjust_scheduler=num_steps > 1							)
		snake_case_		   =					Accelerator(gradient_accumulation_plugin=_A							)
		snake_case_		   =					torch.nn.Linear(2					,     4							)
		snake_case_		   =					torch.optim.AdamW(model.parameters()					,     lr=10.0							)
		snake_case_		   =					get_linear_schedule_with_warmup(optimizer=_A					,     num_warmup_steps=0					,     num_training_steps=20							)

		snake_case_   ,				snake_case_   ,				snake_case_		   =					accelerator.prepare(_A					,     _A					,     _A							)

		for i in range(10 * num_steps							):
			with accelerator.accumulate(_A							):
				optimizer.step()
				scheduler.step()

			if i == (10 * num_steps - 2):
				assert (
				    scheduler.get_last_lr()[0] != 0
				), f"Wrong lr found at second-to-last step, expected non-zero, got {scheduler.get_last_lr()[0]}. num_steps: {num_steps}"
		assert (
		    scheduler.get_last_lr()[0] == 0
		), f"Wrong lr found at last step, expected 0, got {scheduler.get_last_lr()[0]}"
		GradientState._reset_state()






@require_cpu
class 		UpperCAmelCase       (    unittest.TestCase      ):







						'''simple docstring'''





						def 	snake_case__						(						self							:					Tuple						):






							"""simple docstring"""






							debug_launcher(partial(__lowercase	,     num_processes=1						)	,     num_processes=1						)
							debug_launcher(partial(__lowercase	,     num_processes=1	,     split_batches=__lowercase						)	,     num_processes=1						)






						def 	snake_case__						(						self							:					int						):






							"""simple docstring"""






							debug_launcher(partial(__lowercase	,     num_processes=1						)	,     num_processes=1						)
							debug_launcher(partial(__lowercase	,     num_processes=1	,     split_batches=__lowercase						)	,     num_processes=1						)






						def 	snake_case__						(						self							:					Dict						):






							"""simple docstring"""






							debug_launcher(partial(__lowercase	,     num_processes=1	,     step_scheduler_with_optimizer=__lowercase						)	,     num_processes=1						)






						def 	snake_case__						(						self							:					Union[str, Any]						):






							"""simple docstring"""






							debug_launcher(partial(__lowercase	,     num_processes=1	,     step_scheduler_with_optimizer=__lowercase						)	,     num_processes=1						)






						def 	snake_case__						(						self							:					List[Any]						):






							"""simple docstring"""






							AcceleratorState._reset_state(__lowercase						)
							debug_launcher(__lowercase						)
							debug_launcher(partial(__lowercase	,     num_processes=1	,     split_batches=__lowercase						)	,     num_processes=1						)






						def 	snake_case__						(						self							:					Dict						):






							"""simple docstring"""






							AcceleratorState._reset_state(__lowercase						)
							debug_launcher(__lowercase						)
							debug_launcher(partial(__lowercase	,     num_processes=1	,     split_batches=__lowercase						)	,     num_processes=1						)






						def 	snake_case__						(						self							:					List[str]						):






							"""simple docstring"""






							AcceleratorState._reset_state(__lowercase						)
							debug_launcher(partial(__lowercase	,     step_scheduler_with_optimizer=__lowercase						)						)






						def 	snake_case__						(						self							:					Optional[Any]						):






							"""simple docstring"""






							AcceleratorState._reset_state(__lowercase						)
							debug_launcher(partial(__lowercase	,     step_scheduler_with_optimizer=__lowercase						)						)






						@require_huggingface_suite
						def 	snake_case__						(						self							:					Optional[Any]						):






							"""simple docstring"""






							AcceleratorState._reset_state(__lowercase						)
							debug_launcher(partial(__lowercase	,     num_processes=1						)						)
							debug_launcher(__lowercase						)
