





"""simple docstring"""
import os
import tempfile
import unittest

from transformers import DistilBertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
		    DistilBertForMaskedLM,
		    DistilBertForMultipleChoice,
		    DistilBertForQuestionAnswering,
		    DistilBertForSequenceClassification,
		    DistilBertForTokenClassification,
		    DistilBertModel,
		)






class 					_UpperCAmelCase   (	lowercase_ ):


					def __init__(					self			:Dict     ,       __UpperCamelCase			:Tuple     ,       __UpperCamelCase			:int=13     ,       __UpperCamelCase			:Tuple=7     ,       __UpperCamelCase			:str=True     ,       __UpperCamelCase			:Any=True     ,       __UpperCamelCase			:Optional[Any]=False     ,       __UpperCamelCase			:Tuple=True     ,       __UpperCamelCase			:int=99     ,       __UpperCamelCase			:str=32     ,       __UpperCamelCase			:Dict=5     ,       __UpperCamelCase			:Optional[Any]=4     ,       __UpperCamelCase			:Optional[Any]=37     ,       __UpperCamelCase			:str="gelu"     ,       __UpperCamelCase			:Tuple=0.1     ,       __UpperCamelCase			:Dict=0.1     ,       __UpperCamelCase			:List[str]=5_12     ,       __UpperCamelCase			:Dict=16     ,       __UpperCamelCase			:Union[str, Any]=2     ,       __UpperCamelCase			:List[str]=0.02     ,       __UpperCamelCase			:Any=3     ,       __UpperCamelCase			:Dict=4     ,       __UpperCamelCase			:int=None     ,       ):
												A		=  parent
												A		=  batch_size
												A		=  seq_length
												A		=  is_training
												A		=  use_input_mask
												A		=  use_token_type_ids
												A		=  use_labels
												A		=  vocab_size
												A		=  hidden_size
												A		=  num_hidden_layers
												A		=  num_attention_heads
												A		=  intermediate_size
												A		=  hidden_act
												A		=  hidden_dropout_prob
												A		=  attention_probs_dropout_prob
												A		=  max_position_embeddings
												A		=  type_vocab_size
												A		=  type_sequence_label_size
												A		=  initializer_range
												A		=  num_labels
												A		=  num_choices
												A		=  scope


					def 	lowerCamelCase       (					self			:Dict		):
												A		=  ids_tensor([self.batch_size, self.seq_length]     ,       self.vocab_size		)

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

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

												A		=  self.get_config()

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


					def 	lowerCamelCase       (					self			:List[Any]		):
												return DistilBertConfig(
												    vocab_size=self.vocab_size     ,       dim=self.hidden_size     ,       n_layers=self.num_hidden_layers     ,       n_heads=self.num_attention_heads     ,       hidden_dim=self.intermediate_size     ,       hidden_act=self.hidden_act     ,       dropout=self.hidden_dropout_prob     ,       attention_dropout=self.attention_probs_dropout_prob     ,       max_position_embeddings=self.max_position_embeddings     ,       initializer_range=self.initializer_range     ,       )


					def 	lowerCamelCase       (					self			:Optional[Any]     ,       __UpperCamelCase			:str     ,       __UpperCamelCase			:int     ,       __UpperCamelCase			:Tuple     ,       __UpperCamelCase			:Tuple     ,       __UpperCamelCase			:List[Any]     ,       __UpperCamelCase			:List[Any]		):
												A		=  DistilBertModel(config=__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.eval()
												A		=  model(__UpperCamelCase     ,       __UpperCamelCase		)
												A		=  model(__UpperCamelCase		)
												self.parent.assertEqual(result.last_hidden_state.shape     ,       (self.batch_size, self.seq_length, self.hidden_size)		)


					def 	lowerCamelCase       (					self			:int     ,       __UpperCamelCase			:Optional[int]     ,       __UpperCamelCase			:List[Any]     ,       __UpperCamelCase			:int     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:List[str]     ,       __UpperCamelCase			:Union[str, Any]		):
												A		=  DistilBertForMaskedLM(config=__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.eval()
												A		=  model(__UpperCamelCase     ,       attention_mask=__UpperCamelCase     ,       labels=__UpperCamelCase		)
												self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.seq_length, self.vocab_size)		)


					def 	lowerCamelCase       (					self			:List[str]     ,       __UpperCamelCase			:str     ,       __UpperCamelCase			:str     ,       __UpperCamelCase			:int     ,       __UpperCamelCase			:int     ,       __UpperCamelCase			:Any     ,       __UpperCamelCase			:List[Any]		):
												A		=  DistilBertForQuestionAnswering(config=__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.eval()
												A		=  model(
												    __UpperCamelCase     ,       attention_mask=__UpperCamelCase     ,       start_positions=__UpperCamelCase     ,       end_positions=__UpperCamelCase		)
												self.parent.assertEqual(result.start_logits.shape     ,       (self.batch_size, self.seq_length)		)
												self.parent.assertEqual(result.end_logits.shape     ,       (self.batch_size, self.seq_length)		)


					def 	lowerCamelCase       (					self			:Optional[int]     ,       __UpperCamelCase			:Tuple     ,       __UpperCamelCase			:Tuple     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:Any     ,       __UpperCamelCase			:List[Any]     ,       __UpperCamelCase			:Union[str, Any]		):
												A		=  self.num_labels
												A		=  DistilBertForSequenceClassification(__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.eval()
												A		=  model(__UpperCamelCase     ,       attention_mask=__UpperCamelCase     ,       labels=__UpperCamelCase		)
												self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.num_labels)		)


					def 	lowerCamelCase       (					self			:Union[str, Any]     ,       __UpperCamelCase			:Optional[Any]     ,       __UpperCamelCase			:List[str]     ,       __UpperCamelCase			:List[str]     ,       __UpperCamelCase			:int     ,       __UpperCamelCase			:Tuple     ,       __UpperCamelCase			:int		):
												A		=  self.num_labels
												A		=  DistilBertForTokenClassification(config=__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.eval()

												A		=  model(__UpperCamelCase     ,       attention_mask=__UpperCamelCase     ,       labels=__UpperCamelCase		)
												self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.seq_length, self.num_labels)		)


					def 	lowerCamelCase       (					self			:Tuple     ,       __UpperCamelCase			:List[str]     ,       __UpperCamelCase			:int     ,       __UpperCamelCase			:List[Any]     ,       __UpperCamelCase			:Optional[int]     ,       __UpperCamelCase			:str     ,       __UpperCamelCase			:List[Any]		):
												A		=  self.num_choices
												A		=  DistilBertForMultipleChoice(config=__UpperCamelCase		)
												model.to(__UpperCamelCase		)
												model.eval()
												A		=  input_ids.unsqueeze(1		).expand(-1     ,       self.num_choices     ,       -1		).contiguous()
												A		=  input_mask.unsqueeze(1		).expand(-1     ,       self.num_choices     ,       -1		).contiguous()
												A		=  model(
												    __UpperCamelCase     ,       attention_mask=__UpperCamelCase     ,       labels=__UpperCamelCase     ,       )
												self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.num_choices)		)


					def 	lowerCamelCase       (					self			:Optional[int]		):
												A		=  self.prepare_config_and_inputs()
												((A),	(A),	(A),	(A),	(A),	(A))		=  config_and_inputs
												A		=  {"input_ids": input_ids, "attention_mask": input_mask}
												return config, inputs_dict







@require_torch
class 					_UpperCAmelCase   (	lowercase_     ,							lowercase_     ,							unittest.TestCase ):
					UpperCamelCase      =							(
					    (
					        DistilBertModel,
					        DistilBertForMaskedLM,
					        DistilBertForMultipleChoice,
					        DistilBertForQuestionAnswering,
					        DistilBertForSequenceClassification,
					        DistilBertForTokenClassification,
					    )
					    if is_torch_available()
					    else None
					)
					UpperCamelCase      =							(
					    {
					        '''feature-extraction''': DistilBertModel,
					        '''fill-mask''': DistilBertForMaskedLM,
					        '''question-answering''': DistilBertForQuestionAnswering,
					        '''text-classification''': DistilBertForSequenceClassification,
					        '''token-classification''': DistilBertForTokenClassification,
					        '''zero-shot''': DistilBertForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					UpperCamelCase      =							True
					UpperCamelCase      =							True
					UpperCamelCase      =							True
					UpperCamelCase      =							True


					def 	lowerCamelCase       (					self			:Dict		):
												A		=  DistilBertModelTester(self		)
												A		=  ConfigTester(self     ,       config_class=__UpperCamelCase     ,       dim=37		)


					def 	lowerCamelCase       (					self			:Optional[Any]		):
												self.config_tester.run_common_tests()


					def 	lowerCamelCase       (					self			:Dict		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_model(*__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Any		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_masked_lm(*__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:List[Any]		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_question_answering(*__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Dict		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_sequence_classification(*__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Optional[int]		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_token_classification(*__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:str		):
												A		=  self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_distilbert_for_multiple_choice(*__UpperCamelCase		)


					@slow
					def 	lowerCamelCase       (					self			:Dict		):
												for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			A		=  DistilBertModel.from_pretrained(__UpperCamelCase		)
																			self.assertIsNotNone(__UpperCamelCase		)


					@slow
					@require_torch_gpu
					def 	lowerCamelCase       (					self			:List[Any]		):
												A,	A		=  self.model_tester.prepare_config_and_inputs_for_common()
												for model_class in self.all_model_classes:
																			# BertForMultipleChoice behaves incorrectly in JIT environments.
																			if model_class == DistilBertForMultipleChoice:
																										return

																			A		=  True
																			A		=  model_class(config=__UpperCamelCase		)

																			A		=  self._prepare_for_class(__UpperCamelCase     ,       __UpperCamelCase		)
																			A		=  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     ,       "traced_model.pt"		)		)
																										A		=  torch.jit.load(os.path.join(__UpperCamelCase     ,       "traced_model.pt"		)     ,       map_location=__UpperCamelCase		)
																										loaded(inputs_dict["input_ids"].to(__UpperCamelCase		)     ,       inputs_dict["attention_mask"].to(__UpperCamelCase		)		)







@require_torch
class 					_UpperCAmelCase   (	unittest.TestCase ):


					@slow
					def 	lowerCamelCase       (					self			:Tuple		):
												A		=  DistilBertModel.from_pretrained("distilbert-base-uncased"		)
												A		=  torch.tensor([[0, 3_45, 2_32, 3_28, 7_40, 1_40, 16_95, 69, 60_78, 15_88, 2]]		)
												A		=  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
												with torch.no_grad():
																			A		=  model(__UpperCamelCase     ,       attention_mask=__UpperCamelCase		)[0]
												A		=  torch.Size((1, 11, 7_68)		)
												self.assertEqual(output.shape     ,       __UpperCamelCase		)
												A		=  torch.tensor(
												    [[[-0.1_639, 0.3_299, 0.1_648], [-0.1_746, 0.3_289, 0.1_710], [-0.1_884, 0.3_357, 0.1_810]]]		)

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









"""simple docstring"""
import unittest

from transformers import RoFormerTokenizer, RoFormerTokenizerFast
from transformers.testing_utils import require_rjieba, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin






@require_rjieba
@require_tokenizers
class 					_UpperCAmelCase   (	lowercase_     ,							unittest.TestCase ):
					UpperCamelCase      =							RoFormerTokenizer
					UpperCamelCase      =							RoFormerTokenizerFast
					UpperCamelCase      =							True
					UpperCamelCase      =							True


					def 	lowerCamelCase       (					self			:List[str]		):
												super().setUp()


					def 	lowerCamelCase       (					self			:int     ,       **__UpperCamelCase			:List[Any]		):
												return self.tokenizer_class.from_pretrained("junnyu/roformer_chinese_base"     ,       **__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Tuple     ,       **__UpperCamelCase			:Optional[int]		):
												return self.rust_tokenizer_class.from_pretrained("junnyu/roformer_chinese_base"     ,       **__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Any		):
												A		=  "永和服装饰品有限公司,今天天气非常好"
												A		=  "永和 服装 饰品 有限公司 , 今 天 天 气 非常 好"
												return input_text, output_text


					def 	lowerCamelCase       (					self			:int		):
												A		=  self.get_tokenizer()
												A,	A		=  self.get_chinese_input_output_texts()
												A		=  tokenizer.tokenize(__UpperCamelCase		)

												self.assertListEqual(__UpperCamelCase     ,       output_text.split()		)

												A		=  tokens + [tokenizer.unk_token]
												A		=  [2_29_43, 2_13_32, 3_44_31, 4_59_04, 1_17, 3_06, 12_31, 12_31, 26_53, 3_39_94, 12_66, 1_00]
												self.assertListEqual(tokenizer.convert_tokens_to_ids(__UpperCamelCase		)     ,       __UpperCamelCase		)


					def 	lowerCamelCase       (					self			:str		):
												A		=  self.get_rust_tokenizer()
												A,	A		=  self.get_chinese_input_output_texts()
												A		=  tokenizer.tokenize(__UpperCamelCase		)
												self.assertListEqual(__UpperCamelCase     ,       output_text.split()		)
												A		=  tokens + [tokenizer.unk_token]
												A		=  [2_29_43, 2_13_32, 3_44_31, 4_59_04, 1_17, 3_06, 12_31, 12_31, 26_53, 3_39_94, 12_66, 1_00]
												self.assertListEqual(tokenizer.convert_tokens_to_ids(__UpperCamelCase		)     ,       __UpperCamelCase		)


					def 	lowerCamelCase       (					self			:Any		):
												pass


					def 	lowerCamelCase       (					self			:Tuple		):
												pass


					def 	lowerCamelCase       (					self			:List[str]		):
												pass


