





'''simple docstring'''






import unittest

from transformers import XLMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
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 (
          XLMForMultipleChoice,
          XLMForQuestionAnswering,
          XLMForQuestionAnsweringSimple,
          XLMForSequenceClassification,
          XLMForTokenClassification,
          XLMModel,
          XLMWithLMHeadModel,
      )
      from transformers.models.xlm.modeling_xlm import XLM_PRETRAINED_MODEL_ARCHIVE_LIST




class 			UpperCAmelCase					:




      def __init__(      self      :			Tuple      ,      __snake_case      :			List[str]      ,      __snake_case      :			int=13      ,      __snake_case      :			Optional[Any]=7      ,      __snake_case      :			int=True      ,      __snake_case      :			Optional[Any]=True      ,      __snake_case      :			Optional[Any]=True      ,      __snake_case      :			List[Any]=True      ,      __snake_case      :			int=True      ,      __snake_case      :			int=False      ,      __snake_case      :			Optional[Any]=False      ,      __snake_case      :			Any=False      ,      __snake_case      :			Optional[Any]=2      ,      __snake_case      :			Tuple=99      ,      __snake_case      :			int=0      ,      __snake_case      :			int=32      ,      __snake_case      :			Any=5      ,      __snake_case      :			List[Any]=4      ,      __snake_case      :			List[str]=0.1      ,      __snake_case      :			List[Any]=0.1      ,      __snake_case      :			Dict=5_12      ,      __snake_case      :			List[str]=2      ,      __snake_case      :			Union[str, Any]=0.02      ,      __snake_case      :			Dict=2      ,      __snake_case      :			List[str]=4      ,      __snake_case      :			Union[str, Any]="last"      ,      __snake_case      :			Dict=True      ,      __snake_case      :			Any=None      ,      __snake_case      :			List[str]=0      ,      )    ->				List[str]:
            _lowerCAmelCase												=							parent
            _lowerCAmelCase												=							batch_size
            _lowerCAmelCase												=							seq_length
            _lowerCAmelCase												=							is_training
            _lowerCAmelCase												=							use_input_lengths
            _lowerCAmelCase												=							use_token_type_ids
            _lowerCAmelCase												=							use_labels
            _lowerCAmelCase												=							gelu_activation
            _lowerCAmelCase												=							sinusoidal_embeddings
            _lowerCAmelCase												=							causal
            _lowerCAmelCase												=							asm
            _lowerCAmelCase												=							n_langs
            _lowerCAmelCase												=							vocab_size
            _lowerCAmelCase												=							n_special
            _lowerCAmelCase												=							hidden_size
            _lowerCAmelCase												=							num_hidden_layers
            _lowerCAmelCase												=							num_attention_heads
            _lowerCAmelCase												=							hidden_dropout_prob
            _lowerCAmelCase												=							attention_probs_dropout_prob
            _lowerCAmelCase												=							max_position_embeddings
            _lowerCAmelCase												=							type_sequence_label_size
            _lowerCAmelCase												=							initializer_range
            _lowerCAmelCase												=							num_labels
            _lowerCAmelCase												=							num_choices
            _lowerCAmelCase												=							summary_type
            _lowerCAmelCase												=							use_proj
            _lowerCAmelCase												=							scope
            _lowerCAmelCase												=							bos_token_id




      def     lowercase__   (      self      :			List[Any]  )    ->				Union[str, Any]:
            _lowerCAmelCase												=							ids_tensor([self.batch_size, self.seq_length]      ,      self.vocab_size  )
            _lowerCAmelCase												=							random_attention_mask([self.batch_size, self.seq_length]  )

            _lowerCAmelCase												=							None
            if self.use_input_lengths:
                  _lowerCAmelCase												=							(
                      ids_tensor([self.batch_size]      ,      vocab_size=2  ) + self.seq_length - 2
                  )  # small variation of seq_length

            _lowerCAmelCase												=							None
            if self.use_token_type_ids:
                  _lowerCAmelCase												=							ids_tensor([self.batch_size, self.seq_length]      ,      self.n_langs  )

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

            _lowerCAmelCase												=							self.get_config()

            return (
                config,
                input_ids,
                token_type_ids,
                input_lengths,
                sequence_labels,
                token_labels,
                is_impossible_labels,
                choice_labels,
                input_mask,
            )




      def     lowercase__   (      self      :			Union[str, Any]  )    ->				Optional[Any]:
            return XLMConfig(
                vocab_size=self.vocab_size      ,      n_special=self.n_special      ,      emb_dim=self.hidden_size      ,      n_layers=self.num_hidden_layers      ,      n_heads=self.num_attention_heads      ,      dropout=self.hidden_dropout_prob      ,      attention_dropout=self.attention_probs_dropout_prob      ,      gelu_activation=self.gelu_activation      ,      sinusoidal_embeddings=self.sinusoidal_embeddings      ,      asm=self.asm      ,      causal=self.causal      ,      n_langs=self.n_langs      ,      max_position_embeddings=self.max_position_embeddings      ,      initializer_range=self.initializer_range      ,      summary_type=self.summary_type      ,      use_proj=self.use_proj      ,      num_labels=self.num_labels      ,      bos_token_id=self.bos_token_id      ,      )




      def     lowercase__   (      self      :			Optional[int]      ,      __snake_case      :			Tuple      ,      __snake_case      :			Union[str, Any]      ,      __snake_case      :			Dict      ,      __snake_case      :			str      ,      __snake_case      :			str      ,      __snake_case      :			Dict      ,      __snake_case      :			Tuple      ,      __snake_case      :			Union[str, Any]      ,      __snake_case      :			Optional[int]      ,      )    ->				List[Any]:
            _lowerCAmelCase												=							XLMModel(config=__snake_case  )
            model.to(__snake_case  )
            model.eval()
            _lowerCAmelCase												=							model(__snake_case      ,      lengths=__snake_case      ,      langs=__snake_case  )
            _lowerCAmelCase												=							model(__snake_case      ,      langs=__snake_case  )
            _lowerCAmelCase												=							model(__snake_case  )
            self.parent.assertEqual(result.last_hidden_state.shape      ,      (self.batch_size, self.seq_length, self.hidden_size)  )




      def     lowercase__   (      self      :			Tuple      ,      __snake_case      :			Any      ,      __snake_case      :			str      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			int      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			List[str]      ,      __snake_case      :			Union[str, Any]      ,      __snake_case      :			Dict      ,      __snake_case      :			Optional[int]      ,      )    ->				Optional[int]:
            _lowerCAmelCase												=							XLMWithLMHeadModel(__snake_case  )
            model.to(__snake_case  )
            model.eval()

            _lowerCAmelCase												=							model(__snake_case      ,      token_type_ids=__snake_case      ,      labels=__snake_case  )
            self.parent.assertEqual(result.loss.shape      ,      ()  )
            self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.seq_length, self.vocab_size)  )




      def     lowercase__   (      self      :			int      ,      __snake_case      :			Union[str, Any]      ,      __snake_case      :			Tuple      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			Union[str, Any]      ,      __snake_case      :			Tuple      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			List[Any]      ,      __snake_case      :			int      ,      __snake_case      :			Dict      ,      )    ->				List[Any]:
            _lowerCAmelCase												=							XLMForQuestionAnsweringSimple(__snake_case  )
            model.to(__snake_case  )
            model.eval()

            _lowerCAmelCase												=							model(__snake_case  )

            _lowerCAmelCase												=							model(__snake_case      ,      start_positions=__snake_case      ,      end_positions=__snake_case  )
            _lowerCAmelCase												=							outputs
            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     lowercase__   (      self      :			Union[str, Any]      ,      __snake_case      :			List[str]      ,      __snake_case      :			Optional[int]      ,      __snake_case      :			List[Any]      ,      __snake_case      :			Optional[int]      ,      __snake_case      :			Dict      ,      __snake_case      :			List[str]      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			Any      ,      __snake_case      :			int      ,      )    ->				str:
            _lowerCAmelCase												=							XLMForQuestionAnswering(__snake_case  )
            model.to(__snake_case  )
            model.eval()

            _lowerCAmelCase												=							model(__snake_case  )

            _lowerCAmelCase												=							model(
                __snake_case      ,      start_positions=__snake_case      ,      end_positions=__snake_case      ,      cls_index=__snake_case      ,      is_impossible=__snake_case      ,      p_mask=__snake_case      ,      )

            _lowerCAmelCase												=							model(
                __snake_case      ,      start_positions=__snake_case      ,      end_positions=__snake_case      ,      cls_index=__snake_case      ,      is_impossible=__snake_case      ,      )

            ((_lowerCAmelCase)					,							)												=							result_with_labels.to_tuple()

            _lowerCAmelCase												=							model(__snake_case      ,      start_positions=__snake_case      ,      end_positions=__snake_case  )

            ((_lowerCAmelCase)					,							)												=							result_with_labels.to_tuple()

            self.parent.assertEqual(result_with_labels.loss.shape      ,      ()  )
            self.parent.assertEqual(result.start_top_log_probs.shape      ,      (self.batch_size, model.config.start_n_top)  )
            self.parent.assertEqual(result.start_top_index.shape      ,      (self.batch_size, model.config.start_n_top)  )
            self.parent.assertEqual(
                result.end_top_log_probs.shape      ,      (self.batch_size, model.config.start_n_top * model.config.end_n_top)  )
            self.parent.assertEqual(
                result.end_top_index.shape      ,      (self.batch_size, model.config.start_n_top * model.config.end_n_top)  )
            self.parent.assertEqual(result.cls_logits.shape      ,      (self.batch_size,)  )




      def     lowercase__   (      self      :			str      ,      __snake_case      :			Any      ,      __snake_case      :			Optional[int]      ,      __snake_case      :			List[str]      ,      __snake_case      :			Union[str, Any]      ,      __snake_case      :			Tuple      ,      __snake_case      :			List[Any]      ,      __snake_case      :			Optional[int]      ,      __snake_case      :			Tuple      ,      __snake_case      :			str      ,      )    ->				Any:
            _lowerCAmelCase												=							XLMForSequenceClassification(__snake_case  )
            model.to(__snake_case  )
            model.eval()

            _lowerCAmelCase												=							model(__snake_case  )
            _lowerCAmelCase												=							model(__snake_case      ,      labels=__snake_case  )
            self.parent.assertEqual(result.loss.shape      ,      ()  )
            self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.type_sequence_label_size)  )




      def     lowercase__   (      self      :			Optional[int]      ,      __snake_case      :			Dict      ,      __snake_case      :			Tuple      ,      __snake_case      :			List[str]      ,      __snake_case      :			List[str]      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			Any      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			Union[str, Any]      ,      __snake_case      :			Union[str, Any]      ,      )    ->				int:
            _lowerCAmelCase												=							self.num_labels
            _lowerCAmelCase												=							XLMForTokenClassification(__snake_case  )
            model.to(__snake_case  )
            model.eval()

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




      def     lowercase__   (      self      :			Dict      ,      __snake_case      :			str      ,      __snake_case      :			Tuple      ,      __snake_case      :			str      ,      __snake_case      :			str      ,      __snake_case      :			Dict      ,      __snake_case      :			str      ,      __snake_case      :			Tuple      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			List[str]      ,      )    ->				Union[str, Any]:
            _lowerCAmelCase												=							self.num_choices
            _lowerCAmelCase												=							XLMForMultipleChoice(config=__snake_case  )
            model.to(__snake_case  )
            model.eval()
            _lowerCAmelCase												=							input_ids.unsqueeze(1  ).expand(-1      ,      self.num_choices      ,      -1  ).contiguous()
            _lowerCAmelCase												=							token_type_ids.unsqueeze(1  ).expand(-1      ,      self.num_choices      ,      -1  ).contiguous()
            _lowerCAmelCase												=							input_mask.unsqueeze(1  ).expand(-1      ,      self.num_choices      ,      -1  ).contiguous()
            _lowerCAmelCase												=							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.num_choices)  )




      def     lowercase__   (      self      :			Tuple  )    ->				List[Any]:
            _lowerCAmelCase												=							self.prepare_config_and_inputs()
            (
                (
                _lowerCAmelCase
            )					,							(
                _lowerCAmelCase
            )					,							(
                _lowerCAmelCase
            )					,							(
                _lowerCAmelCase
            )					,							(
                _lowerCAmelCase
            )					,							(
                _lowerCAmelCase
            )					,							(
                _lowerCAmelCase
            )					,							(
                _lowerCAmelCase
            )					,							(
                _lowerCAmelCase
            )					,							
            )												=							config_and_inputs
            _lowerCAmelCase												=							{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """lengths""": input_lengths}
            return config, inputs_dict




@require_torch
class 			UpperCAmelCase					(      snake_case_							,		snake_case_							,		snake_case_							,		unittest.TestCase       ):
      _lowercase:      Optional[int]											=       (
          (
              XLMModel,
              XLMWithLMHeadModel,
              XLMForQuestionAnswering,
              XLMForSequenceClassification,
              XLMForQuestionAnsweringSimple,
              XLMForTokenClassification,
              XLMForMultipleChoice,
          )
          if is_torch_available()
          else ()
      )
      _lowercase:      List[Any]											=       (
          (XLMWithLMHeadModel,) if is_torch_available() else ()
      )  # TODO (PVP): Check other models whether language generation is also applicable
      _lowercase:      List[str]											=       (
          {
              '''feature-extraction''': XLMModel,
              '''fill-mask''': XLMWithLMHeadModel,
              '''question-answering''': XLMForQuestionAnsweringSimple,
              '''text-classification''': XLMForSequenceClassification,
              '''text-generation''': XLMWithLMHeadModel,
              '''token-classification''': XLMForTokenClassification,
              '''zero-shot''': XLMForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )




      def     lowercase__   (      self      :			str      ,      __snake_case      :			Dict      ,      __snake_case      :			Tuple      ,      __snake_case      :			List[str]      ,      __snake_case      :			List[Any]      ,      __snake_case      :			Optional[int]  )    ->				Optional[Any]:
            if (
                pipeline_test_casse_name == "QAPipelineTests"
                and tokenizer_name is not None
                and not tokenizer_name.endswith("""Fast"""  )
            ):
                  # `QAPipelineTests` fails for a few models when the slower tokenizer are used.
                  # (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
                  # TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
                  return True

            return False




      def     lowercase__   (      self      :			Tuple      ,      __snake_case      :			List[Any]      ,      __snake_case      :			int      ,      __snake_case      :			Any=False  )    ->				Tuple:
            _lowerCAmelCase												=							super()._prepare_for_class(__snake_case      ,      __snake_case      ,      return_labels=__snake_case  )

            if return_labels:
                  if model_class.__name__ == "XLMForQuestionAnswering":
                        _lowerCAmelCase												=							torch.zeros(
                            self.model_tester.batch_size      ,      dtype=torch.long      ,      device=__snake_case  )
                        _lowerCAmelCase												=							torch.zeros(
                            self.model_tester.batch_size      ,      dtype=torch.long      ,      device=__snake_case  )

            return inputs_dict




      def     lowercase__   (      self      :			Union[str, Any]  )    ->				Union[str, Any]:
            _lowerCAmelCase												=							XLMModelTester(self  )
            _lowerCAmelCase												=							ConfigTester(self      ,      config_class=__snake_case      ,      emb_dim=37  )




      def     lowercase__   (      self      :			Union[str, Any]  )    ->				int:
            self.config_tester.run_common_tests()




      def     lowercase__   (      self      :			Tuple  )    ->				Dict:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_model(*__snake_case  )




      def     lowercase__   (      self      :			List[str]  )    ->				Tuple:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_lm_head(*__snake_case  )




      def     lowercase__   (      self      :			int  )    ->				Dict:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_simple_qa(*__snake_case  )




      def     lowercase__   (      self      :			Optional[int]  )    ->				str:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_qa(*__snake_case  )




      def     lowercase__   (      self      :			str  )    ->				Dict:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_sequence_classif(*__snake_case  )




      def     lowercase__   (      self      :			Optional[int]  )    ->				List[str]:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_token_classif(*__snake_case  )




      def     lowercase__   (      self      :			int  )    ->				Union[str, Any]:
            _lowerCAmelCase												=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_xlm_for_multiple_choice(*__snake_case  )




      def     lowercase__   (      self      :			Any      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			Union[str, Any]      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			Tuple      ,      __snake_case      :			Any=False      ,      __snake_case      :			Any=1  )    ->				List[Any]:
            self.assertIsInstance(__snake_case      ,      __snake_case  )
            self.assertListEqual(
                [isinstance(__snake_case      ,      __snake_case  ) for iter_attentions in attentions]      ,      [True] * len(__snake_case  )  )
            self.assertEqual(len(__snake_case  )      ,      (max_length - min_length) * num_beam_groups  )

            for idx, iter_attentions in enumerate(__snake_case  ):
                  # adds PAD dummy token
                  _lowerCAmelCase												=							min_length + idx + 1
                  _lowerCAmelCase												=							min_length + idx + 1

                  _lowerCAmelCase												=							(
                      batch_size * num_beam_groups,
                      config.num_attention_heads,
                      tgt_len,
                      src_len,
                  )
                  # check attn size
                  self.assertListEqual(
                      [layer_attention.shape for layer_attention in iter_attentions]      ,      [expected_shape] * len(__snake_case  )  )




      def     lowercase__   (      self      :			List[Any]      ,      __snake_case      :			Union[str, Any]      ,      __snake_case      :			Optional[int]      ,      __snake_case      :			List[str]      ,      __snake_case      :			Optional[Any]      ,      __snake_case      :			int      ,      __snake_case      :			List[Any]=False      ,      __snake_case      :			Dict=1  )    ->				Dict:
            self.assertIsInstance(__snake_case      ,      __snake_case  )
            self.assertListEqual(
                [isinstance(__snake_case      ,      __snake_case  ) for iter_hidden_states in hidden_states]      ,      [True] * len(__snake_case  )      ,      )
            self.assertEqual(len(__snake_case  )      ,      (max_length - min_length) * num_beam_groups  )

            for idx, iter_hidden_states in enumerate(__snake_case  ):
                  # adds PAD dummy token
                  _lowerCAmelCase												=							min_length + idx + 1
                  _lowerCAmelCase												=							(batch_size * num_beam_groups, seq_len, config.hidden_size)
                  # check hidden size
                  self.assertListEqual(
                      [layer_hidden_states.shape for layer_hidden_states in iter_hidden_states]      ,      [expected_shape] * len(__snake_case  )      ,      )
            pass




      @slow
      def     lowercase__   (      self      :			Union[str, Any]  )    ->				str:
            for model_name in XLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  _lowerCAmelCase												=							XLMModel.from_pretrained(__snake_case  )
                  self.assertIsNotNone(__snake_case  )




@require_torch
class 			UpperCAmelCase					(      unittest.TestCase       ):




      @slow
      def     lowercase__   (      self      :			Dict  )    ->				Optional[Any]:
            _lowerCAmelCase												=							XLMWithLMHeadModel.from_pretrained("""xlm-mlm-en-2048"""  )
            model.to(__snake_case  )
            _lowerCAmelCase												=							torch.tensor([[14, 4_47]]      ,      dtype=torch.long      ,      device=__snake_case  )  # the president
            _lowerCAmelCase												=							[
                14,
                4_47,
                14,
                4_47,
                14,
                4_47,
                14,
                4_47,
                14,
                4_47,
                14,
                4_47,
                14,
                4_47,
                14,
                4_47,
                14,
                4_47,
                14,
                4_47,
            ]  # the president the president the president the president the president the president the president the president the president the president
            # TODO(PVP): this and other input_ids I tried for generation give pretty bad results. Not sure why. Model might just not be made for auto-regressive inference
            _lowerCAmelCase												=							model.generate(__snake_case      ,      do_sample=__snake_case  )
            self.assertListEqual(output_ids[0].cpu().numpy().tolist()      ,      __snake_case  )













'''simple docstring'''






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 			UpperCAmelCase					(      unittest.TestCase       ):




      def     lowercase__   (      self      :			List[str]  )    ->				Dict:
            _lowerCAmelCase												=							tempfile.mkdtemp()

            # fmt: off
            _lowerCAmelCase												=							["""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
            _lowerCAmelCase												=							dict(zip(__snake_case      ,      range(len(__snake_case  )  )  )  )
            _lowerCAmelCase												=							["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""]
            _lowerCAmelCase												=							{"""unk_token""": """<unk>"""}

            _lowerCAmelCase												=							os.path.join(self.tmpdirname      ,      VOCAB_FILES_NAMES["""vocab_file"""]  )
            _lowerCAmelCase												=							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(__snake_case  ) + """\n"""  )
            with open(self.merges_file      ,      """w"""      ,      encoding="""utf-8"""  ) as fp:
                  fp.write("""\n""".join(__snake_case  )  )

            _lowerCAmelCase												=							{
                """do_resize""": True,
                """size""": 20,
                """do_center_crop""": True,
                """crop_size""": 18,
                """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],
            }
            _lowerCAmelCase												=							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     lowercase__   (      self      :			Optional[int]      ,      **__snake_case      :			Dict  )    ->				List[str]:
            return CLIPTokenizer.from_pretrained(self.tmpdirname      ,      **__snake_case  )




      def     lowercase__   (      self      :			List[str]      ,      **__snake_case      :			Any  )    ->				List[Any]:
            return CLIPTokenizerFast.from_pretrained(self.tmpdirname      ,      **__snake_case  )




      def     lowercase__   (      self      :			Tuple      ,      **__snake_case      :			List[str]  )    ->				int:
            return ViTImageProcessor.from_pretrained(self.tmpdirname      ,      **__snake_case  )




      def     lowercase__   (      self      :			str  )    ->				Optional[Any]:
            shutil.rmtree(self.tmpdirname  )




      def     lowercase__   (      self      :			str  )    ->				List[Any]:
            _lowerCAmelCase												=							[np.random.randint(2_55      ,      size=(3, 30, 4_00)      ,      dtype=np.uinta  )]

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

            return image_inputs




      def     lowercase__   (      self      :			Optional[Any]  )    ->				List[str]:
            _lowerCAmelCase												=							self.get_tokenizer()
            _lowerCAmelCase												=							self.get_rust_tokenizer()
            _lowerCAmelCase												=							self.get_image_processor()

            _lowerCAmelCase												=							CLIPSegProcessor(tokenizer=__snake_case      ,      image_processor=__snake_case  )
            processor_slow.save_pretrained(self.tmpdirname  )
            _lowerCAmelCase												=							CLIPSegProcessor.from_pretrained(self.tmpdirname      ,      use_fast=__snake_case  )

            _lowerCAmelCase												=							CLIPSegProcessor(tokenizer=__snake_case      ,      image_processor=__snake_case  )
            processor_fast.save_pretrained(self.tmpdirname  )
            _lowerCAmelCase												=							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      ,      __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     lowercase__   (      self      :			Optional[int]  )    ->				Dict:
            _lowerCAmelCase												=							CLIPSegProcessor(tokenizer=self.get_tokenizer()      ,      image_processor=self.get_image_processor()  )
            processor.save_pretrained(self.tmpdirname  )

            _lowerCAmelCase												=							self.get_tokenizer(bos_token="""(BOS)"""      ,      eos_token="""(EOS)"""  )
            _lowerCAmelCase												=							self.get_image_processor(do_normalize=__snake_case      ,      padding_value=1.0  )

            _lowerCAmelCase												=							CLIPSegProcessor.from_pretrained(
                self.tmpdirname      ,      bos_token="""(BOS)"""      ,      eos_token="""(EOS)"""      ,      do_normalize=__snake_case      ,      padding_value=1.0  )

            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     lowercase__   (      self      :			Tuple  )    ->				List[Any]:
            _lowerCAmelCase												=							self.get_image_processor()
            _lowerCAmelCase												=							self.get_tokenizer()

            _lowerCAmelCase												=							CLIPSegProcessor(tokenizer=__snake_case      ,      image_processor=__snake_case  )

            _lowerCAmelCase												=							self.prepare_image_inputs()

            _lowerCAmelCase												=							image_processor(__snake_case      ,      return_tensors="""np"""  )
            _lowerCAmelCase												=							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     lowercase__   (      self      :			Any  )    ->				str:
            _lowerCAmelCase												=							self.get_image_processor()
            _lowerCAmelCase												=							self.get_tokenizer()

            _lowerCAmelCase												=							CLIPSegProcessor(tokenizer=__snake_case      ,      image_processor=__snake_case  )

            _lowerCAmelCase												=							"""lower newer"""

            _lowerCAmelCase												=							processor(text=__snake_case  )

            _lowerCAmelCase												=							tokenizer(__snake_case  )

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




      def     lowercase__   (      self      :			str  )    ->				Union[str, Any]:
            _lowerCAmelCase												=							self.get_image_processor()
            _lowerCAmelCase												=							self.get_tokenizer()

            _lowerCAmelCase												=							CLIPSegProcessor(tokenizer=__snake_case      ,      image_processor=__snake_case  )

            _lowerCAmelCase												=							"""lower newer"""
            _lowerCAmelCase												=							self.prepare_image_inputs()

            _lowerCAmelCase												=							processor(text=__snake_case      ,      images=__snake_case  )

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

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




      def     lowercase__   (      self      :			Union[str, Any]  )    ->				Dict:
            _lowerCAmelCase												=							self.get_image_processor()
            _lowerCAmelCase												=							self.get_tokenizer()

            _lowerCAmelCase												=							CLIPSegProcessor(tokenizer=__snake_case      ,      image_processor=__snake_case  )

            _lowerCAmelCase												=							self.prepare_image_inputs()
            _lowerCAmelCase												=							self.prepare_image_inputs()

            _lowerCAmelCase												=							processor(images=__snake_case      ,      visual_prompt=__snake_case  )

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

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




      def     lowercase__   (      self      :			int  )    ->				Tuple:
            _lowerCAmelCase												=							self.get_image_processor()
            _lowerCAmelCase												=							self.get_tokenizer()

            _lowerCAmelCase												=							CLIPSegProcessor(tokenizer=__snake_case      ,      image_processor=__snake_case  )

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

            _lowerCAmelCase												=							processor.batch_decode(__snake_case  )
            _lowerCAmelCase												=							tokenizer.batch_decode(__snake_case  )

            self.assertListEqual(__snake_case      ,      __snake_case  )






