


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   __a      :
 def __init__(       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__=13    ,			lowerCAmelCase__=7    ,			lowerCAmelCase__=True    ,			lowerCAmelCase__=True    ,			lowerCAmelCase__=True    ,			lowerCAmelCase__=True    ,			lowerCAmelCase__=99    ,			lowerCAmelCase__=32    ,			lowerCAmelCase__=5    ,			lowerCAmelCase__=4    ,			lowerCAmelCase__=37    ,			lowerCAmelCase__="gelu"    ,			lowerCAmelCase__=0.1    ,			lowerCAmelCase__=0.1    ,			lowerCAmelCase__=128    ,			lowerCAmelCase__=32    ,			lowerCAmelCase__=16    ,			lowerCAmelCase__=2    ,			lowerCAmelCase__=0.0_2    ,			lowerCAmelCase__=3    ,			lowerCAmelCase__=4    ,			lowerCAmelCase__=None    ,			) ->    List[Any]:




        '''simple docstring'''
        lowercase__:       Union[str, Any]        =       parent
        lowercase__:       str        =       batch_size
        lowercase__:       Dict        =       seq_length
        lowercase__:       str        =       is_training
        lowercase__:       List[str]        =       use_input_mask
        lowercase__:       str        =       use_token_type_ids
        lowercase__:       Tuple        =       use_labels
        lowercase__:       int        =       vocab_size
        lowercase__:       Dict        =       hidden_size
        lowercase__:       Tuple        =       num_hidden_layers
        lowercase__:       Tuple        =       num_attention_heads
        lowercase__:       List[Any]        =       intermediate_size
        lowercase__:       Dict        =       hidden_act
        lowercase__:       List[str]        =       hidden_dropout_prob
        lowercase__:       str        =       attention_probs_dropout_prob
        lowercase__:       Dict        =       max_position_embeddings
        lowercase__:       Optional[Any]        =       type_vocab_size
        lowercase__:       List[str]        =       type_sequence_label_size
        lowercase__:       Optional[int]        =       initializer_range
        lowercase__:       Optional[int]        =       num_labels
        lowercase__:       Union[str, Any]        =       num_choices
        lowercase__:       int        =       scope
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[Any]:




        '''simple docstring'''
        lowercase__:       Dict        =       ids_tensor([self.batch_size, self.seq_length]    ,			self.vocab_size			)

        lowercase__:       List[str]        =       None
        if self.use_input_mask:
               lowercase__:       Tuple        =       random_attention_mask([self.batch_size, self.seq_length]			)

        lowercase__:       Optional[int]        =       None
        if self.use_token_type_ids:
               lowercase__:       Union[str, Any]        =       ids_tensor([self.batch_size, self.seq_length]    ,			self.type_vocab_size			)

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

        lowercase__:       Dict        =       self.get_config()

        return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Optional[int]:




        '''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=lowerCAmelCase__    ,			initializer_range=self.initializer_range    ,			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Dict:




        '''simple docstring'''
        (
            lowercase__						
        ):       Tuple        =       self.prepare_config_and_inputs()

        lowercase__:       Optional[int]        =       True
        lowercase__:       Optional[int]        =       floats_tensor([self.batch_size, self.seq_length, self.hidden_size]			)
        lowercase__:       Union[str, 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 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__			) ->    int:




        '''simple docstring'''
        lowercase__:       List[Any]        =       NezhaModel(config=lowerCAmelCase__			)
        model.to(lowerCAmelCase__			)
        model.eval()
        lowercase__:       Dict        =       model(lowerCAmelCase__    ,			attention_mask=lowerCAmelCase__    ,			token_type_ids=lowerCAmelCase__			)
        lowercase__:       Union[str, Any]        =       model(lowerCAmelCase__    ,			token_type_ids=lowerCAmelCase__			)
        lowercase__:       str        =       model(lowerCAmelCase__			)
        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 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			) ->    List[Any]:




        '''simple docstring'''
        lowercase__:       Dict        =       True
        lowercase__:       Optional[Any]        =       NezhaModel(lowerCAmelCase__			)
        model.to(lowerCAmelCase__			)
        model.eval()
        lowercase__:       List[Any]        =       model(
            lowerCAmelCase__    ,			attention_mask=lowerCAmelCase__    ,			token_type_ids=lowerCAmelCase__    ,			encoder_hidden_states=lowerCAmelCase__    ,			encoder_attention_mask=lowerCAmelCase__    ,			)
        lowercase__:       str        =       model(
            lowerCAmelCase__    ,			attention_mask=lowerCAmelCase__    ,			token_type_ids=lowerCAmelCase__    ,			encoder_hidden_states=lowerCAmelCase__    ,			)
        lowercase__:       List[str]        =       model(lowerCAmelCase__    ,			attention_mask=lowerCAmelCase__    ,			token_type_ids=lowerCAmelCase__			)
        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 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__			) ->    Any:




        '''simple docstring'''
        lowercase__:       Tuple        =       NezhaForMaskedLM(config=lowerCAmelCase__			)
        model.to(lowerCAmelCase__			)
        model.eval()
        lowercase__:       int        =       model(lowerCAmelCase__    ,			attention_mask=lowerCAmelCase__    ,			token_type_ids=lowerCAmelCase__    ,			labels=lowerCAmelCase__			)
        self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.seq_length, self.vocab_size)			)
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__			) ->    List[str]:




        '''simple docstring'''
        lowercase__:       Any        =       NezhaForNextSentencePrediction(config=lowerCAmelCase__			)
        model.to(lowerCAmelCase__			)
        model.eval()
        lowercase__:       int        =       model(
            lowerCAmelCase__    ,			attention_mask=lowerCAmelCase__    ,			token_type_ids=lowerCAmelCase__    ,			labels=lowerCAmelCase__    ,			)
        self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, 2)			)
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__			) ->    List[str]:




        '''simple docstring'''
        lowercase__:       Union[str, Any]        =       NezhaForPreTraining(config=lowerCAmelCase__			)
        model.to(lowerCAmelCase__			)
        model.eval()
        lowercase__:       List[str]        =       model(
            lowerCAmelCase__    ,			attention_mask=lowerCAmelCase__    ,			token_type_ids=lowerCAmelCase__    ,			labels=lowerCAmelCase__    ,			next_sentence_label=lowerCAmelCase__    ,			)
        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 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__			) ->    Optional[int]:




        '''simple docstring'''
        lowercase__:       List[Any]        =       NezhaForQuestionAnswering(config=lowerCAmelCase__			)
        model.to(lowerCAmelCase__			)
        model.eval()
        lowercase__:       List[Any]        =       model(
            lowerCAmelCase__    ,			attention_mask=lowerCAmelCase__    ,			token_type_ids=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 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__			) ->    Tuple:




        '''simple docstring'''
        lowercase__:       Optional[Any]        =       self.num_labels
        lowercase__:       List[Any]        =       NezhaForSequenceClassification(lowerCAmelCase__			)
        model.to(lowerCAmelCase__			)
        model.eval()
        lowercase__:       Union[str, Any]        =       model(lowerCAmelCase__    ,			attention_mask=lowerCAmelCase__    ,			token_type_ids=lowerCAmelCase__    ,			labels=lowerCAmelCase__			)
        self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.num_labels)			)
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__			) ->    Optional[Any]:




        '''simple docstring'''
        lowercase__:       Union[str, Any]        =       self.num_labels
        lowercase__:       Dict        =       NezhaForTokenClassification(config=lowerCAmelCase__			)
        model.to(lowerCAmelCase__			)
        model.eval()
        lowercase__:       List[str]        =       model(lowerCAmelCase__    ,			attention_mask=lowerCAmelCase__    ,			token_type_ids=lowerCAmelCase__    ,			labels=lowerCAmelCase__			)
        self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.seq_length, self.num_labels)			)
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__			) ->    Dict:




        '''simple docstring'''
        lowercase__:       List[str]        =       self.num_choices
        lowercase__:       str        =       NezhaForMultipleChoice(config=lowerCAmelCase__			)
        model.to(lowerCAmelCase__			)
        model.eval()
        lowercase__:       Optional[Any]        =       input_ids.unsqueeze(1			).expand(-1    ,			self.num_choices    ,			-1			).contiguous()
        lowercase__:       Optional[Any]        =       token_type_ids.unsqueeze(1			).expand(-1    ,			self.num_choices    ,			-1			).contiguous()
        lowercase__:       Optional[Any]        =       input_mask.unsqueeze(1			).expand(-1    ,			self.num_choices    ,			-1			).contiguous()
        lowercase__:       int        =       model(
            lowerCAmelCase__    ,			attention_mask=lowerCAmelCase__    ,			token_type_ids=lowerCAmelCase__    ,			labels=lowerCAmelCase__    ,			)
        self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.num_choices)			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Dict:




        '''simple docstring'''
        lowercase__:       str        =       self.prepare_config_and_inputs()
        (
            lowercase__						
        ):       str        =       config_and_inputs
        lowercase__:       Union[str, Any]        =       {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
        return config, inputs_dict





@require_torch
class   __a      (   __UpperCamelCase	,	__UpperCamelCase	,	__UpperCamelCase	,	unittest.TestCase				):
 __lowercase			:							List[Any]       					=			(
     (
         NezhaModel,
         NezhaForMaskedLM,
         NezhaForMultipleChoice,
         NezhaForNextSentencePrediction,
         NezhaForPreTraining,
         NezhaForQuestionAnswering,
         NezhaForSequenceClassification,
         NezhaForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 __lowercase			:							Any       					=			(
     {
         'feature-extraction': NezhaModel,
         'fill-mask': NezhaForMaskedLM,
         'question-answering': NezhaForQuestionAnswering,
         'text-classification': NezhaForSequenceClassification,
         'token-classification': NezhaForTokenClassification,
         'zero-shot': NezhaForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 __lowercase			:							Dict       					=			True
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__=False			) ->    List[Any]:




        '''simple docstring'''
        lowercase__:       Any        =       super()._prepare_for_class(lowerCAmelCase__    ,			lowerCAmelCase__    ,			return_labels=lowerCAmelCase__			)

        if return_labels:
               if model_class in get_values(lowerCAmelCase__			):
                      lowercase__:       int        =       torch.zeros(
                          (self.model_tester.batch_size, self.model_tester.seq_length)    ,			dtype=torch.long    ,			device=lowerCAmelCase__			)
                      lowercase__:       Dict        =       torch.zeros(
                          self.model_tester.batch_size    ,			dtype=torch.long    ,			device=lowerCAmelCase__			)
        return inputs_dict
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[Any]:




        '''simple docstring'''
        lowercase__:       List[Any]        =       NezhaModelTester(self			)
        lowercase__:       Optional[int]        =       ConfigTester(self    ,			config_class=lowerCAmelCase__    ,			hidden_size=37			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Dict:




        '''simple docstring'''
        self.config_tester.run_common_tests()
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Dict:




        '''simple docstring'''
        lowercase__:       Dict        =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCAmelCase__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Tuple:




        '''simple docstring'''
        lowercase__:       Union[str, Any]        =       self.model_tester.prepare_config_and_inputs_for_decoder()
        self.model_tester.create_and_check_model_as_decoder(*lowerCAmelCase__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    int:




        '''simple docstring'''
        (
            lowercase__						
        ):       List[Any]        =       self.model_tester.prepare_config_and_inputs_for_decoder()

        lowercase__:       str        =       None

        self.model_tester.create_and_check_model_as_decoder(
            lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			lowerCAmelCase__    ,			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Optional[Any]:




        '''simple docstring'''
        lowercase__:       Tuple        =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    int:




        '''simple docstring'''
        lowercase__:       Union[str, Any]        =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*lowerCAmelCase__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Optional[int]:




        '''simple docstring'''
        lowercase__:       List[Any]        =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_next_sequence_prediction(*lowerCAmelCase__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Tuple:




        '''simple docstring'''
        lowercase__:       int        =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_pretraining(*lowerCAmelCase__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Optional[int]:




        '''simple docstring'''
        lowercase__:       int        =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Optional[int]:




        '''simple docstring'''
        lowercase__:       Union[str, Any]        =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Union[str, Any]:




        '''simple docstring'''
        lowercase__:       Any        =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase__			)
 @slow
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    int:




        '''simple docstring'''
        for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowercase__:       List[str]        =       NezhaModel.from_pretrained(lowerCAmelCase__			)
               self.assertIsNotNone(lowerCAmelCase__			)
 @slow
 @require_torch_gpu
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[Any]:




        '''simple docstring'''
        lowercase__:       int        =       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

               lowercase__:       Optional[int]        =       True
               lowercase__:       Optional[int]        =       model_class(config=lowerCAmelCase__			)

               lowercase__:       Dict        =       self._prepare_for_class(lowerCAmelCase__    ,			lowerCAmelCase__			)
               lowercase__:       List[Any]        =       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__    ,			'bert.pt'			)			)
                      lowercase__:       List[str]        =       torch.jit.load(os.path.join(lowerCAmelCase__    ,			'bert.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 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[Any]:




        '''simple docstring'''
        lowercase__:       Optional[Any]        =       NezhaModel.from_pretrained('sijunhe/nezha-cn-base'			)
        lowercase__:       Tuple        =       torch.tensor([[0, 1, 2, 3, 4, 5]]			)
        lowercase__:       Dict        =       torch.tensor([[0, 1, 1, 1, 1, 1]]			)
        with torch.no_grad():
               lowercase__:       Union[str, Any]        =       model(lowerCAmelCase__    ,			attention_mask=lowerCAmelCase__			)[0]
        lowercase__:       Optional[Any]        =       torch.Size((1, 6, 768)			)
        self.assertEqual(output.shape    ,			lowerCAmelCase__			)
        lowercase__:       Optional[Any]        =       torch.tensor([[[0.0_6_8_5, 0.2_4_4_1, 0.1_1_0_2], [0.0_6_0_0, 0.1_9_0_6, 0.1_3_4_9], [0.0_2_2_1, 0.0_8_1_9, 0.0_5_8_6]]]			)

        self.assertTrue(torch.allclose(output[:, 1:4, 1:4]    ,			lowerCAmelCase__    ,			atol=1E-4			)			)
 @slow
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[str]:




        '''simple docstring'''
        lowercase__:       int        =       NezhaForMaskedLM.from_pretrained('sijunhe/nezha-cn-base'			)
        lowercase__:       Optional[Any]        =       torch.tensor([[0, 1, 2, 3, 4, 5]]			)
        lowercase__:       Optional[Any]        =       torch.tensor([[1, 1, 1, 1, 1, 1]]			)
        with torch.no_grad():
               lowercase__:       Dict        =       model(lowerCAmelCase__    ,			attention_mask=lowerCAmelCase__			)[0]
        lowercase__:       int        =       torch.Size((1, 6, 21_128)			)
        self.assertEqual(output.shape    ,			lowerCAmelCase__			)
        lowercase__:       Union[str, Any]        =       torch.tensor(
            [[-2.7_9_3_9, -1.7_9_0_2, -2.2_1_8_9], [-2.8_5_8_5, -1.8_9_0_8, -2.3_7_2_3], [-2.6_4_9_9, -1.7_7_5_0, -2.2_5_5_8]]			)

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






import json
import os
import unittest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_ftfy, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin





@require_tokenizers
class   __a      (   __UpperCamelCase	,	unittest.TestCase				):
 __lowercase			:							Optional[int]       					=			CLIPTokenizer
 __lowercase			:							str       					=			CLIPTokenizerFast
 __lowercase			:							Tuple       					=			True
 __lowercase			:							str       					=			{}
 __lowercase			:							Dict       					=			False
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    int:




        '''simple docstring'''
        super().setUp()

        # fmt: off
        lowercase__:       str        =       ['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
        lowercase__:       List[str]        =       dict(zip(lowerCAmelCase__    ,			range(len(lowerCAmelCase__			)			)			)			)
        lowercase__:       int        =       ['#version: 0.2', 'l o', 'lo w</w>', 'e r</w>']
        lowercase__:       Optional[int]        =       {'unk_token': '<unk>'}

        lowercase__:       int        =       os.path.join(self.tmpdirname    ,			VOCAB_FILES_NAMES['vocab_file']			)
        lowercase__:       Union[str, Any]        =       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(lowerCAmelCase__			) + '\n'			)
        with open(self.merges_file    ,			'w'    ,			encoding='utf-8'			) as fp:
               fp.write('\n'.join(lowerCAmelCase__			)			)
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			**lowerCAmelCase__			) ->    List[str]:




        '''simple docstring'''
        kwargs.update(self.special_tokens_map			)
        return CLIPTokenizer.from_pretrained(self.tmpdirname    ,			**lowerCAmelCase__			)
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			**lowerCAmelCase__			) ->    Optional[int]:




        '''simple docstring'''
        kwargs.update(self.special_tokens_map			)
        return CLIPTokenizerFast.from_pretrained(self.tmpdirname    ,			**lowerCAmelCase__			)
 def 		SCREAMING_SNAKE_CASE__       (       self    ,			lowerCAmelCase__			) ->    Union[str, Any]:




        '''simple docstring'''
        lowercase__:       Dict        =       'lower newer'
        lowercase__:       Dict        =       'lower newer'
        return input_text, output_text
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[Any]:




        '''simple docstring'''
        lowercase__:       Union[str, Any]        =       CLIPTokenizer(self.vocab_file    ,			self.merges_file    ,			**self.special_tokens_map			)
        lowercase__:       Dict        =       'lower newer'
        lowercase__:       Union[str, Any]        =       ['lo', 'w', 'er</w>', 'n', 'e', 'w', 'er</w>']
        lowercase__:       Any        =       tokenizer.tokenize(lowerCAmelCase__			)
        self.assertListEqual(lowerCAmelCase__    ,			lowerCAmelCase__			)

        lowercase__:       Tuple        =       tokens + [tokenizer.unk_token]
        lowercase__:       Tuple        =       [10, 2, 16, 9, 3, 2, 16, 20]
        self.assertListEqual(tokenizer.convert_tokens_to_ids(lowerCAmelCase__			)    ,			lowerCAmelCase__			)
 @require_ftfy
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Tuple:




        '''simple docstring'''
        for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
               with self.subTest(F'{tokenizer.__class__.__name__} ({pretrained_name})'			):
                      lowercase__:       List[str]        =       self.tokenizer_class.from_pretrained(lowerCAmelCase__    ,			**lowerCAmelCase__			)
                      lowercase__:       Optional[int]        =       self.rust_tokenizer_class.from_pretrained(lowerCAmelCase__    ,			**lowerCAmelCase__			)

                      lowercase__:       Optional[int]        =       'A\n\'ll 11p223RF☆ho!!to?\'d\'d\'\'d of a cat to-$\'\'d.'
                      lowercase__:       Optional[Any]        =       tokenizer_s.tokenize(lowerCAmelCase__			)
                      lowercase__:       Dict        =       tokenizer_r.tokenize(lowerCAmelCase__			)

                      self.assertListEqual(lowerCAmelCase__    ,			lowerCAmelCase__			)

                      # Test that the tokenization is identical on an example containing a character (Latin Small Letter A
                      # with Tilde) encoded in 2 different ways
                      lowercase__:       Dict        =       'xa\u0303y' + ' ' + 'x\xe3y'
                      lowercase__:       Tuple        =       tokenizer_s.tokenize(lowerCAmelCase__			)
                      lowercase__:       int        =       tokenizer_r.tokenize(lowerCAmelCase__			)

                      self.assertListEqual(lowerCAmelCase__    ,			lowerCAmelCase__			)

                      # Test that the tokenization is identical on unicode of space type
                      lowercase__:       str        =       [
                          '\u0009',  # (horizontal tab, '\t')
                          '\u000B',  # (vertical tab)
                          '\u000C',  # (form feed)
                          '\u0020',  # (space, ' ')
                          '\u200E',  # (left-to-right mark):w
                          '\u200F',  # (right-to-left mark)
                      ]
                      for unicode_seq in spaces_unicodes:
                             lowercase__:       Optional[Any]        =       tokenizer_s.tokenize(lowerCAmelCase__			)
                             lowercase__:       Tuple        =       tokenizer_r.tokenize(lowerCAmelCase__			)

                             self.assertListEqual(lowerCAmelCase__    ,			lowerCAmelCase__			)

                      # Test that the tokenization is identical on unicode of line break type
                      lowercase__:       str        =       [
                          '\u000A',  # (line feed, '\n')
                          '\r\n',  # (carriage return and line feed, '\r\n')
                          '\u000D',  # (carriage return, '\r')
                          '\r',  # (carriage return, '\r')
                          '\u000D',  # (carriage return, '\r')
                          '\u2028',  # (line separator)
                          '\u2029',  # (paragraph separator)
                          # "\u0085", # (next line)
                      ]

                      # The tokenization is not identical for the character "\u0085" (next line). The slow version using ftfy transforms
                      # it into the Horizontal Ellipsis character "…" ("\u2026") while the fast version transforms it into a
                      # space (and thus into an empty list).

                      for unicode_seq in line_break_unicodes:
                             lowercase__:       Optional[int]        =       tokenizer_s.tokenize(lowerCAmelCase__			)
                             lowercase__:       Optional[int]        =       tokenizer_r.tokenize(lowerCAmelCase__			)

                             self.assertListEqual(lowerCAmelCase__    ,			lowerCAmelCase__			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Optional[int]:




        '''simple docstring'''
        # Test which aims to verify that the offsets are well adapted to the argument `add_prefix_space`
        for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
               with self.subTest(F'{tokenizer.__class__.__name__} ({pretrained_name})'			):
                      lowercase__:       Optional[int]        =       'hello'  # `hello` is a token in the vocabulary of `pretrained_name`
                      lowercase__:       Optional[int]        =       F'{text_of_1_token} {text_of_1_token}'

                      lowercase__:       int        =       self.rust_tokenizer_class.from_pretrained(
                          lowerCAmelCase__    ,			use_fast=lowerCAmelCase__    ,			)
                      lowercase__:       Dict        =       tokenizer_r(lowerCAmelCase__    ,			return_offsets_mapping=lowerCAmelCase__    ,			add_special_tokens=lowerCAmelCase__			)
                      self.assertEqual(encoding.offset_mapping[0]    ,			(0, len(lowerCAmelCase__			))			)
                      self.assertEqual(
                          encoding.offset_mapping[1]    ,			(len(lowerCAmelCase__			) + 1, len(lowerCAmelCase__			) + 1 + len(lowerCAmelCase__			))    ,			)

                      lowercase__:       Any        =       F' {text}'

                      lowercase__:       Union[str, Any]        =       self.rust_tokenizer_class.from_pretrained(
                          lowerCAmelCase__    ,			use_fast=lowerCAmelCase__    ,			)
                      lowercase__:       int        =       tokenizer_r(lowerCAmelCase__    ,			return_offsets_mapping=lowerCAmelCase__    ,			add_special_tokens=lowerCAmelCase__			)
                      self.assertEqual(encoding.offset_mapping[0]    ,			(1, 1 + len(lowerCAmelCase__			))			)
                      self.assertEqual(
                          encoding.offset_mapping[1]    ,			(1 + len(lowerCAmelCase__			) + 1, 1 + len(lowerCAmelCase__			) + 1 + len(lowerCAmelCase__			))    ,			)
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[str]:




        '''simple docstring'''
        # Test related to the breaking change introduced in transformers v4.17.0
        # We need to check that an error in raised when the user try to load a previous version of the tokenizer.
        with self.assertRaises(lowerCAmelCase__			) as context:
               self.rust_tokenizer_class.from_pretrained('robot-test/old-clip-tokenizer'			)

        self.assertTrue(
            context.exception.args[0].startswith(
                'The `backend_tokenizer` provided does not match the expected format.'			)			)
 @require_ftfy
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    List[Any]:




        '''simple docstring'''
        super().test_tokenization_python_rust_equals()
 def 		SCREAMING_SNAKE_CASE__       (       self			) ->    Tuple:




        '''simple docstring'''
        # CLIP always lower cases letters
        pass


