



from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


UpperCAmelCase__     = logging.get_logger(__name__)

UpperCAmelCase__     = {
    "microsoft/focalnet-tiny": "https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json",
}







class     __lowerCAmelCase					(    A       ,		A		):
			UpperCamelCase           =  '''focalnet'''



			def __init__(		self     :						str				,	A     :						Any=2_24				,	A     :						Optional[Any]=4				,	A     :						List[Any]=3				,	A     :						int=96				,	A     :						Any=False				,	A     :						Union[str, Any]=[1_92, 3_84, 7_68, 7_68]				,	A     :						Optional[int]=[2, 2, 6, 2]				,	A     :						Any=[2, 2, 2, 2]				,	A     :						Dict=[3, 3, 3, 3]				,	A     :						List[str]="gelu"				,	A     :						Dict=4.0				,	A     :						List[str]=0.0				,	A     :						List[Any]=0.1				,	A     :						Optional[int]=False				,	A     :						Union[str, Any]=1E-4				,	A     :						Any=False				,	A     :						Dict=False				,	A     :						Dict=False				,	A     :						List[Any]=0.0_2				,	A     :						Union[str, Any]=1E-5				,	A     :						Optional[Any]=32				,	A     :						Union[str, Any]=None				,	A     :						Dict=None				,	**A     :						Optional[Any]				,	)     ->					int:



						"""simple docstring"""






						super().__init__(**A)

						_UpperCAmelCase	 =     image_size
						_UpperCAmelCase	 =     patch_size
						_UpperCAmelCase	 =     num_channels
						_UpperCAmelCase	 =     embed_dim
						_UpperCAmelCase	 =     use_conv_embed
						_UpperCAmelCase	 =     hidden_sizes
						_UpperCAmelCase	 =     depths
						_UpperCAmelCase	 =     focal_levels
						_UpperCAmelCase	 =     focal_windows
						_UpperCAmelCase	 =     hidden_act
						_UpperCAmelCase	 =     mlp_ratio
						_UpperCAmelCase	 =     hidden_dropout_prob
						_UpperCAmelCase	 =     drop_path_rate
						_UpperCAmelCase	 =     use_layerscale
						_UpperCAmelCase	 =     layerscale_value
						_UpperCAmelCase	 =     use_post_layernorm
						_UpperCAmelCase	 =     use_post_layernorm_in_modulation
						_UpperCAmelCase	 =     normalize_modulator
						_UpperCAmelCase	 =     initializer_range
						_UpperCAmelCase	 =     layer_norm_eps
						_UpperCAmelCase	 =     encoder_stride
						_UpperCAmelCase	 =     ['stem'] + [F"stage{idx}" for idx in range(1				,	len(self.depths) + 1)]
						_UpperCAmelCase ,  _UpperCAmelCase	 =     get_aligned_output_features_output_indices(
						    out_features=A				,	out_indices=A				,	stage_names=self.stage_names)





import logging
from pathlib import Path

import numpy as np
import pytorch_lightning as pl
import torch
from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint
from pytorch_lightning.utilities import rank_zero_only
from utils_rag import save_json





def 							A			(  _UpperCAmelCase       :     List[Any]					)       ->				int:







			'''simple docstring'''
			_UpperCAmelCase	 =     filter(lambda _UpperCAmelCase					: p.requires_grad							,     model.parameters()					)
			_UpperCAmelCase	 =     sum([np.prod(p.size()					) for p in model_parameters]					)
			return params


UpperCAmelCase__     = logging.getLogger(__name__)





def 							A			(  _UpperCAmelCase       :     List[Any]							,     _UpperCAmelCase       :     Union[str, Any]					)       ->				Any:







			'''simple docstring'''
			if metric == "rouge2":
						_UpperCAmelCase	 =     '{val_avg_rouge2:.4f}-{step_count}'
			elif metric == "bleu":
						_UpperCAmelCase	 =     '{val_avg_bleu:.4f}-{step_count}'
			elif metric == "em":
						_UpperCAmelCase	 =     '{val_avg_em:.4f}-{step_count}'
			elif metric == "loss":
						_UpperCAmelCase	 =     '{val_avg_loss:.4f}-{step_count}'
			else:
						raise NotImplementedError(
						    F"seq2seq callbacks only support rouge2 and bleu, got {metric}, You can make your own by adding to this"
						    ' function.'					)

			_UpperCAmelCase	 =     ModelCheckpoint(
			    dirpath=_UpperCAmelCase							,     filename=_UpperCAmelCase							,     monitor=F"val_{metric}"							,     mode='max'							,     save_top_k=1							,     every_n_epochs=1							,     )

			return checkpoint_callback





def 							A			(  _UpperCAmelCase       :     Any							,     _UpperCAmelCase       :     int					)       ->				str:







			'''simple docstring'''
			return EarlyStopping(
			    monitor=F"val_{metric}"							,     mode='min' if 'loss' in metric else 'max'							,     patience=_UpperCAmelCase							,     verbose=_UpperCAmelCase							,     )







class     __lowerCAmelCase					(    pl.Callback		):



			def 					_lowerCamelCase (		self     :						Optional[int]				,	A     :						List[Any]				,	A     :						int)     ->					Dict:



						"""simple docstring"""






						_UpperCAmelCase	 =     {F"lr_group_{i}": param['lr'] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups)}
						pl_module.logger.log_metrics(A)



			@rank_zero_only
			def 					_lowerCamelCase (		self     :						Optional[Any]				,	A     :						pl.Trainer				,	A     :						pl.LightningModule				,	A     :						str				,	A     :						int=True)     ->					None:



						"""simple docstring"""






						logger.info(F"***** {type_path} results at step {trainer.global_step:05d} *****")
						_UpperCAmelCase	 =     trainer.callback_metrics
						trainer.logger.log_metrics({k: v for k, v in metrics.items() if k not in ['log', 'progress_bar', 'preds']})
						# Log results
						_UpperCAmelCase	 =     Path(pl_module.hparams.output_dir)
						if type_path == "test":
									_UpperCAmelCase	 =     od / 'test_results.txt'
									_UpperCAmelCase	 =     od / 'test_generations.txt'
						else:
									# this never gets hit. I prefer not to save intermediate generations, and results are in metrics.json
									# If people want this it will be easy enough to add back.
									_UpperCAmelCase	 =     od / F"{type_path}_results/{trainer.global_step:05d}.txt"
									_UpperCAmelCase	 =     od / F"{type_path}_generations/{trainer.global_step:05d}.txt"
									results_file.parent.mkdir(exist_ok=A)
									generations_file.parent.mkdir(exist_ok=A)
						with open(A				,	'a+') as writer:
									for key in sorted(A):
												if key in ["log", "progress_bar", "preds"]:
															continue
												_UpperCAmelCase	 =     metrics[key]
												if isinstance(A				,	torch.Tensor):
															_UpperCAmelCase	 =     val.item()
												_UpperCAmelCase	 =     F"{key}: {val:.6f}\n"
												writer.write(A)

						if not save_generations:
									return

						if "preds" in metrics:
									_UpperCAmelCase	 =     '\n'.join(metrics['preds'])
									generations_file.open('w+').write(A)



			@rank_zero_only
			def 					_lowerCamelCase (		self     :						str				,	A     :						Optional[int]				,	A     :						List[str])     ->					Optional[Any]:



						"""simple docstring"""






						try:
									_UpperCAmelCase	 =     pl_module.model.model.num_parameters()
						except AttributeError:
									_UpperCAmelCase	 =     pl_module.model.num_parameters()

						_UpperCAmelCase	 =     count_trainable_parameters(A)
						# mp stands for million parameters
						trainer.logger.log_metrics({'n_params': npars, 'mp': npars / 1E6, 'grad_mp': n_trainable_pars / 1E6})



			@rank_zero_only
			def 					_lowerCamelCase (		self     :						Dict				,	A     :						pl.Trainer				,	A     :						pl.LightningModule)     ->					int:



						"""simple docstring"""






						save_json(pl_module.metrics				,	pl_module.metrics_save_path)
						return self._write_logs(A				,	A				,	'test')



			@rank_zero_only
			def 					_lowerCamelCase (		self     :						Tuple				,	A     :						pl.Trainer				,	A     :						str)     ->					Dict:



						"""simple docstring"""






						save_json(pl_module.metrics				,	pl_module.metrics_save_path)
						# Uncommenting this will save val generations
						# return self._write_logs(trainer, pl_module, "valid")
