




from __future__ import annotations

import unittest

from transformers import DistilBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
  import tensorflow as tf

  from transformers.models.distilbert.modeling_tf_distilbert import (
      TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFDistilBertForMaskedLM,
      TFDistilBertForMultipleChoice,
      TFDistilBertForQuestionAnswering,
      TFDistilBertForSequenceClassification,
      TFDistilBertForTokenClassification,
      TFDistilBertModel,
  )
class 	_UpperCamelCase					:
      def __init__(	self      :Tuple       ,					lowerCamelCase      :str       ,					)   ->	int:
       UpperCAmelCase__							       =  parent
       UpperCAmelCase__							       =  13
       UpperCAmelCase__							       =  7
       UpperCAmelCase__							       =  True
       UpperCAmelCase__							       =  True
       UpperCAmelCase__							       =  False
       UpperCAmelCase__							       =  True
       UpperCAmelCase__							       =  99
       UpperCAmelCase__							       =  32
       UpperCAmelCase__							       =  2
       UpperCAmelCase__							       =  4
       UpperCAmelCase__							       =  37
       UpperCAmelCase__							       =  "gelu"
       UpperCAmelCase__							       =  0.1
       UpperCAmelCase__							       =  0.1
       UpperCAmelCase__							       =  512
       UpperCAmelCase__							       =  16
       UpperCAmelCase__							       =  2
       UpperCAmelCase__							       =  0.02
       UpperCAmelCase__							       =  3
       UpperCAmelCase__							       =  4
       UpperCAmelCase__							       =  None
      def 		UpperCAmelCase_  (	self      :List[Any]					)   ->	Optional[Any]:
       UpperCAmelCase__							       =  ids_tensor([self.batch_size, self.seq_length]       ,					self.vocab_size					)

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

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

       UpperCAmelCase__							       =  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       ,					)

       return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
      def 		UpperCAmelCase_  (	self      :Optional[int]       ,					lowerCamelCase      :str       ,					lowerCamelCase      :List[str]       ,					lowerCamelCase      :Optional[Any]       ,					lowerCamelCase      :Optional[int]       ,					lowerCamelCase      :Union[str, Any]       ,					lowerCamelCase      :List[Any]					)   ->	Union[str, Any]:
       UpperCAmelCase__							       =  TFDistilBertModel(config=lowerCAmelCase__					)
       UpperCAmelCase__							       =  {"input_ids": input_ids, "attention_mask": input_mask}

       UpperCAmelCase__							       =  model(lowerCAmelCase__					)

       UpperCAmelCase__							       =  [input_ids, input_mask]

       UpperCAmelCase__							       =  model(lowerCAmelCase__					)

       self.parent.assertEqual(result.last_hidden_state.shape       ,					(self.batch_size, self.seq_length, self.hidden_size)					)
      def 		UpperCAmelCase_  (	self      :Optional[int]       ,					lowerCamelCase      :Dict       ,					lowerCamelCase      :int       ,					lowerCamelCase      :Optional[int]       ,					lowerCamelCase      :Tuple       ,					lowerCamelCase      :Optional[Any]       ,					lowerCamelCase      :Dict					)   ->	Union[str, Any]:
       UpperCAmelCase__							       =  TFDistilBertForMaskedLM(config=lowerCAmelCase__					)
       UpperCAmelCase__							       =  {"input_ids": input_ids, "attention_mask": input_mask}
       UpperCAmelCase__							       =  model(lowerCAmelCase__					)
       self.parent.assertEqual(result.logits.shape       ,					(self.batch_size, self.seq_length, self.vocab_size)					)
      def 		UpperCAmelCase_  (	self      :Union[str, Any]       ,					lowerCamelCase      :List[Any]       ,					lowerCamelCase      :List[str]       ,					lowerCamelCase      :List[Any]       ,					lowerCamelCase      :Dict       ,					lowerCamelCase      :Optional[int]       ,					lowerCamelCase      :Optional[int]					)   ->	List[Any]:
       UpperCAmelCase__							       =  TFDistilBertForQuestionAnswering(config=lowerCAmelCase__					)
       UpperCAmelCase__							       =  {
           "input_ids": input_ids,
           "attention_mask": input_mask,
       }
       UpperCAmelCase__							       =  model(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 		UpperCAmelCase_  (	self      :Union[str, Any]       ,					lowerCamelCase      :Any       ,					lowerCamelCase      :str       ,					lowerCamelCase      :Optional[Any]       ,					lowerCamelCase      :Union[str, Any]       ,					lowerCamelCase      :Union[str, Any]       ,					lowerCamelCase      :int					)   ->	Optional[Any]:
       UpperCAmelCase__							       =  self.num_labels
       UpperCAmelCase__							       =  TFDistilBertForSequenceClassification(lowerCAmelCase__					)
       UpperCAmelCase__							       =  {"input_ids": input_ids, "attention_mask": input_mask}
       UpperCAmelCase__							       =  model(lowerCAmelCase__					)
       self.parent.assertEqual(result.logits.shape       ,					(self.batch_size, self.num_labels)					)
      def 		UpperCAmelCase_  (	self      :List[str]       ,					lowerCamelCase      :str       ,					lowerCamelCase      :str       ,					lowerCamelCase      :int       ,					lowerCamelCase      :Optional[Any]       ,					lowerCamelCase      :Optional[Any]       ,					lowerCamelCase      :Union[str, Any]					)   ->	Union[str, Any]:
       UpperCAmelCase__							       =  self.num_choices
       UpperCAmelCase__							       =  TFDistilBertForMultipleChoice(lowerCAmelCase__					)
       UpperCAmelCase__							       =  tf.tile(tf.expand_dims(lowerCAmelCase__       ,					1					)       ,					(1, self.num_choices, 1)					)
       UpperCAmelCase__							       =  tf.tile(tf.expand_dims(lowerCAmelCase__       ,					1					)       ,					(1, self.num_choices, 1)					)
       UpperCAmelCase__							       =  {
           "input_ids": multiple_choice_inputs_ids,
           "attention_mask": multiple_choice_input_mask,
       }
       UpperCAmelCase__							       =  model(lowerCAmelCase__					)
       self.parent.assertEqual(result.logits.shape       ,					(self.batch_size, self.num_choices)					)
      def 		UpperCAmelCase_  (	self      :Union[str, Any]       ,					lowerCamelCase      :Any       ,					lowerCamelCase      :Optional[int]       ,					lowerCamelCase      :Dict       ,					lowerCamelCase      :Optional[Any]       ,					lowerCamelCase      :List[str]       ,					lowerCamelCase      :str					)   ->	Optional[Any]:
       UpperCAmelCase__							       =  self.num_labels
       UpperCAmelCase__							       =  TFDistilBertForTokenClassification(lowerCAmelCase__					)
       UpperCAmelCase__							       =  {"input_ids": input_ids, "attention_mask": input_mask}
       UpperCAmelCase__							       =  model(lowerCAmelCase__					)
       self.parent.assertEqual(result.logits.shape       ,					(self.batch_size, self.seq_length, self.num_labels)					)







      def 		UpperCAmelCase_  (	self      :List[str]					)   ->	Dict:
       UpperCAmelCase__							       =  self.prepare_config_and_inputs()
       (UpperCAmelCase__)							       =  config_and_inputs
       UpperCAmelCase__							       =  {"input_ids": input_ids, "attention_mask": input_mask}
       return config, inputs_dict



@require_tf
class 	_UpperCamelCase					( UpperCamelCase__		,				UpperCamelCase__		,				unittest.TestCase						):
      UpperCAmelCase_						=						(
          (
              TFDistilBertModel,
              TFDistilBertForMaskedLM,
              TFDistilBertForQuestionAnswering,
              TFDistilBertForSequenceClassification,
              TFDistilBertForTokenClassification,
              TFDistilBertForMultipleChoice,
          )
          if is_tf_available()
          else None
      )
      UpperCAmelCase_						=						(
          {
              """feature-extraction""": TFDistilBertModel,
              """fill-mask""": TFDistilBertForMaskedLM,
              """question-answering""": TFDistilBertForQuestionAnswering,
              """text-classification""": TFDistilBertForSequenceClassification,
              """token-classification""": TFDistilBertForTokenClassification,
              """zero-shot""": TFDistilBertForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      UpperCAmelCase_						=						False
      UpperCAmelCase_						=						False
      def 		UpperCAmelCase_  (	self      :str					)   ->	Tuple:
       UpperCAmelCase__							       =  TFDistilBertModelTester(self					)
       UpperCAmelCase__							       =  ConfigTester(self       ,					config_class=lowerCAmelCase__       ,					dim=37					)
      def 		UpperCAmelCase_  (	self      :str					)   ->	int:
       self.config_tester.run_common_tests()
      def 		UpperCAmelCase_  (	self      :List[Any]					)   ->	Tuple:
       UpperCAmelCase__							       =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_model(*lowerCAmelCase__					)
      def 		UpperCAmelCase_  (	self      :Optional[int]					)   ->	Union[str, Any]:
       UpperCAmelCase__							       =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_masked_lm(*lowerCAmelCase__					)
      def 		UpperCAmelCase_  (	self      :Dict					)   ->	Optional[int]:
       UpperCAmelCase__							       =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_question_answering(*lowerCAmelCase__					)
      def 		UpperCAmelCase_  (	self      :Optional[Any]					)   ->	List[str]:
       UpperCAmelCase__							       =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowerCAmelCase__					)
      def 		UpperCAmelCase_  (	self      :List[Any]					)   ->	str:
       UpperCAmelCase__							       =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowerCAmelCase__					)
      def 		UpperCAmelCase_  (	self      :Union[str, Any]					)   ->	Union[str, Any]:
       UpperCAmelCase__							       =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_distilbert_for_token_classification(*lowerCAmelCase__					)







      @slow
      def 		UpperCAmelCase_  (	self      :Optional[Any]					)   ->	Union[str, Any]:




       for model_name in list(TF_DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]					):
        UpperCAmelCase__							       =  TFDistilBertModel.from_pretrained(lowerCAmelCase__					)
        self.assertIsNotNone(lowerCAmelCase__					)



@require_tf
class 	_UpperCamelCase					( unittest.TestCase						):
      @slow
      def 		UpperCAmelCase_  (	self      :Optional[int]					)   ->	Optional[int]:
       UpperCAmelCase__							       =  TFDistilBertModel.from_pretrained("distilbert-base-uncased"					)
       UpperCAmelCase__							       =  tf.constant([[0, 1, 2, 3, 4, 5]]					)
       UpperCAmelCase__							       =  model(lowerCAmelCase__					)[0]

       UpperCAmelCase__							       =  [1, 6, 768]
       self.assertEqual(output.shape       ,					lowerCAmelCase__					)

       UpperCAmelCase__							       =  tf.constant(
           [
               [
                   [0.19_26_18_85, -0.13_73_29_55, 0.4_11_97_99],
                   [0.22_15_01_56, -0.07_42_26_61, 0.39_03_72_04],
                   [0.22_75_60_18, -0.0_89_64_14, 0.3_70_14_67],
               ]
           ]					)
       tf.debugging.assert_near(output[:, :3, :3]       ,					lowerCAmelCase__       ,					atol=1e-4					)












import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

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


if is_vision_available():
     from PIL import Image

     from transformers import CLIPSegProcessor, ViTImageProcessor







@require_vision
class 				__lowerCAmelCase       (    unittest.TestCase):







      def    _lowercase  (    self							)   ->						Dict:



        '''simple docstring'''




        a__	:					Any										=tempfile.mkdtemp()

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

        a__	:					Optional[Any]										=os.path.join(self.tmpdirname		,      VOCAB_FILES_NAMES["vocab_file"]							)
        a__	:					Tuple										=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__							)							)

        a__	:					Optional[Any]										={
            "do_resize": True,
            "size": 2_0,
            "do_center_crop": True,
            "crop_size": 1_8,
            "do_normalize": True,
            "image_mean": [0.48_14_54_66, 0.4_57_82_75, 0.40_82_10_73],
            "image_std": [0.26_86_29_54, 0.26_13_02_58, 0.27_57_77_11],
        }
        a__	:					Dict										=os.path.join(self.tmpdirname		,      lowerCAmelCase__							)
        with open(self.image_processor_file		,      "w"		,      encoding="utf-8"							) as fp:
          json.dump(lowerCAmelCase__		,      lowerCAmelCase__							)







      def    _lowercase  (    self		,      **lowerCAmelCase__							)   ->						Union[str, Any]:



        '''simple docstring'''




        return CLIPTokenizer.from_pretrained(self.tmpdirname		,      **lowerCAmelCase__							)







      def    _lowercase  (    self		,      **lowerCAmelCase__							)   ->						List[Any]:



        '''simple docstring'''




        return CLIPTokenizerFast.from_pretrained(self.tmpdirname		,      **lowerCAmelCase__							)







      def    _lowercase  (    self		,      **lowerCAmelCase__							)   ->						Any:



        '''simple docstring'''




        return ViTImageProcessor.from_pretrained(self.tmpdirname		,      **lowerCAmelCase__							)







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



        '''simple docstring'''




        shutil.rmtree(self.tmpdirname							)







      def    _lowercase  (    self							)   ->						Any:



        '''simple docstring'''




        a__	:					Optional[Any]										=[np.random.randint(2_5_5		,      size=(3, 3_0, 4_0_0)		,      dtype=np.uinta							)]

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

        return image_inputs







      def    _lowercase  (    self							)   ->						Dict:



        '''simple docstring'''




        a__	:					Union[str, Any]										=self.get_tokenizer()
        a__	:					int										=self.get_rust_tokenizer()
        a__	:					List[str]										=self.get_image_processor()

        a__	:					Dict										=CLIPSegProcessor(tokenizer=lowerCAmelCase__		,      image_processor=lowerCAmelCase__							)
        processor_slow.save_pretrained(self.tmpdirname							)
        a__	:					Optional[Any]										=CLIPSegProcessor.from_pretrained(self.tmpdirname		,      use_fast=lowerCAmelCase__							)

        a__	:					Tuple										=CLIPSegProcessor(tokenizer=lowerCAmelCase__		,      image_processor=lowerCAmelCase__							)
        processor_fast.save_pretrained(self.tmpdirname							)
        a__	:					Dict										=CLIPSegProcessor.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		,      lowerCAmelCase__							)
        self.assertIsInstance(processor_fast.tokenizer		,      lowerCAmelCase__							)

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







      def    _lowercase  (    self							)   ->						Any:



        '''simple docstring'''




        a__	:					List[str]										=CLIPSegProcessor(tokenizer=self.get_tokenizer()		,      image_processor=self.get_image_processor()							)
        processor.save_pretrained(self.tmpdirname							)

        a__	:					str										=self.get_tokenizer(bos_token="(BOS)"		,      eos_token="(EOS)"							)
        a__	:					int										=self.get_image_processor(do_normalize=lowerCAmelCase__		,      padding_value=1.0							)

        a__	:					Optional[Any]										=CLIPSegProcessor.from_pretrained(
            self.tmpdirname		,      bos_token="(BOS)"		,      eos_token="(EOS)"		,      do_normalize=lowerCAmelCase__		,      padding_value=1.0							)

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

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







      def    _lowercase  (    self							)   ->						List[Any]:



        '''simple docstring'''




        a__	:					str										=self.get_image_processor()
        a__	:					Optional[int]										=self.get_tokenizer()

        a__	:					Dict										=CLIPSegProcessor(tokenizer=lowerCAmelCase__		,      image_processor=lowerCAmelCase__							)

        a__	:					str										=self.prepare_image_inputs()

        a__	:					Any										=image_processor(lowerCAmelCase__		,      return_tensors="np"							)
        a__	:					Optional[int]										=processor(images=lowerCAmelCase__		,      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    _lowercase  (    self							)   ->						Union[str, Any]:



        '''simple docstring'''




        a__	:					Optional[int]										=self.get_image_processor()
        a__	:					List[Any]										=self.get_tokenizer()

        a__	:					Optional[int]										=CLIPSegProcessor(tokenizer=lowerCAmelCase__		,      image_processor=lowerCAmelCase__							)

        a__	:					Union[str, Any]										="lower newer"

        a__	:					List[str]										=processor(text=lowerCAmelCase__							)

        a__	:					str										=tokenizer(lowerCAmelCase__							)

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







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



        '''simple docstring'''




        a__	:					Any										=self.get_image_processor()
        a__	:					Dict										=self.get_tokenizer()

        a__	:					Union[str, Any]										=CLIPSegProcessor(tokenizer=lowerCAmelCase__		,      image_processor=lowerCAmelCase__							)

        a__	:					Dict										="lower newer"
        a__	:					int										=self.prepare_image_inputs()

        a__	:					Any										=processor(text=lowerCAmelCase__		,      images=lowerCAmelCase__							)

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

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







      def    _lowercase  (    self							)   ->						str:



        '''simple docstring'''




        a__	:					Union[str, Any]										=self.get_image_processor()
        a__	:					Optional[Any]										=self.get_tokenizer()

        a__	:					str										=CLIPSegProcessor(tokenizer=lowerCAmelCase__		,      image_processor=lowerCAmelCase__							)

        a__	:					int										=self.prepare_image_inputs()
        a__	:					Union[str, Any]										=self.prepare_image_inputs()

        a__	:					Tuple										=processor(images=lowerCAmelCase__		,      visual_prompt=lowerCAmelCase__							)

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

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







      def    _lowercase  (    self							)   ->						Tuple:



        '''simple docstring'''




        a__	:					Optional[int]										=self.get_image_processor()
        a__	:					Any										=self.get_tokenizer()

        a__	:					Tuple										=CLIPSegProcessor(tokenizer=lowerCAmelCase__		,      image_processor=lowerCAmelCase__							)

        a__	:					Dict										=[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

        a__	:					Optional[Any]										=processor.batch_decode(lowerCAmelCase__							)
        a__	:					Dict										=tokenizer.batch_decode(lowerCAmelCase__							)

        self.assertListEqual(lowerCAmelCase__		,      lowerCAmelCase__							)






