

import unittest

from transformers import YosoConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

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 (
	    YosoForMaskedLM,
	    YosoForMultipleChoice,
	    YosoForQuestionAnswering,
	    YosoForSequenceClassification,
	    YosoForTokenClassification,
	    YosoModel,
	)
	from transformers.models.yoso.modeling_yoso import YOSO_PRETRAINED_MODEL_ARCHIVE_LIST


class 			_SCREAMING_SNAKE_CASE      :







						def __init__(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE=13     ,						_SCREAMING_SNAKE_CASE=7     ,						_SCREAMING_SNAKE_CASE=True     ,						_SCREAMING_SNAKE_CASE=True     ,						_SCREAMING_SNAKE_CASE=True     ,						_SCREAMING_SNAKE_CASE=True     ,						_SCREAMING_SNAKE_CASE=99     ,						_SCREAMING_SNAKE_CASE=32     ,						_SCREAMING_SNAKE_CASE=5     ,						_SCREAMING_SNAKE_CASE=4     ,						_SCREAMING_SNAKE_CASE=37     ,						_SCREAMING_SNAKE_CASE="gelu"     ,						_SCREAMING_SNAKE_CASE=0.1     ,						_SCREAMING_SNAKE_CASE=0.1     ,						_SCREAMING_SNAKE_CASE=512     ,						_SCREAMING_SNAKE_CASE=16     ,						_SCREAMING_SNAKE_CASE=2     ,						_SCREAMING_SNAKE_CASE=0.0_2     ,						_SCREAMING_SNAKE_CASE=3     ,						_SCREAMING_SNAKE_CASE=4     ,						_SCREAMING_SNAKE_CASE=None     ,						)->							Dict:
													lowerCamelCase_								=parent
													lowerCamelCase_								=batch_size
													lowerCamelCase_								=seq_length
													lowerCamelCase_								=is_training
													lowerCamelCase_								=use_input_mask
													lowerCamelCase_								=use_token_type_ids
													lowerCamelCase_								=use_labels
													lowerCamelCase_								=vocab_size
													lowerCamelCase_								=hidden_size
													lowerCamelCase_								=num_hidden_layers
													lowerCamelCase_								=num_attention_heads
													lowerCamelCase_								=intermediate_size
													lowerCamelCase_								=hidden_act
													lowerCamelCase_								=hidden_dropout_prob
													lowerCamelCase_								=attention_probs_dropout_prob
													lowerCamelCase_								=max_position_embeddings
													lowerCamelCase_								=type_vocab_size
													lowerCamelCase_								=type_sequence_label_size
													lowerCamelCase_								=initializer_range
													lowerCamelCase_								=num_labels
													lowerCamelCase_								=num_choices
													lowerCamelCase_								=scope







						def        _snake_case				(			self							)->							List[str]:
													lowerCamelCase_								=ids_tensor([self.batch_size, self.seq_length]     ,						self.vocab_size							)

													lowerCamelCase_								=None
													if self.use_input_mask:
																				lowerCamelCase_								=random_attention_mask([self.batch_size, self.seq_length]							)

													lowerCamelCase_								=None
													if self.use_token_type_ids:
																				lowerCamelCase_								=ids_tensor([self.batch_size, self.seq_length]     ,						self.type_vocab_size							)

													lowerCamelCase_								=None
													lowerCamelCase_								=None
													lowerCamelCase_								=None
													if self.use_labels:
																				lowerCamelCase_								=ids_tensor([self.batch_size]     ,						self.type_sequence_label_size							)
																				lowerCamelCase_								=ids_tensor([self.batch_size, self.seq_length]     ,						self.num_labels							)
																				lowerCamelCase_								=ids_tensor([self.batch_size]     ,						self.num_choices							)

													lowerCamelCase_								=self.get_config()

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







						def        _snake_case				(			self							)->							int:
													return YosoConfig(
													    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     ,						is_decoder=_SCREAMING_SNAKE_CASE     ,						initializer_range=self.initializer_range     ,						)







						def        _snake_case				(			self							)->							Tuple:
													lowerCamelCase_								=self.get_config()
													lowerCamelCase_								=300
													return config







						def        _snake_case				(			self							)->							str:
													(
													    (
													    lowerCamelCase_
													)	,   (
													    lowerCamelCase_
													)	,   (
													    lowerCamelCase_
													)	,   (
													    lowerCamelCase_
													)	,   (
													    lowerCamelCase_
													)	,   (
													    lowerCamelCase_
													)	,   (
													    lowerCamelCase_
													)	,   
													)								=self.prepare_config_and_inputs()

													lowerCamelCase_								=True
													lowerCamelCase_								=floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
													lowerCamelCase_								=ids_tensor([self.batch_size, self.seq_length]     ,						vocab_size=2							)

													return (
													    config,
													    input_ids,
													    token_type_ids,
													    input_mask,
													    sequence_labels,
													    token_labels,
													    choice_labels,
													    encoder_hidden_states,
													    encoder_attention_mask,
													)







						def        _snake_case				(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE							)->							Tuple:
													lowerCamelCase_								=YosoModel(config=_SCREAMING_SNAKE_CASE							)
													model.to(_SCREAMING_SNAKE_CASE							)
													model.eval()
													lowerCamelCase_								=model(_SCREAMING_SNAKE_CASE     ,						attention_mask=_SCREAMING_SNAKE_CASE     ,						token_type_ids=_SCREAMING_SNAKE_CASE							)
													lowerCamelCase_								=model(_SCREAMING_SNAKE_CASE     ,						token_type_ids=_SCREAMING_SNAKE_CASE							)
													lowerCamelCase_								=model(_SCREAMING_SNAKE_CASE							)
													self.parent.assertEqual(result.last_hidden_state.shape     ,						(self.batch_size, self.seq_length, self.hidden_size)							)







						def        _snake_case				(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						)->							List[Any]:
													lowerCamelCase_								=True
													lowerCamelCase_								=YosoModel(_SCREAMING_SNAKE_CASE							)
													model.to(_SCREAMING_SNAKE_CASE							)
													model.eval()
													lowerCamelCase_								=model(
													    _SCREAMING_SNAKE_CASE     ,						attention_mask=_SCREAMING_SNAKE_CASE     ,						token_type_ids=_SCREAMING_SNAKE_CASE     ,						encoder_hidden_states=_SCREAMING_SNAKE_CASE     ,						encoder_attention_mask=_SCREAMING_SNAKE_CASE     ,						)
													lowerCamelCase_								=model(
													    _SCREAMING_SNAKE_CASE     ,						attention_mask=_SCREAMING_SNAKE_CASE     ,						token_type_ids=_SCREAMING_SNAKE_CASE     ,						encoder_hidden_states=_SCREAMING_SNAKE_CASE     ,						)
													lowerCamelCase_								=model(_SCREAMING_SNAKE_CASE     ,						attention_mask=_SCREAMING_SNAKE_CASE     ,						token_type_ids=_SCREAMING_SNAKE_CASE							)
													self.parent.assertEqual(result.last_hidden_state.shape     ,						(self.batch_size, self.seq_length, self.hidden_size)							)







						def        _snake_case				(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE							)->							List[str]:
													lowerCamelCase_								=YosoForMaskedLM(config=_SCREAMING_SNAKE_CASE							)
													model.to(_SCREAMING_SNAKE_CASE							)
													model.eval()
													lowerCamelCase_								=model(_SCREAMING_SNAKE_CASE     ,						attention_mask=_SCREAMING_SNAKE_CASE     ,						token_type_ids=_SCREAMING_SNAKE_CASE     ,						labels=_SCREAMING_SNAKE_CASE							)
													self.parent.assertEqual(result.logits.shape     ,						(self.batch_size, self.seq_length, self.vocab_size)							)







						def        _snake_case				(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE							)->							Tuple:
													lowerCamelCase_								=YosoForQuestionAnswering(config=_SCREAMING_SNAKE_CASE							)
													model.to(_SCREAMING_SNAKE_CASE							)
													model.eval()
													lowerCamelCase_								=model(
													    _SCREAMING_SNAKE_CASE     ,						attention_mask=_SCREAMING_SNAKE_CASE     ,						token_type_ids=_SCREAMING_SNAKE_CASE     ,						start_positions=_SCREAMING_SNAKE_CASE     ,						end_positions=_SCREAMING_SNAKE_CASE     ,						)
													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        _snake_case				(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE							)->							str:
													lowerCamelCase_								=self.num_labels
													lowerCamelCase_								=YosoForSequenceClassification(_SCREAMING_SNAKE_CASE							)
													model.to(_SCREAMING_SNAKE_CASE							)
													model.eval()
													lowerCamelCase_								=model(_SCREAMING_SNAKE_CASE     ,						attention_mask=_SCREAMING_SNAKE_CASE     ,						token_type_ids=_SCREAMING_SNAKE_CASE     ,						labels=_SCREAMING_SNAKE_CASE							)
													self.parent.assertEqual(result.logits.shape     ,						(self.batch_size, self.num_labels)							)







						def        _snake_case				(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE							)->							str:
													lowerCamelCase_								=self.num_labels
													lowerCamelCase_								=YosoForTokenClassification(config=_SCREAMING_SNAKE_CASE							)
													model.to(_SCREAMING_SNAKE_CASE							)
													model.eval()
													lowerCamelCase_								=model(_SCREAMING_SNAKE_CASE     ,						attention_mask=_SCREAMING_SNAKE_CASE     ,						token_type_ids=_SCREAMING_SNAKE_CASE     ,						labels=_SCREAMING_SNAKE_CASE							)
													self.parent.assertEqual(result.logits.shape     ,						(self.batch_size, self.seq_length, self.num_labels)							)







						def        _snake_case				(			self     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE     ,						_SCREAMING_SNAKE_CASE							)->							List[str]:
													lowerCamelCase_								=self.num_choices
													lowerCamelCase_								=YosoForMultipleChoice(config=_SCREAMING_SNAKE_CASE							)
													model.to(_SCREAMING_SNAKE_CASE							)
													model.eval()
													lowerCamelCase_								=input_ids.unsqueeze(1							).expand(-1     ,						self.num_choices     ,						-1							).contiguous()
													lowerCamelCase_								=token_type_ids.unsqueeze(1							).expand(-1     ,						self.num_choices     ,						-1							).contiguous()
													lowerCamelCase_								=input_mask.unsqueeze(1							).expand(-1     ,						self.num_choices     ,						-1							).contiguous()
													lowerCamelCase_								=model(
													    _SCREAMING_SNAKE_CASE     ,						attention_mask=_SCREAMING_SNAKE_CASE     ,						token_type_ids=_SCREAMING_SNAKE_CASE     ,						labels=_SCREAMING_SNAKE_CASE     ,						)
													self.parent.assertEqual(result.logits.shape     ,						(self.batch_size, self.num_choices)							)







						def        _snake_case				(			self							)->							Tuple:
													lowerCamelCase_								=self.prepare_config_and_inputs()
													(
													    (
													    lowerCamelCase_
													)	,   (
													    lowerCamelCase_
													)	,   (
													    lowerCamelCase_
													)	,   (
													    lowerCamelCase_
													)	,   (
													    lowerCamelCase_
													)	,   (
													    lowerCamelCase_
													)	,   (
													    lowerCamelCase_
													)	,   
													)								=config_and_inputs
													lowerCamelCase_								={"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
													return config, inputs_dict


@require_torch
class 			_SCREAMING_SNAKE_CASE      (  lowerCAmelCase__ ,    lowerCAmelCase__ ,    unittest.TestCase):
						_UpperCamelCase:str	=		(
						    (
						        YosoModel,
						        YosoForMaskedLM,
						        YosoForMultipleChoice,
						        YosoForQuestionAnswering,
						        YosoForSequenceClassification,
						        YosoForTokenClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						_UpperCamelCase:int	=		False
						_UpperCamelCase:Tuple	=		False
						_UpperCamelCase:Tuple	=		False

						_UpperCamelCase:int	=		()
						_UpperCamelCase:Union[str, Any]	=		(
						    {
						        "feature-extraction": YosoModel,
						        "fill-mask": YosoForMaskedLM,
						        "question-answering": YosoForQuestionAnswering,
						        "text-classification": YosoForSequenceClassification,
						        "token-classification": YosoForTokenClassification,
						        "zero-shot": YosoForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)







						def        _snake_case				(			self							)->							Union[str, Any]:
													lowerCamelCase_								=YosoModelTester(self							)
													lowerCamelCase_								=ConfigTester(self     ,						config_class=_SCREAMING_SNAKE_CASE     ,						hidden_size=37							)







						def        _snake_case				(			self							)->							Optional[Any]:
													self.config_tester.run_common_tests()







						def        _snake_case				(			self							)->							str:
													lowerCamelCase_								=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE							)







						def        _snake_case				(			self							)->							List[Any]:
													lowerCamelCase_								=self.model_tester.prepare_config_and_inputs()
													for type in ["absolute", "relative_key", "relative_key_query"]:
																				lowerCamelCase_								=type
																				self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE							)







						def        _snake_case				(			self							)->							List[str]:
													lowerCamelCase_								=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE							)







						def        _snake_case				(			self							)->							Optional[int]:
													lowerCamelCase_								=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_multiple_choice(*_SCREAMING_SNAKE_CASE							)







						def        _snake_case				(			self							)->							List[str]:
													lowerCamelCase_								=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE							)







						def        _snake_case				(			self							)->							Union[str, Any]:
													lowerCamelCase_								=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE							)







						def        _snake_case				(			self							)->							Dict:
													lowerCamelCase_								=self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE							)







						@slow
						def        _snake_case				(			self							)->							Dict:
													for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				lowerCamelCase_								=YosoModel.from_pretrained(_SCREAMING_SNAKE_CASE							)
																				self.assertIsNotNone(_SCREAMING_SNAKE_CASE							)







						def        _snake_case				(			self							)->							Tuple:
													return


@require_torch
class 			_SCREAMING_SNAKE_CASE      (  unittest.TestCase):







						@slow
						def        _snake_case				(			self							)->							Union[str, Any]:
													lowerCamelCase_								=YosoModel.from_pretrained("""uw-madison/yoso-4096"""							)
													lowerCamelCase_								=torch.tensor([[0, 1, 2, 3, 4, 5]]							)

													with torch.no_grad():
																				lowerCamelCase_								=model(_SCREAMING_SNAKE_CASE							)[0]

													lowerCamelCase_								=torch.Size((1, 6, 768)							)
													self.assertEqual(output.shape     ,						_SCREAMING_SNAKE_CASE							)

													lowerCamelCase_								=torch.tensor(
													    [[[-0.0_6_1_1, 0.1_2_4_2, 0.0_8_4_0], [0.0_2_8_0, -0.0_0_4_8, 0.1_1_2_5], [0.0_1_0_6, 0.0_2_2_6, 0.0_7_5_1]]]							)

													self.assertTrue(torch.allclose(output[:, :3, :3]     ,						_SCREAMING_SNAKE_CASE     ,						atol=1E-4							)							)







						@slow
						def        _snake_case				(			self							)->							int:
													lowerCamelCase_								=YosoForMaskedLM.from_pretrained("""uw-madison/yoso-4096"""							)
													lowerCamelCase_								=torch.tensor([[0, 1, 2, 3, 4, 5]]							)

													with torch.no_grad():
																				lowerCamelCase_								=model(_SCREAMING_SNAKE_CASE							)[0]

													lowerCamelCase_								=5_0265

													lowerCamelCase_								=torch.Size((1, 6, vocab_size)							)
													self.assertEqual(output.shape     ,						_SCREAMING_SNAKE_CASE							)

													lowerCamelCase_								=torch.tensor(
													    [[[-2.1_3_1_3, -3.7_2_8_5, -2.2_4_0_7], [-2.7_0_4_7, -3.3_3_1_4, -2.6_4_0_8], [0.0_6_2_9, -2.5_1_6_6, -0.3_3_5_6]]]							)

													self.assertTrue(torch.allclose(output[:, :3, :3]     ,						_SCREAMING_SNAKE_CASE     ,						atol=1E-4							)							)







						@slow
						def        _snake_case				(			self							)->							str:
													lowerCamelCase_								=YosoForMaskedLM.from_pretrained("""uw-madison/yoso-4096"""							)
													lowerCamelCase_								=torch.arange(4096							).unsqueeze(0							)

													with torch.no_grad():
																				lowerCamelCase_								=model(_SCREAMING_SNAKE_CASE							)[0]

													lowerCamelCase_								=5_0265

													lowerCamelCase_								=torch.Size((1, 4096, vocab_size)							)
													self.assertEqual(output.shape     ,						_SCREAMING_SNAKE_CASE							)

													lowerCamelCase_								=torch.tensor(
													    [[[-2.3_9_1_4, -4.3_7_4_2, -5.0_9_5_6], [-4.0_9_8_8, -4.2_3_8_4, -7.0_4_0_6], [-3.1_4_2_7, -3.7_1_9_2, -6.6_8_0_0]]]							)

													self.assertTrue(torch.allclose(output[:, :3, :3]     ,						_SCREAMING_SNAKE_CASE     ,						atol=1E-4							)							)



from __future__ import annotations




def        __UpperCamelCase     (      _A					:							int      ,  _A					:							list[int]      ,  _A					:							list[int]      ,  _A					:							int		)				->int:



							"""simple docstring"""

							# Base Case
							if counter == 0 or capacity == 0:
														return 0

							# If weight of the nth item is more than Knapsack of capacity,
							#   then this item cannot be included in the optimal solution,
							# else return the maximum of two cases:
							#   (1) nth item included
							#   (2) not included
							if weights[counter - 1] > capacity:
														return knapsack(_A      ,  _A      ,  _A      ,  counter - 1		)
							else:
														lowerCamelCase_								=capacity - weights[counter - 1]
														lowerCamelCase_								=values[counter - 1] + knapsack(
														    _A      ,  _A      ,  _A      ,  counter - 1		)
														lowerCamelCase_								=knapsack(_A      ,  _A      ,  _A      ,  counter - 1		)
														return max(_A      ,  _A		)


if __name__ == "__main__":
	import doctest

	doctest.testmod()
