
"""simple docstring"""



import math

import tensorflow as tf
from packaging import version


def 			_UpperCamelCase						(    _A   )     ->	Tuple:





    """simple docstring"""

    _UpperCAmelCase        = tf.convert_to_tensor(_A   )
    _UpperCAmelCase        = 0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0   )       ,   x.dtype   )   ))

    return x * cdf


def 			_UpperCamelCase						(    _A   )     ->	Union[str, Any]:





    """simple docstring"""

    _UpperCAmelCase        = tf.convert_to_tensor(_A   )
    _UpperCAmelCase        = tf.cast(math.pi       ,   x.dtype   )
    _UpperCAmelCase        = tf.cast(0.044_715       ,   x.dtype   )
    _UpperCAmelCase        = 0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi   ) * (x + coeff * tf.pow(_A       ,   3   ))   ))

    return x * cdf


def 			_UpperCamelCase						(    _A   )     ->	Optional[Any]:





    """simple docstring"""

    _UpperCAmelCase        = tf.convert_to_tensor(_A   )

    return x * tf.tanh(tf.math.softplus(_A   )   )


def 			_UpperCamelCase						(    _A   )     ->	Dict:





    """simple docstring"""

    _UpperCAmelCase        = tf.convert_to_tensor(_A   )
    _UpperCAmelCase        = tf.cast(0.044_715       ,   x.dtype   )
    _UpperCAmelCase        = tf.cast(0.7_978_845_608       ,   x.dtype   )

    return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x)   ))


def 			_UpperCamelCase						(    _A   )     ->	Tuple:





    """simple docstring"""

    _UpperCAmelCase        = tf.convert_to_tensor(_A   )
    _UpperCAmelCase        = tf.cast(1.702       ,   x.dtype   )
    return x * tf.math.sigmoid(coeff * x   )


def 			_UpperCamelCase						(    _A   )     ->	Optional[int]:





    """simple docstring"""

    return tf.clip_by_value(_gelu(_A   )       ,   -1_0       ,   1_0   )


def 			_UpperCamelCase						(    _A       ,   _A=-1   )     ->	str:





    """simple docstring"""

    _UpperCAmelCase     ,_UpperCAmelCase        = tf.split(_A       ,   2       ,   axis=_A   )
    return a * tf.math.sigmoid(_A   )


if version.parse(tf.version.VERSION) >= version.parse('''2.4'''):


 def 			_UpperCamelCase						(    _A   )     ->	Dict:





     """simple docstring"""

     return tf.keras.activations.gelu(_A       ,   approximate=_A   )

 a       :						Optional[Any]       		=		tf.keras.activations.gelu







 a       :						Dict       		=		approximate_gelu_wrap
else:
 a       :						Dict       		=		_gelu
 a       :						Any       		=		_gelu_new


a       :						Any       		=		{
    '''gelu''': gelu,
    '''gelu_10''': gelu_aa,
    '''gelu_fast''': gelu_fast,
    '''gelu_new''': gelu_new,
    '''glu''': glu,
    '''mish''': mish,
    '''quick_gelu''': quick_gelu,
    '''relu''': tf.keras.activations.relu,
    '''sigmoid''': tf.keras.activations.sigmoid,
    '''silu''': tf.keras.activations.swish,
    '''swish''': tf.keras.activations.swish,
    '''tanh''': tf.keras.activations.tanh,
}







def 			_UpperCamelCase						(    _A   )     ->	Union[str, Any]:





    """simple docstring"""

    if activation_string in ACTaFN:
        return ACTaFN[activation_string]
    else:
        raise KeyError(F"""function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()   )}"""   )

"""simple docstring"""



import os
import warnings
from typing import List, Optional

from ...tokenization_utils_base import BatchEncoding
from ...utils import logging
from .configuration_rag import RagConfig


a       :						List[str]       		=		logging.get_logger(__name__)




class 		a_    :







    def __init__(      self      :							List[str]  ,       __UpperCamelCase      :							Any  ,       __UpperCamelCase      :							Tuple  )		->Tuple:






        '''simple docstring'''






        _UpperCAmelCase        = question_encoder
        _UpperCAmelCase        = generator
        _UpperCAmelCase        = self.question_encoder







    def   _snake_case       (      self      :							int  ,       __UpperCamelCase      :							str  )		->Dict:






        '''simple docstring'''






        if os.path.isfile(__UpperCamelCase  ):
            raise ValueError(f"""Provided path ({save_directory}) should be a directory, not a file"""  )
        os.makedirs(__UpperCamelCase  ,       exist_ok=__UpperCamelCase  )
        _UpperCAmelCase        = os.path.join(__UpperCamelCase  ,       """question_encoder_tokenizer"""  )
        _UpperCAmelCase        = os.path.join(__UpperCamelCase  ,       """generator_tokenizer"""  )
        self.question_encoder.save_pretrained(__UpperCamelCase  )
        self.generator.save_pretrained(__UpperCamelCase  )







    @classmethod
    def   _snake_case       (      cls      :							Optional[Any]  ,       __UpperCamelCase      :							Tuple  ,       **__UpperCamelCase      :							Optional[Any]  )		->Tuple:






        '''simple docstring'''






        from ..auto.tokenization_auto import AutoTokenizer

        _UpperCAmelCase        = kwargs.pop("""config"""  ,       __UpperCamelCase  )

        if config is None:
            _UpperCAmelCase        = RagConfig.from_pretrained(__UpperCamelCase  )

        _UpperCAmelCase        = AutoTokenizer.from_pretrained(
            __UpperCamelCase  ,       config=config.question_encoder  ,       subfolder="""question_encoder_tokenizer"""  )
        _UpperCAmelCase        = AutoTokenizer.from_pretrained(
            __UpperCamelCase  ,       config=config.generator  ,       subfolder="""generator_tokenizer"""  )
        return cls(question_encoder=__UpperCamelCase  ,       generator=__UpperCamelCase  )







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






        '''simple docstring'''






        return self.current_tokenizer(*__UpperCamelCase  ,       **__UpperCamelCase  )







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






        '''simple docstring'''






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







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






        '''simple docstring'''






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







    def   _snake_case       (      self      :							Union[str, Any]  )		->Dict:






        '''simple docstring'''






        _UpperCAmelCase        = self.question_encoder







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






        '''simple docstring'''






        _UpperCAmelCase        = self.generator







    def   _snake_case       (      self      :							List[Any]  ,       __UpperCamelCase      :							List[str]  ,       __UpperCamelCase      :							Optional[List[str]] = None  ,       __UpperCamelCase      :							Optional[int] = None  ,       __UpperCamelCase      :							Optional[int] = None  ,       __UpperCamelCase      :							str = "longest"  ,       __UpperCamelCase      :							str = None  ,       __UpperCamelCase      :							bool = True  ,       **__UpperCamelCase      :							Tuple  ,       )		->BatchEncoding:






        '''simple docstring'''






        warnings.warn(
            """`prepare_seq2seq_batch` is deprecated and will be removed in version 5 of 🤗 Transformers. Use the """
            """regular `__call__` method to prepare your inputs and the tokenizer under the `with_target_tokenizer` """
            """context manager to prepare your targets. See the documentation of your specific tokenizer for more """
            """details"""  ,       __UpperCamelCase  ,       )
        if max_length is None:
            _UpperCAmelCase        = self.current_tokenizer.model_max_length
        _UpperCAmelCase        = self(
            __UpperCamelCase  ,       add_special_tokens=__UpperCamelCase  ,       return_tensors=__UpperCamelCase  ,       max_length=__UpperCamelCase  ,       padding=__UpperCamelCase  ,       truncation=__UpperCamelCase  ,       **__UpperCamelCase  ,       )
        if tgt_texts is None:
            return model_inputs
        # Process tgt_texts
        if max_target_length is None:
            _UpperCAmelCase        = self.current_tokenizer.model_max_length
        _UpperCAmelCase        = self(
            text_target=__UpperCamelCase  ,       add_special_tokens=__UpperCamelCase  ,       return_tensors=__UpperCamelCase  ,       padding=__UpperCamelCase  ,       max_length=__UpperCamelCase  ,       truncation=__UpperCamelCase  ,       **__UpperCamelCase  ,       )
        _UpperCAmelCase        = labels["""input_ids"""]
        return model_inputs