



'''simple docstring'''





import unittest

from transformers import DebertaConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

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


if is_torch_available():
       import torch

       from transformers import (
           DebertaForMaskedLM,
           DebertaForQuestionAnswering,
           DebertaForSequenceClassification,
           DebertaForTokenClassification,
           DebertaModel,
       )
       from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST







class     UpperCAmelCase_       (       _a	):
    """simple docstring"""







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




    def lowerCamelCase       (    self					:     int						):
          snake_case__  :    Optional[int]    						=      ids_tensor([self.batch_size, self.seq_length] ,     self.vocab_size						)

          snake_case__  :    int    						=      None
          if self.use_input_mask:
                snake_case__  :    Any    						=      ids_tensor([self.batch_size, self.seq_length] ,     vocab_size=2						)

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

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

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

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




    def lowerCamelCase       (    self					:     Tuple						):
          return DebertaConfig(
              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 ,     initializer_range=self.initializer_range ,     relative_attention=self.relative_attention ,     position_biased_input=self.position_biased_input ,     pos_att_type=self.pos_att_type ,     )




    def lowerCamelCase       (    self					:     List[Any]						):
          snake_case__  :    Any    						=      self.get_config()
          snake_case__  :    List[Any]    						=      300
          return config




    def lowerCamelCase       (    self					:     Dict ,     snake_case_					:     Optional[Any]						):
          self.parent.assertListEqual(list(result.loss.size()						) ,     []						)




    def lowerCamelCase       (    self					:     List[Any] ,     snake_case_					:     Optional[Any] ,     snake_case_					:     Tuple ,     snake_case_					:     int ,     snake_case_					:     List[str] ,     snake_case_					:     int ,     snake_case_					:     str ,     snake_case_					:     Optional[Any]						):
          snake_case__  :    Optional[int]    						=      DebertaModel(config=snake_case_						)
          model.to(snake_case_						)
          model.eval()
          snake_case__  :    Union[str, Any]    						=      model(snake_case_ ,     attention_mask=snake_case_ ,     token_type_ids=snake_case_						)[0]
          snake_case__  :    int    						=      model(snake_case_ ,     token_type_ids=snake_case_						)[0]
          snake_case__  :    int    						=      model(snake_case_						)[0]

          self.parent.assertListEqual(list(sequence_output.size()						) ,     [self.batch_size, self.seq_length, self.hidden_size]						)




    def lowerCamelCase       (    self					:     Dict ,     snake_case_					:     Tuple ,     snake_case_					:     Dict ,     snake_case_					:     Dict ,     snake_case_					:     Optional[Any] ,     snake_case_					:     Union[str, Any] ,     snake_case_					:     Dict ,     snake_case_					:     str						):
          snake_case__  :    List[str]    						=      DebertaForMaskedLM(config=snake_case_						)
          model.to(snake_case_						)
          model.eval()
          snake_case__  :    Dict    						=      model(snake_case_ ,     attention_mask=snake_case_ ,     token_type_ids=snake_case_ ,     labels=snake_case_						)

          self.parent.assertEqual(result.logits.shape ,     (self.batch_size, self.seq_length, self.vocab_size)						)




    def lowerCamelCase       (    self					:     Optional[int] ,     snake_case_					:     Optional[Any] ,     snake_case_					:     Tuple ,     snake_case_					:     Tuple ,     snake_case_					:     Union[str, Any] ,     snake_case_					:     str ,     snake_case_					:     Optional[int] ,     snake_case_					:     int						):
          snake_case__  :    int    						=      self.num_labels
          snake_case__  :    Dict    						=      DebertaForSequenceClassification(snake_case_						)
          model.to(snake_case_						)
          model.eval()
          snake_case__  :    List[str]    						=      model(snake_case_ ,     attention_mask=snake_case_ ,     token_type_ids=snake_case_ ,     labels=snake_case_						)
          self.parent.assertListEqual(list(result.logits.size()						) ,     [self.batch_size, self.num_labels]						)
          self.check_loss_output(snake_case_						)




    def lowerCamelCase       (    self					:     Optional[Any] ,     snake_case_					:     List[str] ,     snake_case_					:     Optional[Any] ,     snake_case_					:     List[Any] ,     snake_case_					:     Optional[Any] ,     snake_case_					:     Dict ,     snake_case_					:     int ,     snake_case_					:     str						):
          snake_case__  :    Optional[int]    						=      self.num_labels
          snake_case__  :    Tuple    						=      DebertaForTokenClassification(config=snake_case_						)
          model.to(snake_case_						)
          model.eval()
          snake_case__  :    Optional[int]    						=      model(snake_case_ ,     attention_mask=snake_case_ ,     token_type_ids=snake_case_ ,     labels=snake_case_						)
          self.parent.assertEqual(result.logits.shape ,     (self.batch_size, self.seq_length, self.num_labels)						)




    def lowerCamelCase       (    self					:     Dict ,     snake_case_					:     Optional[Any] ,     snake_case_					:     List[Any] ,     snake_case_					:     Optional[Any] ,     snake_case_					:     Optional[int] ,     snake_case_					:     List[Any] ,     snake_case_					:     Optional[int] ,     snake_case_					:     Optional[Any]						):
          snake_case__  :    str    						=      DebertaForQuestionAnswering(config=snake_case_						)
          model.to(snake_case_						)
          model.eval()
          snake_case__  :    Union[str, Any]    						=      model(
              snake_case_ ,     attention_mask=snake_case_ ,     token_type_ids=snake_case_ ,     start_positions=snake_case_ ,     end_positions=snake_case_ ,     )
          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					:     str						):
          snake_case__  :    List[str]    						=      self.prepare_config_and_inputs()
          (
              (
              snake_case__
          )						,				(
              snake_case__
          )						,				(
              snake_case__
          )						,				(
              snake_case__
          )						,				(
              snake_case__
          )						,				(
              snake_case__
          )						,				(
              snake_case__
          )						,				
          )  :    str    						=      config_and_inputs
          snake_case__  :    int    						=      {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
          return config, inputs_dict







@require_torch
class     UpperCAmelCase_       (       _a		,		_a		,		unittest.TestCase	):
    """simple docstring"""







    lowercase					=		(
        (
            DebertaModel,
            DebertaForMaskedLM,
            DebertaForSequenceClassification,
            DebertaForTokenClassification,
            DebertaForQuestionAnswering,
        )
        if is_torch_available()
        else ()
    )
    lowercase					=		(
        {
            "feature-extraction": DebertaModel,
            "fill-mask": DebertaForMaskedLM,
            "question-answering": DebertaForQuestionAnswering,
            "text-classification": DebertaForSequenceClassification,
            "token-classification": DebertaForTokenClassification,
            "zero-shot": DebertaForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )

    lowercase					=		True
    lowercase					=		False
    lowercase					=		False
    lowercase					=		False
    lowercase					=		False




    def lowerCamelCase       (    self					:     int						):
          snake_case__  :    Union[str, Any]    						=      DebertaModelTester(self						)
          snake_case__  :    List[str]    						=      ConfigTester(self ,     config_class=snake_case_ ,     hidden_size=37						)




    def lowerCamelCase       (    self					:     int						):
          self.config_tester.run_common_tests()




    def lowerCamelCase       (    self					:     Optional[Any]						):
          snake_case__  :    List[Any]    						=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_model(*snake_case_						)




    def lowerCamelCase       (    self					:     int						):
          snake_case__  :    Any    						=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_sequence_classification(*snake_case_						)




    def lowerCamelCase       (    self					:     Optional[Any]						):
          snake_case__  :    Optional[int]    						=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_masked_lm(*snake_case_						)




    def lowerCamelCase       (    self					:     Dict						):
          snake_case__  :    Dict    						=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_question_answering(*snake_case_						)




    def lowerCamelCase       (    self					:     Dict						):
          snake_case__  :    List[str]    						=      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_deberta_for_token_classification(*snake_case_						)




    @slow
    def lowerCamelCase       (    self					:     Optional[int]						):
          for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                snake_case__  :    Union[str, Any]    						=      DebertaModel.from_pretrained(snake_case_						)
                self.assertIsNotNone(snake_case_						)







@require_torch
@require_sentencepiece
@require_tokenizers
class     UpperCAmelCase_       (       unittest.TestCase	):
    """simple docstring"""







    @unittest.skip(reason="""Model not available yet"""						)
    def lowerCamelCase       (    self					:     int						):
          pass




    @slow
    def lowerCamelCase       (    self					:     Tuple						):
          snake_case__  :    List[str]    						=      DebertaModel.from_pretrained("""microsoft/deberta-base"""						)

          snake_case__  :    Optional[Any]    						=      torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]						)
          snake_case__  :    int    						=      torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
          with torch.no_grad():
                snake_case__  :    Any    						=      model(snake_case_ ,     attention_mask=snake_case_						)[0]
          # compare the actual values for a slice.
          snake_case__  :    Optional[int]    						=      torch.tensor(
              [[[-0.5986, -0.8055, -0.8462], [1.4484, -0.9348, -0.8059], [0.3123, 0.0032, -1.4131]]]						)
          self.assertTrue(torch.allclose(output[:, 1:4, 1:4] ,     snake_case_ ,     atol=1E-4						) ,     f"{output[:, 1:4, 1:4]}"						)


import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import BertTokenizer, BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import FEATURE_EXTRACTOR_NAME, is_vision_available


if is_vision_available():
	from PIL import Image

	from transformers import ChineseCLIPImageProcessor, ChineseCLIPProcessor







@require_vision
class        A_   (			unittest.TestCase   ):




	'''simple docstring'''


	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    tempfile.mkdtemp()

		lowercase       =    [
		    '[UNK]',
		    '[CLS]',
		    '[SEP]',
		    '[PAD]',
		    '[MASK]',
		    '的',
		    '价',
		    '格',
		    '是',
		    '15',
		    '便',
		    'alex',
		    '##andra',
		    '，',
		    '。',
		    '-',
		    't',
		    'shirt',
		]
		lowercase       =    os.path.join(self.tmpdirname		,    VOCAB_FILES_NAMES['vocab_file']       )
		with open(self.vocab_file		,    'w'		,    encoding='utf-8'       ) as vocab_writer:
			vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]       )       )

		lowercase       =    {
		    'do_resize': True,
		    'size': {'height': 224, 'width': 224},
		    'do_center_crop': True,
		    'crop_size': {'height': 18, 'width': 18},
		    'do_normalize': True,
		    'image_mean': [0.48_145_466, 0.4_578_275, 0.40_821_073],
		    'image_std': [0.26_862_954, 0.26_130_258, 0.27_577_711],
		    'do_convert_rgb': True,
		}
		lowercase       =    os.path.join(self.tmpdirname		,    snake_case       )
		with open(self.image_processor_file		,    'w'		,    encoding='utf-8'       ) as fp:
			json.dump(snake_case		,    snake_case       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    **snake_case       ):
		return BertTokenizer.from_pretrained(self.tmpdirname		,    **snake_case       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    **snake_case       ):
		return BertTokenizerFast.from_pretrained(self.tmpdirname		,    **snake_case       )





	def 	SCREAMING_SNAKE_CASE__						(       self		,    **snake_case       ):
		return ChineseCLIPImageProcessor.from_pretrained(self.tmpdirname		,    **snake_case       )





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		shutil.rmtree(self.tmpdirname       )





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    [np.random.randint(255		,    size=(3, 30, 400)		,    dtype=np.uinta       )]

		lowercase       =    [Image.fromarray(np.moveaxis(snake_case		,    0		,    -1       )       ) for x in image_inputs]

		return image_inputs





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    self.get_tokenizer()
		lowercase       =    self.get_rust_tokenizer()
		lowercase       =    self.get_image_processor()

		lowercase       =    ChineseCLIPProcessor(tokenizer=snake_case		,    image_processor=snake_case       )
		processor_slow.save_pretrained(self.tmpdirname       )
		lowercase       =    ChineseCLIPProcessor.from_pretrained(self.tmpdirname		,    use_fast=snake_case       )

		lowercase       =    ChineseCLIPProcessor(tokenizer=snake_case		,    image_processor=snake_case       )
		processor_fast.save_pretrained(self.tmpdirname       )
		lowercase       =    ChineseCLIPProcessor.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		,    snake_case       )
		self.assertIsInstance(processor_fast.tokenizer		,    snake_case       )

		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		,    snake_case       )
		self.assertIsInstance(processor_fast.image_processor		,    snake_case       )





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    ChineseCLIPProcessor(tokenizer=self.get_tokenizer()		,    image_processor=self.get_image_processor()       )
		processor.save_pretrained(self.tmpdirname       )

		lowercase       =    self.get_tokenizer(cls_token='(CLS)'		,    sep_token='(SEP)'       )
		lowercase       =    self.get_image_processor(do_normalize=snake_case       )

		lowercase       =    ChineseCLIPProcessor.from_pretrained(
		    self.tmpdirname		,    cls_token='(CLS)'		,    sep_token='(SEP)'		,    do_normalize=snake_case       )

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

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





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    self.get_image_processor()
		lowercase       =    self.get_tokenizer()

		lowercase       =    ChineseCLIPProcessor(tokenizer=snake_case		,    image_processor=snake_case       )

		lowercase       =    self.prepare_image_inputs()

		lowercase       =    image_processor(snake_case		,    return_tensors='np'       )
		lowercase       =    processor(images=snake_case		,    return_tensors='np'       )

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





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    self.get_image_processor()
		lowercase       =    self.get_tokenizer()

		lowercase       =    ChineseCLIPProcessor(tokenizer=snake_case		,    image_processor=snake_case       )

		lowercase       =    'Alexandra，T-shirt的价格是15便士。'

		lowercase       =    processor(text=snake_case       )

		lowercase       =    tokenizer(snake_case       )

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





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    self.get_image_processor()
		lowercase       =    self.get_tokenizer()

		lowercase       =    ChineseCLIPProcessor(tokenizer=snake_case		,    image_processor=snake_case       )

		lowercase       =    'Alexandra，T-shirt的价格是15便士。'
		lowercase       =    self.prepare_image_inputs()

		lowercase       =    processor(text=snake_case		,    images=snake_case       )

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

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





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    self.get_image_processor()
		lowercase       =    self.get_tokenizer()

		lowercase       =    ChineseCLIPProcessor(tokenizer=snake_case		,    image_processor=snake_case       )

		lowercase       =    [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

		lowercase       =    processor.batch_decode(snake_case       )
		lowercase       =    tokenizer.batch_decode(snake_case       )

		self.assertListEqual(snake_case		,    snake_case       )





	def 	SCREAMING_SNAKE_CASE__						(       self       ):
		lowercase       =    self.get_image_processor()
		lowercase       =    self.get_tokenizer()

		lowercase       =    ChineseCLIPProcessor(tokenizer=snake_case		,    image_processor=snake_case       )

		lowercase       =    'Alexandra，T-shirt的价格是15便士。'
		lowercase       =    self.prepare_image_inputs()

		lowercase       =    processor(text=snake_case		,    images=snake_case       )

		self.assertListEqual(list(inputs.keys()       )		,    processor.model_input_names       )

