
"""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	(						_A      )    ->  Any:





    """simple docstring"""

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


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


def  _UpperCamelCase	(						_A					,      _A      )    ->  Dict:





    """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=_A					,      filename=_A					,      monitor=F"""val_{metric}"""					,      mode="""max"""					,      save_top_k=1					,      every_n_epochs=1					,      )

    return checkpoint_callback


def  _UpperCamelCase	(						_A					,      _A      )    ->  Tuple:





    """simple docstring"""

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







class 		a_						( pl.Callback	):







    def      _snake_case					(			self			:       Dict     ,					__UpperCamelCase			:       Tuple     ,					__UpperCamelCase			:       Tuple     )		->List[str]:






        '''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(__UpperCamelCase     )







    @rank_zero_only
    def      _snake_case					(			self			:       Optional[Any]     ,					__UpperCamelCase			:       pl.Trainer     ,					__UpperCamelCase			:       pl.LightningModule     ,					__UpperCamelCase			:       str     ,					__UpperCamelCase			:       str=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=__UpperCamelCase     )
            generations_file.parent.mkdir(exist_ok=__UpperCamelCase     )
        with open(__UpperCamelCase     ,					"""a+"""     ) as writer:
            for key in sorted(__UpperCamelCase     ):
                if key in ["log", "progress_bar", "preds"]:
                    continue
                _UpperCAmelCase          =   metrics[key]
                if isinstance(__UpperCamelCase     ,					torch.Tensor     ):
                    _UpperCAmelCase          =   val.item()
                _UpperCAmelCase          =   f"""{key}: {val:.6f}\n"""
                writer.write(__UpperCamelCase     )

        if not save_generations:
            return

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







    @rank_zero_only
    def      _snake_case					(			self			:       Tuple     ,					__UpperCamelCase			:       Dict     ,					__UpperCamelCase			:       List[Any]     )		->List[str]:






        '''simple docstring'''






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

        _UpperCAmelCase          =   count_trainable_parameters(__UpperCamelCase     )
        # 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      _snake_case					(			self			:       Optional[int]     ,					__UpperCamelCase			:       pl.Trainer     ,					__UpperCamelCase			:       pl.LightningModule     )		->Optional[Any]:






        '''simple docstring'''






        save_json(pl_module.metrics     ,					pl_module.metrics_save_path     )
        return self._write_logs(__UpperCamelCase     ,					__UpperCamelCase     ,					"""test"""     )







    @rank_zero_only
    def      _snake_case					(			self			:       List[Any]     ,					__UpperCamelCase			:       pl.Trainer     ,					__UpperCamelCase			:       Any     )		->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")

"""simple docstring"""



import re
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin




class 		a_						( _UpperCAmelCase	):
    a      :				Any     =				['image_processor', 'tokenizer']
    a      :				Optional[int]     =				'AutoImageProcessor'
    a      :				Any     =				'AutoTokenizer'







    def __init__(			self			:       List[str]     ,					__UpperCamelCase			:       List[Any]=None     ,					__UpperCamelCase			:       List[str]=None     ,					**__UpperCamelCase			:       List[Any]     )		->Dict:






        '''simple docstring'''






        _UpperCAmelCase          =   None
        if "feature_extractor" in kwargs:
            warnings.warn(
                """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
                """ instead."""     ,					__UpperCamelCase     ,					)
            _UpperCAmelCase          =   kwargs.pop("""feature_extractor"""     )

        _UpperCAmelCase          =   image_processor if image_processor is not None else feature_extractor
        if image_processor is None:
            raise ValueError("""You need to specify an `image_processor`."""     )
        if tokenizer is None:
            raise ValueError("""You need to specify a `tokenizer`."""     )

        super().__init__(__UpperCamelCase     ,					__UpperCamelCase     )
        _UpperCAmelCase          =   self.image_processor
        _UpperCAmelCase          =   False







    def __call__(			self			:       Union[str, Any]     ,					*__UpperCamelCase			:       str     ,					**__UpperCamelCase			:       Optional[Any]     )		->List[str]:






        '''simple docstring'''






        if self._in_target_context_manager:
            return self.current_processor(*__UpperCamelCase     ,					**__UpperCamelCase     )

        _UpperCAmelCase          =   kwargs.pop("""images"""     ,					__UpperCamelCase     )
        _UpperCAmelCase          =   kwargs.pop("""text"""     ,					__UpperCamelCase     )
        if len(__UpperCamelCase     ) > 0:
            _UpperCAmelCase          =   args[0]
            _UpperCAmelCase          =   args[1:]

        if images is None and text is None:
            raise ValueError("""You need to specify either an `images` or `text` input to process."""     )

        if images is not None:
            _UpperCAmelCase          =   self.image_processor(__UpperCamelCase     ,					*__UpperCamelCase     ,					**__UpperCamelCase     )
        if text is not None:
            _UpperCAmelCase          =   self.tokenizer(__UpperCamelCase     ,					**__UpperCamelCase     )

        if text is None:
            return inputs
        elif images is None:
            return encodings
        else:
            _UpperCAmelCase          =   encodings["""input_ids"""]
            return inputs







    def      _snake_case					(			self			:       Union[str, Any]     ,					*__UpperCamelCase			:       int     ,					**__UpperCamelCase			:       Tuple     )		->Tuple:






        '''simple docstring'''






        return self.tokenizer.batch_decode(*__UpperCamelCase     ,					**__UpperCamelCase     )







    def      _snake_case					(			self			:       Tuple     ,					*__UpperCamelCase			:       int     ,					**__UpperCamelCase			:       Union[str, Any]     )		->int:






        '''simple docstring'''






        return self.tokenizer.decode(*__UpperCamelCase     ,					**__UpperCamelCase     )







    @contextmanager
    def      _snake_case					(			self			:       Tuple     )		->Union[str, Any]:






        '''simple docstring'''






        warnings.warn(
            """`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your """
            """labels by using the argument `text` of the regular `__call__` method (either in the same call as """
            """your images inputs, or in a separate call."""     )
        _UpperCAmelCase          =   True
        _UpperCAmelCase          =   self.tokenizer
        yield
        _UpperCAmelCase          =   self.image_processor
        _UpperCAmelCase          =   False







    def      _snake_case					(			self			:       str     ,					__UpperCamelCase			:       Dict     ,					__UpperCamelCase			:       Optional[Any]=False     ,					__UpperCamelCase			:       Union[str, Any]=None     )		->List[str]:






        '''simple docstring'''






        if added_vocab is None:
            _UpperCAmelCase          =   self.tokenizer.get_added_vocab()

        _UpperCAmelCase          =   {}

        while tokens:
            _UpperCAmelCase          =   re.search(r"""<s_(.*?)>"""     ,					__UpperCamelCase     ,					re.IGNORECASE     )
            if start_token is None:
                break
            _UpperCAmelCase          =   start_token.group(1     )
            _UpperCAmelCase          =   re.search(rf"""</s_{key}>"""     ,					__UpperCamelCase     ,					re.IGNORECASE     )
            _UpperCAmelCase          =   start_token.group()
            if end_token is None:
                _UpperCAmelCase          =   tokens.replace(__UpperCamelCase     ,					""""""     )
            else:
                _UpperCAmelCase          =   end_token.group()
                _UpperCAmelCase          =   re.escape(__UpperCamelCase     )
                _UpperCAmelCase          =   re.escape(__UpperCamelCase     )
                _UpperCAmelCase          =   re.search(f"""{start_token_escaped}(.*?){end_token_escaped}"""     ,					__UpperCamelCase     ,					re.IGNORECASE     )
                if content is not None:
                    _UpperCAmelCase          =   content.group(1     ).strip()
                    if r"<s_" in content and r"</s_" in content:  # non-leaf node
                        _UpperCAmelCase          =   self.tokenajson(__UpperCamelCase     ,					is_inner_value=__UpperCamelCase     ,					added_vocab=__UpperCamelCase     )
                        if value:
                            if len(__UpperCamelCase     ) == 1:
                                _UpperCAmelCase          =   value[0]
                            _UpperCAmelCase          =   value
                    else:  # leaf nodes
                        _UpperCAmelCase          =   []
                        for leaf in content.split(r"""<sep/>"""     ):
                            _UpperCAmelCase          =   leaf.strip()
                            if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>":
                                _UpperCAmelCase          =   leaf[1:-2]  # for categorical special tokens
                            output[key].append(__UpperCamelCase     )
                        if len(output[key]     ) == 1:
                            _UpperCAmelCase          =   output[key][0]

                _UpperCAmelCase          =   tokens[tokens.find(__UpperCamelCase     ) + len(__UpperCamelCase     ) :].strip()
                if tokens[:6] == r"<sep/>":  # non-leaf nodes
                    return [output] + self.tokenajson(tokens[6:]     ,					is_inner_value=__UpperCamelCase     ,					added_vocab=__UpperCamelCase     )

        if len(__UpperCamelCase     ):
            return [output] if is_inner_value else output
        else:
            return [] if is_inner_value else {"text_sequence": tokens}







    @property
    def      _snake_case					(			self			:       Tuple     )		->Optional[int]:






        '''simple docstring'''






        warnings.warn(
            """`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."""     ,					__UpperCamelCase     ,					)
        return self.image_processor_class







    @property
    def      _snake_case					(			self			:       List[str]     )		->Dict:






        '''simple docstring'''






        warnings.warn(
            """`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."""     ,					__UpperCamelCase     ,					)
        return self.image_processor