






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 							snake_case_       :

						"""simple docstring"""



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




											'''simple docstring'''



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



						def        UpperCAmelCase__					(self:					Optional[int]   )    ->     Optional[int]:




											'''simple docstring'''



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

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

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

											__a					:						int      =					None
											__a					:						Union[str, Any]      =					None
											__a					:						int      =					None
											if self.use_labels:
																__a					:						str      =					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					:						List[Any]      =					ids_tensor([self.batch_size] ,					self.num_choices   )

											__a					:						List[Any]      =					self.get_config()

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



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




											'''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=__UpperCAmelCase ,					initializer_range=self.initializer_range ,					)



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




											'''simple docstring'''



											(
											    (
											    __a
											)      ,			(
											    __a
											)      ,			(
											    __a
											)      ,			(
											    __a
											)      ,			(
											    __a
											)      ,			(
											    __a
											)      ,			(
											    __a
											)      ,			
											)					:						Optional[int]      =					self.prepare_config_and_inputs()

											__a					:						Union[str, Any]      =					True
											__a					:						Optional[Any]      =					floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
											__a					:						List[Any]      =					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        UpperCAmelCase__					(self:					Optional[int] ,					__UpperCAmelCase:					Union[str, Any] ,					__UpperCAmelCase:					Union[str, Any] ,					__UpperCAmelCase:					Union[str, Any] ,					__UpperCAmelCase:					Tuple ,					__UpperCAmelCase:					Optional[int] ,					__UpperCAmelCase:					List[Any] ,					__UpperCAmelCase:					str   )    ->     str:




											'''simple docstring'''



											__a					:						Union[str, Any]      =					NezhaModel(config=__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()
											__a					:						List[str]      =					model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					token_type_ids=__UpperCAmelCase   )
											__a					:						str      =					model(__UpperCAmelCase ,					token_type_ids=__UpperCAmelCase   )
											__a					:						str      =					model(__UpperCAmelCase   )
											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        UpperCAmelCase__					(self:					List[str] ,					__UpperCAmelCase:					Optional[int] ,					__UpperCAmelCase:					int ,					__UpperCAmelCase:					Tuple ,					__UpperCAmelCase:					Union[str, Any] ,					__UpperCAmelCase:					Tuple ,					__UpperCAmelCase:					int ,					__UpperCAmelCase:					Dict ,					__UpperCAmelCase:					Optional[Any] ,					__UpperCAmelCase:					str ,					)    ->     Optional[Any]:




											'''simple docstring'''



											__a					:						int      =					True
											__a					:						List[Any]      =					NezhaModel(__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()
											__a					:						Union[str, Any]      =					model(
											    __UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					token_type_ids=__UpperCAmelCase ,					encoder_hidden_states=__UpperCAmelCase ,					encoder_attention_mask=__UpperCAmelCase ,					)
											__a					:						Optional[int]      =					model(
											    __UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					token_type_ids=__UpperCAmelCase ,					encoder_hidden_states=__UpperCAmelCase ,					)
											__a					:						List[str]      =					model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					token_type_ids=__UpperCAmelCase   )
											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        UpperCAmelCase__					(self:					Union[str, Any] ,					__UpperCAmelCase:					Tuple ,					__UpperCAmelCase:					Union[str, Any] ,					__UpperCAmelCase:					Optional[Any] ,					__UpperCAmelCase:					Optional[Any] ,					__UpperCAmelCase:					int ,					__UpperCAmelCase:					str ,					__UpperCAmelCase:					str   )    ->     Dict:




											'''simple docstring'''



											__a					:						Optional[int]      =					NezhaForMaskedLM(config=__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()
											__a					:						str      =					model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					token_type_ids=__UpperCAmelCase ,					labels=__UpperCAmelCase   )
											self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.vocab_size)   )



						def        UpperCAmelCase__					(self:					Dict ,					__UpperCAmelCase:					int ,					__UpperCAmelCase:					Optional[int] ,					__UpperCAmelCase:					str ,					__UpperCAmelCase:					Optional[int] ,					__UpperCAmelCase:					str ,					__UpperCAmelCase:					Tuple ,					__UpperCAmelCase:					Tuple   )    ->     Tuple:




											'''simple docstring'''



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



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




											'''simple docstring'''



											__a					:						Dict      =					NezhaForPreTraining(config=__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()
											__a					:						str      =					model(
											    __UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					token_type_ids=__UpperCAmelCase ,					labels=__UpperCAmelCase ,					next_sentence_label=__UpperCAmelCase ,					)
											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        UpperCAmelCase__					(self:					str ,					__UpperCAmelCase:					Tuple ,					__UpperCAmelCase:					Tuple ,					__UpperCAmelCase:					Optional[int] ,					__UpperCAmelCase:					str ,					__UpperCAmelCase:					Optional[Any] ,					__UpperCAmelCase:					Union[str, Any] ,					__UpperCAmelCase:					List[Any]   )    ->     Tuple:




											'''simple docstring'''



											__a					:						List[Any]      =					NezhaForQuestionAnswering(config=__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()
											__a					:						List[Any]      =					model(
											    __UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					token_type_ids=__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        UpperCAmelCase__					(self:					str ,					__UpperCAmelCase:					Optional[int] ,					__UpperCAmelCase:					str ,					__UpperCAmelCase:					str ,					__UpperCAmelCase:					Any ,					__UpperCAmelCase:					List[str] ,					__UpperCAmelCase:					Optional[Any] ,					__UpperCAmelCase:					str   )    ->     Dict:




											'''simple docstring'''



											__a					:						Tuple      =					self.num_labels
											__a					:						str      =					NezhaForSequenceClassification(__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()
											__a					:						Dict      =					model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					token_type_ids=__UpperCAmelCase ,					labels=__UpperCAmelCase   )
											self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.num_labels)   )



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




											'''simple docstring'''



											__a					:						List[str]      =					self.num_labels
											__a					:						Optional[Any]      =					NezhaForTokenClassification(config=__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()
											__a					:						List[str]      =					model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					token_type_ids=__UpperCAmelCase ,					labels=__UpperCAmelCase   )
											self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.num_labels)   )



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




											'''simple docstring'''



											__a					:						Union[str, Any]      =					self.num_choices
											__a					:						Union[str, Any]      =					NezhaForMultipleChoice(config=__UpperCAmelCase   )
											model.to(__UpperCAmelCase   )
											model.eval()
											__a					:						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					:						List[Any]      =					input_mask.unsqueeze(1   ).expand(-1 ,					self.num_choices ,					-1   ).contiguous()
											__a					:						Any      =					model(
											    __UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					token_type_ids=__UpperCAmelCase ,					labels=__UpperCAmelCase ,					)
											self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.num_choices)   )



						def        UpperCAmelCase__					(self:					List[str]   )    ->     List[str]:




											'''simple docstring'''



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




@require_torch
class 							snake_case_       (    __UpperCamelCase		,							__UpperCamelCase		,							__UpperCamelCase		,							unittest.TestCase      ):

						"""simple docstring"""



						snake_case__        =      (
						    (
						        NezhaModel,
						        NezhaForMaskedLM,
						        NezhaForMultipleChoice,
						        NezhaForNextSentencePrediction,
						        NezhaForPreTraining,
						        NezhaForQuestionAnswering,
						        NezhaForSequenceClassification,
						        NezhaForTokenClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						snake_case__        =      (
						    {
						        """feature-extraction""": NezhaModel,
						        """fill-mask""": NezhaForMaskedLM,
						        """question-answering""": NezhaForQuestionAnswering,
						        """text-classification""": NezhaForSequenceClassification,
						        """token-classification""": NezhaForTokenClassification,
						        """zero-shot""": NezhaForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						snake_case__        =      True



						def        UpperCAmelCase__					(self:					str ,					__UpperCAmelCase:					Dict ,					__UpperCAmelCase:					int ,					__UpperCAmelCase:					Union[str, Any]=False   )    ->     str:




											'''simple docstring'''



											__a					:						Any      =					super()._prepare_for_class(__UpperCAmelCase ,					__UpperCAmelCase ,					return_labels=__UpperCAmelCase   )

											if return_labels:
																if model_class in get_values(__UpperCAmelCase   ):
																					__a					:						Optional[int]      =					torch.zeros(
																					    (self.model_tester.batch_size, self.model_tester.seq_length) ,					dtype=torch.long ,					device=__UpperCAmelCase   )
																					__a					:						List[str]      =					torch.zeros(
																					    self.model_tester.batch_size ,					dtype=torch.long ,					device=__UpperCAmelCase   )
											return inputs_dict



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




											'''simple docstring'''



											__a					:						List[Any]      =					NezhaModelTester(self   )
											__a					:						List[Any]      =					ConfigTester(self ,					config_class=__UpperCAmelCase ,					hidden_size=37   )



						def        UpperCAmelCase__					(self:					Dict   )    ->     List[Any]:




											'''simple docstring'''



											self.config_tester.run_common_tests()



						def        UpperCAmelCase__					(self:					Dict   )    ->     Optional[int]:




											'''simple docstring'''



											__a					:						str      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					Optional[int]   )    ->     int:




											'''simple docstring'''



											__a					:						Dict      =					self.model_tester.prepare_config_and_inputs_for_decoder()
											self.model_tester.create_and_check_model_as_decoder(*__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					str   )    ->     str:




											'''simple docstring'''



											(
											    (
											    __a
											)      ,			(
											    __a
											)      ,			(
											    __a
											)      ,			(
											    __a
											)      ,			(
											    __a
											)      ,			(
											    __a
											)      ,			(
											    __a
											)      ,			(
											    __a
											)      ,			(
											    __a
											)      ,			
											)					:						Any      =					self.model_tester.prepare_config_and_inputs_for_decoder()

											__a					:						List[Any]      =					None

											self.model_tester.create_and_check_model_as_decoder(
											    __UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					__UpperCAmelCase ,					)



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




											'''simple docstring'''



											__a					:						Any      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_lm(*__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					Tuple   )    ->     Any:




											'''simple docstring'''



											__a					:						Optional[Any]      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_multiple_choice(*__UpperCAmelCase   )



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




											'''simple docstring'''



											__a					:						Dict      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_next_sequence_prediction(*__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					Tuple   )    ->     Union[str, Any]:




											'''simple docstring'''



											__a					:						Tuple      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_pretraining(*__UpperCAmelCase   )



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




											'''simple docstring'''



											__a					:						List[str]      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_question_answering(*__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					List[str]   )    ->     str:




											'''simple docstring'''



											__a					:						Any      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_sequence_classification(*__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					Dict   )    ->     List[str]:




											'''simple docstring'''



											__a					:						Any      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_token_classification(*__UpperCAmelCase   )



						@slow
						def        UpperCAmelCase__					(self:					List[str]   )    ->     Optional[Any]:




											'''simple docstring'''



											for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__a					:						Optional[int]      =					NezhaModel.from_pretrained(__UpperCAmelCase   )
																self.assertIsNotNone(__UpperCAmelCase   )



						@slow
						@require_torch_gpu
						def        UpperCAmelCase__					(self:					Optional[Any]   )    ->     List[str]:




											'''simple docstring'''



											__a      ,			__a					:						Dict      =					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					:						Optional[Any]      =					True
																__a					:						Any      =					model_class(config=__UpperCAmelCase   )

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

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




@require_torch
class 							snake_case_       (    unittest.TestCase      ):

						"""simple docstring"""



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




											'''simple docstring'''



											__a					:						Optional[int]      =					NezhaModel.from_pretrained("sijunhe/nezha-cn-base"   )
											__a					:						Union[str, Any]      =					torch.tensor([[0, 1, 2, 3, 4, 5]]   )
											__a					:						str      =					torch.tensor([[0, 1, 1, 1, 1, 1]]   )
											with torch.no_grad():
																__a					:						Union[str, Any]      =					model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase   )[0]
											__a					:						str      =					torch.Size((1, 6, 768)   )
											self.assertEqual(output.shape ,					__UpperCAmelCase   )
											__a					:						Union[str, Any]      =					torch.tensor([[[0.06_85, 0.24_41, 0.11_02], [0.06_00, 0.19_06, 0.13_49], [0.02_21, 0.08_19, 0.05_86]]]   )

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



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




											'''simple docstring'''



											__a					:						Optional[Any]      =					NezhaForMaskedLM.from_pretrained("sijunhe/nezha-cn-base"   )
											__a					:						Any      =					torch.tensor([[0, 1, 2, 3, 4, 5]]   )
											__a					:						Optional[int]      =					torch.tensor([[1, 1, 1, 1, 1, 1]]   )
											with torch.no_grad():
																__a					:						Optional[Any]      =					model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase   )[0]
											__a					:						int      =					torch.Size((1, 6, 21128)   )
											self.assertEqual(output.shape ,					__UpperCAmelCase   )
											__a					:						Union[str, Any]      =					torch.tensor(
											    [[-2.79_39, -1.79_02, -2.21_89], [-2.85_85, -1.89_08, -2.37_23], [-2.64_99, -1.77_50, -2.25_58]]   )

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













import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
			from PIL import Image

			from transformers import OwlViTImageProcessor, OwlViTProcessor




@require_vision
class 							snake_case_       (    unittest.TestCase      ):

						"""simple docstring"""



						def        UpperCAmelCase__					(self:					int   )    ->     str:




											'''simple docstring'''



											__a					:						Union[str, Any]      =					tempfile.mkdtemp()

											# fmt: off
											__a					:						Dict      =					["", "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"]
											# fmt: on
											__a					:						str      =					dict(zip(__UpperCAmelCase ,					range(len(__UpperCAmelCase   )   )   )   )
											__a					:						Union[str, Any]      =					["#version: 0.2", "l o", "lo w</w>", "e r</w>", ""]
											__a					:						Union[str, Any]      =					{"unk_token": "<unk>"}

											__a					:						Dict      =					os.path.join(self.tmpdirname ,					VOCAB_FILES_NAMES["vocab_file"]   )
											__a					:						int      =					os.path.join(self.tmpdirname ,					VOCAB_FILES_NAMES["merges_file"]   )
											with open(self.vocab_file ,					"w" ,					encoding="utf-8"   ) as fp:
																fp.write(json.dumps(__UpperCAmelCase   ) + "\n"   )
											with open(self.merges_file ,					"w" ,					encoding="utf-8"   ) as fp:
																fp.write("\n".join(__UpperCAmelCase   )   )

											__a					:						str      =					{
											    "do_resize": True,
											    "size": 20,
											    "do_center_crop": True,
											    "crop_size": 18,
											    "do_normalize": True,
											    "image_mean": [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73],
											    "image_std": [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11],
											}
											__a					:						Any      =					os.path.join(self.tmpdirname ,					__UpperCAmelCase   )
											with open(self.image_processor_file ,					"w" ,					encoding="utf-8"   ) as fp:
																json.dump(__UpperCAmelCase ,					__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					Tuple ,					**__UpperCAmelCase:					Dict   )    ->     List[str]:




											'''simple docstring'''



											return CLIPTokenizer.from_pretrained(self.tmpdirname ,					pad_token="!" ,					**__UpperCAmelCase   )



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




											'''simple docstring'''



											return CLIPTokenizerFast.from_pretrained(self.tmpdirname ,					pad_token="!" ,					**__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					List[Any] ,					**__UpperCAmelCase:					Dict   )    ->     Optional[int]:




											'''simple docstring'''



											return OwlViTImageProcessor.from_pretrained(self.tmpdirname ,					**__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					str   )    ->     int:




											'''simple docstring'''



											shutil.rmtree(self.tmpdirname   )



						def        UpperCAmelCase__					(self:					Dict   )    ->     Union[str, Any]:




											'''simple docstring'''



											__a					:						Dict      =					[np.random.randint(255 ,					size=(3, 30, 400) ,					dtype=np.uinta   )]

											__a					:						Any      =					[Image.fromarray(np.moveaxis(__UpperCAmelCase ,					0 ,					-1   )   ) for x in image_inputs]

											return image_inputs



						def        UpperCAmelCase__					(self:					Tuple   )    ->     Optional[Any]:




											'''simple docstring'''



											__a					:						Dict      =					self.get_tokenizer()
											__a					:						Dict      =					self.get_rust_tokenizer()
											__a					:						List[str]      =					self.get_image_processor()

											__a					:						Any      =					OwlViTProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase   )
											processor_slow.save_pretrained(self.tmpdirname   )
											__a					:						str      =					OwlViTProcessor.from_pretrained(self.tmpdirname ,					use_fast=__UpperCAmelCase   )

											__a					:						Union[str, Any]      =					OwlViTProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase   )
											processor_fast.save_pretrained(self.tmpdirname   )
											__a					:						List[Any]      =					OwlViTProcessor.from_pretrained(self.tmpdirname   )

											self.assertEqual(processor_slow.tokenizer.get_vocab() ,					tokenizer_slow.get_vocab()   )
											self.assertEqual(processor_fast.tokenizer.get_vocab() ,					tokenizer_fast.get_vocab()   )
											self.assertEqual(tokenizer_slow.get_vocab() ,					tokenizer_fast.get_vocab()   )
											self.assertIsInstance(processor_slow.tokenizer ,					__UpperCAmelCase   )
											self.assertIsInstance(processor_fast.tokenizer ,					__UpperCAmelCase   )

											self.assertEqual(processor_slow.image_processor.to_json_string() ,					image_processor.to_json_string()   )
											self.assertEqual(processor_fast.image_processor.to_json_string() ,					image_processor.to_json_string()   )
											self.assertIsInstance(processor_slow.image_processor ,					__UpperCAmelCase   )
											self.assertIsInstance(processor_fast.image_processor ,					__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					str   )    ->     Dict:




											'''simple docstring'''



											__a					:						Tuple      =					OwlViTProcessor(tokenizer=self.get_tokenizer() ,					image_processor=self.get_image_processor()   )
											processor.save_pretrained(self.tmpdirname   )

											__a					:						Optional[Any]      =					self.get_tokenizer(bos_token="(BOS)" ,					eos_token="(EOS)"   )
											__a					:						str      =					self.get_image_processor(do_normalize=__UpperCAmelCase   )

											__a					:						str      =					OwlViTProcessor.from_pretrained(
											    self.tmpdirname ,					bos_token="(BOS)" ,					eos_token="(EOS)" ,					do_normalize=__UpperCAmelCase   )

											self.assertEqual(processor.tokenizer.get_vocab() ,					tokenizer_add_kwargs.get_vocab()   )
											self.assertIsInstance(processor.tokenizer ,					__UpperCAmelCase   )

											self.assertEqual(processor.image_processor.to_json_string() ,					image_processor_add_kwargs.to_json_string()   )
											self.assertIsInstance(processor.image_processor ,					__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					Optional[int]   )    ->     Dict:




											'''simple docstring'''



											__a					:						Optional[Any]      =					self.get_image_processor()
											__a					:						str      =					self.get_tokenizer()

											__a					:						List[str]      =					OwlViTProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase   )

											__a					:						List[str]      =					self.prepare_image_inputs()

											__a					:						Optional[Any]      =					image_processor(__UpperCAmelCase ,					return_tensors="np"   )
											__a					:						int      =					processor(images=__UpperCAmelCase ,					return_tensors="np"   )

											for key in input_image_proc.keys():
																self.assertAlmostEqual(input_image_proc[key].sum() ,					input_processor[key].sum() ,					delta=1E-2   )



						def        UpperCAmelCase__					(self:					Dict   )    ->     Optional[int]:




											'''simple docstring'''



											__a					:						int      =					self.get_image_processor()
											__a					:						Tuple      =					self.get_tokenizer()

											__a					:						Optional[int]      =					OwlViTProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase   )

											__a					:						Dict      =					"lower newer"

											__a					:						Any      =					processor(text=__UpperCAmelCase ,					return_tensors="np"   )

											__a					:						List[str]      =					tokenizer(__UpperCAmelCase ,					return_tensors="np"   )

											for key in encoded_tok.keys():
																self.assertListEqual(encoded_tok[key][0].tolist() ,					encoded_processor[key][0].tolist()   )



						def        UpperCAmelCase__					(self:					List[Any]   )    ->     Dict:




											'''simple docstring'''



											__a					:						Dict      =					self.get_image_processor()
											__a					:						List[str]      =					self.get_tokenizer()

											__a					:						Optional[Any]      =					OwlViTProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase   )

											__a					:						Optional[int]      =					"lower newer"
											__a					:						int      =					self.prepare_image_inputs()

											__a					:						Optional[int]      =					processor(text=__UpperCAmelCase ,					images=__UpperCAmelCase   )

											self.assertListEqual(list(inputs.keys()   ) ,					["input_ids", "attention_mask", "pixel_values"]   )

											# test if it raises when no input is passed
											with pytest.raises(__UpperCAmelCase   ):
																processor()



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




											'''simple docstring'''



											__a					:						Dict      =					"google/owlvit-base-patch32"
											__a					:						Dict      =					OwlViTProcessor.from_pretrained(__UpperCAmelCase   )

											__a					:						List[str]      =					["cat", "nasa badge"]
											__a					:						List[Any]      =					processor(text=__UpperCAmelCase   )

											__a					:						Any      =					16
											self.assertListEqual(list(inputs.keys()   ) ,					["input_ids", "attention_mask"]   )
											self.assertEqual(inputs["input_ids"].shape ,					(2, seq_length)   )

											# test if it raises when no input is passed
											with pytest.raises(__UpperCAmelCase   ):
																processor()



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




											'''simple docstring'''



											__a					:						Optional[int]      =					"google/owlvit-base-patch32"
											__a					:						Optional[int]      =					OwlViTProcessor.from_pretrained(__UpperCAmelCase   )

											__a					:						Tuple      =					[["cat", "nasa badge"], ["person"]]
											__a					:						List[Any]      =					processor(text=__UpperCAmelCase   )

											__a					:						Tuple      =					16
											__a					:						Optional[Any]      =					len(__UpperCAmelCase   )
											__a					:						Optional[Any]      =					max([len(__UpperCAmelCase   ) for texts in input_texts]   )

											self.assertListEqual(list(inputs.keys()   ) ,					["input_ids", "attention_mask"]   )
											self.assertEqual(inputs["input_ids"].shape ,					(batch_size * num_max_text_queries, seq_length)   )

											# test if it raises when no input is passed
											with pytest.raises(__UpperCAmelCase   ):
																processor()



						def        UpperCAmelCase__					(self:					Tuple   )    ->     Dict:




											'''simple docstring'''



											__a					:						int      =					"google/owlvit-base-patch32"
											__a					:						Dict      =					OwlViTProcessor.from_pretrained(__UpperCAmelCase   )

											__a					:						List[str]      =					["cat", "nasa badge"]
											__a					:						List[Any]      =					processor(text=__UpperCAmelCase   )

											__a					:						int      =					16
											__a					:						Union[str, Any]      =					inputs["input_ids"]
											__a					:						Optional[int]      =					[
											    [49406, 2368, 49407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
											    [49406, 6841, 11301, 49407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
											]

											self.assertListEqual(list(inputs.keys()   ) ,					["input_ids", "attention_mask"]   )
											self.assertEqual(inputs["input_ids"].shape ,					(2, seq_length)   )
											self.assertListEqual(list(input_ids[0]   ) ,					predicted_ids[0]   )
											self.assertListEqual(list(input_ids[1]   ) ,					predicted_ids[1]   )



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




											'''simple docstring'''



											__a					:						Union[str, Any]      =					self.get_image_processor()
											__a					:						List[str]      =					self.get_tokenizer()

											__a					:						Tuple      =					OwlViTProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase   )

											__a					:						str      =					self.prepare_image_inputs()
											__a					:						Any      =					self.prepare_image_inputs()

											__a					:						str      =					processor(images=__UpperCAmelCase ,					query_images=__UpperCAmelCase   )

											self.assertListEqual(list(inputs.keys()   ) ,					["query_pixel_values", "pixel_values"]   )

											# test if it raises when no input is passed
											with pytest.raises(__UpperCAmelCase   ):
																processor()



						def        UpperCAmelCase__					(self:					Any   )    ->     Tuple:




											'''simple docstring'''



											__a					:						int      =					self.get_image_processor()
											__a					:						Tuple      =					self.get_tokenizer()

											__a					:						Optional[int]      =					OwlViTProcessor(tokenizer=__UpperCAmelCase ,					image_processor=__UpperCAmelCase   )

											__a					:						List[str]      =					[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

											__a					:						Tuple      =					processor.batch_decode(__UpperCAmelCase   )
											__a					:						str      =					tokenizer.batch_decode(__UpperCAmelCase   )

											self.assertListEqual(__UpperCAmelCase ,					__UpperCAmelCase   )





