


from __future__ import annotations

import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import is_tf_available, is_vision_available

from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_tf_bert import TFBertModelTester
from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester
from ..deit.test_modeling_tf_deit import TFDeiTModelTester
from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester
from ..vit.test_modeling_tf_vit import TFViTModelTester


if is_tf_available():
   from transformers import (
       TFBertModel,
       TFCLIPVisionModel,
       TFDeiTModel,
       TFRobertaModel,
       TFVisionTextDualEncoderModel,
       TFViTModel,
       VisionTextDualEncoderConfig,
   )

if is_vision_available():
   from PIL import Image

   from transformers import VisionTextDualEncoderProcessor







def 	SCREAMING_SNAKE_CASE_						(      __A			:      Optional[int]    )     ->		Optional[Any]:

       """simple docstring"""

       if isinstance(__A  ,   collections.abc.Iterable    ):
              return x
       return (x, x)
@require_tf
class 			SCREAMING_SNAKE_CASE__  :
      def 	SCREAMING_SNAKE_CASE							(	self    :	Union[str, Any]					,							SCREAMING_SNAKE_CASE__    :	List[Any]					,							SCREAMING_SNAKE_CASE__    :	List[str]					)		->      Optional[int]:
             pass
      def 	SCREAMING_SNAKE_CASE							(	self    :	Tuple					)		->      List[str]:
             pass
      def 	SCREAMING_SNAKE_CASE							(	self    :	int					)		->      Optional[int]:
             pass
      def 	SCREAMING_SNAKE_CASE							(	self    :	Tuple					,							SCREAMING_SNAKE_CASE__    :	Any					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]					,							SCREAMING_SNAKE_CASE__    :	List[Any]					,							SCREAMING_SNAKE_CASE__    :	Any					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=None					,							**SCREAMING_SNAKE_CASE__    :	List[str]					)		->      Dict:
             a_ :		List[Any]							=  VisionTextDualEncoderConfig.from_vision_text_configs(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)

             a_ :		List[str]							=  TFVisionTextDualEncoderModel(SCREAMING_SNAKE_CASE__					)

             a_ :		List[str]							=  model(input_ids=SCREAMING_SNAKE_CASE__					,							pixel_values=SCREAMING_SNAKE_CASE__					,							attention_mask=SCREAMING_SNAKE_CASE__					)

             self.assertEqual(output['text_embeds'].shape					,							(input_ids.shape[0], config.projection_dim)					)
             self.assertEqual(output['image_embeds'].shape					,							(pixel_values.shape[0], config.projection_dim)					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[Any]					,							SCREAMING_SNAKE_CASE__    :	Any					,							SCREAMING_SNAKE_CASE__    :	Optional[int]					,							SCREAMING_SNAKE_CASE__    :	Tuple					,							SCREAMING_SNAKE_CASE__    :	Optional[int]					,							SCREAMING_SNAKE_CASE__    :	List[str]=None					,							**SCREAMING_SNAKE_CASE__    :	int					)		->      List[Any]:
             a_							,		a_ :		List[str]							=  self.get_vision_text_model(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)
             a_ :		int							=  TFVisionTextDualEncoderModel(vision_model=SCREAMING_SNAKE_CASE__					,							text_model=SCREAMING_SNAKE_CASE__					)

             a_ :		int							=  model(input_ids=SCREAMING_SNAKE_CASE__					,							pixel_values=SCREAMING_SNAKE_CASE__					,							attention_mask=SCREAMING_SNAKE_CASE__					)

             self.assertEqual(output['text_embeds'].shape					,							(input_ids.shape[0], model.config.projection_dim)					)
             self.assertEqual(output['image_embeds'].shape					,							(pixel_values.shape[0], model.config.projection_dim)					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	Union[str, Any]					,							SCREAMING_SNAKE_CASE__    :	Optional[int]					,							SCREAMING_SNAKE_CASE__    :	Optional[int]					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]					,							SCREAMING_SNAKE_CASE__    :	int=None					,							**SCREAMING_SNAKE_CASE__    :	List[Any]					)		->      Dict:
             a_							,		a_ :		Union[str, Any]							=  self.get_vision_text_model(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)
             a_ :		Union[str, Any]							=  {'vision_model': vision_model, 'text_model': text_model}
             a_ :		Optional[int]							=  TFVisionTextDualEncoderModel.from_vision_text_pretrained(**SCREAMING_SNAKE_CASE__					)

             a_ :		Optional[Any]							=  model(input_ids=SCREAMING_SNAKE_CASE__					,							pixel_values=SCREAMING_SNAKE_CASE__					,							attention_mask=SCREAMING_SNAKE_CASE__					)

             self.assertEqual(output['text_embeds'].shape					,							(input_ids.shape[0], model.config.projection_dim)					)
             self.assertEqual(output['image_embeds'].shape					,							(pixel_values.shape[0], model.config.projection_dim)					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[Any]					,							SCREAMING_SNAKE_CASE__    :	List[Any]					,							SCREAMING_SNAKE_CASE__    :	int					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]					,							SCREAMING_SNAKE_CASE__    :	str					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]=None					,							**SCREAMING_SNAKE_CASE__    :	Union[str, Any]					)		->      Tuple:
             a_							,		a_ :		Tuple							=  self.get_vision_text_model(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)
             a_ :		List[str]							=  TFVisionTextDualEncoderModel(vision_model=SCREAMING_SNAKE_CASE__					,							text_model=SCREAMING_SNAKE_CASE__					)

             a_ :		Optional[Any]							=  model(input_ids=SCREAMING_SNAKE_CASE__					,							pixel_values=SCREAMING_SNAKE_CASE__					,							attention_mask=SCREAMING_SNAKE_CASE__					)
             a_ :		Optional[Any]							=  output[0].numpy()

             with tempfile.TemporaryDirectory() as tmpdirname:
                    model.save_pretrained(SCREAMING_SNAKE_CASE__					)
                    a_ :		Any							=  TFVisionTextDualEncoderModel.from_pretrained(SCREAMING_SNAKE_CASE__					)

                    a_ :		Tuple							=  model(input_ids=SCREAMING_SNAKE_CASE__					,							pixel_values=SCREAMING_SNAKE_CASE__					,							attention_mask=SCREAMING_SNAKE_CASE__					)
                    a_ :		Tuple							=  after_output[0].numpy()
                    a_ :		Dict							=  np.amax(np.abs(out_a - out_a					)					)
                    self.assertLessEqual(SCREAMING_SNAKE_CASE__					,							1E-5					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	Optional[int]					,							SCREAMING_SNAKE_CASE__    :	List[Any]					,							SCREAMING_SNAKE_CASE__    :	int					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]					,							SCREAMING_SNAKE_CASE__    :	Any					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]=None					,							**SCREAMING_SNAKE_CASE__    :	int					)		->      str:
             a_							,		a_ :		Optional[Any]							=  self.get_vision_text_model(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)
             a_ :		Any							=  TFVisionTextDualEncoderModel(vision_model=SCREAMING_SNAKE_CASE__					,							text_model=SCREAMING_SNAKE_CASE__					)

             a_ :		Optional[Any]							=  model(
                 input_ids=SCREAMING_SNAKE_CASE__					,							pixel_values=SCREAMING_SNAKE_CASE__					,							attention_mask=SCREAMING_SNAKE_CASE__					,							output_attentions=SCREAMING_SNAKE_CASE__					)

             a_ :		Tuple							=  output.vision_model_output.attentions
             self.assertEqual(len(SCREAMING_SNAKE_CASE__					)					,							vision_config.num_hidden_layers					)

             # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token)
             a_ :		Union[str, Any]							=  to_atuple(vision_model.config.image_size					)
             a_ :		Optional[Any]							=  to_atuple(vision_model.config.patch_size					)
             a_ :		str							=  (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
             a_ :		Optional[int]							=  num_patches + 1
             self.assertEqual(vision_attentions[0].shape[-3:]					,							(vision_config.num_attention_heads, seq_len, seq_len)					)

             a_ :		Dict							=  output.text_model_output.attentions
             self.assertEqual(len(SCREAMING_SNAKE_CASE__					)					,							text_config.num_hidden_layers					)

             self.assertEqual(
                 text_attentions[0].shape[-3:]					,							(text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])					,							)
      def 	SCREAMING_SNAKE_CASE							(	self    :	Tuple					,							SCREAMING_SNAKE_CASE__    :	np.ndarray					,							SCREAMING_SNAKE_CASE__    :	np.ndarray					,							SCREAMING_SNAKE_CASE__    :	float					)		->      Tuple:
             a_ :		Optional[Any]							=  np.abs((a - b)					).max()
             self.assertLessEqual(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					,							F"""Difference between torch and flax is {diff} (>= {tol})."""					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	Any					)		->      Tuple:
             a_ :		Dict							=  self.prepare_config_and_inputs()
             self.check_vision_text_dual_encoder_model(**SCREAMING_SNAKE_CASE__					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[Any]					)		->      Any:
             a_ :		Union[str, Any]							=  self.prepare_config_and_inputs()
             self.check_model_from_pretrained_configs(**SCREAMING_SNAKE_CASE__					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[str]					)		->      Any:
             a_ :		Dict							=  self.prepare_config_and_inputs()
             self.check_vision_text_dual_encoder_from_pretrained(**SCREAMING_SNAKE_CASE__					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	str					)		->      Tuple:
             a_ :		Tuple							=  self.prepare_config_and_inputs()
             self.check_save_load(**SCREAMING_SNAKE_CASE__					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	Optional[Any]					)		->      str:
             a_ :		List[str]							=  self.prepare_config_and_inputs()
             self.check_vision_text_output_attention(**SCREAMING_SNAKE_CASE__					)

      @slow
      def 	SCREAMING_SNAKE_CASE							(	self    :	Any					)		->      str:
             a_							,		a_ :		str							=  self.get_pretrained_model_and_inputs()

             a_ :		Optional[int]							=  model_a(**SCREAMING_SNAKE_CASE__					)
             a_ :		Tuple							=  outputs[0].numpy()

             with tempfile.TemporaryDirectory() as tmp_dirname:
                    model_a.save_pretrained(SCREAMING_SNAKE_CASE__					)
                    a_ :		Optional[Any]							=  TFVisionTextDualEncoderModel.from_pretrained(SCREAMING_SNAKE_CASE__					)

                    a_ :		List[Any]							=  model_a(**SCREAMING_SNAKE_CASE__					)
                    a_ :		Optional[int]							=  after_outputs[0].numpy()
                    a_ :		Union[str, Any]							=  np.amax(np.abs(out_a - out_a					)					)
                    self.assertLessEqual(SCREAMING_SNAKE_CASE__					,							1E-5					)
@require_tf
class 			SCREAMING_SNAKE_CASE__  (    lowercase__						,   unittest.TestCase      ):
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[str]					)		->      List[str]:
             a_ :		Optional[int]							=  TFVisionTextDualEncoderModel.from_vision_text_pretrained(
                 'hf-internal-testing/tiny-random-vit'					,							'hf-internal-testing/tiny-random-bert'					)
             a_ :		int							=  1_3
             a_ :		List[str]							=  floats_tensor(
                 [
                     batch_size,
                     model.vision_model.config.num_channels,
                     model.vision_model.config.image_size,
                     model.vision_model.config.image_size,
                 ]					)
             a_ :		List[Any]							=  ids_tensor([batch_size, 4]					,							model.text_model.config.vocab_size					)
             a_ :		Optional[int]							=  random_attention_mask([batch_size, 4]					)
             a_ :		List[Any]							=  {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask}

             return model, inputs
      def 	SCREAMING_SNAKE_CASE							(	self    :	str					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]					,							SCREAMING_SNAKE_CASE__    :	List[Any]					)		->      Dict:
             a_ :		Optional[int]							=  TFViTModel(SCREAMING_SNAKE_CASE__					,							name='vision_model'					)
             a_ :		str							=  TFBertModel(SCREAMING_SNAKE_CASE__					,							name='text_model'					)
             return vision_model, text_model

      def 	SCREAMING_SNAKE_CASE							(	self    :	str					)		->      str:
             a_ :		Dict							=  TFViTModelTester(self					)
             a_ :		Optional[int]							=  TFBertModelTester(self					)
             a_ :		Union[str, Any]							=  vit_model_tester.prepare_config_and_inputs()
             a_ :		Tuple							=  bert_model_tester.prepare_config_and_inputs()

             a_							,		a_							,		a_ :		int							=  vision_config_and_inputs

             (
                 (
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		
             ) :		int							=  text_config_and_inputs

             return {
                 "text_config": text_config,
                 "vision_config": vision_config,
                 "pixel_values": pixel_values,
                 "attention_mask": input_mask,
                 "input_ids": input_ids,
                 "text_token_type_ids": token_type_ids,
                 "text_sequence_labels": sequence_labels,
                 "text_token_labels": token_labels,
                 "text_choice_labels": choice_labels,
             }
@require_tf
class 			SCREAMING_SNAKE_CASE__  (    lowercase__						,   unittest.TestCase      ):
      def 	SCREAMING_SNAKE_CASE							(	self    :	Tuple					)		->      List[Any]:
             # DeiT repo doesn't have TF weights, but we don't actually use the weights at all so let's
             # just reinitialize it.
             a_ :		List[str]							=  TFVisionTextDualEncoderModel.from_vision_text_pretrained(
                 'Rocketknight1/tiny-random-deit-tf'					,							'hf-internal-testing/tiny-random-roberta'					)
             a_ :		Any							=  1_3
             a_ :		List[Any]							=  floats_tensor(
                 [
                     batch_size,
                     model.vision_model.config.num_channels,
                     model.vision_model.config.image_size,
                     model.vision_model.config.image_size,
                 ]					)
             a_ :		Dict							=  ids_tensor([batch_size, 4]					,							model.text_model.config.vocab_size					)
             a_ :		Dict							=  random_attention_mask([batch_size, 4]					)
             a_ :		Optional[int]							=  {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask}

             return model, inputs
      def 	SCREAMING_SNAKE_CASE							(	self    :	Tuple					,							SCREAMING_SNAKE_CASE__    :	Any					,							SCREAMING_SNAKE_CASE__    :	List[Any]					,							SCREAMING_SNAKE_CASE__    :	Any					,							SCREAMING_SNAKE_CASE__    :	str					,							SCREAMING_SNAKE_CASE__    :	int=None					,							**SCREAMING_SNAKE_CASE__    :	List[str]					)		->      Dict:
             a_							,		a_ :		Dict							=  self.get_vision_text_model(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)
             a_ :		List[Any]							=  TFVisionTextDualEncoderModel(vision_model=SCREAMING_SNAKE_CASE__					,							text_model=SCREAMING_SNAKE_CASE__					)

             a_ :		str							=  model(
                 input_ids=SCREAMING_SNAKE_CASE__					,							pixel_values=SCREAMING_SNAKE_CASE__					,							attention_mask=SCREAMING_SNAKE_CASE__					,							output_attentions=SCREAMING_SNAKE_CASE__					)

             a_ :		Tuple							=  output.vision_model_output.attentions
             self.assertEqual(len(SCREAMING_SNAKE_CASE__					)					,							vision_config.num_hidden_layers					)

             # in DEiT, the seq_len equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens)
             a_ :		List[str]							=  to_atuple(vision_model.config.image_size					)
             a_ :		str							=  to_atuple(vision_model.config.patch_size					)
             a_ :		str							=  (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
             a_ :		int							=  num_patches + 2
             self.assertEqual(vision_attentions[0].shape[-3:]					,							(vision_config.num_attention_heads, seq_len, seq_len)					)

             a_ :		Optional[int]							=  output.text_model_output.attentions
             self.assertEqual(len(SCREAMING_SNAKE_CASE__					)					,							text_config.num_hidden_layers					)

             self.assertEqual(
                 text_attentions[0].shape[-3:]					,							(text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])					,							)
      def 	SCREAMING_SNAKE_CASE							(	self    :	Tuple					,							SCREAMING_SNAKE_CASE__    :	List[Any]					,							SCREAMING_SNAKE_CASE__    :	int					)		->      int:
             a_ :		Tuple							=  TFDeiTModel(SCREAMING_SNAKE_CASE__					,							name='vision_model'					)
             a_ :		Any							=  TFRobertaModel(SCREAMING_SNAKE_CASE__					,							name='text_model'					)
             return vision_model, text_model

      def 	SCREAMING_SNAKE_CASE							(	self    :	Optional[Any]					)		->      int:
             a_ :		str							=  TFDeiTModelTester(self					)
             a_ :		Any							=  TFRobertaModelTester(self					)
             a_ :		Union[str, Any]							=  vit_model_tester.prepare_config_and_inputs()
             a_ :		Optional[int]							=  bert_model_tester.prepare_config_and_inputs()

             a_							,		a_							,		a_ :		Optional[Any]							=  vision_config_and_inputs

             (
                 (
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		
             ) :		Dict							=  text_config_and_inputs

             return {
                 "text_config": text_config,
                 "vision_config": vision_config,
                 "pixel_values": pixel_values,
                 "attention_mask": input_mask,
                 "input_ids": input_ids,
                 "text_token_type_ids": token_type_ids,
                 "text_sequence_labels": sequence_labels,
                 "text_token_labels": token_labels,
                 "text_choice_labels": choice_labels,
             }
@require_tf
class 			SCREAMING_SNAKE_CASE__  (    lowercase__						,   unittest.TestCase      ):
      def 	SCREAMING_SNAKE_CASE							(	self    :	Tuple					)		->      Optional[Any]:
             a_ :		Optional[Any]							=  TFVisionTextDualEncoderModel.from_vision_text_pretrained(
                 'Rocketknight1/tiny-random-clip-tf'					,							'hf-internal-testing/tiny-random-bert'					)
             a_ :		Any							=  1_3
             a_ :		Tuple							=  floats_tensor(
                 [
                     batch_size,
                     model.vision_model.config.num_channels,
                     model.vision_model.config.image_size,
                     model.vision_model.config.image_size,
                 ]					)
             a_ :		Optional[Any]							=  ids_tensor([batch_size, 4]					,							model.text_model.config.vocab_size					)
             a_ :		int							=  random_attention_mask([batch_size, 4]					)
             a_ :		int							=  {'pixel_values': pixel_values, 'input_ids': input_ids, 'attention_mask': attention_mask}

             return model, inputs
      def 	SCREAMING_SNAKE_CASE							(	self    :	int					,							SCREAMING_SNAKE_CASE__    :	Any					,							SCREAMING_SNAKE_CASE__    :	str					)		->      Tuple:
             a_ :		Any							=  TFCLIPVisionModel(SCREAMING_SNAKE_CASE__					,							name='vision_model'					)
             a_ :		Union[str, Any]							=  TFBertModel(SCREAMING_SNAKE_CASE__					,							name='text_model'					)
             return vision_model, text_model

      def 	SCREAMING_SNAKE_CASE							(	self    :	Dict					)		->      Optional[int]:
             a_ :		Any							=  TFCLIPVisionModelTester(self					)
             a_ :		int							=  TFBertModelTester(self					)
             a_ :		Optional[int]							=  clip_model_tester.prepare_config_and_inputs()
             a_ :		List[Any]							=  bert_model_tester.prepare_config_and_inputs()

             a_							,		a_ :		List[str]							=  vision_config_and_inputs

             (
                 (
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		(
                 a_
             )							,		
             ) :		Any							=  text_config_and_inputs

             return {
                 "text_config": text_config,
                 "vision_config": vision_config,
                 "pixel_values": pixel_values,
                 "attention_mask": input_mask,
                 "input_ids": input_ids,
                 "text_token_type_ids": token_type_ids,
                 "text_sequence_labels": sequence_labels,
                 "text_token_labels": token_labels,
                 "text_choice_labels": choice_labels,
             }
@require_vision
@require_tf
class 			SCREAMING_SNAKE_CASE__  (    unittest.TestCase      ):
      @slow
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[str]					)		->      Optional[int]:
             a_ :		Optional[Any]							=  TFVisionTextDualEncoderModel.from_pretrained(
                 'clip-italian/clip-italian'					,							logit_scale_init_value=1.0					,							from_pt=SCREAMING_SNAKE_CASE__					)
             a_ :		int							=  VisionTextDualEncoderProcessor.from_pretrained('clip-italian/clip-italian'					)

             a_ :		List[str]							=  Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'					)
             a_ :		Any							=  processor(
                 text=['una foto di un gatto', 'una foto di un cane']					,							images=SCREAMING_SNAKE_CASE__					,							padding=SCREAMING_SNAKE_CASE__					,							return_tensors='np'					)

             a_ :		List[Any]							=  model(**SCREAMING_SNAKE_CASE__					)

             # verify the logits
             self.assertEqual(outputs.logits_per_image.shape					,							(inputs.pixel_values.shape[0], inputs.input_ids.shape[0])					)
             self.assertEqual(
                 outputs.logits_per_text.shape					,							(inputs.input_ids.shape[0], inputs.pixel_values.shape[0])					,							)

             a_ :		Any							=  np.array([[1.2284727, 0.3104122]]					)

             self.assertTrue(np.allclose(outputs.logits_per_image.numpy()					,							SCREAMING_SNAKE_CASE__					,							atol=1E-3					)					)






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 	SCREAMING_SNAKE_CASE_						(      __A			:      Any    )     ->		Optional[int]:

       """simple docstring"""

       a_ :		str							=  filter(lambda __A    : p.requires_grad  ,   model.parameters()    )
       a_ :		List[Any]							=  sum([np.prod(p.size()    ) for p in model_parameters]    )
       return params


UpperCAmelCase_				:			Dict   =	logging.getLogger(__name__)







def 	SCREAMING_SNAKE_CASE_						(      __A			:      int  ,   __A			:      int    )     ->		List[str]:

       """simple docstring"""

       if metric == "rouge2":
              a_ :		Dict							=  '{val_avg_rouge2:.4f}-{step_count}'
       elif metric == "bleu":
              a_ :		Dict							=  '{val_avg_bleu:.4f}-{step_count}'
       elif metric == "em":
              a_ :		Union[str, Any]							=  '{val_avg_em:.4f}-{step_count}'
       elif metric == "loss":
              a_ :		str							=  '{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.'    )

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

       return checkpoint_callback







def 	SCREAMING_SNAKE_CASE_						(      __A			:      int  ,   __A			:      List[Any]    )     ->		int:

       """simple docstring"""

       return EarlyStopping(
           monitor=F"""val_{metric}"""  ,   mode='min' if 'loss' in metric else 'max'  ,   patience=__A  ,   verbose=__A  ,   )
class 			SCREAMING_SNAKE_CASE__  (    pl.Callback      ):
      def 	SCREAMING_SNAKE_CASE							(	self    :	Dict					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]					,							SCREAMING_SNAKE_CASE__    :	List[str]					)		->      Any:
             a_ :		int							=  {F"""lr_group_{i}""": param['lr'] for i, param in enumerate(pl_module.trainer.optimizers[0].param_groups					)}
             pl_module.logger.log_metrics(SCREAMING_SNAKE_CASE__					)
      @rank_zero_only
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[str]					,							SCREAMING_SNAKE_CASE__    :	pl.Trainer					,							SCREAMING_SNAKE_CASE__    :	pl.LightningModule					,							SCREAMING_SNAKE_CASE__    :	str					,							SCREAMING_SNAKE_CASE__    :	List[str]=True					)		->      None:
             logger.info(F"""***** {type_path} results at step {trainer.global_step:05d} *****"""					)
             a_ :		List[str]							=  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
             a_ :		Optional[int]							=  Path(pl_module.hparams.output_dir					)
             if type_path == "test":
                    a_ :		Tuple							=  od / 'test_results.txt'
                    a_ :		Optional[int]							=  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.
                    a_ :		str							=  od / F"""{type_path}_results/{trainer.global_step:05d}.txt"""
                    a_ :		Optional[Any]							=  od / F"""{type_path}_generations/{trainer.global_step:05d}.txt"""
                    results_file.parent.mkdir(exist_ok=SCREAMING_SNAKE_CASE__					)
                    generations_file.parent.mkdir(exist_ok=SCREAMING_SNAKE_CASE__					)
             with open(SCREAMING_SNAKE_CASE__					,							'a+'					) as writer:
                    for key in sorted(SCREAMING_SNAKE_CASE__					):
                           if key in ["log", "progress_bar", "preds"]:
                                  continue
                           a_ :		int							=  metrics[key]
                           if isinstance(SCREAMING_SNAKE_CASE__					,							torch.Tensor					):
                                  a_ :		List[str]							=  val.item()
                           a_ :		int							=  F"""{key}: {val:.6f}\n"""
                           writer.write(SCREAMING_SNAKE_CASE__					)

             if not save_generations:
                    return

             if "preds" in metrics:
                    a_ :		Optional[Any]							=  '\n'.join(metrics['preds']					)
                    generations_file.open('w+'					).write(SCREAMING_SNAKE_CASE__					)
      @rank_zero_only
      def 	SCREAMING_SNAKE_CASE							(	self    :	Any					,							SCREAMING_SNAKE_CASE__    :	Dict					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]					)		->      Dict:
             try:
                    a_ :		Any							=  pl_module.model.model.num_parameters()
             except AttributeError:
                    a_ :		List[str]							=  pl_module.model.num_parameters()

             a_ :		Any							=  count_trainable_parameters(SCREAMING_SNAKE_CASE__					)
             # 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 	SCREAMING_SNAKE_CASE							(	self    :	Union[str, Any]					,							SCREAMING_SNAKE_CASE__    :	pl.Trainer					,							SCREAMING_SNAKE_CASE__    :	pl.LightningModule					)		->      Optional[int]:
             save_json(pl_module.metrics					,							pl_module.metrics_save_path					)
             return self._write_logs(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					,							'test'					)

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


