



"""simple docstring"""

import os
import tempfile
import unittest

from transformers import NezhaConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_gpu, 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 (
	    MODEL_FOR_PRETRAINING_MAPPING,
	    NezhaForMaskedLM,
	    NezhaForMultipleChoice,
	    NezhaForNextSentencePrediction,
	    NezhaForPreTraining,
	    NezhaForQuestionAnswering,
	    NezhaForSequenceClassification,
	    NezhaForTokenClassification,
	    NezhaModel,
	)
	from transformers.models.nezha.modeling_nezha import NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST




class SCREAMING_SNAKE_CASE__					:



						def __init__(self						,     _lowercase						,     _lowercase=13						,     _lowercase=7						,     _lowercase=True						,     _lowercase=True						,     _lowercase=True						,     _lowercase=True						,     _lowercase=99						,     _lowercase=32						,     _lowercase=5						,     _lowercase=4						,     _lowercase=37						,     _lowercase="gelu"						,     _lowercase=0.1						,     _lowercase=0.1						,     _lowercase=128						,     _lowercase=32						,     _lowercase=16						,     _lowercase=2						,     _lowercase=0.02						,     _lowercase=3						,     _lowercase=4						,     _lowercase=None						,     ):





													'''simple docstring'''

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



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Tuple									=       ids_tensor([self.batch_size, self.seq_length]						,     self.vocab_size							)

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

													__a		:		Union[str, Any]									=       None
													if self.use_token_type_ids:
																				__a		:		List[Any]									=       ids_tensor([self.batch_size, self.seq_length]						,     self.type_vocab_size							)

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

													__a		:		Dict									=       self.get_config()

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



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													return NezhaConfig(
													    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=_lowercase						,     initializer_range=self.initializer_range						,     )



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													(
													    (
													    __a
													)							,     (
													    __a
													)							,     (
													    __a
													)							,     (
													    __a
													)							,     (
													    __a
													)							,     (
													    __a
													)							,     (
													    __a
													)							,     
													)		:		Dict									=       self.prepare_config_and_inputs()

													__a		:		Any									=       True
													__a		:		Dict									=       floats_tensor([self.batch_size, self.seq_length, self.hidden_size]							)
													__a		:		str									=       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     lowerCAmelCase__(self						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase							):





													'''simple docstring'''

													__a		:		List[Any]									=       NezhaModel(config=_lowercase							)
													model.to(_lowercase							)
													model.eval()
													__a		:		Any									=       model(_lowercase						,     attention_mask=_lowercase						,     token_type_ids=_lowercase							)
													__a		:		str									=       model(_lowercase						,     token_type_ids=_lowercase							)
													__a		:		int									=       model(_lowercase							)
													self.parent.assertEqual(result.last_hidden_state.shape						,     (self.batch_size, self.seq_length, self.hidden_size)							)
													self.parent.assertEqual(result.pooler_output.shape						,     (self.batch_size, self.hidden_size)							)



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     ):





													'''simple docstring'''

													__a		:		Dict									=       True
													__a		:		Tuple									=       NezhaModel(_lowercase							)
													model.to(_lowercase							)
													model.eval()
													__a		:		List[Any]									=       model(
													    _lowercase						,     attention_mask=_lowercase						,     token_type_ids=_lowercase						,     encoder_hidden_states=_lowercase						,     encoder_attention_mask=_lowercase						,     )
													__a		:		Optional[Any]									=       model(
													    _lowercase						,     attention_mask=_lowercase						,     token_type_ids=_lowercase						,     encoder_hidden_states=_lowercase						,     )
													__a		:		List[str]									=       model(_lowercase						,     attention_mask=_lowercase						,     token_type_ids=_lowercase							)
													self.parent.assertEqual(result.last_hidden_state.shape						,     (self.batch_size, self.seq_length, self.hidden_size)							)
													self.parent.assertEqual(result.pooler_output.shape						,     (self.batch_size, self.hidden_size)							)



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase							):





													'''simple docstring'''

													__a		:		int									=       NezhaForMaskedLM(config=_lowercase							)
													model.to(_lowercase							)
													model.eval()
													__a		:		Any									=       model(_lowercase						,     attention_mask=_lowercase						,     token_type_ids=_lowercase						,     labels=_lowercase							)
													self.parent.assertEqual(result.logits.shape						,     (self.batch_size, self.seq_length, self.vocab_size)							)



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase							):





													'''simple docstring'''

													__a		:		Any									=       NezhaForNextSentencePrediction(config=_lowercase							)
													model.to(_lowercase							)
													model.eval()
													__a		:		Optional[Any]									=       model(
													    _lowercase						,     attention_mask=_lowercase						,     token_type_ids=_lowercase						,     labels=_lowercase						,     )
													self.parent.assertEqual(result.logits.shape						,     (self.batch_size, 2)							)



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase							):





													'''simple docstring'''

													__a		:		Any									=       NezhaForPreTraining(config=_lowercase							)
													model.to(_lowercase							)
													model.eval()
													__a		:		Any									=       model(
													    _lowercase						,     attention_mask=_lowercase						,     token_type_ids=_lowercase						,     labels=_lowercase						,     next_sentence_label=_lowercase						,     )
													self.parent.assertEqual(result.prediction_logits.shape						,     (self.batch_size, self.seq_length, self.vocab_size)							)
													self.parent.assertEqual(result.seq_relationship_logits.shape						,     (self.batch_size, 2)							)



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase							):





													'''simple docstring'''

													__a		:		Any									=       NezhaForQuestionAnswering(config=_lowercase							)
													model.to(_lowercase							)
													model.eval()
													__a		:		List[Any]									=       model(
													    _lowercase						,     attention_mask=_lowercase						,     token_type_ids=_lowercase						,     start_positions=_lowercase						,     end_positions=_lowercase						,     )
													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						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase							):





													'''simple docstring'''

													__a		:		Any									=       self.num_labels
													__a		:		str									=       NezhaForSequenceClassification(_lowercase							)
													model.to(_lowercase							)
													model.eval()
													__a		:		int									=       model(_lowercase						,     attention_mask=_lowercase						,     token_type_ids=_lowercase						,     labels=_lowercase							)
													self.parent.assertEqual(result.logits.shape						,     (self.batch_size, self.num_labels)							)



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase							):





													'''simple docstring'''

													__a		:		int									=       self.num_labels
													__a		:		Tuple									=       NezhaForTokenClassification(config=_lowercase							)
													model.to(_lowercase							)
													model.eval()
													__a		:		int									=       model(_lowercase						,     attention_mask=_lowercase						,     token_type_ids=_lowercase						,     labels=_lowercase							)
													self.parent.assertEqual(result.logits.shape						,     (self.batch_size, self.seq_length, self.num_labels)							)



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase							):





													'''simple docstring'''

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





						def     lowerCAmelCase__(self							):





													'''simple docstring'''

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







@require_torch
class SCREAMING_SNAKE_CASE__					(		__snake_case     ,					__snake_case     ,					__snake_case     ,					unittest.TestCase      ):
						_lowerCAmelCase							=					(
						    (
						        NezhaModel,
						        NezhaForMaskedLM,
						        NezhaForMultipleChoice,
						        NezhaForNextSentencePrediction,
						        NezhaForPreTraining,
						        NezhaForQuestionAnswering,
						        NezhaForSequenceClassification,
						        NezhaForTokenClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						_lowerCAmelCase							=					(
						    {
						        "feature-extraction": NezhaModel,
						        "fill-mask": NezhaForMaskedLM,
						        "question-answering": NezhaForQuestionAnswering,
						        "text-classification": NezhaForSequenceClassification,
						        "token-classification": NezhaForTokenClassification,
						        "zero-shot": NezhaForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						_lowerCAmelCase							=					True



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase						,     _lowercase=False							):





													'''simple docstring'''

													__a		:		Dict									=       super()._prepare_for_class(_lowercase						,     _lowercase						,     return_labels=_lowercase							)

													if return_labels:
																				if model_class in get_values(_lowercase							):
																											__a		:		Any									=       torch.zeros(
																											    (self.model_tester.batch_size, self.model_tester.seq_length)						,     dtype=torch.long						,     device=_lowercase							)
																											__a		:		Union[str, Any]									=       torch.zeros(
																											    self.model_tester.batch_size						,     dtype=torch.long						,     device=_lowercase							)
													return inputs_dict



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Any									=       NezhaModelTester(self							)
													__a		:		str									=       ConfigTester(self						,     config_class=_lowercase						,     hidden_size=37							)



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													self.config_tester.run_common_tests()



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		List[Any]									=       self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*_lowercase							)



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Optional[int]									=       self.model_tester.prepare_config_and_inputs_for_decoder()
													self.model_tester.create_and_check_model_as_decoder(*_lowercase							)



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													(
													    (
													    __a
													)							,     (
													    __a
													)							,     (
													    __a
													)							,     (
													    __a
													)							,     (
													    __a
													)							,     (
													    __a
													)							,     (
													    __a
													)							,     (
													    __a
													)							,     (
													    __a
													)							,     
													)		:		List[Any]									=       self.model_tester.prepare_config_and_inputs_for_decoder()

													__a		:		Optional[Any]									=       None

													self.model_tester.create_and_check_model_as_decoder(
													    _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     _lowercase						,     )



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Optional[int]									=       self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_masked_lm(*_lowercase							)



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		List[Any]									=       self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_multiple_choice(*_lowercase							)



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		List[Any]									=       self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_next_sequence_prediction(*_lowercase							)



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Optional[int]									=       self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_pretraining(*_lowercase							)



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		int									=       self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_question_answering(*_lowercase							)



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Optional[int]									=       self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_sequence_classification(*_lowercase							)



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		List[Any]									=       self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_token_classification(*_lowercase							)



						@slow
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																				__a		:		Union[str, Any]									=       NezhaModel.from_pretrained(_lowercase							)
																				self.assertIsNotNone(_lowercase							)





						@slow
						@require_torch_gpu
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a							,     __a		:		Any									=       self.model_tester.prepare_config_and_inputs_for_common()
													for model_class in self.all_model_classes:
																				# NezhaForMultipleChoice behaves incorrectly in JIT environments.
																				if model_class == NezhaForMultipleChoice:
																											return

																				__a		:		int									=       True
																				__a		:		Dict									=       model_class(config=_lowercase							)

																				__a		:		Any									=       self._prepare_for_class(_lowercase						,     _lowercase							)
																				__a		:		Dict									=       torch.jit.trace(
																				    _lowercase						,     (inputs_dict["""input_ids"""].to("""cpu"""							), inputs_dict["""attention_mask"""].to("""cpu"""							))							)

																				with tempfile.TemporaryDirectory() as tmp:
																											torch.jit.save(_lowercase						,     os.path.join(_lowercase						,     """bert.pt"""							)							)
																											__a		:		List[Any]									=       torch.jit.load(os.path.join(_lowercase						,     """bert.pt"""							)						,     map_location=_lowercase							)
																											loaded(inputs_dict["""input_ids"""].to(_lowercase							)						,     inputs_dict["""attention_mask"""].to(_lowercase							)							)







@require_torch
class SCREAMING_SNAKE_CASE__					(		unittest.TestCase      ):



						@slow
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		List[Any]									=       NezhaModel.from_pretrained("""sijunhe/nezha-cn-base"""							)
													__a		:		Any									=       torch.tensor([[0, 1, 2, 3, 4, 5]]							)
													__a		:		str									=       torch.tensor([[0, 1, 1, 1, 1, 1]]							)
													with torch.no_grad():
																				__a		:		List[str]									=       model(_lowercase						,     attention_mask=_lowercase							)[0]
													__a		:		str									=       torch.Size((1, 6, 768)							)
													self.assertEqual(output.shape						,     _lowercase							)
													__a		:		Union[str, Any]									=       torch.tensor([[[0.0685, 0.2441, 0.1102], [0.0600, 0.1906, 0.1349], [0.0221, 0.0819, 0.0586]]]							)

													self.assertTrue(torch.allclose(output[:, 1:4, 1:4]						,     _lowercase						,     atol=1e-4							)							)





						@slow
						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Optional[int]									=       NezhaForMaskedLM.from_pretrained("""sijunhe/nezha-cn-base"""							)
													__a		:		Tuple									=       torch.tensor([[0, 1, 2, 3, 4, 5]]							)
													__a		:		Union[str, Any]									=       torch.tensor([[1, 1, 1, 1, 1, 1]]							)
													with torch.no_grad():
																				__a		:		Tuple									=       model(_lowercase						,     attention_mask=_lowercase							)[0]
													__a		:		Dict									=       torch.Size((1, 6, 21128)							)
													self.assertEqual(output.shape						,     _lowercase							)
													__a		:		Optional[int]									=       torch.tensor(
													    [[-2.7939, -1.7902, -2.2189], [-2.8585, -1.8908, -2.3723], [-2.6499, -1.7750, -2.2558]]							)

													self.assertTrue(torch.allclose(output[:, 1:4, 1:4]						,     _lowercase						,     atol=1e-4							)							)






"""simple docstring"""

import unittest

from knapsack import knapsack as k




class SCREAMING_SNAKE_CASE__					(		unittest.TestCase      ):



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		str									=       0
													__a		:		Optional[Any]									=       [0]
													__a		:		int									=       [0]
													__a		:		str									=       len(_lowercase							)
													self.assertEqual(k.knapsack(_lowercase						,     _lowercase						,     _lowercase						,     _lowercase							)						,     0							)

													__a		:		int									=       [60]
													__a		:		Union[str, Any]									=       [10]
													__a		:		Tuple									=       len(_lowercase							)
													self.assertEqual(k.knapsack(_lowercase						,     _lowercase						,     _lowercase						,     _lowercase							)						,     0							)



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		int									=       3
													__a		:		str									=       [1, 2, 3]
													__a		:		Optional[Any]									=       [3, 2, 1]
													__a		:		int									=       len(_lowercase							)
													self.assertEqual(k.knapsack(_lowercase						,     _lowercase						,     _lowercase						,     _lowercase							)						,     5							)





						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Dict									=       50
													__a		:		Tuple									=       [60, 100, 120]
													__a		:		List[str]									=       [10, 20, 30]
													__a		:		Union[str, Any]									=       len(_lowercase							)
													self.assertEqual(k.knapsack(_lowercase						,     _lowercase						,     _lowercase						,     _lowercase							)						,     220							)


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

