

"""simple docstring"""


from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...file_utils import TensorType, is_torch_available
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import logging


__A            =      logging.get_logger(__name__)

__A            =      {
    "facebook/blenderbot_small-90M": "https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json",
    # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small
}





class    snake_case     (    __snake_case  ):
				SCREAMING_SNAKE_CASE_       :				int       				=  """blenderbot-small"""
				SCREAMING_SNAKE_CASE_       :				List[Any]       				=  ["""past_key_values"""]
				SCREAMING_SNAKE_CASE_       :				Union[str, Any]       				=  {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}

				def __init__(     self							:    Optional[int]					,  UpperCamelCase__							:    str=5_0_2_6_5					,  UpperCamelCase__							:    Optional[int]=5_1_2					,  UpperCamelCase__							:    Any=8					,  UpperCamelCase__							:    Any=2_0_4_8					,  UpperCamelCase__							:    Union[str, Any]=1_6					,  UpperCamelCase__							:    Tuple=8					,  UpperCamelCase__							:    List[str]=2_0_4_8					,  UpperCamelCase__							:    Tuple=1_6					,  UpperCamelCase__							:    int=0.0					,  UpperCamelCase__							:    Optional[Any]=0.0					,  UpperCamelCase__							:    Any=True					,  UpperCamelCase__							:    Dict=True					,  UpperCamelCase__							:    str="gelu"					,  UpperCamelCase__							:    Union[str, Any]=5_1_2					,  UpperCamelCase__							:    str=0.1					,  UpperCamelCase__							:    Optional[Any]=0.0					,  UpperCamelCase__							:    List[str]=0.0					,  UpperCamelCase__							:    int=0.02					,  UpperCamelCase__							:    Dict=1					,  UpperCamelCase__							:    Dict=False					,  UpperCamelCase__							:    Optional[Any]=0					,  UpperCamelCase__							:    int=1					,  UpperCamelCase__							:    Any=2					,  UpperCamelCase__							:    str=2					,  **UpperCamelCase__							:    Tuple					,  )->       Any:



						'''simple docstring'''


						__lowerCAmelCase:	List[str]   				=      vocab_size
						__lowerCAmelCase:	Optional[Any]   				=      max_position_embeddings
						__lowerCAmelCase:	str   				=      d_model
						__lowerCAmelCase:	Any   				=      encoder_ffn_dim
						__lowerCAmelCase:	Tuple   				=      encoder_layers
						__lowerCAmelCase:	Union[str, Any]   				=      encoder_attention_heads
						__lowerCAmelCase:	Any   				=      decoder_ffn_dim
						__lowerCAmelCase:	Any   				=      decoder_layers
						__lowerCAmelCase:	Union[str, Any]   				=      decoder_attention_heads
						__lowerCAmelCase:	List[str]   				=      dropout
						__lowerCAmelCase:	List[str]   				=      attention_dropout
						__lowerCAmelCase:	List[Any]   				=      activation_dropout
						__lowerCAmelCase:	int   				=      activation_function
						__lowerCAmelCase:	Optional[int]   				=      init_std
						__lowerCAmelCase:	Union[str, Any]   				=      encoder_layerdrop
						__lowerCAmelCase:	str   				=      decoder_layerdrop
						__lowerCAmelCase:	int   				=      use_cache
						__lowerCAmelCase:	Tuple   				=      encoder_layers
						__lowerCAmelCase:	Dict   				=      scale_embedding  # scale factor will be sqrt(d_model) if True

						super().__init__(
						    pad_token_id=UpperCamelCase__					,  bos_token_id=UpperCamelCase__					,  eos_token_id=UpperCamelCase__					,  is_encoder_decoder=UpperCamelCase__					,  decoder_start_token_id=UpperCamelCase__					,  forced_eos_token_id=UpperCamelCase__					,  **UpperCamelCase__					,  )





class    snake_case     (    __snake_case  ):

				@property
				def 							lowercase_					(     self							:    str)->       Mapping[str, Mapping[int, str]]:



						'''simple docstring'''


						if self.task in ["default", "seq2seq-lm"]:
								__lowerCAmelCase:	int   				=      OrderedDict(
								    [
								        ("input_ids", {0: "batch", 1: "encoder_sequence"}),
								        ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
								    ])

								if self.use_past:
										__lowerCAmelCase:	Any   				=      {0: "batch"}
										__lowerCAmelCase:	List[Any]   				=      {0: "batch", 1: "past_decoder_sequence + sequence"}
								else:
										__lowerCAmelCase:	int   				=      {0: "batch", 1: "decoder_sequence"}
										__lowerCAmelCase:	Union[str, Any]   				=      {0: "batch", 1: "decoder_sequence"}

								if self.use_past:
										self.fill_with_past_key_values_(UpperCamelCase__					,  direction="inputs")
						elif self.task == "causal-lm":
								# TODO: figure this case out.
								__lowerCAmelCase:	Any   				=      OrderedDict(
								    [
								        ("input_ids", {0: "batch", 1: "encoder_sequence"}),
								        ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
								    ])
								if self.use_past:
										__lowerCAmelCase:	str   				=      self.num_layers
										for i in range(UpperCamelCase__):
												__lowerCAmelCase:	List[str]   				=      {0: "batch", 2: "past_sequence + sequence"}
												__lowerCAmelCase:	Dict   				=      {0: "batch", 2: "past_sequence + sequence"}
						else:
								__lowerCAmelCase:	Dict   				=      OrderedDict(
								    [
								        ("input_ids", {0: "batch", 1: "encoder_sequence"}),
								        ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
								        ("decoder_input_ids", {0: "batch", 1: "decoder_sequence"}),
								        ("decoder_attention_mask", {0: "batch", 1: "decoder_sequence"}),
								    ])

						return common_inputs

				@property
				def 							lowercase_					(     self							:    List[str])->       Mapping[str, Mapping[int, str]]:



						'''simple docstring'''


						if self.task in ["default", "seq2seq-lm"]:
								__lowerCAmelCase:	int   				=      super().outputs
						else:
								__lowerCAmelCase:	Dict   				=      super(UpperCamelCase__					,  self).outputs
								if self.use_past:
										__lowerCAmelCase:	Tuple   				=      self.num_layers
										for i in range(UpperCamelCase__):
												__lowerCAmelCase:	Any   				=      {0: "batch", 2: "past_sequence + sequence"}
												__lowerCAmelCase:	Any   				=      {0: "batch", 2: "past_sequence + sequence"}
						return common_outputs

				def 							lowercase_					(     self							:    List[Any]					,  UpperCamelCase__							:    PreTrainedTokenizer					,  UpperCamelCase__							:    int = -1					,  UpperCamelCase__							:    int = -1					,  UpperCamelCase__							:    bool = False					,  UpperCamelCase__							:    Optional[TensorType] = None					,  )->       Mapping[str, Any]:



						'''simple docstring'''


						__lowerCAmelCase:	Optional[Any]   				=      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
						    UpperCamelCase__					,  UpperCamelCase__					,  UpperCamelCase__					,  UpperCamelCase__					,  UpperCamelCase__)

						# Generate decoder inputs
						__lowerCAmelCase:	int   				=      seq_length if not self.use_past else 1
						__lowerCAmelCase:	Any   				=      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
						    UpperCamelCase__					,  UpperCamelCase__					,  UpperCamelCase__					,  UpperCamelCase__					,  UpperCamelCase__)
						__lowerCAmelCase:	Dict   				=      {f"decoder_{name}": tensor for name, tensor in decoder_inputs.items()}
						__lowerCAmelCase:	Tuple   				=      dict(**UpperCamelCase__					,  **UpperCamelCase__)

						if self.use_past:
								if not is_torch_available():
										raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed.")
								else:
										import torch
								__lowerCAmelCase:	str   				=      common_inputs["input_ids"].shape
								__lowerCAmelCase:	Any   				=      common_inputs["decoder_input_ids"].shape[1]
								__lowerCAmelCase:	List[str]   				=      self.num_attention_heads
								__lowerCAmelCase:	int   				=      (
								    batch,
								    num_encoder_attention_heads,
								    encoder_seq_length,
								    self._config.hidden_size // num_encoder_attention_heads,
								)
								__lowerCAmelCase:	List[str]   				=      decoder_seq_length + 3
								__lowerCAmelCase:	Tuple   				=      (
								    batch,
								    num_decoder_attention_heads,
								    decoder_past_length,
								    self._config.hidden_size // num_decoder_attention_heads,
								)

								__lowerCAmelCase:	Tuple   				=      torch.cat(
								    [common_inputs["decoder_attention_mask"], torch.ones(UpperCamelCase__					,  UpperCamelCase__)]					,  dim=1)

								__lowerCAmelCase:	List[Any]   				=      []
								# If the number of encoder and decoder layers are present in the model configuration, both are considered
								__lowerCAmelCase:	int   				=      self.num_layers
								__lowerCAmelCase:	List[str]   				=      min(UpperCamelCase__					,  UpperCamelCase__)
								__lowerCAmelCase:	Optional[int]   				=      max(UpperCamelCase__					,  UpperCamelCase__) - min_num_layers
								__lowerCAmelCase:	Dict   				=      "encoder" if num_encoder_layers > num_decoder_layers else "decoder"

								for _ in range(UpperCamelCase__):
										common_inputs["past_key_values"].append(
										    (
										        torch.zeros(UpperCamelCase__),
										        torch.zeros(UpperCamelCase__),
										        torch.zeros(UpperCamelCase__),
										        torch.zeros(UpperCamelCase__),
										    ))
								# TODO: test this.
								__lowerCAmelCase:	Dict   				=      encoder_shape if remaining_side_name == "encoder" else decoder_shape
								for _ in range(UpperCamelCase__					,  UpperCamelCase__):
										common_inputs["past_key_values"].append((torch.zeros(UpperCamelCase__), torch.zeros(UpperCamelCase__)))
						return common_inputs

				def 							lowercase_					(     self							:    Union[str, Any]					,  UpperCamelCase__							:    PreTrainedTokenizer					,  UpperCamelCase__							:    int = -1					,  UpperCamelCase__							:    int = -1					,  UpperCamelCase__							:    bool = False					,  UpperCamelCase__							:    Optional[TensorType] = None					,  )->       Mapping[str, Any]:



						'''simple docstring'''


						__lowerCAmelCase:	str   				=      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
						    UpperCamelCase__					,  UpperCamelCase__					,  UpperCamelCase__					,  UpperCamelCase__					,  UpperCamelCase__)

						if self.use_past:
								if not is_torch_available():
										raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed.")
								else:
										import torch
								__lowerCAmelCase:	Tuple   				=      common_inputs["input_ids"].shape
								# Not using the same length for past_key_values
								__lowerCAmelCase:	Optional[int]   				=      seqlen + 2
								__lowerCAmelCase:	Optional[Any]   				=      self.num_layers
								__lowerCAmelCase:	Dict   				=      self.num_attention_heads
								__lowerCAmelCase:	Tuple   				=      (
								    batch,
								    num_encoder_attention_heads,
								    past_key_values_length,
								    self._config.hidden_size // num_encoder_attention_heads,
								)

								__lowerCAmelCase:	Tuple   				=      common_inputs["attention_mask"].dtype
								__lowerCAmelCase:	Optional[Any]   				=      torch.cat(
								    [common_inputs["attention_mask"], torch.ones(UpperCamelCase__					,  UpperCamelCase__					,  dtype=UpperCamelCase__)]					,  dim=1)
								__lowerCAmelCase:	List[Any]   				=      [
								    (torch.zeros(UpperCamelCase__), torch.zeros(UpperCamelCase__)) for _ in range(UpperCamelCase__)
								]
						return common_inputs

				def 							lowercase_					(     self							:    Tuple					,  UpperCamelCase__							:    PreTrainedTokenizer					,  UpperCamelCase__							:    int = -1					,  UpperCamelCase__							:    int = -1					,  UpperCamelCase__							:    bool = False					,  UpperCamelCase__							:    Optional[TensorType] = None					,  )->       Mapping[str, Any]:



						'''simple docstring'''


						__lowerCAmelCase:	Any   				=      compute_effective_axis_dimension(
						    UpperCamelCase__					,  fixed_dimension=OnnxConfig.default_fixed_batch					,  num_token_to_add=0)

						# If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
						__lowerCAmelCase:	str   				=      tokenizer.num_special_tokens_to_add(UpperCamelCase__)
						__lowerCAmelCase:	Optional[int]   				=      compute_effective_axis_dimension(
						    UpperCamelCase__					,  fixed_dimension=OnnxConfig.default_fixed_sequence					,  num_token_to_add=UpperCamelCase__)

						# Generate dummy inputs according to compute batch and sequence
						__lowerCAmelCase:	List[Any]   				=      [" ".join([tokenizer.unk_token]) * seq_length] * batch_size
						__lowerCAmelCase:	List[Any]   				=      dict(tokenizer(UpperCamelCase__					,  return_tensors=UpperCamelCase__))
						return common_inputs

				def 							lowercase_					(     self							:    Optional[int]					,  UpperCamelCase__							:    PreTrainedTokenizer					,  UpperCamelCase__							:    int = -1					,  UpperCamelCase__							:    int = -1					,  UpperCamelCase__							:    bool = False					,  UpperCamelCase__							:    Optional[TensorType] = None					,  )->       Mapping[str, Any]:



						'''simple docstring'''


						if self.task in ["default", "seq2seq-lm"]:
								__lowerCAmelCase:	List[str]   				=      self._generate_dummy_inputs_for_default_and_seqaseq_lm(
								    UpperCamelCase__					,  batch_size=UpperCamelCase__					,  seq_length=UpperCamelCase__					,  is_pair=UpperCamelCase__					,  framework=UpperCamelCase__)

						elif self.task == "causal-lm":
								__lowerCAmelCase:	Optional[int]   				=      self._generate_dummy_inputs_for_causal_lm(
								    UpperCamelCase__					,  batch_size=UpperCamelCase__					,  seq_length=UpperCamelCase__					,  is_pair=UpperCamelCase__					,  framework=UpperCamelCase__)
						else:
								__lowerCAmelCase:	Optional[Any]   				=      self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
								    UpperCamelCase__					,  batch_size=UpperCamelCase__					,  seq_length=UpperCamelCase__					,  is_pair=UpperCamelCase__					,  framework=UpperCamelCase__)

						return common_inputs




				def 							lowercase_					(     self							:    str					,  UpperCamelCase__							:    str					,  UpperCamelCase__							:    Any					,  UpperCamelCase__							:    Optional[int]					,  UpperCamelCase__							:    Optional[int])->       List[Any]:



						'''simple docstring'''


						if self.task in ["default", "seq2seq-lm"]:
								__lowerCAmelCase:	Optional[int]   				=      super()._flatten_past_key_values_(UpperCamelCase__					,  UpperCamelCase__					,  UpperCamelCase__					,  UpperCamelCase__)
						else:
								__lowerCAmelCase:	Optional[Any]   				=      super(UpperCamelCase__					,  self)._flatten_past_key_values_(
								    UpperCamelCase__					,  UpperCamelCase__					,  UpperCamelCase__					,  UpperCamelCase__)





"""simple docstring"""


import os

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

import datasets


__A						       =      datasets.logging.get_logger(__name__)


__A						       =      "\\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"

__A						       =      "\\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"

__A						       =      "\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"

__A						       =      {
    "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    snake_case     (    datasets.Metric  ):

				def 							lowercase_					(     self							:    Optional[int])->       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 							lowercase_					(     self							:    int					,  UpperCamelCase__							:    Dict)->       List[str]:



						'''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').")
								__lowerCAmelCase:	List[str]   				=      "bleurt-base-128"

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

						elif self.config_name.upper() in CHECKPOINT_URLS:
								__lowerCAmelCase:	Tuple   				=      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
						__lowerCAmelCase:	Union[str, Any]   				=      dl_manager.download_and_extract(CHECKPOINT_URLS[checkpoint_name])
						__lowerCAmelCase:	Dict   				=      score.BleurtScorer(os.path.join(UpperCamelCase__					,  UpperCamelCase__))




				def 							lowercase_					(     self							:    Optional[int]					,  UpperCamelCase__							:    Tuple					,  UpperCamelCase__							:    int)->       str:



						'''simple docstring'''


						__lowerCAmelCase:	str   				=      self.scorer.score(references=UpperCamelCase__					,  candidates=UpperCamelCase__)
						return {"scores": scores}


