



'''simple docstring'''

import unittest

from transformers import DebertaVaConfig, 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
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    DebertaVaForMaskedLM,
		    DebertaVaForMultipleChoice,
		    DebertaVaForQuestionAnswering,
		    DebertaVaForSequenceClassification,
		    DebertaVaForTokenClassification,
		    DebertaVaModel,
		)
		from transformers.models.deberta_va.modeling_deberta_va import DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST



class 		lowerCamelCase__      (						__lowerCAmelCase     ):




					'''simple docstring'''



					def __init__(  self				:	Tuple     ,  UpperCamelCase_				:	Tuple     ,  UpperCamelCase_				:	Any=13     ,  UpperCamelCase_				:	List[Any]=7     ,  UpperCamelCase_				:	Any=True     ,  UpperCamelCase_				:	Optional[Any]=True     ,  UpperCamelCase_				:	Any=True     ,  UpperCamelCase_				:	Any=True     ,  UpperCamelCase_				:	Any=99     ,  UpperCamelCase_				:	List[str]=32     ,  UpperCamelCase_				:	Optional[int]=5     ,  UpperCamelCase_				:	List[Any]=4     ,  UpperCamelCase_				:	Tuple=37     ,  UpperCamelCase_				:	Dict="gelu"     ,  UpperCamelCase_				:	List[str]=0.1     ,  UpperCamelCase_				:	str=0.1     ,  UpperCamelCase_				:	Union[str, Any]=512     ,  UpperCamelCase_				:	List[Any]=16     ,  UpperCamelCase_				:	str=2     ,  UpperCamelCase_				:	Optional[Any]=0.02     ,  UpperCamelCase_				:	int=False     ,  UpperCamelCase_				:	List[Any]=True     ,  UpperCamelCase_				:	Optional[int]="None"     ,  UpperCamelCase_				:	Optional[Any]=3     ,  UpperCamelCase_				:	Optional[Any]=4     ,  UpperCamelCase_				:	Dict=None     ,  )  ->       Union[str, Any]:



						'''simple docstring'''





						_lowercase					:       Any       =		parent
						_lowercase					:       str       =		batch_size
						_lowercase					:       List[Any]       =		seq_length
						_lowercase					:       Union[str, Any]       =		is_training
						_lowercase					:       List[str]       =		use_input_mask
						_lowercase					:       Optional[Any]       =		use_token_type_ids
						_lowercase					:       int       =		use_labels
						_lowercase					:       int       =		vocab_size
						_lowercase					:       Any       =		hidden_size
						_lowercase					:       List[Any]       =		num_hidden_layers
						_lowercase					:       Any       =		num_attention_heads
						_lowercase					:       Any       =		intermediate_size
						_lowercase					:       int       =		hidden_act
						_lowercase					:       int       =		hidden_dropout_prob
						_lowercase					:       List[str]       =		attention_probs_dropout_prob
						_lowercase					:       str       =		max_position_embeddings
						_lowercase					:       int       =		type_vocab_size
						_lowercase					:       Any       =		type_sequence_label_size
						_lowercase					:       Union[str, Any]       =		initializer_range
						_lowercase					:       str       =		num_labels
						_lowercase					:       int       =		num_choices
						_lowercase					:       List[Any]       =		relative_attention
						_lowercase					:       Any       =		position_biased_input
						_lowercase					:       Any       =		pos_att_type
						_lowercase					:       Dict       =		scope




					def 						__UpperCAmelCase     (  self				:	Any					)  ->       Dict:



						'''simple docstring'''





						_lowercase					:       Optional[int]       =		ids_tensor([self.batch_size, self.seq_length]     ,  self.vocab_size					)

						_lowercase					:       Any       =		None
						if self.use_input_mask:
							_lowercase					:       Optional[Any]       =		ids_tensor([self.batch_size, self.seq_length]     ,  vocab_size=2					)

						_lowercase					:       Optional[Any]       =		None
						if self.use_token_type_ids:
							_lowercase					:       Dict       =		ids_tensor([self.batch_size, self.seq_length]     ,  self.type_vocab_size					)

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

						_lowercase					:       Tuple       =		self.get_config()

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




					def 						__UpperCAmelCase     (  self				:	Tuple					)  ->       Optional[int]:



						'''simple docstring'''





						return DebertaVaConfig(
						    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     ,  hidden_dropout_prob=self.hidden_dropout_prob     ,  attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,  max_position_embeddings=self.max_position_embeddings     ,  type_vocab_size=self.type_vocab_size     ,  initializer_range=self.initializer_range     ,  relative_attention=self.relative_attention     ,  position_biased_input=self.position_biased_input     ,  pos_att_type=self.pos_att_type     ,  )




					def 						__UpperCAmelCase     (  self				:	Dict     ,  UpperCamelCase_				:	Any					)  ->       Any:



						'''simple docstring'''





						self.parent.assertListEqual(list(result.loss.size()					)     ,  []					)




					def 						__UpperCAmelCase     (  self				:	str     ,  UpperCamelCase_				:	Union[str, Any]     ,  UpperCamelCase_				:	Optional[int]     ,  UpperCamelCase_				:	List[Any]     ,  UpperCamelCase_				:	Any     ,  UpperCamelCase_				:	Optional[Any]     ,  UpperCamelCase_				:	Any     ,  UpperCamelCase_				:	Optional[int]					)  ->       List[Any]:



						'''simple docstring'''





						_lowercase					:       Any       =		DebertaVaModel(config=lowerCAmelCase_					)
						model.to(lowerCAmelCase_					)
						model.eval()
						_lowercase					:       List[str]       =		model(lowerCAmelCase_     ,  attention_mask=lowerCAmelCase_     ,  token_type_ids=lowerCAmelCase_					)[0]
						_lowercase					:       Any       =		model(lowerCAmelCase_     ,  token_type_ids=lowerCAmelCase_					)[0]
						_lowercase					:       Optional[Any]       =		model(lowerCAmelCase_					)[0]

						self.parent.assertListEqual(list(sequence_output.size()					)     ,  [self.batch_size, self.seq_length, self.hidden_size]					)




					def 						__UpperCAmelCase     (  self				:	Optional[Any]     ,  UpperCamelCase_				:	Union[str, Any]     ,  UpperCamelCase_				:	Any     ,  UpperCamelCase_				:	Dict     ,  UpperCamelCase_				:	List[str]     ,  UpperCamelCase_				:	Tuple     ,  UpperCamelCase_				:	List[str]     ,  UpperCamelCase_				:	Tuple					)  ->       List[str]:



						'''simple docstring'''





						_lowercase					:       Optional[Any]       =		DebertaVaForMaskedLM(config=lowerCAmelCase_					)
						model.to(lowerCAmelCase_					)
						model.eval()
						_lowercase					:       Optional[int]       =		model(lowerCAmelCase_     ,  attention_mask=lowerCAmelCase_     ,  token_type_ids=lowerCAmelCase_     ,  labels=lowerCAmelCase_					)

						self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.seq_length, self.vocab_size)					)




					def 						__UpperCAmelCase     (  self				:	Optional[int]     ,  UpperCamelCase_				:	Union[str, Any]     ,  UpperCamelCase_				:	Union[str, Any]     ,  UpperCamelCase_				:	Any     ,  UpperCamelCase_				:	List[Any]     ,  UpperCamelCase_				:	Dict     ,  UpperCamelCase_				:	Optional[int]     ,  UpperCamelCase_				:	Union[str, Any]					)  ->       Any:



						'''simple docstring'''





						_lowercase					:       Optional[int]       =		self.num_labels
						_lowercase					:       Dict       =		DebertaVaForSequenceClassification(lowerCAmelCase_					)
						model.to(lowerCAmelCase_					)
						model.eval()
						_lowercase					:       Optional[int]       =		model(lowerCAmelCase_     ,  attention_mask=lowerCAmelCase_     ,  token_type_ids=lowerCAmelCase_     ,  labels=lowerCAmelCase_					)
						self.parent.assertListEqual(list(result.logits.size()					)     ,  [self.batch_size, self.num_labels]					)
						self.check_loss_output(lowerCAmelCase_					)




					def 						__UpperCAmelCase     (  self				:	Union[str, Any]     ,  UpperCamelCase_				:	Union[str, Any]     ,  UpperCamelCase_				:	Dict     ,  UpperCamelCase_				:	str     ,  UpperCamelCase_				:	Optional[int]     ,  UpperCamelCase_				:	List[Any]     ,  UpperCamelCase_				:	Optional[int]     ,  UpperCamelCase_				:	Optional[Any]					)  ->       str:



						'''simple docstring'''





						_lowercase					:       Optional[Any]       =		self.num_labels
						_lowercase					:       Dict       =		DebertaVaForTokenClassification(config=lowerCAmelCase_					)
						model.to(lowerCAmelCase_					)
						model.eval()
						_lowercase					:       Union[str, Any]       =		model(lowerCAmelCase_     ,  attention_mask=lowerCAmelCase_     ,  token_type_ids=lowerCAmelCase_     ,  labels=lowerCAmelCase_					)
						self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.seq_length, self.num_labels)					)




					def 						__UpperCAmelCase     (  self				:	Tuple     ,  UpperCamelCase_				:	List[Any]     ,  UpperCamelCase_				:	Optional[int]     ,  UpperCamelCase_				:	Optional[int]     ,  UpperCamelCase_				:	Union[str, Any]     ,  UpperCamelCase_				:	List[str]     ,  UpperCamelCase_				:	int     ,  UpperCamelCase_				:	Optional[Any]					)  ->       Optional[int]:



						'''simple docstring'''





						_lowercase					:       int       =		DebertaVaForQuestionAnswering(config=lowerCAmelCase_					)
						model.to(lowerCAmelCase_					)
						model.eval()
						_lowercase					:       Optional[Any]       =		model(
						    lowerCAmelCase_     ,  attention_mask=lowerCAmelCase_     ,  token_type_ids=lowerCAmelCase_     ,  start_positions=lowerCAmelCase_     ,  end_positions=lowerCAmelCase_     ,  )
						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 						__UpperCAmelCase     (  self				:	Tuple     ,  UpperCamelCase_				:	Union[str, Any]     ,  UpperCamelCase_				:	List[str]     ,  UpperCamelCase_				:	str     ,  UpperCamelCase_				:	Tuple     ,  UpperCamelCase_				:	Dict     ,  UpperCamelCase_				:	Dict     ,  UpperCamelCase_				:	Any					)  ->       str:



						'''simple docstring'''





						_lowercase					:       Optional[Any]       =		DebertaVaForMultipleChoice(config=lowerCAmelCase_					)
						model.to(lowerCAmelCase_					)
						model.eval()
						_lowercase					:       Any       =		input_ids.unsqueeze(1					).expand(-1     ,  self.num_choices     ,  -1					).contiguous()
						_lowercase					:       str       =		token_type_ids.unsqueeze(1					).expand(-1     ,  self.num_choices     ,  -1					).contiguous()
						_lowercase					:       List[str]       =		input_mask.unsqueeze(1					).expand(-1     ,  self.num_choices     ,  -1					).contiguous()
						_lowercase					:       Union[str, Any]       =		model(
						    lowerCAmelCase_     ,  attention_mask=lowerCAmelCase_     ,  token_type_ids=lowerCAmelCase_     ,  labels=lowerCAmelCase_     ,  )
						self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.num_choices)					)




					def 						__UpperCAmelCase     (  self				:	Any					)  ->       Optional[int]:



						'''simple docstring'''





						_lowercase					:       Union[str, Any]       =		self.prepare_config_and_inputs()
						(
						    (
						    _lowercase						
						)  ,    (
						    _lowercase						
						)  ,    (
						    _lowercase						
						)  ,    (
						    _lowercase						
						)  ,    (
						    _lowercase						
						)  ,    (
						    _lowercase						
						)  ,    (
						    _lowercase						
						)  ,    						
						)					:       Optional[int]       =		config_and_inputs
						_lowercase					:       Optional[int]       =		{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
						return config, inputs_dict






@require_torch
class 		lowerCamelCase__      (						__lowerCAmelCase	,   __lowerCAmelCase	,   unittest.TestCase     ):




					'''simple docstring'''



					A_						    =						(
					    (
					        DebertaVaModel,
					        DebertaVaForMaskedLM,
					        DebertaVaForSequenceClassification,
					        DebertaVaForTokenClassification,
					        DebertaVaForQuestionAnswering,
					        DebertaVaForMultipleChoice,
					    )
					    if is_torch_available()
					    else ()
					)
					A_						    =						(
					    {
					        '''feature-extraction''': DebertaVaModel,
					        '''fill-mask''': DebertaVaForMaskedLM,
					        '''question-answering''': DebertaVaForQuestionAnswering,
					        '''text-classification''': DebertaVaForSequenceClassification,
					        '''token-classification''': DebertaVaForTokenClassification,
					        '''zero-shot''': DebertaVaForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)

					A_						    =						True
					A_						    =						False
					A_						    =						False
					A_						    =						False
					A_						    =						False




					def 						__UpperCAmelCase     (  self				:	Union[str, Any]					)  ->       Optional[int]:



						'''simple docstring'''





						_lowercase					:       Tuple       =		DebertaVaModelTester(self					)
						_lowercase					:       Any       =		ConfigTester(self     ,  config_class=lowerCAmelCase_     ,  hidden_size=37					)




					def 						__UpperCAmelCase     (  self				:	Optional[Any]					)  ->       List[str]:



						'''simple docstring'''





						self.config_tester.run_common_tests()




					def 						__UpperCAmelCase     (  self				:	int					)  ->       List[str]:



						'''simple docstring'''





						_lowercase					:       Any       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_model(*lowerCAmelCase_					)




					def 						__UpperCAmelCase     (  self				:	Dict					)  ->       Dict:



						'''simple docstring'''





						_lowercase					:       Any       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_sequence_classification(*lowerCAmelCase_					)




					def 						__UpperCAmelCase     (  self				:	Optional[int]					)  ->       Any:



						'''simple docstring'''





						_lowercase					:       str       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_masked_lm(*lowerCAmelCase_					)




					def 						__UpperCAmelCase     (  self				:	Tuple					)  ->       int:



						'''simple docstring'''





						_lowercase					:       Optional[Any]       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_question_answering(*lowerCAmelCase_					)




					def 						__UpperCAmelCase     (  self				:	int					)  ->       Dict:



						'''simple docstring'''





						_lowercase					:       Any       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_token_classification(*lowerCAmelCase_					)




					def 						__UpperCAmelCase     (  self				:	Optional[Any]					)  ->       Optional[int]:



						'''simple docstring'''





						_lowercase					:       Optional[Any]       =		self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_deberta_for_multiple_choice(*lowerCAmelCase_					)




					@slow
					def 						__UpperCAmelCase     (  self				:	Dict					)  ->       Any:



						'''simple docstring'''





						for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
							_lowercase					:       Optional[int]       =		DebertaVaModel.from_pretrained(lowerCAmelCase_					)
							self.assertIsNotNone(lowerCAmelCase_					)






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




					'''simple docstring'''



					@unittest.skip(reason='Model not available yet'					)
					def 						__UpperCAmelCase     (  self				:	Any					)  ->       int:



						'''simple docstring'''





						pass




					@slow
					def 						__UpperCAmelCase     (  self				:	int					)  ->       Optional[Any]:



						'''simple docstring'''





						_lowercase					:       Dict       =		DebertaVaModel.from_pretrained('microsoft/deberta-v2-xlarge'					)

						_lowercase					:       Optional[int]       =		torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]					)
						_lowercase					:       Union[str, Any]       =		torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]					)
						with torch.no_grad():
							_lowercase					:       int       =		model(lowerCAmelCase_     ,  attention_mask=lowerCAmelCase_					)[0]
						# compare the actual values for a slice.
						_lowercase					:       str       =		torch.tensor(
						    [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]]					)
						self.assertTrue(torch.allclose(output[:, 1:4, 1:4]     ,  lowerCAmelCase_     ,  atol=1E-4					)     ,  F'''{output[:, 1:4, 1:4]}'''					)





'''simple docstring'''

import unittest

from knapsack import greedy_knapsack as kp



class 		lowerCamelCase__      (						unittest.TestCase     ):




					'''simple docstring'''



					def 						__UpperCAmelCase     (  self				:	int					)  ->       Any:



						'''simple docstring'''





						_lowercase					:       List[Any]       =		[10, 20, 30, 40, 50, 60]
						_lowercase					:       Tuple       =		[2, 4, 6, 8, 10, 12]
						_lowercase					:       Optional[Any]       =		100
						self.assertEqual(kp.calc_profit(UpperCamelCase_     ,  UpperCamelCase_     ,  UpperCamelCase_					)     ,  210					)




					def 						__UpperCAmelCase     (  self				:	int					)  ->       int:



						'''simple docstring'''





						self.assertRaisesRegex(UpperCamelCase_     ,  'max_weight must greater than zero.'					)




					def 						__UpperCAmelCase     (  self				:	Dict					)  ->       Optional[Any]:



						'''simple docstring'''





						self.assertRaisesRegex(UpperCamelCase_     ,  'Weight can not be negative.'					)




					def 						__UpperCAmelCase     (  self				:	List[Any]					)  ->       Optional[Any]:



						'''simple docstring'''





						self.assertRaisesRegex(UpperCamelCase_     ,  'Profit can not be negative.'					)




					def 						__UpperCAmelCase     (  self				:	int					)  ->       List[str]:



						'''simple docstring'''





						self.assertRaisesRegex(UpperCamelCase_     ,  'max_weight must greater than zero.'					)




					def 						__UpperCAmelCase     (  self				:	int					)  ->       List[Any]:



						'''simple docstring'''





						self.assertRaisesRegex(
						    UpperCamelCase_     ,  'The length of profit and weight must be same.'					)


if __name__ == "__main__":
		unittest.main()
