



from sklearn.metrics import recall_score

import datasets


UpperCamelCase__					    =    """\nRecall is the fraction of the positive examples that were correctly labeled by the model as positive. It can be computed with the equation:\nRecall = TP / (TP + FN)\nWhere TP is the true positives and FN is the false negatives.\n"""


UpperCamelCase__					    =    """\nArgs:\n- **predictions** (`list` of `int`): The predicted labels.\n- **references** (`list` of `int`): The ground truth labels.\n- **labels** (`list` of `int`): The set of labels to include when `average` is not set to `binary`, and their order when average is `None`. Labels present in the data can be excluded in this input, for example to calculate a multiclass average ignoring a majority negative class, while labels not present in the data will result in 0 components in a macro average. For multilabel targets, labels are column indices. By default, all labels in y_true and y_pred are used in sorted order. Defaults to None.\n- **pos_label** (`int`): The class label to use as the \'positive class\' when calculating the recall. Defaults to `1`.\n- **average** (`string`): This parameter is required for multiclass/multilabel targets. If None, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `\'binary\'`.\n    - `\'binary\'`: Only report results for the class specified by `pos_label`. This is applicable only if the target labels and predictions are binary.\n    - `\'micro\'`: Calculate metrics globally by counting the total true positives, false negatives, and false positives.\n    - `\'macro\'`: Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.\n    - `\'weighted\'`: Calculate metrics for each label, and find their average weighted by support (the number of true instances for each label). This alters `\'macro\'` to account for label imbalance. Note that it can result in an F-score that is not between precision and recall.\n    - `\'samples\'`: Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).\n- **sample_weight** (`list` of `float`): Sample weights Defaults to `None`.\n- **zero_division** (): Sets the value to return when there is a zero division. Defaults to .\n    - `\'warn\'`: If there is a zero division, the return value is `0`, but warnings are also raised.\n    - `0`: If there is a zero division, the return value is `0`.\n    - `1`: If there is a zero division, the return value is `1`.\n\nReturns:\n- **recall** (`float`, or `array` of `float`): Either the general recall score, or the recall scores for individual classes, depending on the values input to `labels` and `average`. Minimum possible value is 0. Maximum possible value is 1. A higher recall means that more of the positive examples have been labeled correctly. Therefore, a higher recall is generally considered better.\n\nExamples:\n\n    Example 1-A simple example with some errors\n        >>> recall_metric = datasets.load_metric(\'recall\')\n        >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1])\n        >>> print(results)\n        {\'recall\': 0.6666666666666666}\n\n    Example 2-The same example as Example 1, but with `pos_label=0` instead of the default `pos_label=1`.\n        >>> recall_metric = datasets.load_metric(\'recall\')\n        >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], pos_label=0)\n        >>> print(results)\n        {\'recall\': 0.5}\n\n    Example 3-The same example as Example 1, but with `sample_weight` included.\n        >>> recall_metric = datasets.load_metric(\'recall\')\n        >>> sample_weight = [0.9, 0.2, 0.9, 0.3, 0.8]\n        >>> results = recall_metric.compute(references=[0, 0, 1, 1, 1], predictions=[0, 1, 0, 1, 1], sample_weight=sample_weight)\n        >>> print(results)\n        {\'recall\': 0.55}\n\n    Example 4-A multiclass example, using different averages.\n        >>> recall_metric = datasets.load_metric(\'recall\')\n        >>> predictions = [0, 2, 1, 0, 0, 1]\n        >>> references = [0, 1, 2, 0, 1, 2]\n        >>> results = recall_metric.compute(predictions=predictions, references=references, average=\'macro\')\n        >>> print(results)\n        {\'recall\': 0.3333333333333333}\n        >>> results = recall_metric.compute(predictions=predictions, references=references, average=\'micro\')\n        >>> print(results)\n        {\'recall\': 0.3333333333333333}\n        >>> results = recall_metric.compute(predictions=predictions, references=references, average=\'weighted\')\n        >>> print(results)\n        {\'recall\': 0.3333333333333333}\n        >>> results = recall_metric.compute(predictions=predictions, references=references, average=None)\n        >>> print(results)\n        {\'recall\': array([1., 0., 0.])}\n"""


UpperCamelCase__					    =    """\n@article{scikit-learn, title={Scikit-learn: Machine Learning in {P}ython}, author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V. and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P. and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.}, journal={Journal of Machine Learning Research}, volume={12}, pages={2825--2830}, year={2011}\n"""
@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION				,       _KWARGS_DESCRIPTION   )
class     a__					(      datasets.Metric   ):






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											return datasets.MetricInfo(
											    description=_DESCRIPTION	,    citation=_CITATION	,    inputs_description=_KWARGS_DESCRIPTION	,    features=datasets.Features(
											        {
											            "predictions": datasets.Sequence(datasets.Value("int32"    )    ),
											            "references": datasets.Sequence(datasets.Value("int32"    )    ),
											        }
											        if self.config_name == "multilabel"
											        else {
											            "predictions": datasets.Value("int32"    ),
											            "references": datasets.Value("int32"    ),
											        }    )	,    reference_urls=["https://scikit-learn.org/stable/modules/generated/sklearn.metrics.recall_score.html"]	,    )






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A	,    _A=None	,    _A=1	,    _A="binary"	,    _A=None	,    _A="warn"	,    ):




											"""simple docstring"""



											__lowerCAmelCase            = recall_score(
											    _SCREAMING_SNAKE_CASE	,    _SCREAMING_SNAKE_CASE	,    labels=_SCREAMING_SNAKE_CASE	,    pos_label=_SCREAMING_SNAKE_CASE	,    average=_SCREAMING_SNAKE_CASE	,    sample_weight=_SCREAMING_SNAKE_CASE	,    zero_division=_SCREAMING_SNAKE_CASE	,    )
											return {"recall": float(_SCREAMING_SNAKE_CASE    ) if score.size == 1 else score}










from __future__ import annotations

import unittest

from transformers import AutoTokenizer, MBartConfig, is_tf_available
from transformers.testing_utils import require_sentencepiece, require_tf, require_tokenizers, slow
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers import TFAutoModelForSeqaSeqLM, TFMBartForConditionalGeneration, TFMBartModel
@require_tf
class     a__					:
						_a							:					List[str]      =					MBartConfig
						_a							:					Union[str, Any]      =					{}
						_a							:					Tuple      =					"""gelu"""






						def __init__(     self	,    _A	,    _A=1_3	,    _A=7	,    _A=True	,    _A=False	,    _A=9_9	,    _A=3_2	,    _A=2	,    _A=4	,    _A=3_7	,    _A=0.1	,    _A=0.1	,    _A=2_0	,    _A=2	,    _A=1	,    _A=0	,    ):




											"""simple docstring"""



											__lowerCAmelCase            = parent
											__lowerCAmelCase            = batch_size
											__lowerCAmelCase            = seq_length
											__lowerCAmelCase            = is_training
											__lowerCAmelCase            = use_labels
											__lowerCAmelCase            = vocab_size
											__lowerCAmelCase            = hidden_size
											__lowerCAmelCase            = num_hidden_layers
											__lowerCAmelCase            = num_attention_heads
											__lowerCAmelCase            = intermediate_size
											__lowerCAmelCase            = hidden_dropout_prob
											__lowerCAmelCase            = attention_probs_dropout_prob
											__lowerCAmelCase            = max_position_embeddings
											__lowerCAmelCase            = eos_token_id
											__lowerCAmelCase            = pad_token_id
											__lowerCAmelCase            = bos_token_id






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = ids_tensor([self.batch_size, self.seq_length - 1]	,    self.vocab_size    )
											__lowerCAmelCase            = tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size    )	,    1    )
											__lowerCAmelCase            = tf.concat([input_ids, eos_tensor]	,    axis=1    )

											__lowerCAmelCase            = ids_tensor([self.batch_size, self.seq_length]	,    self.vocab_size    )

											__lowerCAmelCase            = self.config_cls(
											    vocab_size=self.vocab_size	,    d_model=self.hidden_size	,    encoder_layers=self.num_hidden_layers	,    decoder_layers=self.num_hidden_layers	,    encoder_attention_heads=self.num_attention_heads	,    decoder_attention_heads=self.num_attention_heads	,    encoder_ffn_dim=self.intermediate_size	,    decoder_ffn_dim=self.intermediate_size	,    dropout=self.hidden_dropout_prob	,    attention_dropout=self.attention_probs_dropout_prob	,    max_position_embeddings=self.max_position_embeddings	,    eos_token_ids=[2]	,    bos_token_id=self.bos_token_id	,    pad_token_id=self.pad_token_id	,    decoder_start_token_id=self.pad_token_id	,    **self.config_updates	,    )
											__lowerCAmelCase            = prepare_mbart_inputs_dict(_A	,    _A	,    _A    )
											return config, inputs_dict






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A    ):




											"""simple docstring"""



											__lowerCAmelCase            = TFMBartModel(config=_A    ).get_decoder()
											__lowerCAmelCase            = inputs_dict["input_ids"]

											__lowerCAmelCase            = input_ids[:1, :]
											__lowerCAmelCase            = inputs_dict["attention_mask"][:1, :]
											__lowerCAmelCase            = inputs_dict["head_mask"]
											__lowerCAmelCase            = 1

											# first forward pass
											__lowerCAmelCase            = model(_A	,    attention_mask=_A	,    head_mask=_A	,    use_cache=_A    )

											__lowerCAmelCase     ,							__lowerCAmelCase            = outputs.to_tuple()
											__lowerCAmelCase            = past_key_values[1]







def 			_a   (		SCREAMING_SNAKE_CASE_							:       Optional[int]   ,   SCREAMING_SNAKE_CASE_							:       List[str]   ,   SCREAMING_SNAKE_CASE_							:       Tuple   ,   SCREAMING_SNAKE_CASE_							:       Any=None   ,   SCREAMING_SNAKE_CASE_							:       int=None   ,   SCREAMING_SNAKE_CASE_							:       List[Any]=None   ,   SCREAMING_SNAKE_CASE_							:       List[Any]=None   ,   SCREAMING_SNAKE_CASE_							:       Optional[Any]=None   ,   ):
					if attention_mask is None:
										__lowerCAmelCase            = tf.cast(tf.math.not_equal(SCREAMING_SNAKE_CASE_   ,   config.pad_token_id  )   ,   tf.inta  )
					if decoder_attention_mask is None:
										__lowerCAmelCase            = tf.concat(
										    [
										        tf.ones(decoder_input_ids[:, :1].shape   ,   dtype=tf.inta  ),
										        tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]   ,   config.pad_token_id  )   ,   tf.inta  ),
										    ]   ,   axis=-1   ,   )
					if head_mask is None:
										__lowerCAmelCase            = tf.ones((config.encoder_layers, config.encoder_attention_heads)  )
					if decoder_head_mask is None:
										__lowerCAmelCase            = tf.ones((config.decoder_layers, config.decoder_attention_heads)  )
					if cross_attn_head_mask is None:
										__lowerCAmelCase            = tf.ones((config.decoder_layers, config.decoder_attention_heads)  )
					return {
					    "input_ids": input_ids,
					    "decoder_input_ids": decoder_input_ids,
					    "attention_mask": attention_mask,
					    "decoder_attention_mask": decoder_attention_mask,
					    "head_mask": head_mask,
					    "decoder_head_mask": decoder_head_mask,
					    "cross_attn_head_mask": cross_attn_head_mask,
					}
@require_tf
class     a__					(      snake_case__				,       snake_case__				,       unittest.TestCase   ):
						_a							:					Optional[Any]      =					(TFMBartForConditionalGeneration, TFMBartModel) if is_tf_available() else ()
						_a							:					Any      =					(TFMBartForConditionalGeneration,) if is_tf_available() else ()
						_a							:					Tuple      =					(
						    {
						        """conversational""": TFMBartForConditionalGeneration,
						        """feature-extraction""": TFMBartModel,
						        """summarization""": TFMBartForConditionalGeneration,
						        """text2text-generation""": TFMBartForConditionalGeneration,
						        """translation""": TFMBartForConditionalGeneration,
						    }
						    if is_tf_available()
						    else {}
						)
						_a							:					Optional[int]      =					True
						_a							:					Optional[int]      =					False
						_a							:					List[Any]      =					False






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A	,    _A	,    _A	,    _A    ):




											"""simple docstring"""



											if pipeline_test_casse_name != "FeatureExtractionPipelineTests":
																# Exception encountered when calling layer '...'
																return True

											return False






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = TFMBartModelTester(self    )
											__lowerCAmelCase            = ConfigTester(self	,    config_class=_A    )






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											self.config_tester.run_common_tests()






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.model_tester.prepare_config_and_inputs_for_common()
											self.model_tester.check_decoder_model_past_large_inputs(*_A    )






@require_sentencepiece
@require_tokenizers
@require_tf
class     a__					(      unittest.TestCase   ):
						_a							:					Any      =					[
						    """ UN Chief Says There Is No Military Solution in Syria""",
						]
						_a							:					Optional[Any]      =					[
						    """Şeful ONU declară că nu există o soluţie militară în Siria""",
						]
						_a							:					Any      =					"""facebook/mbart-large-en-ro"""






						@cached_property
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											return AutoTokenizer.from_pretrained(self.model_name    )






						@cached_property
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											__lowerCAmelCase            = TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name    )
											return model






						def        __SCREAMING_SNAKE_CASE(     self	,    **_A    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.translate_src_text(**_A    )
											self.assertListEqual(self.expected_text	,    _A    )






						def        __SCREAMING_SNAKE_CASE(     self	,    **_A    ):




											"""simple docstring"""



											__lowerCAmelCase            = self.tokenizer(self.src_text	,    **_A	,    return_tensors="tf"    )
											__lowerCAmelCase            = self.model.generate(
											    model_inputs.input_ids	,    attention_mask=model_inputs.attention_mask	,    num_beams=2    )
											__lowerCAmelCase            = self.tokenizer.batch_decode(_A	,    skip_special_tokens=_A    )
											return generated_words






						@slow
						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											self._assert_generated_batch_equal_expected()




