




from sklearn.metrics import accuracy_score

import datasets


UpperCamelCase__       							=      """
Accuracy is the proportion of correct predictions among the total number of cases processed. It can be computed with:
Accuracy = (TP + TN) / (TP + TN + FP + FN)
 Where:
TP: True positive
TN: True negative
FP: False positive
FN: False negative
"""


UpperCamelCase__       							=      """
Args:
    predictions (`list` of `int`): Predicted labels.
    references (`list` of `int`): Ground truth labels.
    normalize (`boolean`): If set to False, returns the number of correctly classified samples. Otherwise, returns the fraction of correctly classified samples. Defaults to True.
    sample_weight (`list` of `float`): Sample weights Defaults to None.

Returns:
    accuracy (`float` or `int`): Accuracy score. Minimum possible value is 0. Maximum possible value is 1.0, or the number of examples input, if `normalize` is set to `True`.. A higher score means higher accuracy.

Examples:

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

    Example 2-The same as Example 1, except with `normalize` set to `False`.
        >>> accuracy_metric = datasets.load_metric(\"accuracy\")
        >>> results = accuracy_metric.compute(references=[0, 1, 2, 0, 1, 2], predictions=[0, 1, 1, 2, 1, 0], normalize=False)
        >>> print(results)
        {'accuracy': 3.0}

    Example 3-The same as Example 1, except with `sample_weight` set.
        >>> accuracy_metric = datasets.load_metric(\"accuracy\")
        >>> results = accuracy_metric.compute(references=[0, 1, 2, 0, 1, 2], predictions=[0, 1, 1, 2, 1, 0], sample_weight=[0.5, 2, 0.7, 0.5, 9, 0.4])
        >>> print(results)
        {'accuracy': 0.8778625954198473}
"""


UpperCamelCase__       							=      """
@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  ):


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





   def     lowercase_   (self  :		Dict ,						__UpperCAmelCase  :		Optional[int] ,						__UpperCAmelCase  :		Optional[int] ,						__UpperCAmelCase  :		Tuple=True ,						__UpperCAmelCase  :		List[str]=None	)		->       str:
         """simple docstring"""



         return {
             "accuracy": float(
                 accuracy_score(__UpperCAmelCase ,						__UpperCAmelCase ,						normalize=__UpperCAmelCase ,						sample_weight=__UpperCAmelCase	)	)
         }




import os
import pickle
import unittest
from collections import UserDict, namedtuple

import torch

from accelerate.test_utils.testing import require_cuda, require_torch_min_version
from accelerate.test_utils.training import RegressionModel
from accelerate.utils import (
    convert_outputs_to_fpaa,
    extract_model_from_parallel,
    find_device,
    listify,
    patch_environment,
    recursively_apply,
    send_to_device,
)


UpperCamelCase__       							=      namedtuple('ExampleNamedTuple', 'a b c')





class   A							(       unittest.TestCase  ):


   def     lowercase_   (self  :		str	)		->       List[Any]:
         """simple docstring"""



         UpperCAmelCase__			=						torch.randn(5 ,						2	)
         UpperCAmelCase__			=						torch.device("cuda"	) if torch.cuda.is_available() else torch.device("cpu"	)

         UpperCAmelCase__			=						send_to_device(__UpperCAmelCase ,						__UpperCAmelCase	)
         self.assertTrue(torch.equal(resulta.cpu() ,						__UpperCAmelCase	)	)

         UpperCAmelCase__			=						send_to_device((tensor, [tensor, tensor], 1) ,						__UpperCAmelCase	)
         self.assertIsInstance(__UpperCAmelCase ,						__UpperCAmelCase	)
         self.assertTrue(torch.equal(resulta[0].cpu() ,						__UpperCAmelCase	)	)
         self.assertIsInstance(resulta[1] ,						__UpperCAmelCase	)
         self.assertTrue(torch.equal(resulta[1][0].cpu() ,						__UpperCAmelCase	)	)
         self.assertTrue(torch.equal(resulta[1][1].cpu() ,						__UpperCAmelCase	)	)
         self.assertEqual(resulta[2] ,						1	)

         UpperCAmelCase__			=						send_to_device({"a": tensor, "b": [tensor, tensor], "c": 1} ,						__UpperCAmelCase	)
         self.assertIsInstance(__UpperCAmelCase ,						__UpperCAmelCase	)
         self.assertTrue(torch.equal(resulta["a"].cpu() ,						__UpperCAmelCase	)	)
         self.assertIsInstance(resulta["b"] ,						__UpperCAmelCase	)
         self.assertTrue(torch.equal(resulta["b"][0].cpu() ,						__UpperCAmelCase	)	)
         self.assertTrue(torch.equal(resulta["b"][1].cpu() ,						__UpperCAmelCase	)	)
         self.assertEqual(resulta["c"] ,						1	)

         UpperCAmelCase__			=						send_to_device(ExampleNamedTuple(a=__UpperCAmelCase ,						b=[tensor, tensor] ,						c=1	) ,						__UpperCAmelCase	)
         self.assertIsInstance(__UpperCAmelCase ,						__UpperCAmelCase	)
         self.assertTrue(torch.equal(resulta.a.cpu() ,						__UpperCAmelCase	)	)
         self.assertIsInstance(resulta.b ,						__UpperCAmelCase	)
         self.assertTrue(torch.equal(resulta.b[0].cpu() ,						__UpperCAmelCase	)	)
         self.assertTrue(torch.equal(resulta.b[1].cpu() ,						__UpperCAmelCase	)	)
         self.assertEqual(resulta.c ,						1	)

         UpperCAmelCase__			=						send_to_device(UserDict({"a": tensor, "b": [tensor, tensor], "c": 1}	) ,						__UpperCAmelCase	)
         self.assertIsInstance(__UpperCAmelCase ,						__UpperCAmelCase	)
         self.assertTrue(torch.equal(resulta["a"].cpu() ,						__UpperCAmelCase	)	)
         self.assertIsInstance(resulta["b"] ,						__UpperCAmelCase	)
         self.assertTrue(torch.equal(resulta["b"][0].cpu() ,						__UpperCAmelCase	)	)
         self.assertTrue(torch.equal(resulta["b"][1].cpu() ,						__UpperCAmelCase	)	)
         self.assertEqual(resulta["c"] ,						1	)


   def     lowercase_   (self  :		int	)		->       int:
         """simple docstring"""



         with self.assertRaises(__UpperCAmelCase	) as cm:
               UpperCAmelCase__			=						recursively_apply(torch.tensor ,						(torch.tensor(1	), 1) ,						error_on_other_type=__UpperCAmelCase	)
         self.assertEqual(
             str(cm.exception	) ,						"Unsupported types (<class 'int'>) passed to `tensor`. Only nested list/tuple/dicts of objects that are valid for `is_torch_tensor` should be passed." ,						)


   def     lowercase_   (self  :		List[str]	)		->       Any:
         """simple docstring"""



         UpperCAmelCase__			=						torch.tensor([1, 2, 3, 4, 5]	)
         self.assertEqual(listify(__UpperCAmelCase	) ,						[1, 2, 3, 4, 5]	)

         UpperCAmelCase__			=						torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]	)
         self.assertEqual(listify(__UpperCAmelCase	) ,						[[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]	)

         UpperCAmelCase__			=						torch.tensor([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]], [[1_1, 1_2, 1_3, 1_4, 1_5], [1_6, 1_7, 1_8, 1_9, 2_0]]]	)
         self.assertEqual(
             listify(__UpperCAmelCase	) ,						[[[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]], [[1_1, 1_2, 1_3, 1_4, 1_5], [1_6, 1_7, 1_8, 1_9, 2_0]]]	)


   def     lowercase_   (self  :		Union[str, Any]	)		->       Dict:
         """simple docstring"""



         with patch_environment(aa=1 ,						BB=2	):
               self.assertEqual(os.environ.get("AA"	) ,						"1"	)
               self.assertEqual(os.environ.get("BB"	) ,						"2"	)

         self.assertNotIn("AA" ,						os.environ	)
         self.assertNotIn("BB" ,						os.environ	)


   def     lowercase_   (self  :		int	)		->       str:
         """simple docstring"""



         UpperCAmelCase__			=						RegressionModel()
         UpperCAmelCase__			=						model.forward
         UpperCAmelCase__			=						convert_outputs_to_fpaa(model.forward	)
         UpperCAmelCase__			=						extract_model_from_parallel(__UpperCAmelCase ,						keep_fpaa_wrapper=__UpperCAmelCase	)
         UpperCAmelCase__			=						pickle.dumps(__UpperCAmelCase	)


   @require_cuda
   def     lowercase_   (self  :		List[str]	)		->       Union[str, Any]:
         """simple docstring"""



         UpperCAmelCase__			=						RegressionModel()
         UpperCAmelCase__			=						model.forward
         UpperCAmelCase__			=						torch.cuda.amp.autocast(dtype=torch.floataa	)(model.forward	)
         UpperCAmelCase__			=						convert_outputs_to_fpaa(model.forward	)
         UpperCAmelCase__			=						extract_model_from_parallel(__UpperCAmelCase ,						keep_fpaa_wrapper=__UpperCAmelCase	)
         UpperCAmelCase__			=						pickle.dumps(__UpperCAmelCase	)


   @require_cuda
   @require_torch_min_version(version="2.0"	)
   def     lowercase_   (self  :		Optional[int]	)		->       Union[str, Any]:
         """simple docstring"""



         UpperCAmelCase__			=						RegressionModel()
         UpperCAmelCase__			=						model.forward
         UpperCAmelCase__			=						torch.cuda.amp.autocast(dtype=torch.floataa	)(model.forward	)
         UpperCAmelCase__			=						convert_outputs_to_fpaa(model.forward	)
         UpperCAmelCase__			=						torch.compile(model.forward ,						backend="inductor"	)
         UpperCAmelCase__			=						torch.randn(4 ,						1_0	).cuda()
         UpperCAmelCase__			=						model(__UpperCAmelCase	)


   def     lowercase_   (self  :		List[Any]	)		->       Union[str, Any]:
         """simple docstring"""



         UpperCAmelCase__			=						RegressionModel()
         # could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
         UpperCAmelCase__			=						torch.nn.parallel.DataParallel(__UpperCAmelCase	)
         UpperCAmelCase__			=						extract_model_from_parallel(__UpperCAmelCase	)

         self.assertEqual(__UpperCAmelCase ,						__UpperCAmelCase	)


   @require_torch_min_version(version="2.0"	)
   def     lowercase_   (self  :		Union[str, Any]	)		->       Union[str, Any]:
         """simple docstring"""



         UpperCAmelCase__			=						RegressionModel()
         UpperCAmelCase__			=						torch.compile(__UpperCAmelCase	)

         # could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
         UpperCAmelCase__			=						torch.nn.parallel.DataParallel(__UpperCAmelCase	)
         UpperCAmelCase__			=						torch.compile(__UpperCAmelCase	)
         UpperCAmelCase__			=						extract_model_from_parallel(__UpperCAmelCase	)

         self.assertEqual(compiled_model._orig_mod ,						compiled_model_unwrapped._orig_mod	)





   def     lowercase_   (self  :		int	)		->       int:
         """simple docstring"""



         self.assertEqual(find_device([1, "a", torch.tensor([1, 2, 3]	)]	) ,						torch.device("cpu"	)	)
         self.assertEqual(find_device({"a": 1, "b": torch.tensor([1, 2, 3]	)}	) ,						torch.device("cpu"	)	)
         self.assertIsNone(find_device([1, "a"]	)	)



