





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



      def 	lowerCAmelCase__    (	self:						Optional[int]       )						->    List[str]:
         '''simple docstring'''

         UpperCAmelCase_											=tempfile.mkdtemp()

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

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

         UpperCAmelCase_											={
             "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],
         }
         UpperCAmelCase_											=os.path.join(self.tmpdirname		,  A_       )
         with open(self.image_processor_file		,  "w"		,  encoding="utf-8"       ) as fp:
            json.dump(A_		,  A_       )



      def 	lowerCAmelCase__    (	self:						Optional[Any]		,  **_lowerCAmelCase:						Dict       )						->    Any:
         '''simple docstring'''

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



      def 	lowerCAmelCase__    (	self:						Optional[int]		,  **_lowerCAmelCase:						str       )						->    str:
         '''simple docstring'''

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



      def 	lowerCAmelCase__    (	self:						Any		,  **_lowerCAmelCase:						Tuple       )						->    Tuple:
         '''simple docstring'''

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



      def 	lowerCAmelCase__    (	self:						Optional[int]       )						->    Optional[Any]:
         '''simple docstring'''

         shutil.rmtree(self.tmpdirname       )



      def 	lowerCAmelCase__    (	self:						int       )						->    Optional[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=[np.random.randint(255		,  size=(3, 30, 400)		,  dtype=np.uinta       )]

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

         return image_inputs



      def 	lowerCAmelCase__    (	self:						Optional[Any]       )						->    Optional[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=self.get_tokenizer()
         UpperCAmelCase_											=self.get_rust_tokenizer()
         UpperCAmelCase_											=self.get_image_processor()

         UpperCAmelCase_											=CLIPSegProcessor(tokenizer=A_		,  image_processor=A_       )
         processor_slow.save_pretrained(self.tmpdirname       )
         UpperCAmelCase_											=CLIPSegProcessor.from_pretrained(self.tmpdirname		,  use_fast=A_       )

         UpperCAmelCase_											=CLIPSegProcessor(tokenizer=A_		,  image_processor=A_       )
         processor_fast.save_pretrained(self.tmpdirname       )
         UpperCAmelCase_											=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		,  A_       )
         self.assertIsInstance(processor_fast.tokenizer		,  A_       )

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



      def 	lowerCAmelCase__    (	self:						Union[str, Any]       )						->    Any:
         '''simple docstring'''

         UpperCAmelCase_											=CLIPSegProcessor(tokenizer=self.get_tokenizer()		,  image_processor=self.get_image_processor()       )
         processor.save_pretrained(self.tmpdirname       )

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

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

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

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



      def 	lowerCAmelCase__    (	self:						Any       )						->    str:
         '''simple docstring'''

         UpperCAmelCase_											=self.get_image_processor()
         UpperCAmelCase_											=self.get_tokenizer()

         UpperCAmelCase_											=CLIPSegProcessor(tokenizer=A_		,  image_processor=A_       )

         UpperCAmelCase_											=self.prepare_image_inputs()

         UpperCAmelCase_											=image_processor(A_		,  return_tensors="np"       )
         UpperCAmelCase_											=processor(images=A_		,  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 	lowerCAmelCase__    (	self:						List[str]       )						->    List[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=self.get_image_processor()
         UpperCAmelCase_											=self.get_tokenizer()

         UpperCAmelCase_											=CLIPSegProcessor(tokenizer=A_		,  image_processor=A_       )

         UpperCAmelCase_											="lower newer"

         UpperCAmelCase_											=processor(text=A_       )

         UpperCAmelCase_											=tokenizer(A_       )

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



      def 	lowerCAmelCase__    (	self:						List[Any]       )						->    str:
         '''simple docstring'''

         UpperCAmelCase_											=self.get_image_processor()
         UpperCAmelCase_											=self.get_tokenizer()

         UpperCAmelCase_											=CLIPSegProcessor(tokenizer=A_		,  image_processor=A_       )

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

         UpperCAmelCase_											=processor(text=A_		,  images=A_       )

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

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



      def 	lowerCAmelCase__    (	self:						Union[str, Any]       )						->    Any:
         '''simple docstring'''

         UpperCAmelCase_											=self.get_image_processor()
         UpperCAmelCase_											=self.get_tokenizer()

         UpperCAmelCase_											=CLIPSegProcessor(tokenizer=A_		,  image_processor=A_       )

         UpperCAmelCase_											=self.prepare_image_inputs()
         UpperCAmelCase_											=self.prepare_image_inputs()

         UpperCAmelCase_											=processor(images=A_		,  visual_prompt=A_       )

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

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






      def 	lowerCAmelCase__    (	self:						Optional[int]       )						->    Dict:
         '''simple docstring'''

         UpperCAmelCase_											=self.get_image_processor()
         UpperCAmelCase_											=self.get_tokenizer()

         UpperCAmelCase_											=CLIPSegProcessor(tokenizer=A_		,  image_processor=A_       )

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

         UpperCAmelCase_											=processor.batch_decode(A_       )
         UpperCAmelCase_											=tokenizer.batch_decode(A_       )

         self.assertListEqual(A_		,  A_       )











__lowercase  :   Dict						      ="""0.21.0"""

from .accelerator import Accelerator
from .big_modeling import (
    cpu_offload,
    cpu_offload_with_hook,
    disk_offload,
    dispatch_model,
    init_empty_weights,
    init_on_device,
    load_checkpoint_and_dispatch,
)
from .data_loader import skip_first_batches
from .launchers import debug_launcher, notebook_launcher
from .state import PartialState
from .utils import (
    DeepSpeedPlugin,
    DistributedDataParallelKwargs,
    DistributedType,
    FullyShardedDataParallelPlugin,
    GradScalerKwargs,
    InitProcessGroupKwargs,
    find_executable_batch_size,
    infer_auto_device_map,
    is_rich_available,
    load_checkpoint_in_model,
    synchronize_rng_states,
)


if is_rich_available():
     from .utils import rich




