





"""simple docstring"""






from ...configuration_utils import PretrainedConfig
from ...utils import logging


A	     =					logging.get_logger(__name__)

A	     =					{
    '''edbeeching/decision-transformer-gym-hopper-medium''': (
        '''https://huggingface.co/edbeeching/decision-transformer-gym-hopper-medium/resolve/main/config.json'''
    ),
    # See all DecisionTransformer models at https://huggingface.co/models?filter=decision_transformer
}




class 			__lowercase     (     _UpperCamelCase							):







					'''simple docstring'''




					__lowerCAmelCase					=					'''decision_transformer'''
					__lowerCAmelCase					=					['''past_key_values''']
					__lowerCAmelCase					=					{
					    '''max_position_embeddings''': '''n_positions''',
					    '''num_attention_heads''': '''n_head''',
					    '''num_hidden_layers''': '''n_layer''',
					}







					def __init__(  self		,     _UpperCAmelCase=17		,     _UpperCAmelCase=4		,     _UpperCAmelCase=128		,     _UpperCAmelCase=4096		,     _UpperCAmelCase=True		,     _UpperCAmelCase=1		,     _UpperCAmelCase=1024		,     _UpperCAmelCase=3		,     _UpperCAmelCase=1		,     _UpperCAmelCase=None		,     _UpperCAmelCase="relu"		,     _UpperCAmelCase=0.1		,     _UpperCAmelCase=0.1		,     _UpperCAmelCase=0.1		,     _UpperCAmelCase=1e-5		,     _UpperCAmelCase=0.0_2		,     _UpperCAmelCase=True		,     _UpperCAmelCase=True		,     _UpperCAmelCase=50256		,     _UpperCAmelCase=50256		,     _UpperCAmelCase=False		,     _UpperCAmelCase=False		,     **_UpperCAmelCase		,     ):
									__a	:  int         =					state_dim
									__a	:  Dict         =					act_dim
									__a	:  Optional[int]         =					hidden_size
									__a	:  Dict         =					max_ep_len
									__a	:  Any         =					action_tanh
									__a	:  Any         =					vocab_size
									__a	:  int         =					n_positions
									__a	:  Dict         =					n_layer
									__a	:  Optional[int]         =					n_head
									__a	:  List[str]         =					n_inner
									__a	:  Tuple         =					activation_function
									__a	:  Dict         =					resid_pdrop
									__a	:  Dict         =					embd_pdrop
									__a	:  Any         =					attn_pdrop
									__a	:  str         =					layer_norm_epsilon
									__a	:  Optional[Any]         =					initializer_range
									__a	:  Dict         =					scale_attn_weights
									__a	:  List[str]         =					use_cache
									__a	:  Any         =					scale_attn_by_inverse_layer_idx
									__a	:  Optional[Any]         =					reorder_and_upcast_attn

									__a	:  Tuple         =					bos_token_id
									__a	:  List[Any]         =					eos_token_id

									super().__init__(bos_token_id=_UpperCAmelCase		,     eos_token_id=_UpperCAmelCase		,     **_UpperCAmelCase       )






"""simple docstring"""






import unittest

from transformers import SqueezeBertConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

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 (
		    SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    SqueezeBertForMaskedLM,
		    SqueezeBertForMultipleChoice,
		    SqueezeBertForQuestionAnswering,
		    SqueezeBertForSequenceClassification,
		    SqueezeBertForTokenClassification,
		    SqueezeBertModel,
		)




class 			__lowercase     (     _UpperCamelCase							):







					'''simple docstring'''




					def __init__(  self		,     _UpperCAmelCase		,     _UpperCAmelCase=13		,     _UpperCAmelCase=7		,     _UpperCAmelCase=True		,     _UpperCAmelCase=True		,     _UpperCAmelCase=False		,     _UpperCAmelCase=True		,     _UpperCAmelCase=99		,     _UpperCAmelCase=32		,     _UpperCAmelCase=5		,     _UpperCAmelCase=4		,     _UpperCAmelCase=64		,     _UpperCAmelCase="gelu"		,     _UpperCAmelCase=0.1		,     _UpperCAmelCase=0.1		,     _UpperCAmelCase=512		,     _UpperCAmelCase=16		,     _UpperCAmelCase=2		,     _UpperCAmelCase=0.0_2		,     _UpperCAmelCase=3		,     _UpperCAmelCase=4		,     _UpperCAmelCase=None		,     _UpperCAmelCase=2		,     _UpperCAmelCase=2		,     _UpperCAmelCase=2		,     _UpperCAmelCase=2		,     _UpperCAmelCase=4		,     _UpperCAmelCase=1		,     ):
									__a	:  Dict         =					parent
									__a	:  str         =					batch_size
									__a	:  Union[str, Any]         =					seq_length
									__a	:  Any         =					is_training
									__a	:  int         =					use_input_mask
									__a	:  Optional[int]         =					use_token_type_ids
									__a	:  int         =					use_labels
									__a	:  int         =					vocab_size
									__a	:  int         =					hidden_size
									__a	:  str         =					num_hidden_layers
									__a	:  str         =					num_attention_heads
									__a	:  Any         =					intermediate_size
									__a	:  Union[str, Any]         =					hidden_act
									__a	:  Optional[int]         =					hidden_dropout_prob
									__a	:  str         =					attention_probs_dropout_prob
									__a	:  int         =					max_position_embeddings
									__a	:  Union[str, Any]         =					type_vocab_size
									__a	:  List[str]         =					type_sequence_label_size
									__a	:  List[str]         =					initializer_range
									__a	:  Optional[int]         =					num_labels
									__a	:  List[str]         =					num_choices
									__a	:  int         =					scope
									__a	:  Union[str, Any]         =					q_groups
									__a	:  Dict         =					k_groups
									__a	:  List[str]         =					v_groups
									__a	:  Any         =					post_attention_groups
									__a	:  Optional[int]         =					intermediate_groups
									__a	:  List[str]         =					output_groups







					def 	_lowerCamelCase						(  self       ):
									__a	:  int         =					ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size       )

									__a	:  Optional[Any]         =					None
									if self.use_input_mask:
													__a	:  int         =					random_attention_mask([self.batch_size, self.seq_length]       )

									__a	:  List[str]         =					None
									__a	:  Union[str, Any]         =					None
									__a	:  int         =					None
									if self.use_labels:
													__a	:  List[str]         =					ids_tensor([self.batch_size]		,     self.type_sequence_label_size       )
													__a	:  int         =					ids_tensor([self.batch_size, self.seq_length]		,     self.num_labels       )
													__a	:  Dict         =					ids_tensor([self.batch_size]		,     self.num_choices       )

									__a	:  int         =					self.get_config()

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







					def 	_lowerCamelCase						(  self       ):
									return SqueezeBertConfig(
									    embedding_size=self.hidden_size		,     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		,     attention_probs_dropout_prob=self.hidden_dropout_prob		,     attention_dropout=self.attention_probs_dropout_prob		,     max_position_embeddings=self.max_position_embeddings		,     initializer_range=self.initializer_range		,     q_groups=self.q_groups		,     k_groups=self.k_groups		,     v_groups=self.v_groups		,     post_attention_groups=self.post_attention_groups		,     intermediate_groups=self.intermediate_groups		,     output_groups=self.output_groups		,     )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  Dict         =					SqueezeBertModel(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  Optional[Any]         =					model(_UpperCAmelCase		,     _UpperCAmelCase       )
									__a	:  Dict         =					model(_UpperCAmelCase       )
									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       ):
									__a	:  Optional[Any]         =					SqueezeBertForMaskedLM(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  int         =					model(_UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     labels=_UpperCAmelCase       )
									self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.vocab_size)       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  int         =					SqueezeBertForQuestionAnswering(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  Optional[int]         =					model(
									    _UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     start_positions=_UpperCAmelCase		,     end_positions=_UpperCAmelCase       )
									self.parent.assertEqual(result.start_logits.shape		,     (self.batch_size, self.seq_length)       )
									self.parent.assertEqual(result.end_logits.shape		,     (self.batch_size, self.seq_length)       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  Any         =					self.num_labels
									__a	:  List[Any]         =					SqueezeBertForSequenceClassification(_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  int         =					model(_UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     labels=_UpperCAmelCase       )
									self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_labels)       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  List[Any]         =					self.num_labels
									__a	:  List[str]         =					SqueezeBertForTokenClassification(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()

									__a	:  Any         =					model(_UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     labels=_UpperCAmelCase       )
									self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.num_labels)       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  List[str]         =					self.num_choices
									__a	:  Union[str, Any]         =					SqueezeBertForMultipleChoice(config=_UpperCAmelCase       )
									model.to(_UpperCAmelCase       )
									model.eval()
									__a	:  int         =					input_ids.unsqueeze(1       ).expand(-1		,     self.num_choices		,     -1       ).contiguous()
									__a	:  Optional[Any]         =					input_mask.unsqueeze(1       ).expand(-1		,     self.num_choices		,     -1       ).contiguous()
									__a	:  Union[str, Any]         =					model(
									    _UpperCAmelCase		,     attention_mask=_UpperCAmelCase		,     labels=_UpperCAmelCase		,     )
									self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_choices)       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Any         =					self.prepare_config_and_inputs()
									((__a)    ,  (__a)    ,  (__a)    ,  (__a)    ,  (__a)    ,  (__a))	:  Optional[Any]         =					config_and_inputs
									__a	:  List[Any]         =					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
									return config, inputs_dict






@require_torch
class 			__lowercase     (     _UpperCamelCase				, _UpperCamelCase				, unittest.TestCase							):







					'''simple docstring'''




					__lowerCAmelCase					=					(
					    (
					        SqueezeBertModel,
					        SqueezeBertForMaskedLM,
					        SqueezeBertForMultipleChoice,
					        SqueezeBertForQuestionAnswering,
					        SqueezeBertForSequenceClassification,
					        SqueezeBertForTokenClassification,
					    )
					    if is_torch_available()
					    else None
					)
					__lowerCAmelCase					=					(
					    {
					        '''feature-extraction''': SqueezeBertModel,
					        '''fill-mask''': SqueezeBertForMaskedLM,
					        '''question-answering''': SqueezeBertForQuestionAnswering,
					        '''text-classification''': SqueezeBertForSequenceClassification,
					        '''token-classification''': SqueezeBertForTokenClassification,
					        '''zero-shot''': SqueezeBertForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					__lowerCAmelCase					=					False
					__lowerCAmelCase					=					True
					__lowerCAmelCase					=					False







					def 	_lowerCamelCase						(  self       ):
									__a	:  Union[str, Any]         =					SqueezeBertModelTester(self       )
									__a	:  Dict         =					ConfigTester(self		,     config_class=_UpperCAmelCase		,     dim=37       )







					def 	_lowerCamelCase						(  self       ):
									self.config_tester.run_common_tests()







					def 	_lowerCamelCase						(  self       ):
									__a	:  Tuple         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_squeezebert_model(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  List[Any]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_squeezebert_for_masked_lm(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  str         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_squeezebert_for_question_answering(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[Any]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_squeezebert_for_sequence_classification(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[int]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_squeezebert_for_token_classification(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  List[str]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_squeezebert_for_multiple_choice(*_UpperCAmelCase       )







					@slow
					def 	_lowerCamelCase						(  self       ):
									for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													__a	:  Any         =					SqueezeBertModel.from_pretrained(_UpperCAmelCase       )
													self.assertIsNotNone(_UpperCAmelCase       )






@require_sentencepiece
@require_tokenizers
@require_torch
class 			__lowercase     (     unittest.TestCase							):







					'''simple docstring'''




					@slow
					def 	_lowerCamelCase						(  self       ):
									__a	:  int         =					SqueezeBertForSequenceClassification.from_pretrained('''squeezebert/squeezebert-mnli'''       )

									__a	:  Tuple         =					torch.tensor([[1, 29414, 232, 328, 740, 1140, 12695, 69, 13, 1588, 2]]       )
									__a	:  List[str]         =					model(_UpperCAmelCase       )[0]
									__a	:  int         =					torch.Size((1, 3)       )
									self.assertEqual(output.shape		,     _UpperCAmelCase       )
									__a	:  int         =					torch.tensor([[0.6_4_0_1, -0.0_3_4_9, -0.6_0_4_1]]       )
									self.assertTrue(torch.allclose(_UpperCAmelCase		,     _UpperCAmelCase		,     atol=1e-4       )       )