





'''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_rag import save_json
def    UpperCamelCase__      (		lowerCAmelCase  ):





      """simple docstring"""


      _lowerCAmelCase							     =							filter(lambda lowerCAmelCase  : p.requires_grad							,	model.parameters()  )
      _lowerCAmelCase							     =							sum([np.prod(p.size()  ) for p in model_parameters]  )
      return params


A__				:   int      =logging.getLogger(__name__)
def    UpperCamelCase__      (		lowerCAmelCase							,	lowerCAmelCase  ):





      """simple docstring"""


      if metric == "rouge2":
            _lowerCAmelCase							     =							"""{val_avg_rouge2:.4f}-{step_count}"""
      elif metric == "bleu":
            _lowerCAmelCase							     =							"""{val_avg_bleu:.4f}-{step_count}"""
      elif metric == "em":
            _lowerCAmelCase							     =							"""{val_avg_em:.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."""  )

      _lowerCAmelCase							     =							ModelCheckpoint(
          dirpath=lowerCAmelCase							,	filename=lowerCAmelCase							,	monitor=f"val_{metric}"							,	mode="""max"""							,	save_top_k=3							,	every_n_epochs=1							,	)
      return checkpoint_callback
def    UpperCamelCase__      (		lowerCAmelCase							,	lowerCAmelCase  ):





      """simple docstring"""


      return EarlyStopping(
          monitor=f"val_{metric}"							,	mode="""min""" if """loss""" in metric else """max"""							,	patience=lowerCAmelCase							,	verbose=lowerCAmelCase							,	)





class  UpperCAmelCase     (			pl.Callback   ):




      def 				lowercase__    (			self			: Optional[Any]			,			__snake_case			: Union[str, Any]			,			__snake_case			: Any  )				->      Union[str, Any]:
            _lowerCAmelCase							     =							{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 				lowercase__    (			self			: Any			,			__snake_case			: pl.Trainer			,			__snake_case			: pl.LightningModule			,			__snake_case			: str			,			__snake_case			: List[Any]=True  )				->      None:
            logger.info(f"***** {type_path} results at step {trainer.global_step:05d} *****"  )
            _lowerCAmelCase							     =							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
            _lowerCAmelCase							     =							Path(pl_module.hparams.output_dir  )
            if type_path == "test":
                  _lowerCAmelCase							     =							od / """test_results.txt"""
                  _lowerCAmelCase							     =							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.
                  _lowerCAmelCase							     =							od / f"{type_path}_results/{trainer.global_step:05d}.txt"
                  _lowerCAmelCase							     =							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
                        _lowerCAmelCase							     =							metrics[key]
                        if isinstance(__lowerCamelCase			,			torch.Tensor  ):
                              _lowerCAmelCase							     =							val.item()
                        _lowerCAmelCase							     =							f"{key}: {val:.6f}\n"
                        writer.write(__lowerCamelCase  )

            if not save_generations:
                  return

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




      @rank_zero_only
      def 				lowercase__    (			self			: Any			,			__snake_case			: int			,			__snake_case			: Tuple  )				->      Any:
            try:
                  _lowerCAmelCase							     =							pl_module.model.model.num_parameters()
            except AttributeError:
                  _lowerCAmelCase							     =							pl_module.model.num_parameters()

            _lowerCAmelCase							     =							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 				lowercase__    (			self			: List[Any]			,			__snake_case			: pl.Trainer			,			__snake_case			: pl.LightningModule  )				->      Optional[Any]:
            save_json(pl_module.metrics			,			pl_module.metrics_save_path  )
            return self._write_logs(__lowerCamelCase			,			__lowerCamelCase			,			"""test"""  )




      @rank_zero_only
      def 				lowercase__    (			self			: List[Any]			,			__snake_case			: pl.Trainer			,			__snake_case			: Any  )				->      Tuple:
            save_json(pl_module.metrics			,			pl_module.metrics_save_path  )
            # Uncommenting this will save val generations
            # return self._write_logs(trainer, pl_module, "valid")












import random
import unittest

import numpy as np

import transformers
from transformers import is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax


if is_flax_available():
		import os

		import jax.numpy as jnp
		from jax import jit

		from transformers import AutoTokenizer, FlaxAutoModelForCausalLM
		from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model

		__lowerCAmelCase		:				List[Any]       			=		'0.12'  # assumed parallelism: 8


if is_torch_available():
		import torch




def 			__magic_name__				( A		:					Dict,  A		:					Union[str, Any],  A		:					Optional[int]=None   ):






			'''simple docstring'''







			if rng is None:
						a    =			random.Random()

			a    =			1
			for dim in shape:
						total_dims *= dim

			a    =			[]
			for _ in range(A   ):
						values.append(rng.randint(0,  vocab_size - 1   )   )

			a    =			np.array(A,  dtype=jnp.intaa   ).reshape(A   )

			return output




def 			__magic_name__				( A		:					Dict,  A		:					Union[str, Any]=None   ):






			'''simple docstring'''







			a    =			ids_tensor(A,  vocab_size=2,  rng=A   )
			# make sure that at least one token is attended to for each batch
			a    =			1
			return attn_mask




@require_flax
class     snake_case__							:
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_       :							List[str]							 =					None
				SCREAMING_SNAKE_CASE_       :							Any							 =					()





				def 							__UpperCAmelCase							(      self				:     int							)					->     List[str]:
							a    ,		a    =			self.model_tester.prepare_config_and_inputs_for_common()

							# cut to half length & take max batch_size 3
							a    =			2
							a    =			inputs["input_ids"].shape[-1] // 2
							a    =			inputs["input_ids"][:max_batch_size, :sequence_length]

							a    =			jnp.ones_like(__lowerCamelCase							)
							a    =			attention_mask[:max_batch_size, :sequence_length]

							# generate max 5 tokens
							a    =			input_ids.shape[-1] + 5
							if config.eos_token_id is not None and config.pad_token_id is None:
										# hack to allow generate for models such as GPT2 as is done in `generate()`
										a    =			config.eos_token_id
							return config, input_ids, attention_mask, max_length





				@is_pt_flax_cross_test
				def 							__UpperCAmelCase							(      self				:     Optional[Any]							)					->     int:
							a    ,		a    ,		a    ,		a    =			self._get_input_ids_and_config()
							a    =			False
							a    =			max_length
							a    =			0

							for model_class in self.all_generative_model_classes:
										a    =			model_class(__lowerCamelCase							)

										a    =			model_class.__name__[4:]  # Skip the "Flax" at the beginning
										a    =			getattr(__lowerCamelCase					,      __lowerCamelCase							)
										a    =			pt_model_class(__lowerCamelCase							).eval()
										a    =			load_flax_weights_in_pytorch_model(__lowerCamelCase					,      flax_model.params							)

										a    =			flax_model.generate(__lowerCamelCase							).sequences
										a    =			pt_model.generate(torch.tensor(__lowerCamelCase					,      dtype=torch.long							)							)

										if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]:
													a    =			flax_generation_outputs[:, : pt_generation_outputs.shape[-1]]

										self.assertListEqual(pt_generation_outputs.numpy().tolist()					,      flax_generation_outputs.tolist()							)





				def 							__UpperCAmelCase							(      self				:     List[str]							)					->     Optional[int]:
							a    ,		a    ,		a    ,		a    =			self._get_input_ids_and_config()
							a    =			False
							a    =			max_length

							for model_class in self.all_generative_model_classes:
										a    =			model_class(__lowerCamelCase							)

										a    =			model.generate(__lowerCamelCase							).sequences
										self.assertEqual(generation_outputs.shape[-1]					,      __lowerCamelCase							)

										a    =			jit(model.generate							)
										a    =			jit_generate(__lowerCamelCase							).sequences

										self.assertListEqual(generation_outputs.tolist()					,      jit_generation_outputs.tolist()							)





				def 							__UpperCAmelCase							(      self				:     Optional[int]							)					->     Any:
							a    ,		a    ,		a    ,		a    =			self._get_input_ids_and_config()
							a    =			True
							a    =			max_length

							for model_class in self.all_generative_model_classes:
										a    =			model_class(__lowerCamelCase							)

										a    =			model.generate(__lowerCamelCase							).sequences
										self.assertEqual(generation_outputs.shape[-1]					,      __lowerCamelCase							)

										a    =			jit(model.generate							)
										a    =			jit_generate(__lowerCamelCase							).sequences

										self.assertListEqual(generation_outputs.tolist()					,      jit_generation_outputs.tolist()							)





				def 							__UpperCAmelCase							(      self				:     int							)					->     Dict:
							a    ,		a    ,		a    ,		a    =			self._get_input_ids_and_config()
							a    =			False
							a    =			max_length
							a    =			2

							for model_class in self.all_generative_model_classes:
										a    =			model_class(__lowerCamelCase							)

										a    =			model.generate(__lowerCamelCase							).sequences
										self.assertEqual(generation_outputs.shape[-1]					,      __lowerCamelCase							)

										a    =			jit(model.generate							)
										a    =			jit_generate(__lowerCamelCase							).sequences

										self.assertListEqual(generation_outputs.tolist()					,      jit_generation_outputs.tolist()							)





				def 							__UpperCAmelCase							(      self				:     Any							)					->     Union[str, Any]:
							a    ,		a    ,		a    ,		a    =			self._get_input_ids_and_config()
							a    =			False
							a    =			max_length
							a    =			2
							a    =			2

							for model_class in self.all_generative_model_classes:
										a    =			model_class(__lowerCamelCase							)

										a    =			model.generate(__lowerCamelCase							).sequences
										self.assertEqual(generation_outputs.shape[0]					,      input_ids.shape[0] * config.num_return_sequences							)





				def 							__UpperCAmelCase							(      self				:     Optional[Any]							)					->     Dict:
							a    ,		a    ,		a    ,		a    =			self._get_input_ids_and_config()
							a    =			True
							a    =			max_length
							a    =			0.8
							a    =			10
							a    =			0.3
							a    =			1
							a    =			8
							a    =			9

							for model_class in self.all_generative_model_classes:
										a    =			model_class(__lowerCamelCase							)

										a    =			model.generate(__lowerCamelCase							).sequences
										self.assertEqual(generation_outputs.shape[-1]					,      __lowerCamelCase							)

										a    =			jit(model.generate							)
										a    =			jit_generate(__lowerCamelCase							).sequences

										self.assertListEqual(generation_outputs.tolist()					,      jit_generation_outputs.tolist()							)





				def 							__UpperCAmelCase							(      self				:     Optional[Any]							)					->     Optional[Any]:
							a    ,		a    ,		a    ,		a    =			self._get_input_ids_and_config()
							a    =			max_length
							a    =			1
							a    =			8
							a    =			9

							for model_class in self.all_generative_model_classes:
										a    =			model_class(__lowerCamelCase							)

										a    =			model.generate(__lowerCamelCase							).sequences
										self.assertEqual(generation_outputs.shape[-1]					,      __lowerCamelCase							)

										a    =			jit(model.generate							)
										a    =			jit_generate(__lowerCamelCase							).sequences

										self.assertListEqual(generation_outputs.tolist()					,      jit_generation_outputs.tolist()							)





				def 							__UpperCAmelCase							(      self				:     List[str]							)					->     Union[str, Any]:
							a    ,		a    ,		a    ,		a    =			self._get_input_ids_and_config()
							a    =			max_length
							a    =			2
							a    =			1
							a    =			8
							a    =			9

							for model_class in self.all_generative_model_classes:
										a    =			model_class(__lowerCamelCase							)

										a    =			model.generate(__lowerCamelCase							).sequences
										self.assertEqual(generation_outputs.shape[-1]					,      __lowerCamelCase							)

										a    =			jit(model.generate							)
										a    =			jit_generate(__lowerCamelCase							).sequences

										self.assertListEqual(generation_outputs.tolist()					,      jit_generation_outputs.tolist()							)





				def 							__UpperCAmelCase							(      self				:     Union[str, Any]							)					->     Dict:
							a    ,		a    ,		a    ,		a    =			self._get_input_ids_and_config()

							# pad attention mask on the left
							a    =			attention_mask.at[(0, 0)].set(0							)

							a    =			False
							a    =			max_length

							for model_class in self.all_generative_model_classes:
										a    =			model_class(__lowerCamelCase							)

										a    =			model.generate(__lowerCamelCase					,      attention_mask=__lowerCamelCase							).sequences
										self.assertEqual(generation_outputs.shape[-1]					,      __lowerCamelCase							)

										a    =			jit(model.generate							)
										a    =			jit_generate(__lowerCamelCase					,      attention_mask=__lowerCamelCase							).sequences

										self.assertListEqual(generation_outputs.tolist()					,      jit_generation_outputs.tolist()							)





				def 							__UpperCAmelCase							(      self				:     Tuple							)					->     Tuple:
							a    ,		a    ,		a    ,		a    =			self._get_input_ids_and_config()

							# pad attention mask on the left
							a    =			attention_mask.at[(0, 0)].set(0							)

							a    =			True
							a    =			max_length

							for model_class in self.all_generative_model_classes:
										a    =			model_class(__lowerCamelCase							)

										a    =			model.generate(__lowerCamelCase					,      attention_mask=__lowerCamelCase							).sequences
										self.assertEqual(generation_outputs.shape[-1]					,      __lowerCamelCase							)

										a    =			jit(model.generate							)
										a    =			jit_generate(__lowerCamelCase					,      attention_mask=__lowerCamelCase							).sequences

										self.assertListEqual(generation_outputs.tolist()					,      jit_generation_outputs.tolist()							)





				def 							__UpperCAmelCase							(      self				:     Optional[int]							)					->     List[Any]:
							a    ,		a    ,		a    ,		a    =			self._get_input_ids_and_config()

							# pad attention mask on the left
							a    =			attention_mask.at[(0, 0)].set(0							)

							a    =			2
							a    =			max_length

							for model_class in self.all_generative_model_classes:
										a    =			model_class(__lowerCamelCase							)

										a    =			model.generate(__lowerCamelCase					,      attention_mask=__lowerCamelCase							).sequences
										self.assertEqual(generation_outputs.shape[-1]					,      __lowerCamelCase							)

										a    =			jit(model.generate							)
										a    =			jit_generate(__lowerCamelCase					,      attention_mask=__lowerCamelCase							).sequences

										self.assertListEqual(generation_outputs.tolist()					,      jit_generation_outputs.tolist()							)




@require_flax
class     snake_case__							(unittest.TestCase   ):
				"""simple docstring"""



				def 							__UpperCAmelCase							(      self				:     Dict							)					->     Optional[Any]:
							a    =			AutoTokenizer.from_pretrained("hf-internal-testing/tiny-bert"							)
							a    =			FlaxAutoModelForCausalLM.from_pretrained("hf-internal-testing/tiny-bert-flax-only"							)

							a    =			"Hello world"
							a    =			tokenizer(__lowerCamelCase					,      return_tensors="np"							).input_ids

							# typos are quickly detected (the correct argument is `do_sample`)
							with self.assertRaisesRegex(__lowerCamelCase					,      "do_samples"							):
										model.generate(__lowerCamelCase					,      do_samples=__lowerCamelCase							)

							# arbitrary arguments that will not be used anywhere are also not accepted
							with self.assertRaisesRegex(__lowerCamelCase					,      "foo"							):
										a    =			{"foo": "bar"}
										model.generate(__lowerCamelCase					,      **__lowerCamelCase							)






