



'''simple docstring'''
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 import save_json






def A__					(  UpperCAmelCase_							):
						_UpperCamelCase      :	Optional[int]				       =     filter(lambda UpperCAmelCase_							: p.requires_grad						,						model.parameters()							)
						_UpperCamelCase      :	Tuple				       =     sum([np.prod(p.size()							) for p in model_parameters]							)
						return params


snake_case_				:			Union[str, Any]						 =						logging.getLogger(__name__)






class 							lowercase__			(							pl.Callback     ):





	def 			UpperCamelCase_						(				self      :       int				,lowerCamelCase__      :       Optional[int]				,lowerCamelCase__      :       int				):







							'''simple docstring'''
							_UpperCamelCase      :	Optional[int]				       =     {F'lr_group_{i}': param['lr'] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups				)}
							pl_module.logger.log_metrics(lowerCamelCase__				)





	@rank_zero_only
	def 			UpperCamelCase_						(				self      :       Any				,lowerCamelCase__      :       pl.Trainer				,lowerCamelCase__      :       pl.LightningModule				,lowerCamelCase__      :       str				,lowerCamelCase__      :       Optional[Any]=True				):







							'''simple docstring'''
							logger.info(F'***** {type_path} results at step {trainer.global_step:05d} *****'				)
							_UpperCamelCase      :	Optional[int]				       =     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      :	List[Any]				       =     Path(pl_module.hparams.output_dir				)
							if type_path == "test":
													_UpperCamelCase      :	List[Any]				       =     od / 'test_results.txt'
													_UpperCamelCase      :	Any				       =     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      :	Dict				       =     od / F'{type_path}_results/{trainer.global_step:05d}.txt'
													_UpperCamelCase      :	List[str]				       =     od / F'{type_path}_generations/{trainer.global_step:05d}.txt'
													results_file.parent.mkdir(exist_ok=lowerCamelCase__				)
													generations_file.parent.mkdir(exist_ok=lowerCamelCase__				)
							with open(lowerCamelCase__				,'a+'				) as writer:
													for key in sorted(lowerCamelCase__				):
																			if key in ["log", "progress_bar", "preds"]:
																									continue
																			_UpperCamelCase      :	Optional[Any]				       =     metrics[key]
																			if isinstance(lowerCamelCase__				,torch.Tensor				):
																									_UpperCamelCase      :	str				       =     val.item()
																			_UpperCamelCase      :	Optional[int]				       =     F'{key}: {val:.6f}\n'
																			writer.write(lowerCamelCase__				)

							if not save_generations:
													return

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





	@rank_zero_only
	def 			UpperCamelCase_						(				self      :       Optional[Any]				,lowerCamelCase__      :       str				,lowerCamelCase__      :       Optional[Any]				):







							'''simple docstring'''
							try:
													_UpperCamelCase      :	List[str]				       =     pl_module.model.model.num_parameters()
							except AttributeError:
													_UpperCamelCase      :	Tuple				       =     pl_module.model.num_parameters()

							_UpperCamelCase      :	Tuple				       =     count_trainable_parameters(lowerCamelCase__				)
							# 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 			UpperCamelCase_						(				self      :       str				,lowerCamelCase__      :       pl.Trainer				,lowerCamelCase__      :       pl.LightningModule				):







							'''simple docstring'''
							save_json(pl_module.metrics				,pl_module.metrics_save_path				)
							return self._write_logs(lowerCamelCase__				,lowerCamelCase__				,'test'				)





	@rank_zero_only
	def 			UpperCamelCase_						(				self      :       Optional[int]				,lowerCamelCase__      :       pl.Trainer				,lowerCamelCase__      :       List[str]				):







							'''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")






def A__					(  UpperCAmelCase_						,						UpperCAmelCase_						,						UpperCAmelCase_=1						,						UpperCAmelCase_=False							):
						if metric == "rouge2":
												_UpperCamelCase      :	List[str]				       =     '{val_avg_rouge2:.4f}-{step_count}'
						elif metric == "bleu":
												_UpperCamelCase      :	List[str]				       =     '{val_avg_bleu:.4f}-{step_count}'
						elif metric == "loss":
												_UpperCamelCase      :	int				       =     '{val_avg_loss:.4f}-{step_count}'
						else:
												raise NotImplementedError(
												    f'seq2seq callbacks only support rouge2, bleu and loss, got {metric}, You can make your own by adding to'
												    ' this function.'							)

						_UpperCamelCase      :	Tuple				       =     ModelCheckpoint(
						    dirpath=UpperCAmelCase_						,						filename=UpperCAmelCase_						,						monitor=f'val_{metric}'						,						mode='min' if 'loss' in metric else 'max'						,						save_top_k=UpperCAmelCase_						,						)
						return checkpoint_callback






def A__					(  UpperCAmelCase_						,						UpperCAmelCase_							):
						return EarlyStopping(
						    monitor=f'val_{metric}'						,						mode='min' if 'loss' in metric else 'max'						,						patience=UpperCAmelCase_						,						verbose=UpperCAmelCase_						,						)







'''simple docstring'''
import os

from ...utils import logging
from .utils import DataProcessor, InputExample


snake_case_				:			Any						 =						logging.get_logger(__name__)






class 							lowercase__			(							lowercase     ):





	def __init__(				self      :       Union[str, Any]				,lowerCamelCase__      :       Tuple				,lowerCamelCase__      :       Optional[int]=None				):







							'''simple docstring'''
							_UpperCamelCase      :	Any				       =     language
							_UpperCamelCase      :	int				       =     train_language





	def 			UpperCamelCase_						(				self      :       Optional[int]				,lowerCamelCase__      :       Dict				):







							'''simple docstring'''
							_UpperCamelCase      :	Tuple				       =     self.language if self.train_language is None else self.train_language
							_UpperCamelCase      :	List[Any]				       =     self._read_tsv(os.path.join(lowerCamelCase__				,F'XNLI-MT-1.0/multinli/multinli.train.{lg}.tsv'				)				)
							_UpperCamelCase      :	Union[str, Any]				       =     []
							for i, line in enumerate(lowerCamelCase__				):
													if i == 0:
																			continue
													_UpperCamelCase      :	Optional[Any]				       =     F'train-{i}'
													_UpperCamelCase      :	List[str]				       =     line[0]
													_UpperCamelCase      :	Union[str, Any]				       =     line[1]
													_UpperCamelCase      :	Tuple				       =     'contradiction' if line[2] == 'contradictory' else line[2]
													if not isinstance(lowerCamelCase__				,lowerCamelCase__				):
																			raise ValueError(F'Training input {text_a} is not a string'				)
													if not isinstance(lowerCamelCase__				,lowerCamelCase__				):
																			raise ValueError(F'Training input {text_b} is not a string'				)
													if not isinstance(lowerCamelCase__				,lowerCamelCase__				):
																			raise ValueError(F'Training label {label} is not a string'				)
													examples.append(InputExample(guid=lowerCamelCase__				,text_a=lowerCamelCase__				,text_b=lowerCamelCase__				,label=lowerCamelCase__				)				)
							return examples





	def 			UpperCamelCase_						(				self      :       Optional[Any]				,lowerCamelCase__      :       List[str]				):







							'''simple docstring'''
							_UpperCamelCase      :	Optional[int]				       =     self._read_tsv(os.path.join(lowerCamelCase__				,'XNLI-1.0/xnli.test.tsv'				)				)
							_UpperCamelCase      :	List[str]				       =     []
							for i, line in enumerate(lowerCamelCase__				):
													if i == 0:
																			continue
													_UpperCamelCase      :	Optional[int]				       =     line[0]
													if language != self.language:
																			continue
													_UpperCamelCase      :	Optional[int]				       =     F'test-{i}'
													_UpperCamelCase      :	Optional[int]				       =     line[6]
													_UpperCamelCase      :	List[str]				       =     line[7]
													_UpperCamelCase      :	str				       =     line[1]
													if not isinstance(lowerCamelCase__				,lowerCamelCase__				):
																			raise ValueError(F'Training input {text_a} is not a string'				)
													if not isinstance(lowerCamelCase__				,lowerCamelCase__				):
																			raise ValueError(F'Training input {text_b} is not a string'				)
													if not isinstance(lowerCamelCase__				,lowerCamelCase__				):
																			raise ValueError(F'Training label {label} is not a string'				)
													examples.append(InputExample(guid=lowerCamelCase__				,text_a=lowerCamelCase__				,text_b=lowerCamelCase__				,label=lowerCamelCase__				)				)
							return examples





	def 			UpperCamelCase_						(				self      :       str				):







							'''simple docstring'''
							return ["contradiction", "entailment", "neutral"]
snake_case_				:			int						 =						{
    'xnli': XnliProcessor,
}

snake_case_				:			Tuple						 =						{
    'xnli': 'classification',
}

snake_case_				:			Any						 =						{
    'xnli': 3,
}


