"""simple docstring"""







import os

from bleurt import score  # From: git+https://github.com/google-research/bleurt.git

import datasets


__SCREAMING_SNAKE_CASE								=datasets.logging.get_logger(__name__)


__SCREAMING_SNAKE_CASE								="\\n@inproceedings{bleurt,\n  title={BLEURT: Learning Robust Metrics for Text Generation},\n  author={Thibault Sellam and Dipanjan Das and Ankur P. Parikh},\n  booktitle={ACL},\n  year={2020},\n  url={https://arxiv.org/abs/2004.04696}\n}\n"

__SCREAMING_SNAKE_CASE								="\\nBLEURT a learnt evaluation metric for Natural Language Generation. It is built using multiple phases of transfer learning starting from a pretrained BERT model (Devlin et al. 2018)\nand then employing another pre-training phrase using synthetic data. Finally it is trained on WMT human annotations. You may run BLEURT out-of-the-box or fine-tune\nit for your specific application (the latter is expected to perform better).\n\nSee the project's README at https://github.com/google-research/bleurt#readme for more information.\n"

__SCREAMING_SNAKE_CASE								="\nBLEURT score.\n\nArgs:\n    `predictions` (list of str): prediction/candidate sentences\n    `references` (list of str): reference sentences\n    `checkpoint` BLEURT checkpoint. Will default to BLEURT-tiny if None.\n\nReturns:\n    'scores': List of scores.\nExamples:\n\n    >>> predictions = [\"hello there\", \"general kenobi\"]\n    >>> references = [\"hello there\", \"general kenobi\"]\n    >>> bleurt = datasets.load_metric(\"bleurt\")\n    >>> results = bleurt.compute(predictions=predictions, references=references)\n    >>> print([round(v, 2) for v in results[\"scores\"]])\n    [1.03, 1.04]\n"

__SCREAMING_SNAKE_CASE								={
    "bleurt-tiny-128": "https://storage.googleapis.com/bleurt-oss/bleurt-tiny-128.zip",
    "bleurt-tiny-512": "https://storage.googleapis.com/bleurt-oss/bleurt-tiny-512.zip",
    "bleurt-base-128": "https://storage.googleapis.com/bleurt-oss/bleurt-base-128.zip",
    "bleurt-base-512": "https://storage.googleapis.com/bleurt-oss/bleurt-base-512.zip",
    "bleurt-large-128": "https://storage.googleapis.com/bleurt-oss/bleurt-large-128.zip",
    "bleurt-large-512": "https://storage.googleapis.com/bleurt-oss/bleurt-large-512.zip",
    "BLEURT-20-D3": "https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D3.zip",
    "BLEURT-20-D6": "https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D6.zip",
    "BLEURT-20-D12": "https://storage.googleapis.com/bleurt-oss-21/BLEURT-20-D12.zip",
    "BLEURT-20": "https://storage.googleapis.com/bleurt-oss-21/BLEURT-20.zip",
}







@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION		,   _KWARGS_DESCRIPTION						)
class  UpperCamelCase	(    datasets.Metric						):
   def     _UpperCAmelCase     (    self    )			->   Optional[Any]:


        '''simple docstring'''


        return datasets.MetricInfo(
            description=_DESCRIPTION  ,citation=_CITATION  ,homepage='https://github.com/google-research/bleurt'  ,inputs_description=_KWARGS_DESCRIPTION  ,features=datasets.Features(
                {
                    'predictions': datasets.Value('string'  ,id='sequence'    ),
                    'references': datasets.Value('string'  ,id='sequence'    ),
                }    )  ,codebase_urls=['https://github.com/google-research/bleurt']  ,reference_urls=['https://github.com/google-research/bleurt', 'https://arxiv.org/abs/2004.04696']  ,)
   def     _UpperCAmelCase     (    self  ,__UpperCamelCase    )			->   Dict:


        '''simple docstring'''


        if self.config_name == "default":
             logger.warning(
                 'Using default BLEURT-Base checkpoint for sequence maximum length 128. '
                 'You can use a bigger model for better results with e.g.: datasets.load_metric(\'bleurt\', \'bleurt-large-512\').'    )
             lowercase_			:       Any									=			'bleurt-base-128'

        if self.config_name.lower() in CHECKPOINT_URLS:
             lowercase_			:       Optional[int]									=			self.config_name.lower()

        elif self.config_name.upper() in CHECKPOINT_URLS:
             lowercase_			:       int									=			self.config_name.upper()

        else:
             raise KeyError(
                 f'''{self.config_name} model not found. You should supply the name of a model checkpoint for bleurt in {CHECKPOINT_URLS.keys()}'''    )

        # download the model checkpoint specified by self.config_name and set up the scorer
        lowercase_			:       int									=			dl_manager.download_and_extract(CHECKPOINT_URLS[checkpoint_name]    )
        lowercase_			:       Optional[Any]									=			score.BleurtScorer(os.path.join(__UpperCamelCase  ,__UpperCamelCase    )    )







   def     _UpperCAmelCase     (    self  ,__UpperCamelCase  ,__UpperCamelCase    )			->   Dict:


        '''simple docstring'''


        lowercase_			:       List[str]									=			self.scorer.score(references=__UpperCamelCase  ,candidates=__UpperCamelCase    )
        return {"scores": scores}


"""simple docstring"""







import unittest

import numpy as np

from transformers import DistilBertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
       import jax.numpy as jnp

       from transformers.models.distilbert.modeling_flax_distilbert import (
           FlaxDistilBertForMaskedLM,
           FlaxDistilBertForMultipleChoice,
           FlaxDistilBertForQuestionAnswering,
           FlaxDistilBertForSequenceClassification,
           FlaxDistilBertForTokenClassification,
           FlaxDistilBertModel,
       )







class  UpperCamelCase	(    unittest.TestCase						):
   def __init__(    self  ,__UpperCamelCase  ,__UpperCamelCase=13  ,__UpperCamelCase=7  ,__UpperCamelCase=True  ,__UpperCamelCase=True  ,__UpperCamelCase=True  ,__UpperCamelCase=True  ,__UpperCamelCase=99  ,__UpperCamelCase=32  ,__UpperCamelCase=5  ,__UpperCamelCase=4  ,__UpperCamelCase=37  ,__UpperCamelCase="gelu"  ,__UpperCamelCase=0.1  ,__UpperCamelCase=0.1  ,__UpperCamelCase=512  ,__UpperCamelCase=16  ,__UpperCamelCase=2  ,__UpperCamelCase=0.02  ,__UpperCamelCase=4  ,)			->   str:


        '''simple docstring'''


        lowercase_			:       str									=			parent
        lowercase_			:       List[str]									=			batch_size
        lowercase_			:       Optional[Any]									=			seq_length
        lowercase_			:       str									=			is_training
        lowercase_			:       Any									=			use_attention_mask
        lowercase_			:       int									=			use_token_type_ids
        lowercase_			:       Union[str, Any]									=			use_labels
        lowercase_			:       int									=			vocab_size
        lowercase_			:       Union[str, Any]									=			hidden_size
        lowercase_			:       Optional[int]									=			num_hidden_layers
        lowercase_			:       Dict									=			num_attention_heads
        lowercase_			:       Dict									=			intermediate_size
        lowercase_			:       str									=			hidden_act
        lowercase_			:       Union[str, Any]									=			hidden_dropout_prob
        lowercase_			:       Dict									=			attention_probs_dropout_prob
        lowercase_			:       Union[str, Any]									=			max_position_embeddings
        lowercase_			:       List[Any]									=			type_vocab_size
        lowercase_			:       str									=			type_sequence_label_size
        lowercase_			:       Optional[Any]									=			initializer_range
        lowercase_			:       Any									=			num_choices
   def     _UpperCAmelCase     (    self    )			->   List[Any]:


        '''simple docstring'''


        lowercase_			:       Optional[Any]									=			ids_tensor([self.batch_size, self.seq_length]  ,self.vocab_size    )

        lowercase_			:       List[Any]									=			None
        if self.use_attention_mask:
             lowercase_			:       Dict									=			random_attention_mask([self.batch_size, self.seq_length]    )

        lowercase_			:       Optional[Any]									=			DistilBertConfig(
            vocab_size=self.vocab_size  ,dim=self.hidden_size  ,n_layers=self.num_hidden_layers  ,n_heads=self.num_attention_heads  ,hidden_dim=self.intermediate_size  ,hidden_act=self.hidden_act  ,dropout=self.hidden_dropout_prob  ,attention_dropout=self.attention_probs_dropout_prob  ,max_position_embeddings=self.max_position_embeddings  ,initializer_range=self.initializer_range  ,tie_weights_=__UpperCamelCase  ,)

        return config, input_ids, attention_mask







   def     _UpperCAmelCase     (    self    )			->   Dict:


        '''simple docstring'''


        lowercase_			:       Any									=			self.prepare_config_and_inputs()
        lowercase_							,						lowercase_							,						lowercase_			:       str									=			config_and_inputs
        lowercase_			:       List[str]									=			{'input_ids': input_ids, 'attention_mask': attention_mask}
        return config, inputs_dict







@require_flax
class  UpperCamelCase	(    lowercase_		,   unittest.TestCase						):
   lowercase							=						(
       (
           FlaxDistilBertModel,
           FlaxDistilBertForMaskedLM,
           FlaxDistilBertForMultipleChoice,
           FlaxDistilBertForQuestionAnswering,
           FlaxDistilBertForSequenceClassification,
           FlaxDistilBertForTokenClassification,
           FlaxDistilBertForQuestionAnswering,
       )
       if is_flax_available()
       else ()
   )
   def     _UpperCAmelCase     (    self    )			->   List[Any]:


        '''simple docstring'''


        lowercase_			:       Optional[Any]									=			FlaxDistilBertModelTester(self    )







   @slow
   def     _UpperCAmelCase     (    self    )			->   List[str]:


        '''simple docstring'''


        for model_class_name in self.all_model_classes:
             lowercase_			:       Optional[int]									=			model_class_name.from_pretrained('distilbert-base-uncased'    )
             lowercase_			:       List[str]									=			model(np.ones((1, 1)    )    )
             self.assertIsNotNone(__UpperCamelCase    )







@require_flax
class  UpperCamelCase	(    unittest.TestCase						):
   @slow
   def     _UpperCAmelCase     (    self    )			->   str:


        '''simple docstring'''


        lowercase_			:       Optional[int]									=			FlaxDistilBertModel.from_pretrained('distilbert-base-uncased'    )
        lowercase_			:       Tuple									=			np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]    )
        lowercase_			:       Optional[int]									=			np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]    )
        lowercase_			:       Any									=			model(__UpperCamelCase  ,attention_mask=__UpperCamelCase    )[0]
        lowercase_			:       Tuple									=			(1, 11, 768)
        self.assertEqual(output.shape  ,__UpperCamelCase    )
        lowercase_			:       Union[str, Any]									=			np.array([[[-0.1639, 0.3299, 0.1648], [-0.1746, 0.3289, 0.1710], [-0.1884, 0.3357, 0.1810]]]    )

        self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]  ,__UpperCamelCase  ,atol=1e-4    )    )

