





'''simple docstring'''






import inspect
import unittest

from transformers import DecisionTransformerConfig, 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, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import DecisionTransformerModel
		from transformers.models.decision_transformer.modeling_decision_transformer import (
		    DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
		)







class 			lowerCamelCase					:


					def __init__(					self	:  List[Any]       ,     __snake_case	:  List[Any]       ,     __snake_case	:  Any=13       ,     __snake_case	:  Tuple=7       ,     __snake_case	:  Optional[int]=6       ,     __snake_case	:  int=17       ,     __snake_case	:  List[Any]=23       ,     __snake_case	:  Optional[Any]=11       ,     __snake_case	:  Dict=True       ,     ):





											'''simple docstring'''





											_snake_case:   Optional[int]  					=		parent
											_snake_case:   List[Any]  					=		batch_size
											_snake_case:   Optional[Any]  					=		seq_length
											_snake_case:   Any  					=		act_dim
											_snake_case:   Optional[int]  					=		state_dim
											_snake_case:   str  					=		hidden_size
											_snake_case:   List[str]  					=		max_length
											_snake_case:   List[str]  					=		is_training


					def    SCREAMING_SNAKE_CASE_		(					self	:  List[str]       ):





											'''simple docstring'''





											_snake_case:   Tuple  					=		floats_tensor((self.batch_size, self.seq_length, self.state_dim)       )
											_snake_case:   List[Any]  					=		floats_tensor((self.batch_size, self.seq_length, self.act_dim)       )
											_snake_case:   Dict  					=		floats_tensor((self.batch_size, self.seq_length, 1)       )
											_snake_case:   Any  					=		floats_tensor((self.batch_size, self.seq_length, 1)       )
											_snake_case:   List[str]  					=		ids_tensor((self.batch_size, self.seq_length)       ,     vocab_size=10_00       )
											_snake_case:   Optional[int]  					=		random_attention_mask((self.batch_size, self.seq_length)       )

											_snake_case:   int  					=		self.get_config()

											return (
											    config,
											    states,
											    actions,
											    rewards,
											    returns_to_go,
											    timesteps,
											    attention_mask,
											)


					def    SCREAMING_SNAKE_CASE_		(					self	:  int       ):





											'''simple docstring'''





											return DecisionTransformerConfig(
											    batch_size=self.batch_size       ,     seq_length=self.seq_length       ,     act_dim=self.act_dim       ,     state_dim=self.state_dim       ,     hidden_size=self.hidden_size       ,     max_length=self.max_length       ,     )


					def    SCREAMING_SNAKE_CASE_		(					self	:  Union[str, Any]       ,     __snake_case	:  str       ,     __snake_case	:  List[Any]       ,     __snake_case	:  Optional[int]       ,     __snake_case	:  Optional[Any]       ,     __snake_case	:  str       ,     __snake_case	:  int       ,     __snake_case	:  List[Any]       ,     ):





											'''simple docstring'''





											_snake_case:   Union[str, Any]  					=		DecisionTransformerModel(config=__snake_case       )
											model.to(__snake_case       )
											model.eval()
											_snake_case:   Optional[Any]  					=		model(__snake_case       ,     __snake_case       ,     __snake_case       ,     __snake_case       ,     __snake_case       ,     __snake_case       )

											self.parent.assertEqual(result.state_preds.shape       ,     states.shape       )
											self.parent.assertEqual(result.action_preds.shape       ,     actions.shape       )
											self.parent.assertEqual(result.return_preds.shape       ,     returns_to_go.shape       )
											self.parent.assertEqual(
											    result.last_hidden_state.shape       ,     (self.batch_size, self.seq_length * 3, self.hidden_size)       )  # seq length *3 as there are 3 modelities: states, returns and actions




					def    SCREAMING_SNAKE_CASE_		(					self	:  Any       ):





											'''simple docstring'''





											_snake_case:   List[Any]  					=		self.prepare_config_and_inputs()
											(
											    (
											    _snake_case
											)					,						(
											    _snake_case
											)					,						(
											    _snake_case
											)					,						(
											    _snake_case
											)					,						(
											    _snake_case
											)					,						(
											    _snake_case
											)					,						(
											    _snake_case
											)					,						
											):   Tuple  					=		config_and_inputs
											_snake_case:   Optional[Any]  					=		{
											    'states': states,
											    'actions': actions,
											    'rewards': rewards,
											    'returns_to_go': returns_to_go,
											    'timesteps': timesteps,
											    'attention_mask': attention_mask,
											}
											return config, inputs_dict







@require_torch
class 			lowerCamelCase					(						__UpperCAmelCase     ,							__UpperCAmelCase     ,							__UpperCAmelCase     ,							unittest.TestCase  ):
					_SCREAMING_SNAKE_CASE							   =    (DecisionTransformerModel,) if is_torch_available() else ()
					_SCREAMING_SNAKE_CASE							   =    ()
					_SCREAMING_SNAKE_CASE							   =    {"feature-extraction": DecisionTransformerModel} if is_torch_available() else {}

					# Ignoring of a failing test from GenerationTesterMixin, as the model does not use inputs_ids
					_SCREAMING_SNAKE_CASE							   =    False

					# Ignoring of a failing tests from ModelTesterMixin, as the model does not implement these features
					_SCREAMING_SNAKE_CASE							   =    False
					_SCREAMING_SNAKE_CASE							   =    False
					_SCREAMING_SNAKE_CASE							   =    False
					_SCREAMING_SNAKE_CASE							   =    False
					_SCREAMING_SNAKE_CASE							   =    False
					_SCREAMING_SNAKE_CASE							   =    False
					_SCREAMING_SNAKE_CASE							   =    False
					_SCREAMING_SNAKE_CASE							   =    False
					_SCREAMING_SNAKE_CASE							   =    False


					def    SCREAMING_SNAKE_CASE_		(					self	:  int       ):





											'''simple docstring'''





											_snake_case:   int  					=		DecisionTransformerModelTester(self       )
											_snake_case:   str  					=		ConfigTester(self       ,     config_class=__snake_case       ,     hidden_size=37       )


					def    SCREAMING_SNAKE_CASE_		(					self	:  Any       ):





											'''simple docstring'''





											self.config_tester.run_common_tests()


					def    SCREAMING_SNAKE_CASE_		(					self	:  Optional[int]       ):





											'''simple docstring'''





											_snake_case:   Any  					=		self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__snake_case       )


					@slow
					def    SCREAMING_SNAKE_CASE_		(					self	:  Union[str, Any]       ):





											'''simple docstring'''





											for model_name in DECISION_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																	_snake_case:   List[str]  					=		DecisionTransformerModel.from_pretrained(__snake_case       )
																	self.assertIsNotNone(__snake_case       )




					def    SCREAMING_SNAKE_CASE_		(					self	:  List[str]       ):





											'''simple docstring'''





											_snake_case					,						_snake_case:   str  					=		self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																	_snake_case:   List[Any]  					=		model_class(__snake_case       )
																	_snake_case:   Dict  					=		inspect.signature(model.forward       )
																	# signature.parameters is an OrderedDict => so arg_names order is deterministic
																	_snake_case:   List[str]  					=		[*signature.parameters.keys()]

																	_snake_case:   Tuple  					=		[
																	    'states',
																	    'actions',
																	    'rewards',
																	    'returns_to_go',
																	    'timesteps',
																	    'attention_mask',
																	]

																	self.assertListEqual(arg_names[: len(__snake_case       )]       ,     __snake_case       )







@require_torch
class 			lowerCamelCase					(						unittest.TestCase  ):


					@slow
					def    SCREAMING_SNAKE_CASE_		(					self	:  Optional[int]       ):





											'''simple docstring'''





											_snake_case:   Optional[int]  					=		2  # number of steps of autoregressive prediction we will perform
											_snake_case:   int  					=		10  # defined by the RL environment, may be normalized
											_snake_case:   Optional[int]  					=		DecisionTransformerModel.from_pretrained('edbeeching/decision-transformer-gym-hopper-expert'       )
											_snake_case:   List[Any]  					=		model.to(__snake_case       )
											_snake_case:   Union[str, Any]  					=		model.config
											torch.manual_seed(0       )
											_snake_case:   List[Any]  					=		torch.randn(1       ,     1       ,     config.state_dim       ).to(device=__snake_case       ,     dtype=torch.floataa       )  # env.reset()

											_snake_case:   Tuple  					=		torch.tensor(
											    [[0.242_793, -0.28_693_074, 0.8_742_613], [0.67_815_274, -0.08_101_085, -0.12_952_147]]       ,     device=__snake_case       )

											_snake_case:   int  					=		torch.tensor(__snake_case       ,     device=__snake_case       ,     dtype=torch.floataa       ).reshape(1       ,     1       ,     1       )
											_snake_case:   List[Any]  					=		state
											_snake_case:   List[Any]  					=		torch.zeros(1       ,     0       ,     config.act_dim       ,     device=__snake_case       ,     dtype=torch.floataa       )
											_snake_case:   Optional[Any]  					=		torch.zeros(1       ,     0       ,     device=__snake_case       ,     dtype=torch.floataa       )
											_snake_case:   Tuple  					=		torch.tensor(0       ,     device=__snake_case       ,     dtype=torch.long       ).reshape(1       ,     1       )

											for step in range(__snake_case       ):
																	_snake_case:   Optional[Any]  					=		torch.cat([actions, torch.zeros(1       ,     1       ,     config.act_dim       ,     device=__snake_case       )]       ,     dim=1       )
																	_snake_case:   Optional[int]  					=		torch.cat([rewards, torch.zeros(1       ,     1       ,     device=__snake_case       )]       ,     dim=1       )

																	_snake_case:   str  					=		torch.ones(1       ,     states.shape[1]       ).to(dtype=torch.long       ,     device=states.device       )

																	with torch.no_grad():
																							_snake_case					,						_snake_case					,						_snake_case:   Tuple  					=		model(
																							    states=__snake_case       ,     actions=__snake_case       ,     rewards=__snake_case       ,     returns_to_go=__snake_case       ,     timesteps=__snake_case       ,     attention_mask=__snake_case       ,     return_dict=__snake_case       ,     )

																	self.assertEqual(action_pred.shape       ,     actions.shape       )
																	self.assertTrue(torch.allclose(action_pred[0, -1]       ,     expected_outputs[step]       ,     atol=1e-4       )       )
																	_snake_case					,						_snake_case					,						_snake_case					,						_snake_case:   Dict  					=		(  # env.step(action)
																	    torch.randn(1       ,     1       ,     config.state_dim       ).to(device=__snake_case       ,     dtype=torch.floataa       ),
																	    1.0,
																	    False,
																	    {},
																	)

																	_snake_case:   Optional[Any]  					=		action_pred[0, -1]
																	_snake_case:   str  					=		torch.cat([states, state]       ,     dim=1       )
																	_snake_case:   List[str]  					=		returns_to_go[0, -1] - reward
																	_snake_case:   Union[str, Any]  					=		torch.cat([returns_to_go, pred_return.reshape(1       ,     1       ,     1       )]       ,     dim=1       )
																	_snake_case:   int  					=		torch.cat(
																	    [timesteps, torch.ones((1, 1)       ,     device=__snake_case       ,     dtype=torch.long       ) * (step + 1)]       ,     dim=1       )












'''simple docstring'''






from sklearn.metrics import matthews_corrcoef

import datasets


A   : Dict             =			'\nCompute the Matthews correlation coefficient (MCC)\n\nThe Matthews correlation coefficient is used in machine learning as a\nmeasure of the quality of binary and multiclass classifications. It takes\ninto account true and false positives and negatives and is generally\nregarded as a balanced measure which can be used even if the classes are of\nvery different sizes. The MCC is in essence a correlation coefficient value\nbetween -1 and +1. A coefficient of +1 represents a perfect prediction, 0\nan average random prediction and -1 an inverse prediction.  The statistic\nis also known as the phi coefficient. [source: Wikipedia]\n'

A   : int             =			'\nArgs:\n    predictions (list of int): Predicted labels, as returned by a model.\n    references (list of int): Ground truth labels.\n    sample_weight (list of int, float, or bool): Sample weights. Defaults to `None`.\nReturns:\n    matthews_correlation (dict containing float): Matthews correlation.\nExamples:\n    Example 1, a basic example with only predictions and references as inputs:\n        >>> matthews_metric = datasets.load_metric("matthews_correlation")\n        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n        ...                                     predictions=[1, 2, 2, 0, 3, 3])\n        >>> print(round(results[\'matthews_correlation\'], 2))\n        0.54\n\n    Example 2, the same example as above, but also including sample weights:\n        >>> matthews_metric = datasets.load_metric("matthews_correlation")\n        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n        ...                                     predictions=[1, 2, 2, 0, 3, 3],\n        ...                                     sample_weight=[0.5, 3, 1, 1, 1, 2])\n        >>> print(round(results[\'matthews_correlation\'], 2))\n        0.1\n\n    Example 3, the same example as above, but with sample weights that cause a negative correlation:\n        >>> matthews_metric = datasets.load_metric("matthews_correlation")\n        >>> results = matthews_metric.compute(references=[1, 3, 2, 0, 3, 2],\n        ...                                     predictions=[1, 2, 2, 0, 3, 3],\n        ...                                     sample_weight=[0.5, 1, 0, 0, 0, 1])\n        >>> print(round(results[\'matthews_correlation\'], 2))\n        -0.25\n'

A   : Dict             =			'\\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 			lowerCamelCase					(						datasets.Metric  ):


					def    SCREAMING_SNAKE_CASE_		(					self	:  List[Any]       ):





											'''simple docstring'''





											return datasets.MetricInfo(
											    description=_DESCRIPTION       ,     citation=_CITATION       ,     inputs_description=_KWARGS_DESCRIPTION       ,     features=datasets.Features(
											        {
											            'predictions': datasets.Value('int32'       ),
											            'references': datasets.Value('int32'       ),
											        }       )       ,     reference_urls=[
											        'https://scikit-learn.org/stable/modules/generated/sklearn.metrics.matthews_corrcoef.html'
											    ]       ,     )




					def    SCREAMING_SNAKE_CASE_		(					self	:  Optional[Any]       ,     __snake_case	:  int       ,     __snake_case	:  List[Any]       ,     __snake_case	:  Union[str, Any]=None       ):





											'''simple docstring'''





											return {
											    "matthews_correlation": float(matthews_corrcoef(__snake_case       ,     __snake_case       ,     sample_weight=__snake_case       )       ),
											}





