




'''simple docstring'''




import io
import os
import unicodedata
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


a_      :		Tuple							      =       logging.get_logger(__name__)

a_      :		Tuple							      =       """▁"""

a_      :		int							      =       {"""vocab_file""": """vocab.txt""", """sentencepiece_model_ckpt""": """sentencepiece.bpe.model"""}

a_      :		List[Any]							      =       {
    """sentencepiece_model_file""": """sentencepiece.bpe.model""",
    """vocab_file""": """vocab.txt""",
}

a_      :		Dict							      =       {
    """vocab_file""": {
        """ernie-m-base""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt""",
        """ernie-m-large""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/vocab.txt""",
    },
    """sentencepiece_model_file""": {
        """ernie-m-base""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model""",
        """ernie-m-large""": """https://huggingface.co/susnato/ernie-m-base_pytorch/blob/main/sentencepiece.bpe.model""",
    },
}

a_      :		List[str]							      =       {
    """ernie-m-base""": 5_14,
    """ernie-m-large""": 5_14,
}

a_      :		Optional[int]							      =       {
    """ernie-m-base""": {"""do_lower_case""": False},
    """ernie-m-large""": {"""do_lower_case""": False},
}


class   __UpperCamelCase       ( lowerCamelCase__				):
       lowercase	:    List[str]      						=["input_ids"]

       lowercase	:    List[Any]      						=VOCAB_FILES_NAMES
       lowercase	:    Dict      						=PRETRAINED_INIT_CONFIGURATION
       lowercase	:    Union[str, Any]      						=PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       lowercase	:    Optional[Any]      						=PRETRAINED_VOCAB_FILES_MAP
       lowercase	:    List[Any]      						=RESOURCE_FILES_NAMES






       def __init__(							self,		lowerCAmelCase,		lowerCAmelCase=None,		lowerCAmelCase=False,		lowerCAmelCase="utf8",		lowerCAmelCase="[UNK]",		lowerCAmelCase="[SEP]",		lowerCAmelCase="[PAD]",		lowerCAmelCase="[CLS]",		lowerCAmelCase="[MASK]",		lowerCAmelCase = None,		**lowerCAmelCase,		):



              """simple docstring"""







              lowerCamelCase_							={} if sp_model_kwargs is None else sp_model_kwargs
              super().__init__(
                  do_lower_case=lowerCAmelCase,		unk_token=lowerCAmelCase,		sep_token=lowerCAmelCase,		pad_token=lowerCAmelCase,		cls_token=lowerCAmelCase,		mask_token=lowerCAmelCase,		vocab_file=lowerCAmelCase,		encoding=lowerCAmelCase,		sp_model_kwargs=self.sp_model_kwargs,		**lowerCAmelCase,		)
              lowerCamelCase_							=do_lower_case
              lowerCamelCase_							=sentencepiece_model_ckpt
              lowerCamelCase_							=spm.SentencePieceProcessor(**self.sp_model_kwargs  )
              self.sp_model.Load(lowerCAmelCase  )

              # to mimic paddlenlp.transformers.ernie_m.tokenizer.ErnieMTokenizer functioning
              if vocab_file is not None:
                     lowerCamelCase_							=self.load_vocab(filepath=lowerCAmelCase  )
              else:
                     lowerCamelCase_							={self.sp_model.id_to_piece(lowerCAmelCase  ): id for id in range(self.sp_model.get_piece_size()  )}
              lowerCamelCase_							={v: k for k, v in self.vocab.items()}






       def      lowercase__     (							self,		lowerCAmelCase  ):



              """simple docstring"""







              if text is None:
                     return None

              lowerCamelCase_							=self.tokenize(lowerCAmelCase  )
              lowerCamelCase_,      lowerCamelCase_							='''''', []

              for i, ch in enumerate(lowerCAmelCase  ):
                     if ch in self.SP_CHAR_MAPPING:
                            lowerCamelCase_							=self.SP_CHAR_MAPPING.get(lowerCAmelCase  )
                     else:
                            lowerCamelCase_							=unicodedata.normalize('''NFKC''',		lowerCAmelCase  )
                     if self.is_whitespace(lowerCAmelCase  ):
                            continue
                     normalized_text += ch
                     char_mapping.extend([i] * len(lowerCAmelCase  )  )

              lowerCamelCase_,      lowerCamelCase_,      lowerCamelCase_							=normalized_text, [], 0

              if self.do_lower_case:
                     lowerCamelCase_							=text.lower()

              for token in split_tokens:
                     if token[:1] == "▁":
                            lowerCamelCase_							=token[1:]
                     lowerCamelCase_							=text[offset:].index(lowerCAmelCase  ) + offset
                     lowerCamelCase_							=start + len(lowerCAmelCase  )

                     token_mapping.append((char_mapping[start], char_mapping[end - 1] + 1)  )
                     lowerCamelCase_							=end
              return token_mapping






       @property
       def      lowercase__     (							self  ):



              """simple docstring"""







              return len(self.vocab  )






       def      lowercase__     (							self  ):



              """simple docstring"""







              return dict(self.vocab,		**self.added_tokens_encoder  )






       def __getstate__(							self  ):



              """simple docstring"""







              lowerCamelCase_							=self.__dict__.copy()
              lowerCamelCase_							=None
              return state






       def __setstate__(							self,		lowerCAmelCase  ):



              """simple docstring"""







              lowerCamelCase_							=d

              # for backward compatibility
              if not hasattr(self,		'''sp_model_kwargs'''  ):
                     lowerCamelCase_							={}

              lowerCamelCase_							=spm.SentencePieceProcessor(**self.sp_model_kwargs  )
              self.sp_model.Load(self.sentencepiece_model_ckpt  )






       def      lowercase__     (							self,		lowerCAmelCase  ):



              """simple docstring"""







              return "".join((self.SP_CHAR_MAPPING.get(lowerCAmelCase,		lowerCAmelCase  ) for c in text)  )






       def      lowercase__     (							self,		lowerCAmelCase,		lowerCAmelCase=False,		lowerCAmelCase=64,		lowerCAmelCase=0.1  ):



              """simple docstring"""







              if self.sp_model_kwargs.get('''enable_sampling'''  ) is True:
                     lowerCamelCase_							=True
              if self.sp_model_kwargs.get('''alpha'''  ) is not None:
                     lowerCamelCase_							=self.sp_model_kwargs.get('''alpha'''  )
              if self.sp_model_kwargs.get('''nbest_size'''  ) is not None:
                     lowerCamelCase_							=self.sp_model_kwargs.get('''nbest_size'''  )

              if not enable_sampling:
                     lowerCamelCase_							=self.sp_model.EncodeAsPieces(lowerCAmelCase  )
              else:
                     lowerCamelCase_							=self.sp_model.SampleEncodeAsPieces(lowerCAmelCase,		lowerCAmelCase,		lowerCAmelCase  )
              lowerCamelCase_							=[]
              for pi, piece in enumerate(lowerCAmelCase  ):
                     if piece == SPIECE_UNDERLINE:
                            if not pieces[pi + 1].startswith(lowerCAmelCase  ) and pi != 0:
                                   new_pieces.append(lowerCAmelCase  )
                                   continue
                            else:
                                   continue
                     lowerCamelCase_							=0
                     for i, chunk in enumerate(lowerCAmelCase  ):
                            if chunk == SPIECE_UNDERLINE:
                                   continue
                            if self.is_ch_char(lowerCAmelCase  ) or self.is_punct(lowerCAmelCase  ):
                                   if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE:
                                          new_pieces.append(piece[lst_i:i]  )
                                   new_pieces.append(lowerCAmelCase  )
                                   lowerCamelCase_							=i + 1
                            elif chunk.isdigit() and i > 0 and not piece[i - 1].isdigit():
                                   if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE:
                                          new_pieces.append(piece[lst_i:i]  )
                                   lowerCamelCase_							=i
                            elif not chunk.isdigit() and i > 0 and piece[i - 1].isdigit():
                                   if i > lst_i and piece[lst_i:i] != SPIECE_UNDERLINE:
                                          new_pieces.append(piece[lst_i:i]  )
                                   lowerCamelCase_							=i
                     if len(lowerCAmelCase  ) > lst_i:
                            new_pieces.append(piece[lst_i:]  )
              return new_pieces






       def      lowercase__     (							self,		lowerCAmelCase  ):



              """simple docstring"""







              lowerCamelCase_							=''''''.join(lowerCAmelCase  ).replace(lowerCAmelCase,		''' '''  ).strip()
              return out_string






       def      lowercase__     (							self,		lowerCAmelCase  ):



              """simple docstring"""







              lowerCamelCase_							=self.convert_ids_to_tokens(lowerCAmelCase  )
              lowerCamelCase_							=''''''.join(lowerCAmelCase  ).replace(lowerCAmelCase,		''' '''  ).strip()
              return out_string






       def      lowercase__     (							self,		lowerCAmelCase  ):



              """simple docstring"""







              return self.vocab.get(lowerCAmelCase,		self.vocab.get(self.unk_token  )  )






       def      lowercase__     (							self,		lowerCAmelCase  ):



              """simple docstring"""







              return self.reverse_vocab.get(lowerCAmelCase,		self.unk_token  )






       def      lowercase__     (							self,		lowerCAmelCase,		lowerCAmelCase=None  ):



              """simple docstring"""







              if token_ids_a is None:
                     return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
              lowerCamelCase_							=[self.cls_token_id]
              lowerCamelCase_							=[self.sep_token_id]
              return _cls + token_ids_a + _sep + _sep + token_ids_a + _sep






       def      lowercase__     (							self,		lowerCAmelCase,		lowerCAmelCase=None  ):



              """simple docstring"""







              if offset_mapping_a is None:
                     return [(0, 0)] + offset_mapping_a + [(0, 0)]

              return [(0, 0)] + offset_mapping_a + [(0, 0), (0, 0)] + offset_mapping_a + [(0, 0)]






       def      lowercase__     (							self,		lowerCAmelCase,		lowerCAmelCase=None,		lowerCAmelCase=False  ):



              """simple docstring"""







              if already_has_special_tokens:
                     if token_ids_a is not None:
                            raise ValueError(
                                '''You should not supply a second sequence if the provided sequence of '''
                                '''ids is already formatted with special tokens for the model.'''  )
                     return [1 if x in [self.sep_token_id, self.cls_token_id] else 0 for x in token_ids_a]

              if token_ids_a is not None:
                     return [1] + ([0] * len(lowerCAmelCase  )) + [1, 1] + ([0] * len(lowerCAmelCase  )) + [1]
              return [1] + ([0] * len(lowerCAmelCase  )) + [1]






       def      lowercase__     (							self,		lowerCAmelCase,		lowerCAmelCase = None  ):



              """simple docstring"""







              if token_ids_a is None:
                     # [CLS] X [SEP]
                     return (len(lowerCAmelCase  ) + 2) * [0]

              # [CLS] A [SEP] [SEP] B [SEP]
              return [0] * (len(lowerCAmelCase  ) + 1) + [1] * (len(lowerCAmelCase  ) + 3)






       def      lowercase__     (							self,		lowerCAmelCase  ):



              """simple docstring"""







              if "\u4e00" <= char <= "\u9fff":
                     return True
              return False






       def      lowercase__     (							self,		lowerCAmelCase  ):



              """simple docstring"""







              if ("a" <= char <= "z") or ("A" <= char <= "Z"):
                     return True
              return False






       def      lowercase__     (							self,		lowerCAmelCase  ):



              """simple docstring"""







              if char in ",;:.?!~，；：。？！《》【】":
                     return True
              return False






       def      lowercase__     (							self,		lowerCAmelCase  ):



              """simple docstring"""







              if char == " " or char == "\t" or char == "\n" or char == "\r":
                     return True
              if len(lowerCAmelCase  ) == 1:
                     lowerCamelCase_							=unicodedata.category(lowerCAmelCase  )
                     if cat == "Zs":
                            return True
              return False






       def      lowercase__     (							self,		lowerCAmelCase  ):



              """simple docstring"""







              lowerCamelCase_							={}
              with io.open(lowerCAmelCase,		'''r''',		encoding='''utf-8'''  ) as f:
                     for index, line in enumerate(lowerCAmelCase  ):
                            lowerCamelCase_							=line.rstrip('''\n'''  )
                            lowerCamelCase_							=int(lowerCAmelCase  )

              return token_to_idx






       def      lowercase__     (							self,		lowerCAmelCase,		lowerCAmelCase = None  ):



              """simple docstring"""







              lowerCamelCase_							=0
              if os.path.isdir(lowerCAmelCase  ):
                     lowerCamelCase_							=os.path.join(
                         lowerCAmelCase,		(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']  )
              else:
                     lowerCamelCase_							=(filename_prefix + '''-''' if filename_prefix else '''''') + save_directory
              with open(lowerCAmelCase,		'''w''',		encoding='''utf-8'''  ) as writer:
                     for token, token_index in sorted(self.vocab.items(),		key=lambda lowerCAmelCase  : kv[1]  ):
                            if index != token_index:
                                   logger.warning(
                                       f'''Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.'''
                                       ''' Please check that the vocabulary is not corrupted!'''  )
                                   lowerCamelCase_							=token_index
                            writer.write(token + '''\n'''  )
                            index += 1

              lowerCamelCase_							=os.path.join(lowerCAmelCase,		'''sentencepiece.bpe.model'''  )
              with open(lowerCAmelCase,		'''wb'''  ) as fi:
                     lowerCamelCase_							=self.sp_model.serialized_model_proto()
                     fi.write(lowerCAmelCase  )

              return (vocab_file,)








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       A    (   A_ ):


							def __init__(self      ,				lowerCAmelCase      ,				lowerCAmelCase=1_3      ,				lowerCAmelCase=7      ,				lowerCAmelCase=True      ,				lowerCAmelCase=True      ,				lowerCAmelCase=False      ,				lowerCAmelCase=True      ,				lowerCAmelCase=9_9      ,				lowerCAmelCase=3_2      ,				lowerCAmelCase=5      ,				lowerCAmelCase=4      ,				lowerCAmelCase=3_7      ,				lowerCAmelCase="gelu"      ,				lowerCAmelCase=0.1      ,				lowerCAmelCase=0.1      ,				lowerCAmelCase=5_1_2      ,				lowerCAmelCase=1_6      ,				lowerCAmelCase=2      ,				lowerCAmelCase=0.02      ,				lowerCAmelCase=3      ,				lowerCAmelCase=4      ,				lowerCAmelCase=None      ,				):
									__lowercase=			parent
									__lowercase=			batch_size
									__lowercase=			seq_length
									__lowercase=			is_training
									__lowercase=			use_input_mask
									__lowercase=			use_token_type_ids
									__lowercase=			use_labels
									__lowercase=			vocab_size
									__lowercase=			hidden_size
									__lowercase=			num_hidden_layers
									__lowercase=			num_attention_heads
									__lowercase=			intermediate_size
									__lowercase=			hidden_act
									__lowercase=			hidden_dropout_prob
									__lowercase=			attention_probs_dropout_prob
									__lowercase=			max_position_embeddings
									__lowercase=			type_vocab_size
									__lowercase=			type_sequence_label_size
									__lowercase=			initializer_range
									__lowercase=			num_labels
									__lowercase=			num_choices
									__lowercase=			scope


							def 			_A       (self					):
									__lowercase=			ids_tensor([self.batch_size, self.seq_length]      ,				self.vocab_size					)

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

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

									__lowercase=			self.get_config()

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


							def 			_A       (self					):
									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 			_A       (self      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase					):
									__lowercase=			DistilBertModel(config=lowerCAmelCase					)
									model.to(lowerCAmelCase					)
									model.eval()
									__lowercase=			model(lowerCAmelCase      ,				lowerCAmelCase					)
									__lowercase=			model(lowerCAmelCase					)
									self.parent.assertEqual(result.last_hidden_state.shape      ,				(self.batch_size, self.seq_length, self.hidden_size)					)


							def 			_A       (self      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase					):
									__lowercase=			DistilBertForMaskedLM(config=lowerCAmelCase					)
									model.to(lowerCAmelCase					)
									model.eval()
									__lowercase=			model(lowerCAmelCase      ,				attention_mask=lowerCAmelCase      ,				labels=lowerCAmelCase					)
									self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.seq_length, self.vocab_size)					)


							def 			_A       (self      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase					):
									__lowercase=			DistilBertForQuestionAnswering(config=lowerCAmelCase					)
									model.to(lowerCAmelCase					)
									model.eval()
									__lowercase=			model(
									    lowerCAmelCase      ,				attention_mask=lowerCAmelCase      ,				start_positions=lowerCAmelCase      ,				end_positions=lowerCAmelCase					)
									self.parent.assertEqual(result.start_logits.shape      ,				(self.batch_size, self.seq_length)					)
									self.parent.assertEqual(result.end_logits.shape      ,				(self.batch_size, self.seq_length)					)


							def 			_A       (self      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase					):
									__lowercase=			self.num_labels
									__lowercase=			DistilBertForSequenceClassification(lowerCAmelCase					)
									model.to(lowerCAmelCase					)
									model.eval()
									__lowercase=			model(lowerCAmelCase      ,				attention_mask=lowerCAmelCase      ,				labels=lowerCAmelCase					)
									self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.num_labels)					)


							def 			_A       (self      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase					):
									__lowercase=			self.num_labels
									__lowercase=			DistilBertForTokenClassification(config=lowerCAmelCase					)
									model.to(lowerCAmelCase					)
									model.eval()

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


							def 			_A       (self      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase      ,				lowerCAmelCase					):
									__lowercase=			self.num_choices
									__lowercase=			DistilBertForMultipleChoice(config=lowerCAmelCase					)
									model.to(lowerCAmelCase					)
									model.eval()
									__lowercase=			input_ids.unsqueeze(1					).expand(-1      ,				self.num_choices      ,				-1					).contiguous()
									__lowercase=			input_mask.unsqueeze(1					).expand(-1      ,				self.num_choices      ,				-1					).contiguous()
									__lowercase=			model(
									    lowerCAmelCase      ,				attention_mask=lowerCAmelCase      ,				labels=lowerCAmelCase      ,				)
									self.parent.assertEqual(result.logits.shape      ,				(self.batch_size, self.num_choices)					)







							def 			_A       (self					):
									__lowercase=			self.prepare_config_and_inputs()
									((__lowercase),							(__lowercase),							(__lowercase),							(__lowercase),							(__lowercase),							(__lowercase))=			config_and_inputs
									__lowercase=			{'input_ids': input_ids, 'attention_mask': input_mask}
									return config, inputs_dict





@require_torch
class       A    (   A_					,				A_					,				unittest.TestCase ):
							UpperCamelCase_   :							Any       =(
							    (
							        DistilBertModel,
							        DistilBertForMaskedLM,
							        DistilBertForMultipleChoice,
							        DistilBertForQuestionAnswering,
							        DistilBertForSequenceClassification,
							        DistilBertForTokenClassification,
							    )
							    if is_torch_available()
							    else None
							)
							UpperCamelCase_   :							Optional[int]       =(
							    {
							        '''feature-extraction''': DistilBertModel,
							        '''fill-mask''': DistilBertForMaskedLM,
							        '''question-answering''': DistilBertForQuestionAnswering,
							        '''text-classification''': DistilBertForSequenceClassification,
							        '''token-classification''': DistilBertForTokenClassification,
							        '''zero-shot''': DistilBertForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							UpperCamelCase_   :							str       =True
							UpperCamelCase_   :							str       =True
							UpperCamelCase_   :							Union[str, Any]       =True
							UpperCamelCase_   :							Optional[int]       =True


							def 			_A       (self					):
									__lowercase=			DistilBertModelTester(self					)
									__lowercase=			ConfigTester(self      ,				config_class=lowerCAmelCase      ,				dim=3_7					)


							def 			_A       (self					):
									self.config_tester.run_common_tests()


							def 			_A       (self					):
									__lowercase=			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_model(*lowerCAmelCase					)


							def 			_A       (self					):
									__lowercase=			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_masked_lm(*lowerCAmelCase					)


							def 			_A       (self					):
									__lowercase=			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_question_answering(*lowerCAmelCase					)


							def 			_A       (self					):
									__lowercase=			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowerCAmelCase					)


							def 			_A       (self					):
									__lowercase=			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_token_classification(*lowerCAmelCase					)


							def 			_A       (self					):
									__lowercase=			self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowerCAmelCase					)


							@slow
							def 			_A       (self					):
									for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__lowercase=			DistilBertModel.from_pretrained(lowerCAmelCase					)
											self.assertIsNotNone(lowerCAmelCase					)







							@slow
							@require_torch_gpu
							def 			_A       (self					):
									__lowercase,							__lowercase=			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

											__lowercase=			True
											__lowercase=			model_class(config=lowerCAmelCase					)

											__lowercase=			self._prepare_for_class(lowerCAmelCase      ,				lowerCAmelCase					)
											__lowercase=			torch.jit.trace(
											    lowerCAmelCase      ,				(inputs_dict['input_ids'].to('cpu'					), inputs_dict['attention_mask'].to('cpu'					))					)

											with tempfile.TemporaryDirectory() as tmp:
													torch.jit.save(lowerCAmelCase      ,				os.path.join(lowerCAmelCase      ,				'traced_model.pt'					)					)
													__lowercase=			torch.jit.load(os.path.join(lowerCAmelCase      ,				'traced_model.pt'					)      ,				map_location=lowerCAmelCase					)
													loaded(inputs_dict['input_ids'].to(lowerCAmelCase					)      ,				inputs_dict['attention_mask'].to(lowerCAmelCase					)					)





@require_torch
class       A    (   unittest.TestCase ):


							@slow
							def 			_A       (self					):
									__lowercase=			DistilBertModel.from_pretrained('distilbert-base-uncased'					)
									__lowercase=			torch.tensor([[0, 3_4_5, 2_3_2, 3_2_8, 7_4_0, 1_4_0, 1_6_9_5, 6_9, 6_0_7_8, 1_5_8_8, 2]]					)
									__lowercase=			torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]					)
									with torch.no_grad():
											__lowercase=			model(lowerCAmelCase      ,				attention_mask=lowerCAmelCase					)[0]
									__lowercase=			torch.Size((1, 1_1, 7_6_8)					)
									self.assertEqual(output.shape      ,				lowerCAmelCase					)
									__lowercase=			torch.tensor(
									    [[[-0.16_39, 0.32_99, 0.16_48], [-0.17_46, 0.32_89, 0.17_10], [-0.18_84, 0.33_57, 0.18_10]]]					)

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






