
import math
import unittest

from transformers import BioGptConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers import (
          BioGptForCausalLM,
          BioGptForSequenceClassification,
          BioGptForTokenClassification,
          BioGptModel,
          BioGptTokenizer,
      )
      from transformers.models.biogpt.modeling_biogpt import BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST

class 						lowercase_				:

 def __init__(					self   ,		__UpperCamelCase   ,		__UpperCamelCase=1_3   ,		__UpperCamelCase=7   ,		__UpperCamelCase=True   ,		__UpperCamelCase=True   ,		__UpperCamelCase=False   ,		__UpperCamelCase=True   ,		__UpperCamelCase=9_9   ,		__UpperCamelCase=3_2   ,		__UpperCamelCase=5   ,		__UpperCamelCase=4   ,		__UpperCamelCase=3_7   ,		__UpperCamelCase="gelu"   ,		__UpperCamelCase=0.1   ,		__UpperCamelCase=0.1   ,		__UpperCamelCase=5_1_2   ,		__UpperCamelCase=1_6   ,		__UpperCamelCase=2   ,		__UpperCamelCase=0.02   ,		__UpperCamelCase=3   ,		__UpperCamelCase=4   ,		__UpperCamelCase=None   ,		):




        """simple docstring"""


        UpperCamelCase_											=				parent
        UpperCamelCase_											=				batch_size
        UpperCamelCase_											=				seq_length
        UpperCamelCase_											=				is_training
        UpperCamelCase_											=				use_input_mask
        UpperCamelCase_											=				use_token_type_ids
        UpperCamelCase_											=				use_labels
        UpperCamelCase_											=				vocab_size
        UpperCamelCase_											=				hidden_size
        UpperCamelCase_											=				num_hidden_layers
        UpperCamelCase_											=				num_attention_heads
        UpperCamelCase_											=				intermediate_size
        UpperCamelCase_											=				hidden_act
        UpperCamelCase_											=				hidden_dropout_prob
        UpperCamelCase_											=				attention_probs_dropout_prob
        UpperCamelCase_											=				max_position_embeddings
        UpperCamelCase_											=				type_vocab_size
        UpperCamelCase_											=				type_sequence_label_size
        UpperCamelCase_											=				initializer_range
        UpperCamelCase_											=				num_labels
        UpperCamelCase_											=				num_choices
        UpperCamelCase_											=				scope

 def lowerCamelCase_ (					self   ):




        """simple docstring"""


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

        UpperCamelCase_											=				None
        if self.use_input_mask:
               UpperCamelCase_											=				random_attention_mask([self.batch_size, self.seq_length]   )

        UpperCamelCase_											=				None
        if self.use_token_type_ids:
               UpperCamelCase_											=				ids_tensor([self.batch_size, self.seq_length]   ,		self.type_vocab_size   )

        UpperCamelCase_											=				None
        UpperCamelCase_											=				None
        UpperCamelCase_											=				None
        if self.use_labels:
               UpperCamelCase_											=				ids_tensor([self.batch_size]   ,		self.type_sequence_label_size   )
               UpperCamelCase_											=				ids_tensor([self.batch_size, self.seq_length]   ,		self.num_labels   )
               UpperCamelCase_											=				ids_tensor([self.batch_size]   ,		self.num_choices   )

        UpperCamelCase_											=				self.get_config()

        return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels

 def lowerCamelCase_ (					self   ):




        """simple docstring"""


        return BioGptConfig(
            vocab_size=self.vocab_size   ,		hidden_size=self.hidden_size   ,		num_hidden_layers=self.num_hidden_layers   ,		num_attention_heads=self.num_attention_heads   ,		intermediate_size=self.intermediate_size   ,		hidden_act=self.hidden_act   ,		hidden_dropout_prob=self.hidden_dropout_prob   ,		attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,		max_position_embeddings=self.max_position_embeddings   ,		type_vocab_size=self.type_vocab_size   ,		is_decoder=__lowerCAmelCase   ,		initializer_range=self.initializer_range   ,		)

 def lowerCamelCase_ (					self   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ):




        """simple docstring"""


        UpperCamelCase_											=				BioGptModel(config=__lowerCAmelCase   )
        model.to(__lowerCAmelCase   )
        model.eval()
        UpperCamelCase_											=				model(__lowerCAmelCase   ,		attention_mask=__lowerCAmelCase   )
        UpperCamelCase_											=				model(__lowerCAmelCase   )
        self.parent.assertEqual(result.last_hidden_state.shape   ,		(self.batch_size, self.seq_length, self.hidden_size)   )

 def lowerCamelCase_ (					self   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		):




        """simple docstring"""


        UpperCamelCase_											=				BioGptForCausalLM(config=__lowerCAmelCase   )
        model.to(__lowerCAmelCase   )
        model.eval()
        UpperCamelCase_											=				model(__lowerCAmelCase   ,		attention_mask=__lowerCAmelCase   ,		token_type_ids=__lowerCAmelCase   ,		labels=__lowerCAmelCase   )
        self.parent.assertEqual(result.logits.shape   ,		(self.batch_size, self.seq_length, self.vocab_size)   )

 def lowerCamelCase_ (					self   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		*__UpperCamelCase   ):




        """simple docstring"""


        UpperCamelCase_											=				BioGptModel(config=__lowerCAmelCase   )
        model.to(__lowerCAmelCase   )
        model.eval()

        # create attention mask
        UpperCamelCase_											=				torch.ones(input_ids.shape   ,		dtype=torch.long   ,		device=__lowerCAmelCase   )
        UpperCamelCase_											=				self.seq_length // 2
        UpperCamelCase_											=				0

        # first forward pass
        UpperCamelCase_				, UpperCamelCase_											=				model(__lowerCAmelCase   ,		attention_mask=__lowerCAmelCase   ).to_tuple()

        # create hypothetical next token and extent to next_input_ids
        UpperCamelCase_											=				ids_tensor((self.batch_size, 1)   ,		config.vocab_size   )

        # change a random masked slice from input_ids
        UpperCamelCase_											=				ids_tensor((1,)   ,		__lowerCAmelCase   ).item() + 1
        UpperCamelCase_											=				ids_tensor((self.batch_size, 1)   ,		config.vocab_size   ).squeeze(-1   )
        UpperCamelCase_											=				random_other_next_tokens

        # append to next input_ids and attn_mask
        UpperCamelCase_											=				torch.cat([input_ids, next_tokens]   ,		dim=-1   )
        UpperCamelCase_											=				torch.cat(
            [attn_mask, torch.ones((attn_mask.shape[0], 1)   ,		dtype=torch.long   ,		device=__lowerCAmelCase   )]   ,		dim=1   ,		)

        # get two different outputs
        UpperCamelCase_											=				model(__lowerCAmelCase   ,		attention_mask=__lowerCAmelCase   )["""last_hidden_state"""]
        UpperCamelCase_											=				model(__lowerCAmelCase   ,		past_key_values=__lowerCAmelCase   ,		attention_mask=__lowerCAmelCase   )["""last_hidden_state"""]

        # select random slice
        UpperCamelCase_											=				ids_tensor((1,)   ,		output_from_past.shape[-1]   ).item()
        UpperCamelCase_											=				output_from_no_past[:, -1, random_slice_idx].detach()
        UpperCamelCase_											=				output_from_past[:, 0, random_slice_idx].detach()

        # test that outputs are equal for slice
        self.parent.assertTrue(torch.allclose(__lowerCAmelCase   ,		__lowerCAmelCase   ,		atol=1e-3   )   )

 def lowerCamelCase_ (					self   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		*__UpperCamelCase   ):




        """simple docstring"""


        UpperCamelCase_											=				BioGptModel(config=__lowerCAmelCase   ).to(__lowerCAmelCase   ).eval()

        UpperCamelCase_											=				torch.ones(input_ids.shape   ,		dtype=torch.long   ,		device=__lowerCAmelCase   )

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

        UpperCamelCase_				, UpperCamelCase_											=				outputs.to_tuple()

        # create hypothetical multiple next token and extent to next_input_ids
        UpperCamelCase_											=				ids_tensor((self.batch_size, 3)   ,		config.vocab_size   )
        UpperCamelCase_											=				ids_tensor((self.batch_size, 3)   ,		2   )

        # append to next input_ids and
        UpperCamelCase_											=				torch.cat([input_ids, next_tokens]   ,		dim=-1   )
        UpperCamelCase_											=				torch.cat([attention_mask, next_attn_mask]   ,		dim=-1   )

        UpperCamelCase_											=				model(__lowerCAmelCase   ,		attention_mask=__lowerCAmelCase   )["""last_hidden_state"""]
        UpperCamelCase_											=				model(__lowerCAmelCase   ,		attention_mask=__lowerCAmelCase   ,		past_key_values=__lowerCAmelCase   )[
            """last_hidden_state"""
        ]

        # select random slice
        UpperCamelCase_											=				ids_tensor((1,)   ,		output_from_past.shape[-1]   ).item()
        UpperCamelCase_											=				output_from_no_past[:, -3:, random_slice_idx].detach()
        UpperCamelCase_											=				output_from_past[:, :, random_slice_idx].detach()

        self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]   )

        # test that outputs are equal for slice
        self.parent.assertTrue(torch.allclose(__lowerCAmelCase   ,		__lowerCAmelCase   ,		atol=1e-3   )   )

 def lowerCamelCase_ (					self   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		*__UpperCamelCase   ,		__UpperCamelCase=False   ):




        """simple docstring"""


        UpperCamelCase_											=				BioGptForCausalLM(__lowerCAmelCase   )
        model.to(__lowerCAmelCase   )
        if gradient_checkpointing:
               model.gradient_checkpointing_enable()

        UpperCamelCase_											=				model(__lowerCAmelCase   ,		labels=__lowerCAmelCase   )
        self.parent.assertEqual(result.loss.shape   ,		()   )
        self.parent.assertEqual(result.logits.shape   ,		(self.batch_size, self.seq_length, self.vocab_size)   )
        result.loss.backward()

 def lowerCamelCase_ (					self   ,		__UpperCamelCase   ,		*__UpperCamelCase   ):




        """simple docstring"""


        UpperCamelCase_											=				BioGptModel(__lowerCAmelCase   )
        UpperCamelCase_											=				model.config.initializer_range / math.sqrt(2 * model.config.num_hidden_layers   )
        for key in model.state_dict().keys():
               if "c_proj" in key and "weight" in key:
                      self.parent.assertLessEqual(abs(torch.std(model.state_dict()[key]   ) - model_std   )   ,		0.001   )
                      self.parent.assertLessEqual(abs(torch.mean(model.state_dict()[key]   ) - 0.0   )   ,		0.01   )

 def lowerCamelCase_ (					self   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		__UpperCamelCase   ,		*__UpperCamelCase   ):




        """simple docstring"""


        UpperCamelCase_											=				self.num_labels
        UpperCamelCase_											=				BioGptForTokenClassification(__lowerCAmelCase   )
        model.to(__lowerCAmelCase   )
        model.eval()
        UpperCamelCase_											=				model(__lowerCAmelCase   ,		attention_mask=__lowerCAmelCase   ,		token_type_ids=__lowerCAmelCase   )
        self.parent.assertEqual(result.logits.shape   ,		(self.batch_size, self.seq_length, self.num_labels)   )

 def lowerCamelCase_ (					self   ):




        """simple docstring"""


        UpperCamelCase_											=				self.prepare_config_and_inputs()
        (
            (
            UpperCamelCase_			
        )				, (
            UpperCamelCase_			
        )				, (
            UpperCamelCase_			
        )				, (
            UpperCamelCase_			
        )				, (
            UpperCamelCase_			
        )				, (
            UpperCamelCase_			
        )				, (
            UpperCamelCase_			
        )				, 			
        )											=				config_and_inputs
        UpperCamelCase_											=				{"""input_ids""": input_ids, """attention_mask""": input_mask}
        return config, inputs_dict



@require_torch
class 						lowercase_				(				__SCREAMING_SNAKE_CASE							,		__SCREAMING_SNAKE_CASE							,		__SCREAMING_SNAKE_CASE							,		unittest.TestCase ):
 A__  : Optional[int]      					=			(
     (BioGptModel, BioGptForCausalLM, BioGptForSequenceClassification, BioGptForTokenClassification)
     if is_torch_available()
     else ()
 )
 A__  : Dict      					=			(BioGptForCausalLM,) if is_torch_available() else ()
 A__  : List[str]      					=			(
     {
         '''feature-extraction''': BioGptModel,
         '''text-classification''': BioGptForSequenceClassification,
         '''text-generation''': BioGptForCausalLM,
         '''token-classification''': BioGptForTokenClassification,
         '''zero-shot''': BioGptForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 A__  : List[Any]      					=			False

 def lowerCamelCase_ (					self   ):




        """simple docstring"""


        UpperCamelCase_											=				BioGptModelTester(self   )
        UpperCamelCase_											=				ConfigTester(self   ,		config_class=__lowerCAmelCase   ,		hidden_size=3_7   )

 def lowerCamelCase_ (					self   ):




        """simple docstring"""


        self.config_tester.run_common_tests()

 def lowerCamelCase_ (					self   ):




        """simple docstring"""


        UpperCamelCase_											=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__lowerCAmelCase   )

 def lowerCamelCase_ (					self   ):




        """simple docstring"""


        UpperCamelCase_											=				self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
               UpperCamelCase_											=				type
               self.model_tester.create_and_check_model(*__lowerCAmelCase   )

 def lowerCamelCase_ (					self   ):




        """simple docstring"""


        UpperCamelCase_											=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_biogpt_model_attention_mask_past(*__lowerCAmelCase   )

 def lowerCamelCase_ (					self   ):




        """simple docstring"""


        UpperCamelCase_											=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_forward_and_backwards(*__lowerCAmelCase   ,		gradient_checkpointing=__lowerCAmelCase   )

 def lowerCamelCase_ (					self   ):




        """simple docstring"""


        UpperCamelCase_											=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_biogpt_model_past_large_inputs(*__lowerCAmelCase   )

 def lowerCamelCase_ (					self   ):




        """simple docstring"""


        UpperCamelCase_											=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_biogpt_weight_initialization(*__lowerCAmelCase   )

 def lowerCamelCase_ (					self   ):




        """simple docstring"""


        UpperCamelCase_											=				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_biogpt_for_token_classification(*__lowerCAmelCase   )

 @slow
 def lowerCamelCase_ (					self   ):




        """simple docstring"""


        UpperCamelCase_											=				BioGptForCausalLM.from_pretrained("""microsoft/biogpt"""   )
        model.to(__lowerCAmelCase   )
        UpperCamelCase_											=				BioGptTokenizer.from_pretrained("""microsoft/biogpt"""   )

        UpperCamelCase_											=				"""left"""

        # Define PAD Token = EOS Token = 50256
        UpperCamelCase_											=				tokenizer.eos_token
        UpperCamelCase_											=				model.config.eos_token_id

        # use different length sentences to test batching
        UpperCamelCase_											=				[
            """Hello, my dog is a little""",
            """Today, I""",
        ]

        UpperCamelCase_											=				tokenizer(__lowerCAmelCase   ,		return_tensors="""pt"""   ,		padding=__lowerCAmelCase   )
        UpperCamelCase_											=				inputs["""input_ids"""].to(__lowerCAmelCase   )

        UpperCamelCase_											=				model.generate(
            input_ids=__lowerCAmelCase   ,		attention_mask=inputs["""attention_mask"""].to(__lowerCAmelCase   )   ,		)

        UpperCamelCase_											=				tokenizer(sentences[0]   ,		return_tensors="""pt"""   ).input_ids.to(__lowerCAmelCase   )
        UpperCamelCase_											=				model.generate(input_ids=__lowerCAmelCase   )

        UpperCamelCase_											=				inputs_non_padded.shape[-1] - inputs["""attention_mask"""][-1].long().sum().cpu().item()
        UpperCamelCase_											=				tokenizer(sentences[1]   ,		return_tensors="""pt"""   ).input_ids.to(__lowerCAmelCase   )
        UpperCamelCase_											=				model.generate(input_ids=__lowerCAmelCase   ,		max_length=model.config.max_length - num_paddings   )

        UpperCamelCase_											=				tokenizer.batch_decode(__lowerCAmelCase   ,		skip_special_tokens=__lowerCAmelCase   )
        UpperCamelCase_											=				tokenizer.decode(output_non_padded[0]   ,		skip_special_tokens=__lowerCAmelCase   )
        UpperCamelCase_											=				tokenizer.decode(output_padded[0]   ,		skip_special_tokens=__lowerCAmelCase   )

        UpperCamelCase_											=				[
            """Hello, my dog is a little bit bigger than a little bit.""",
            """Today, I have a good idea of how to use the information""",
        ]
        self.assertListEqual(__lowerCAmelCase   ,		__lowerCAmelCase   )
        self.assertListEqual(__lowerCAmelCase   ,		[non_padded_sentence, padded_sentence]   )

 @slow
 def lowerCamelCase_ (					self   ):




        """simple docstring"""


        for model_name in BIOGPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               UpperCamelCase_											=				BioGptModel.from_pretrained(__lowerCAmelCase   )
               self.assertIsNotNone(__lowerCAmelCase   )

 def lowerCamelCase_ (					self   ):




        """simple docstring"""


        UpperCamelCase_				, UpperCamelCase_											=				self.model_tester.prepare_config_and_inputs_for_common()
        UpperCamelCase_											=				3
        UpperCamelCase_											=				input_dict["""input_ids"""]
        UpperCamelCase_											=				input_ids.ne(1   ).to(__lowerCAmelCase   )
        UpperCamelCase_											=				ids_tensor([self.model_tester.batch_size]   ,		self.model_tester.type_sequence_label_size   )
        UpperCamelCase_											=				BioGptForSequenceClassification(__lowerCAmelCase   )
        model.to(__lowerCAmelCase   )
        model.eval()
        UpperCamelCase_											=				model(__lowerCAmelCase   ,		attention_mask=__lowerCAmelCase   ,		labels=__lowerCAmelCase   )
        self.assertEqual(result.logits.shape   ,		(self.model_tester.batch_size, self.model_tester.num_labels)   )

 def lowerCamelCase_ (					self   ):




        """simple docstring"""


        UpperCamelCase_				, UpperCamelCase_											=				self.model_tester.prepare_config_and_inputs_for_common()
        UpperCamelCase_											=				3
        UpperCamelCase_											=				"""multi_label_classification"""
        UpperCamelCase_											=				input_dict["""input_ids"""]
        UpperCamelCase_											=				input_ids.ne(1   ).to(__lowerCAmelCase   )
        UpperCamelCase_											=				ids_tensor(
            [self.model_tester.batch_size, config.num_labels]   ,		self.model_tester.type_sequence_label_size   ).to(torch.float   )
        UpperCamelCase_											=				BioGptForSequenceClassification(__lowerCAmelCase   )
        model.to(__lowerCAmelCase   )
        model.eval()
        UpperCamelCase_											=				model(__lowerCAmelCase   ,		attention_mask=__lowerCAmelCase   ,		labels=__lowerCAmelCase   )
        self.assertEqual(result.logits.shape   ,		(self.model_tester.batch_size, self.model_tester.num_labels)   )



@require_torch
class 						lowercase_				(				unittest.TestCase ):

 @slow
 def lowerCamelCase_ (					self   ):




        """simple docstring"""


        UpperCamelCase_											=				BioGptForCausalLM.from_pretrained("""microsoft/biogpt"""   )
        UpperCamelCase_											=				torch.tensor([[2, 4_8_0_5, 9, 6_5_6, 2_1]]   )
        UpperCamelCase_											=				model(__lowerCAmelCase   )[0]

        UpperCamelCase_											=				4_2_3_8_4

        UpperCamelCase_											=				torch.Size((1, 5, vocab_size)   )
        self.assertEqual(output.shape   ,		__lowerCAmelCase   )

        UpperCamelCase_											=				torch.tensor(
            [[[-9.5_236, -9.8_918, 1_0.4_5_5_7], [-1_1.0_4_6_9, -9.6_423, 8.1_022], [-8.8_664, -7.8_826, 5.5_325]]]   )

        self.assertTrue(torch.allclose(output[:, :3, :3]   ,		__lowerCAmelCase   ,		atol=1e-4   )   )

 @slow
 def lowerCamelCase_ (					self   ):




        """simple docstring"""


        UpperCamelCase_											=				BioGptTokenizer.from_pretrained("""microsoft/biogpt"""   )
        UpperCamelCase_											=				BioGptForCausalLM.from_pretrained("""microsoft/biogpt"""   )
        model.to(__lowerCAmelCase   )

        torch.manual_seed(0   )
        UpperCamelCase_											=				tokenizer("""COVID-19 is"""   ,		return_tensors="""pt"""   ).to(__lowerCAmelCase   )
        UpperCamelCase_											=				model.generate(
            **__lowerCAmelCase   ,		min_length=1_0_0   ,		max_length=1_0_2_4   ,		num_beams=5   ,		early_stopping=__lowerCAmelCase   ,		)
        UpperCamelCase_											=				tokenizer.decode(output_ids[0]   ,		skip_special_tokens=__lowerCAmelCase   )

        UpperCamelCase_											=				(
            """COVID-19 is a global pandemic caused by severe acute respiratory syndrome coronavirus 2 (SARS-CoV-2), the"""
            """ causative agent of coronavirus disease 2019 (COVID-19), which has spread to more than 200 countries and"""
            """ territories, including the United States (US), Canada, Australia, New Zealand, the United Kingdom (UK),"""
            """ and the United States of America (USA), as of March 11, 2020, with more than 800,000 confirmed cases and"""
            """ more than 800,000 deaths."""
        )
        self.assertEqual(__lowerCAmelCase   ,		__lowerCAmelCase   )










from sklearn.metrics import fa_score

import datasets


A    :					Any =      '''
The F1 score is the harmonic mean of the precision and recall. It can be computed with the equation:
F1 = 2 * (precision * recall) / (precision + recall)
'''


A    :					List[Any] =      '''
Args:
    predictions (`list` of `int`): Predicted labels.
    references (`list` of `int`): Ground truth labels.
    labels (`list` of `int`): The set of labels to include when `average` is not set to `\'binary\'`, and the order of the labels if `average` is `None`. Labels present in the data can be excluded, for example to calculate a multiclass average ignoring a majority negative class. 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 `predictions` and `references` are used in sorted order. Defaults to None.
    pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1.
    average (`string`): This parameter is required for multiclass/multilabel targets. If set to `None`, the scores for each class are returned. Otherwise, this determines the type of averaging performed on the data. Defaults to `\'binary\'`.

        - \'binary\': Only report results for the class specified by `pos_label`. This is applicable only if the classes found in `predictions` and `references` are binary.
        - \'micro\': Calculate metrics globally by counting the total true positives, false negatives and false positives.
        - \'macro\': Calculate metrics for each label, and find their unweighted mean. This does not take label imbalance into account.
        - \'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. This option can result in an F-score that is not between precision and recall.
        - \'samples\': Calculate metrics for each instance, and find their average (only meaningful for multilabel classification).
    sample_weight (`list` of `float`): Sample weights Defaults to None.

Returns:
    f1 (`float` or `array` of `float`): F1 score or list of f1 scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher f1 scores are better.

Examples:

    Example 1-A simple binary example
        >>> f1_metric = datasets.load_metric("f1")
        >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0])
        >>> print(results)
        {\'f1\': 0.5}

    Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`.
        >>> f1_metric = datasets.load_metric("f1")
        >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0)
        >>> print(round(results[\'f1\'], 2))
        0.67

    Example 3-The same simple binary example as in Example 1, but with `sample_weight` included.
        >>> f1_metric = datasets.load_metric("f1")
        >>> results = f1_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], sample_weight=[0.9, 0.5, 3.9, 1.2, 0.3])
        >>> print(round(results[\'f1\'], 2))
        0.35

    Example 4-A multiclass example, with different values for the `average` input.
        >>> predictions = [0, 2, 1, 0, 0, 1]
        >>> references = [0, 1, 2, 0, 1, 2]
        >>> results = f1_metric.compute(predictions=predictions, references=references, average="macro")
        >>> print(round(results[\'f1\'], 2))
        0.27
        >>> results = f1_metric.compute(predictions=predictions, references=references, average="micro")
        >>> print(round(results[\'f1\'], 2))
        0.33
        >>> results = f1_metric.compute(predictions=predictions, references=references, average="weighted")
        >>> print(round(results[\'f1\'], 2))
        0.27
        >>> results = f1_metric.compute(predictions=predictions, references=references, average=None)
        >>> print(results)
        {\'f1\': array([0.8, 0. , 0. ])}
'''


A    :					List[Any] =      '''
@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}
}
'''


@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION   ,     _KWARGS_DESCRIPTION    )
class   A			(datasets.Metric    ):
			'''simple docstring'''




			def    a_       (			self   :    Optional[int]   )	->	List[Any]:







					"""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.f1_score.html"""]							,	)







			def    a_       (			self   :    Any							,	__lowerCAmelCase   :    Tuple							,	__lowerCAmelCase   :    Union[str, Any]							,	__lowerCAmelCase   :    Dict=None							,	__lowerCAmelCase   :    List[str]=1							,	__lowerCAmelCase   :    Any="binary"							,	__lowerCAmelCase   :    Optional[int]=None   )	->	List[Any]:







					"""simple docstring"""






					A__        =   fa_score(
					    __lowerCAmelCase							,	__lowerCAmelCase							,	labels=__lowerCAmelCase							,	pos_label=__lowerCAmelCase							,	average=__lowerCAmelCase							,	sample_weight=__lowerCAmelCase   )
					return {"f1": float(__lowerCAmelCase   ) if score.size == 1 else score}



