






"""simple docstring"""







from sklearn.metrics import mean_squared_error

import datasets


_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}
}
"""

_UpperCamelCase	     =    """\
Mean Squared Error(MSE) is the average of the square of difference between the predicted
and actual values.
"""


_UpperCamelCase	     =    """
Args:
    predictions: array-like of shape (n_samples,) or (n_samples, n_outputs)
        Estimated target values.
    references: array-like of shape (n_samples,) or (n_samples, n_outputs)
        Ground truth (correct) target values.
    sample_weight: array-like of shape (n_samples,), default=None
        Sample weights.
    multioutput: {\"raw_values\", \"uniform_average\"} or array-like of shape (n_outputs,), default=\"uniform_average\"
        Defines aggregating of multiple output values. Array-like value defines weights used to average errors.

        \"raw_values\" : Returns a full set of errors in case of multioutput input.

        \"uniform_average\" : Errors of all outputs are averaged with uniform weight.

    squared : bool, default=True
        If True returns MSE value, if False returns RMSE (Root Mean Squared Error) value.

Returns:
    mse : mean squared error.
Examples:

    >>> mse_metric = datasets.load_metric(\"mse\")
    >>> predictions = [2.5, 0.0, 2, 8]
    >>> references = [3, -0.5, 2, 7]
    >>> results = mse_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'mse': 0.375}
    >>> rmse_result = mse_metric.compute(predictions=predictions, references=references, squared=False)
    >>> print(rmse_result)
    {'mse': 0.6123724356957945}

    If you're using multi-dimensional lists, then set the config as follows :

    >>> mse_metric = datasets.load_metric(\"mse\", \"multilist\")
    >>> predictions = [[0.5, 1], [-1, 1], [7, -6]]
    >>> references = [[0, 2], [-1, 2], [8, -5]]
    >>> results = mse_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'mse': 0.7083333333333334}
    >>> results = mse_metric.compute(predictions=predictions, references=references, multioutput='raw_values')
    >>> print(results) # doctest: +NORMALIZE_WHITESPACE
    {'mse': array([0.41666667, 1. ])}
"""







@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION       ,	_KWARGS_DESCRIPTION					)
class    lowerCamelCase__   (			datasets.Metric					):






    def 							_UpperCamelCase				(					self   ):
          return datasets.MetricInfo(
              description=_DESCRIPTION      ,citation=_CITATION      ,inputs_description=_KWARGS_DESCRIPTION      ,features=datasets.Features(self._get_feature_types()   )      ,reference_urls=[
                  """https://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html"""
              ]      ,)






    def 							_UpperCamelCase				(					self   ):
          if self.config_name == "multilist":
                return {
                    "predictions": datasets.Sequence(datasets.Value("""float"""   )   ),
                    "references": datasets.Sequence(datasets.Value("""float"""   )   ),
                }
          else:
                return {
                    "predictions": datasets.Value("""float"""   ),
                    "references": datasets.Value("""float"""   ),
                }






    def 							_UpperCamelCase				(					self      ,A      ,A      ,A=None      ,A="uniform_average"      ,A=True   ):
          UpperCAmelCase							     =			mean_squared_error(
              snake_case_      ,snake_case_      ,sample_weight=snake_case_      ,multioutput=snake_case_      ,squared=snake_case_   )

          return {"mse": mse}





import subprocess
import sys

from transformers import BertConfig, BertModel, BertTokenizer, pipeline
from transformers.testing_utils import TestCasePlus, require_torch

class 							_UpperCamelCase					(       _A	):






			'''simple docstring'''

			@require_torch
			def    lowerCAmelCase__						(							self			:							List[str]   ):
							# this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
							# `transformers` is loaded, and it's too late for inside pytest - so we are changing it
							# while running an external program

							# python one-liner segments

							# this must be loaded before socket.socket is monkey-patched
							UpperCamelCase_:				Optional[Any]						=    """
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        """

							UpperCamelCase_:				List[Any]						=    """
mname = \"hf-internal-testing/tiny-random-bert\"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
BertTokenizer.from_pretrained(mname)
pipe = pipeline(task=\"fill-mask\", model=mname)
print(\"success\")
        """

							UpperCamelCase_:				Dict						=    """
import socket
def offline_socket(*args, **kwargs): raise RuntimeError(\"Offline mode is enabled, we shouldn't access internet\")
socket.socket = offline_socket
        """

							# Force fetching the files so that we can use the cache
							UpperCamelCase_:				int						=    """hf-internal-testing/tiny-random-bert"""
							BertConfig.from_pretrained(snake_case_   )
							BertModel.from_pretrained(snake_case_   )
							BertTokenizer.from_pretrained(snake_case_   )
							pipeline(task="""fill-mask"""    ,						model=snake_case_   )

							# baseline - just load from_pretrained with normal network
							UpperCamelCase_:				Tuple						=    [sys.executable, """-c""", """\n""".join([load, run, mock]   )]

							# should succeed
							UpperCamelCase_:				Optional[int]						=    self.get_env()
							# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
							UpperCamelCase_:				int						=    """1"""
							UpperCamelCase_:				Optional[Any]						=    subprocess.run(snake_case_    ,						env=snake_case_    ,						check=snake_case_    ,						capture_output=snake_case_   )
							self.assertEqual(result.returncode    ,						0    ,						result.stderr   )
							self.assertIn("""success"""    ,						result.stdout.decode()   )

			@require_torch
			def    lowerCAmelCase__						(							self			:							Any   ):
							# python one-liner segments
							# this must be loaded before socket.socket is monkey-patched
							UpperCamelCase_:				str						=    """
from transformers import BertConfig, BertModel, BertTokenizer, pipeline
        """

							UpperCamelCase_:				Any						=    """
mname = \"hf-internal-testing/tiny-random-bert\"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
BertTokenizer.from_pretrained(mname)
pipe = pipeline(task=\"fill-mask\", model=mname)
print(\"success\")
        """

							UpperCamelCase_:				Optional[int]						=    """
import socket
def offline_socket(*args, **kwargs): raise socket.error(\"Faking flaky internet\")
socket.socket = offline_socket
        """

							# Force fetching the files so that we can use the cache
							UpperCamelCase_:				Optional[int]						=    """hf-internal-testing/tiny-random-bert"""
							BertConfig.from_pretrained(snake_case_   )
							BertModel.from_pretrained(snake_case_   )
							BertTokenizer.from_pretrained(snake_case_   )
							pipeline(task="""fill-mask"""    ,						model=snake_case_   )

							# baseline - just load from_pretrained with normal network
							UpperCamelCase_:				Optional[int]						=    [sys.executable, """-c""", """\n""".join([load, run, mock]   )]

							# should succeed
							UpperCamelCase_:				List[str]						=    self.get_env()
							UpperCamelCase_:				List[Any]						=    subprocess.run(snake_case_    ,						env=snake_case_    ,						check=snake_case_    ,						capture_output=snake_case_   )
							self.assertEqual(result.returncode    ,						0    ,						result.stderr   )
							self.assertIn("""success"""    ,						result.stdout.decode()   )

			@require_torch
			def    lowerCAmelCase__						(							self			:							int   ):
							# this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
							# `transformers` is loaded, and it's too late for inside pytest - so we are changing it
							# while running an external program

							# python one-liner segments

							# this must be loaded before socket.socket is monkey-patched
							UpperCamelCase_:				Dict						=    """
from transformers import BertConfig, BertModel, BertTokenizer
        """

							UpperCamelCase_:				Optional[Any]						=    """
mname = \"hf-internal-testing/tiny-random-bert-sharded\"
BertConfig.from_pretrained(mname)
BertModel.from_pretrained(mname)
print(\"success\")
        """

							UpperCamelCase_:				Dict						=    """
import socket
def offline_socket(*args, **kwargs): raise ValueError(\"Offline mode is enabled\")
socket.socket = offline_socket
        """

							# baseline - just load from_pretrained with normal network
							UpperCamelCase_:				Union[str, Any]						=    [sys.executable, """-c""", """\n""".join([load, run]   )]

							# should succeed
							UpperCamelCase_:				Union[str, Any]						=    self.get_env()
							UpperCamelCase_:				List[Any]						=    subprocess.run(snake_case_    ,						env=snake_case_    ,						check=snake_case_    ,						capture_output=snake_case_   )
							self.assertEqual(result.returncode    ,						0    ,						result.stderr   )
							self.assertIn("""success"""    ,						result.stdout.decode()   )

							# next emulate no network
							UpperCamelCase_:				Optional[int]						=    [sys.executable, """-c""", """\n""".join([load, mock, run]   )]

							# Doesn't fail anymore since the model is in the cache due to other tests, so commenting this.
							# env["TRANSFORMERS_OFFLINE"] = "0"
							# result = subprocess.run(cmd, env=env, check=False, capture_output=True)
							# self.assertEqual(result.returncode, 1, result.stderr)

							# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
							UpperCamelCase_:				Union[str, Any]						=    """1"""
							UpperCamelCase_:				int						=    subprocess.run(snake_case_    ,						env=snake_case_    ,						check=snake_case_    ,						capture_output=snake_case_   )
							self.assertEqual(result.returncode    ,						0    ,						result.stderr   )
							self.assertIn("""success"""    ,						result.stdout.decode()   )

			@require_torch
			def    lowerCAmelCase__						(							self			:							List[str]   ):
							UpperCamelCase_:				Dict						=    """
from transformers import pipeline
        """
							UpperCamelCase_:				Union[str, Any]						=    """
mname = \"hf-internal-testing/tiny-random-bert\"
pipe = pipeline(model=mname)
        """

							UpperCamelCase_:				Dict						=    """
import socket
def offline_socket(*args, **kwargs): raise socket.error(\"Offline mode is enabled\")
socket.socket = offline_socket
        """
							UpperCamelCase_:				Optional[Any]						=    self.get_env()
							UpperCamelCase_:				int						=    """1"""
							UpperCamelCase_:				int						=    [sys.executable, """-c""", """\n""".join([load, mock, run]   )]
							UpperCamelCase_:				Any						=    subprocess.run(snake_case_    ,						env=snake_case_    ,						check=snake_case_    ,						capture_output=snake_case_   )
							self.assertEqual(result.returncode    ,						1    ,						result.stderr   )
							self.assertIn(
							    """You cannot infer task automatically within `pipeline` when using offline mode"""    ,						result.stderr.decode().replace("""\n"""    ,						""""""   )    ,						)




			@require_torch
			def    lowerCAmelCase__						(							self			:							Optional[int]   ):
							UpperCamelCase_:				str						=    """
from transformers import AutoModel
        """
							UpperCamelCase_:				Dict						=    """
mname = \"hf-internal-testing/test_dynamic_model\"
AutoModel.from_pretrained(mname, trust_remote_code=True)
print(\"success\")
        """

							# baseline - just load from_pretrained with normal network
							UpperCamelCase_:				Optional[int]						=    [sys.executable, """-c""", """\n""".join([load, run]   )]

							# should succeed
							UpperCamelCase_:				Optional[Any]						=    self.get_env()
							UpperCamelCase_:				int						=    subprocess.run(snake_case_    ,						env=snake_case_    ,						check=snake_case_    ,						capture_output=snake_case_   )
							self.assertEqual(result.returncode    ,						0    ,						result.stderr   )
							self.assertIn("""success"""    ,						result.stdout.decode()   )

							# should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
							UpperCamelCase_:				Union[str, Any]						=    """1"""
							UpperCamelCase_:				List[str]						=    subprocess.run(snake_case_    ,						env=snake_case_    ,						check=snake_case_    ,						capture_output=snake_case_   )
							self.assertEqual(result.returncode    ,						0    ,						result.stderr   )
							self.assertIn("""success"""    ,						result.stdout.decode()   )






