






from sklearn.metrics import precision_score

import datasets


a		:	List[Any]      					=			"\nPrecision is the fraction of correctly labeled positive examples out of all of the examples that were labeled as positive. It is computed via the equation:\nPrecision = TP / (TP + FP)\nwhere TP is the True positives (i.e. the examples correctly labeled as positive) and FP is the False positive examples (i.e. the examples incorrectly labeled as positive).\n"


a		:	str      					=			"\nArgs:\n    predictions (`list` of `int`): Predicted class labels.\n    references (`list` of `int`): Actual class labels.\n    labels (`list` of `int`): The set of labels to include when `average` is not set to `'binary'`. If `average` is `None`, it should be the label order. 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.\n    pos_label (`int`): The class to be considered the positive class, in the case where `average` is set to `binary`. Defaults to 1.\n    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'`.\n\n        - '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.\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. This option 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 (`int` or `string`): Sets the value to return when there is a zero division. Defaults to 'warn'.\n\n        - 0: Returns 0 when there is a zero division.\n        - 1: Returns 1 when there is a zero division.\n        - 'warn': Raises warnings and then returns 0 when there is a zero division.\n\nReturns:\n    precision (`float` or `array` of `float`): Precision score or list of precision scores, depending on the value passed to `average`. Minimum possible value is 0. Maximum possible value is 1. Higher values indicate that fewer negative examples were incorrectly labeled as positive, which means that, generally, higher scores are better.\n\nExamples:\n\n    Example 1-A simple binary example\n        >>> precision_metric = datasets.load_metric(\"precision\")\n        >>> results = precision_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0])\n        >>> print(results)\n        {'precision': 0.5}\n\n    Example 2-The same simple binary example as in Example 1, but with `pos_label` set to `0`.\n        >>> precision_metric = datasets.load_metric(\"precision\")\n        >>> results = precision_metric.compute(references=[0, 1, 0, 1, 0], predictions=[0, 0, 1, 1, 0], pos_label=0)\n        >>> print(round(results['precision'], 2))\n        0.67\n\n    Example 3-The same simple binary example as in Example 1, but with `sample_weight` included.\n        >>> precision_metric = datasets.load_metric(\"precision\")\n        >>> results = precision_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])\n        >>> print(results)\n        {'precision': 0.23529411764705882}\n\n    Example 4-A multiclass example, with different values for the `average` input.\n        >>> predictions = [0, 2, 1, 0, 0, 1]\n        >>> references = [0, 1, 2, 0, 1, 2]\n        >>> results = precision_metric.compute(predictions=predictions, references=references, average='macro')\n        >>> print(results)\n        {'precision': 0.2222222222222222}\n        >>> results = precision_metric.compute(predictions=predictions, references=references, average='micro')\n        >>> print(results)\n        {'precision': 0.3333333333333333}\n        >>> results = precision_metric.compute(predictions=predictions, references=references, average='weighted')\n        >>> print(results)\n        {'precision': 0.2222222222222222}\n        >>> results = precision_metric.compute(predictions=predictions, references=references, average=None)\n        >>> print([round(res, 2) for res in results['precision']])\n        [0.67, 0.0, 0.0]\n"


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



      """simple docstring"""


      def      UpperCAmelCase				(				self :      Tuple )							->				Optional[Any]:
        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.precision_score.html"""]		,				)




      def      UpperCAmelCase				(				self :      Any		,				__lowercase :      Dict		,				__lowercase :      List[Any]		,				__lowercase :      List[Any]=None		,				__lowercase :      Optional[Any]=1		,				__lowercase :      Tuple="binary"		,				__lowercase :      Dict=None		,				__lowercase :      Union[str, Any]="warn"		,				)							->				List[Any]:
        __UpperCAmelCase							:     Dict									=  precision_score(
            __lowercase		,				__lowercase		,				labels=__lowercase		,				pos_label=__lowercase		,				average=__lowercase		,				sample_weight=__lowercase		,				zero_division=__lowercase		,				)
        return {"precision": float(__lowercase ) if score.size == 1 else score}










import unittest

from transformers.testing_utils import is_torch_available, require_torch, tooslow

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


if is_torch_available():
       import torch

       from transformers import (
           CpmAntConfig,
           CpmAntForCausalLM,
           CpmAntModel,
           CpmAntTokenizer,
       )
@require_torch
class    a   :



      """simple docstring"""


      def __init__(				self :      List[str]		,				__lowercase :      List[str]		,				__lowercase :      Tuple=2		,				__lowercase :      Dict=8		,				__lowercase :      int=True		,				__lowercase :      Dict=False		,				__lowercase :      List[Any]=False		,				__lowercase :      List[str]=False		,				__lowercase :      Any=False		,				__lowercase :      List[Any]=99		,				__lowercase :      int=32		,				__lowercase :      Optional[Any]=3		,				__lowercase :      Any=4		,				__lowercase :      List[Any]=37		,				__lowercase :      Dict=32		,				__lowercase :      Any=128		,				__lowercase :      Optional[Any]=8		,				__lowercase :      str=8		,				__lowercase :      str=8		,				__lowercase :      Optional[int]=1.0		,				__lowercase :      int=True		,				)							->				Optional[int]:
        __UpperCAmelCase							:     Optional[Any]									=  parent
        __UpperCAmelCase							:     Optional[int]									=  batch_size
        __UpperCAmelCase							:     List[Any]									=  seq_length
        __UpperCAmelCase							:     Optional[int]									=  is_training
        __UpperCAmelCase							:     Optional[Any]									=  use_token_type_ids
        __UpperCAmelCase							:     Optional[int]									=  use_input_mask
        __UpperCAmelCase							:     Optional[int]									=  use_labels
        __UpperCAmelCase							:     str									=  use_mc_token_ids
        __UpperCAmelCase							:     Any									=  vocab_size
        __UpperCAmelCase							:     str									=  hidden_size
        __UpperCAmelCase							:     str									=  num_hidden_layers
        __UpperCAmelCase							:     Union[str, Any]									=  num_attention_heads
        __UpperCAmelCase							:     Optional[int]									=  intermediate_size
        __UpperCAmelCase							:     Tuple									=  num_buckets
        __UpperCAmelCase							:     Any									=  max_distance
        __UpperCAmelCase							:     Any									=  prompt_length
        __UpperCAmelCase							:     int									=  prompt_types
        __UpperCAmelCase							:     List[Any]									=  segment_types
        __UpperCAmelCase							:     Any									=  init_std
        __UpperCAmelCase							:     int									=  return_dict
      def      UpperCAmelCase				(				self :      Optional[Any] )							->				List[Any]:
        __UpperCAmelCase							:     Optional[Any]									=  {}
        __UpperCAmelCase							:     Dict									=  ids_tensor([self.batch_size, self.seq_length]		,				self.vocab_size ).type(torch.intaa )
        __UpperCAmelCase							:     List[str]									=  False

        __UpperCAmelCase							:     List[str]									=  self.get_config()

        return (config, input_ids)
      def      UpperCAmelCase				(				self :      Union[str, Any] )							->				Optional[int]:
        return CpmAntConfig(
            vocab_size=self.vocab_size		,				hidden_size=self.hidden_size		,				num_hidden_layers=self.num_hidden_layers		,				num_attention_heads=self.num_attention_heads		,				dim_ff=self.intermediate_size		,				position_bias_num_buckets=self.num_buckets		,				position_bias_max_distance=self.max_distance		,				prompt_types=self.prompt_types		,				prompt_length=self.prompt_length		,				segment_types=self.segment_types		,				use_cache=__lowercase		,				init_std=self.init_std		,				return_dict=self.return_dict		,				)
      def      UpperCAmelCase				(				self :      Tuple		,				__lowercase :      Union[str, Any]		,				__lowercase :      List[str]		,				*__lowercase :      Optional[int] )							->				List[Any]:
        __UpperCAmelCase							:     int									=  CpmAntModel(config=__lowercase )
        model.to(__lowercase )
        model.eval()

        __UpperCAmelCase							:     str									=  model(**__lowercase ).last_hidden_state

        self.parent.assertEqual(hidden_states.shape		,				(self.batch_size, self.seq_length, config.hidden_size) )
      def      UpperCAmelCase				(				self :      List[str]		,				__lowercase :      List[str]		,				__lowercase :      Optional[Any]		,				*__lowercase :      Optional[int] )							->				List[str]:
        __UpperCAmelCase							:     List[Any]									=  CpmAntForCausalLM(__lowercase )
        model.to(__lowercase )
        __UpperCAmelCase							:     Optional[Any]									=  input_ids["""input_ids"""].to(__lowercase )
        model.eval()

        __UpperCAmelCase							:     int									=  model(**__lowercase )
        self.parent.assertEqual(
            model_output.logits.shape		,				(self.batch_size, self.seq_length, config.vocab_size + config.prompt_types * config.prompt_length)		,				)




      def      UpperCAmelCase				(				self :      List[Any] )							->				Dict:
        __UpperCAmelCase				,			__UpperCAmelCase							:     Optional[int]									=  self.prepare_config_and_inputs()
        return config, inputs_dict


@require_torch
class    a   (			lowercase__							,	lowercase__							,	unittest.TestCase       ):



      """simple docstring"""


      a  :			List[Any]								=     (CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
      a  :			Optional[int]								=     (
          {'feature-extraction': CpmAntModel, 'text-generation': CpmAntForCausalLM} if is_torch_available() else {}
      )

      a  :			List[Any]								=     False
      a  :			Any								=     False
      a  :			List[Any]								=     False
      a  :			Tuple								=     False
      a  :			Any								=     False
      def      UpperCAmelCase				(				self :      Optional[int] )							->				Optional[Any]:
        __UpperCAmelCase							:     List[Any]									=  CpmAntModelTester(self )
        __UpperCAmelCase							:     Dict									=  ConfigTester(self		,				config_class=__lowercase )
      def      UpperCAmelCase				(				self :      str )							->				Optional[int]:
        self.config_tester.create_and_test_config_common_properties()
        self.config_tester.create_and_test_config_to_json_string()
        self.config_tester.create_and_test_config_to_json_file()
        self.config_tester.create_and_test_config_from_and_save_pretrained()
        self.config_tester.check_config_can_be_init_without_params()
        self.config_tester.check_config_arguments_init()
      def      UpperCAmelCase				(				self :      List[str] )							->				List[str]:
        unittest.skip("""CPMAnt doesn't support input_embeds.""" )(self.test_inputs_embeds )
      def      UpperCAmelCase				(				self :      Any )							->				Union[str, Any]:
        unittest.skip(
            """CPMAnt doesn't support retain grad in hidden_states or attentions, because prompt management will peel off the output.hidden_states from graph.\
                 So is attentions. We strongly recommand you use loss to tune model.""" )(self.test_retain_grad_hidden_states_attentions )
      def      UpperCAmelCase				(				self :      str )							->				List[Any]:
        __UpperCAmelCase				,			__UpperCAmelCase							:     Optional[Any]									=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_cpmant_model(__lowercase		,				__lowercase )




      def      UpperCAmelCase				(				self :      Optional[Any] )							->				str:
        __UpperCAmelCase				,			__UpperCAmelCase							:     Dict									=  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_lm_head_model(__lowercase		,				__lowercase )


@require_torch
class    a   (			unittest.TestCase       ):



      """simple docstring"""


      @tooslow
      def      UpperCAmelCase				(				self :      str )							->				Optional[Any]:
        __UpperCAmelCase							:     List[Any]									=  """今天天气真好！"""
        __UpperCAmelCase							:     int									=  """openbmb/cpm-ant-10b"""
        __UpperCAmelCase							:     Tuple									=  CpmAntModel.from_pretrained(__lowercase )
        __UpperCAmelCase							:     Optional[int]									=  CpmAntTokenizer.from_pretrained(__lowercase )
        __UpperCAmelCase							:     Union[str, Any]									=  tokenizer(__lowercase		,				return_tensors="""pt""" )
        __UpperCAmelCase							:     List[Any]									=  model(**__lowercase ).last_hidden_state

        __UpperCAmelCase							:     str									=  torch.tensor(
            [[[6.1_708, 5.9_244, 1.0_835], [6.5_207, 6.2_893, -11.3_324], [-1.0_107, -0.0_576, -5.9_577]]]		,				)
        self.assertTrue(torch.allclose(hidden_states[:, :3, :3]		,				__lowercase		,				atol=1e-2 ) )


@require_torch
class    a   (			unittest.TestCase       ):



      """simple docstring"""


      @tooslow
      def      UpperCAmelCase				(				self :      List[Any] )							->				str:
        __UpperCAmelCase							:     List[Any]									=  """今天天气真好！"""
        __UpperCAmelCase							:     Optional[Any]									=  """openbmb/cpm-ant-10b"""
        __UpperCAmelCase							:     Tuple									=  CpmAntForCausalLM.from_pretrained(__lowercase )
        __UpperCAmelCase							:     Tuple									=  CpmAntTokenizer.from_pretrained(__lowercase )
        __UpperCAmelCase							:     List[Any]									=  tokenizer(__lowercase		,				return_tensors="""pt""" )
        __UpperCAmelCase							:     Tuple									=  model(**__lowercase ).logits

        __UpperCAmelCase							:     int									=  torch.tensor(
            [[[-6.4_267, -6.4_083, -6.3_958], [-5.8_802, -5.9_447, -5.7_811], [-5.3_896, -5.4_820, -5.4_295]]]		,				)
        self.assertTrue(torch.allclose(hidden_states[:, :3, :3]		,				__lowercase		,				atol=1e-2 ) )
      @tooslow
      def      UpperCAmelCase				(				self :      Dict )							->				Dict:
        __UpperCAmelCase							:     List[str]									=  """openbmb/cpm-ant-10b"""
        __UpperCAmelCase							:     Dict									=  CpmAntForCausalLM.from_pretrained(__lowercase )
        __UpperCAmelCase							:     int									=  CpmAntTokenizer.from_pretrained(__lowercase )
        __UpperCAmelCase							:     List[Any]									=  """今天天气不错，"""
        __UpperCAmelCase							:     Optional[int]									=  """今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的"""
        __UpperCAmelCase							:     Optional[int]									=  tokenizer(__lowercase		,				return_tensors="""pt""" )
        __UpperCAmelCase							:     Tuple									=  model.generate(**__lowercase )
        __UpperCAmelCase							:     List[Any]									=  tokenizer.batch_decode(__lowercase )
        self.assertEqual(__lowercase		,				__lowercase )




      @tooslow
      def      UpperCAmelCase				(				self :      Optional[Any] )							->				Any:
        __UpperCAmelCase							:     int									=  """openbmb/cpm-ant-10b"""
        __UpperCAmelCase							:     Any									=  CpmAntForCausalLM.from_pretrained(__lowercase )
        __UpperCAmelCase							:     Optional[Any]									=  CpmAntTokenizer.from_pretrained(__lowercase )
        __UpperCAmelCase							:     Optional[Any]									=  ["""今天天气不错，""", """新年快乐，万事如意！"""]
        __UpperCAmelCase							:     str									=  [
            """今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的""",
            """新年快乐，万事如意！在这辞旧迎新的美好时刻，我谨代表《农村新技术》杂志社全体同仁，向一直以来关心、支持《农村新技术》杂志发展的各级领导、各界朋友和广大读者致以最诚挚的""",
        ]
        __UpperCAmelCase							:     Union[str, Any]									=  tokenizer(__lowercase		,				return_tensors="""pt"""		,				padding=__lowercase )
        __UpperCAmelCase							:     int									=  model.generate(**__lowercase )
        __UpperCAmelCase							:     Optional[int]									=  tokenizer.batch_decode(__lowercase )
        self.assertEqual(__lowercase		,				__lowercase )


