



from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional

from packaging import version


if TYPE_CHECKING:
     from ... import PreTrainedTokenizer, TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import is_torch_available, logging


SCREAMING_SNAKE_CASE_:List[Any]					=						logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_:str					=						{
    """bigscience/bloom""": """https://huggingface.co/bigscience/bloom/resolve/main/config.json""",
    """bigscience/bloom-560m""": """https://huggingface.co/bigscience/bloom-560m/blob/main/config.json""",
    """bigscience/bloom-1b1""": """https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json""",
    """bigscience/bloom-1b7""": """https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json""",
    """bigscience/bloom-3b""": """https://huggingface.co/bigscience/bloom-3b/blob/main/config.json""",
    """bigscience/bloom-7b1""": """https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json""",
}






class SCREAMING_SNAKE_CASE__					(		SCREAMING_SNAKE_CASE__   ):




    '''simple docstring'''






    __lowerCamelCase      :			Dict =				"bloom"
    __lowerCamelCase      :			Union[str, Any] =				["past_key_values"]
    __lowerCamelCase      :			List[str] =				{
        "num_hidden_layers": "n_layer",
        "num_attention_heads": "n_head",
    }






    def __init__(	self,		lowerCamelCase__=25_0880,		lowerCamelCase__=64,		lowerCamelCase__=2,		lowerCamelCase__=8,		lowerCamelCase__=1e-5,		lowerCamelCase__=0.02,		lowerCamelCase__=True,		lowerCamelCase__=1,		lowerCamelCase__=2,		lowerCamelCase__=False,		lowerCamelCase__=0.0,		lowerCamelCase__=0.0,		lowerCamelCase__=1,		lowerCamelCase__=False,		**lowerCamelCase__,		):
        A			:	str										=       vocab_size
        # Backward compatibility with n_embed kwarg
        A			:	str										=       kwargs.pop("""n_embed""",		lowerCamelCase__		)
        A			:	List[Any]										=       hidden_size if n_embed is None else n_embed
        A			:	Optional[Any]										=       n_layer
        A			:	Any										=       n_head
        A			:	Any										=       layer_norm_epsilon
        A			:	str										=       initializer_range
        A			:	List[str]										=       use_cache
        A			:	Union[str, Any]										=       pretraining_tp
        A			:	int										=       apply_residual_connection_post_layernorm
        A			:	str										=       hidden_dropout
        A			:	List[str]										=       attention_dropout

        A			:	Any										=       bos_token_id
        A			:	Optional[int]										=       eos_token_id
        A			:	Optional[Any]										=       slow_but_exact

        super().__init__(bos_token_id=lowerCamelCase__,		eos_token_id=lowerCamelCase__,		**lowerCamelCase__		)






class SCREAMING_SNAKE_CASE__					(		SCREAMING_SNAKE_CASE__   ):




    '''simple docstring'''






    __lowerCamelCase      :			Union[str, Any] =				version.parse("1.12"   )






    def __init__(	self,		lowerCamelCase__,		lowerCamelCase__ = "default",		lowerCamelCase__ = None,		lowerCamelCase__ = False,		):
        super().__init__(lowerCamelCase__,		task=lowerCamelCase__,		patching_specs=lowerCamelCase__,		use_past=lowerCamelCase__		)
        if not getattr(self._config,		"""pad_token_id""",		lowerCamelCase__		):
            # TODO: how to do that better?
            A			:	Optional[Any]										=       0






    @property
    def       _lowerCAmelCase (	self		):
        A			:	Any										=       OrderedDict({"""input_ids""": {0: """batch""", 1: """sequence"""}}		)
        if self.use_past:
            # BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344
            self.fill_with_past_key_values_(lowerCamelCase__,		direction="""inputs""",		inverted_values_shape=lowerCamelCase__		)
            A			:	List[Any]										=       {0: """batch""", 1: """past_sequence + sequence"""}
        else:
            A			:	str										=       {0: """batch""", 1: """sequence"""}

        return common_inputs






    @property
    def       _lowerCAmelCase (	self		):
        return self._config.n_layer






    @property
    def       _lowerCAmelCase (	self		):
        return self._config.n_head






    @property
    def       _lowerCAmelCase (	self		):
        return 1e-3






    def       _lowerCAmelCase (	self,		lowerCamelCase__,		lowerCamelCase__ = -1,		lowerCamelCase__ = -1,		lowerCamelCase__ = False,		lowerCamelCase__ = None,		):
        A			:	int										=       super(lowerCamelCase__,		self		).generate_dummy_inputs(
            lowerCamelCase__,		batch_size=lowerCamelCase__,		seq_length=lowerCamelCase__,		is_pair=lowerCamelCase__,		framework=lowerCamelCase__		)

        # We need to order the input in the way they appears in the forward()
        A			:	Optional[Any]										=       OrderedDict({"""input_ids""": common_inputs["""input_ids"""]}		)

        # Need to add the past_keys
        if self.use_past:
            if not is_torch_available():
                raise ValueError("""Cannot generate dummy past_keys inputs without PyTorch installed."""		)
            else:
                import torch

                A					,							A			:	Optional[Any]										=       common_inputs["""input_ids"""].shape
                # Not using the same length for past_key_values
                A			:	Dict										=       seqlen + 2
                A			:	Optional[int]										=       self._config.hidden_size // self.num_attention_heads
                A			:	Tuple										=       (
                    batch * self.num_attention_heads,
                    head_dim,
                    past_key_values_length,
                )
                A			:	List[Any]										=       (
                    batch * self.num_attention_heads,
                    past_key_values_length,
                    head_dim,
                )
                A			:	Optional[Any]										=       [
                    (torch.zeros(lowerCamelCase__		), torch.zeros(lowerCamelCase__		)) for _ in range(self.num_layers		)
                ]

        A			:	Tuple										=       common_inputs["""attention_mask"""]
        if self.use_past:
            A			:	Tuple										=       ordered_inputs["""attention_mask"""].dtype
            A			:	Any										=       torch.cat(
                [ordered_inputs["""attention_mask"""], torch.ones(lowerCamelCase__,		lowerCamelCase__,		dtype=lowerCamelCase__		)],		dim=1		)

        return ordered_inputs






    @property
    def       _lowerCAmelCase (	self		):
        return 13







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 OwlViTImageProcessor, OwlViTProcessor






@require_vision
class SCREAMING_SNAKE_CASE__					(		unittest.TestCase   ):




    '''simple docstring'''






    def       _lowerCAmelCase (	self		):
        A			:	str										=       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			:	Optional[int]										=       dict(zip(lowerCamelCase__,		range(len(lowerCamelCase__		)		)		)		)
        A			:	Optional[Any]										=       ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""]
        A			:	Union[str, Any]										=       {"""unk_token""": """<unk>"""}

        A			:	List[Any]										=       os.path.join(self.tmpdirname,		VOCAB_FILES_NAMES["""vocab_file"""]		)
        A			:	int										=       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			:	int										=       {
            """do_resize""": True,
            """size""": 20,
            """do_center_crop""": True,
            """crop_size""": 18,
            """do_normalize""": True,
            """image_mean""": [0.4814_5466, 0.457_8275, 0.4082_1073],
            """image_std""": [0.2686_2954, 0.2613_0258, 0.2757_7711],
        }
        A			:	List[Any]										=       os.path.join(self.tmpdirname,		lowerCamelCase__		)
        with open(self.image_processor_file,		"""w""",		encoding="""utf-8"""		) as fp:
            json.dump(lowerCamelCase__,		lowerCamelCase__		)






    def       _lowerCAmelCase (	self,		**lowerCamelCase__		):
        return CLIPTokenizer.from_pretrained(self.tmpdirname,		pad_token="""!""",		**lowerCamelCase__		)






    def       _lowerCAmelCase (	self,		**lowerCamelCase__		):
        return CLIPTokenizerFast.from_pretrained(self.tmpdirname,		pad_token="""!""",		**lowerCamelCase__		)






    def       _lowerCAmelCase (	self,		**lowerCamelCase__		):
        return OwlViTImageProcessor.from_pretrained(self.tmpdirname,		**lowerCamelCase__		)






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






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

        A			:	Optional[int]										=       [Image.fromarray(np.moveaxis(lowerCamelCase__,		0,		-1		)		) for x in image_inputs]

        return image_inputs






    def       _lowerCAmelCase (	self		):
        A			:	Optional[Any]										=       self.get_tokenizer()
        A			:	Optional[Any]										=       self.get_rust_tokenizer()
        A			:	Optional[int]										=       self.get_image_processor()

        A			:	List[str]										=       OwlViTProcessor(tokenizer=lowerCamelCase__,		image_processor=lowerCamelCase__		)
        processor_slow.save_pretrained(self.tmpdirname		)
        A			:	Optional[int]										=       OwlViTProcessor.from_pretrained(self.tmpdirname,		use_fast=lowerCamelCase__		)

        A			:	Tuple										=       OwlViTProcessor(tokenizer=lowerCamelCase__,		image_processor=lowerCamelCase__		)
        processor_fast.save_pretrained(self.tmpdirname		)
        A			:	str										=       OwlViTProcessor.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       _lowerCAmelCase (	self		):
        A			:	List[str]										=       OwlViTProcessor(tokenizer=self.get_tokenizer(),		image_processor=self.get_image_processor()		)
        processor.save_pretrained(self.tmpdirname		)

        A			:	Optional[int]										=       self.get_tokenizer(bos_token="""(BOS)""",		eos_token="""(EOS)"""		)
        A			:	Tuple										=       self.get_image_processor(do_normalize=lowerCamelCase__		)

        A			:	Optional[Any]										=       OwlViTProcessor.from_pretrained(
            self.tmpdirname,		bos_token="""(BOS)""",		eos_token="""(EOS)""",		do_normalize=lowerCamelCase__		)

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

        A			:	List[str]										=       OwlViTProcessor(tokenizer=lowerCamelCase__,		image_processor=lowerCamelCase__		)

        A			:	Optional[Any]										=       self.prepare_image_inputs()

        A			:	Optional[Any]										=       image_processor(lowerCamelCase__,		return_tensors="""np"""		)
        A			:	Any										=       processor(images=lowerCamelCase__,		return_tensors="""np"""		)

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






    def       _lowerCAmelCase (	self		):
        A			:	int										=       self.get_image_processor()
        A			:	Optional[Any]										=       self.get_tokenizer()

        A			:	Optional[int]										=       OwlViTProcessor(tokenizer=lowerCamelCase__,		image_processor=lowerCamelCase__		)

        A			:	Any										=       """lower newer"""

        A			:	Union[str, Any]										=       processor(text=lowerCamelCase__,		return_tensors="""np"""		)

        A			:	str										=       tokenizer(lowerCamelCase__,		return_tensors="""np"""		)

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






    def       _lowerCAmelCase (	self		):
        A			:	Tuple										=       self.get_image_processor()
        A			:	int										=       self.get_tokenizer()

        A			:	str										=       OwlViTProcessor(tokenizer=lowerCamelCase__,		image_processor=lowerCamelCase__		)

        A			:	List[str]										=       """lower newer"""
        A			:	Any										=       self.prepare_image_inputs()

        A			:	Tuple										=       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       _lowerCAmelCase (	self		):
        A			:	str										=       """google/owlvit-base-patch32"""
        A			:	Dict										=       OwlViTProcessor.from_pretrained(lowerCamelCase__		)

        A			:	str										=       ["""cat""", """nasa badge"""]
        A			:	Optional[int]										=       processor(text=lowerCamelCase__		)

        A			:	Any										=       16
        self.assertListEqual(list(inputs.keys()		),		["""input_ids""", """attention_mask"""]		)
        self.assertEqual(inputs["""input_ids"""].shape,		(2, seq_length)		)

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






    def       _lowerCAmelCase (	self		):
        A			:	Tuple										=       """google/owlvit-base-patch32"""
        A			:	Any										=       OwlViTProcessor.from_pretrained(lowerCamelCase__		)

        A			:	int										=       [["""cat""", """nasa badge"""], ["""person"""]]
        A			:	List[Any]										=       processor(text=lowerCamelCase__		)

        A			:	Dict										=       16
        A			:	List[str]										=       len(lowerCamelCase__		)
        A			:	List[str]										=       max([len(lowerCamelCase__		) for texts in input_texts]		)

        self.assertListEqual(list(inputs.keys()		),		["""input_ids""", """attention_mask"""]		)
        self.assertEqual(inputs["""input_ids"""].shape,		(batch_size * num_max_text_queries, seq_length)		)

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






    def       _lowerCAmelCase (	self		):
        A			:	Dict										=       """google/owlvit-base-patch32"""
        A			:	int										=       OwlViTProcessor.from_pretrained(lowerCamelCase__		)

        A			:	str										=       ["""cat""", """nasa badge"""]
        A			:	Optional[Any]										=       processor(text=lowerCamelCase__		)

        A			:	int										=       16
        A			:	Optional[Any]										=       inputs["""input_ids"""]
        A			:	Optional[int]										=       [
            [4_9406, 2368, 4_9407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [4_9406, 6841, 1_1301, 4_9407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        ]

        self.assertListEqual(list(inputs.keys()		),		["""input_ids""", """attention_mask"""]		)
        self.assertEqual(inputs["""input_ids"""].shape,		(2, seq_length)		)
        self.assertListEqual(list(input_ids[0]		),		predicted_ids[0]		)
        self.assertListEqual(list(input_ids[1]		),		predicted_ids[1]		)






    def       _lowerCAmelCase (	self		):
        A			:	Tuple										=       self.get_image_processor()
        A			:	Dict										=       self.get_tokenizer()

        A			:	Optional[Any]										=       OwlViTProcessor(tokenizer=lowerCamelCase__,		image_processor=lowerCamelCase__		)

        A			:	Any										=       self.prepare_image_inputs()
        A			:	Optional[Any]										=       self.prepare_image_inputs()

        A			:	List[str]										=       processor(images=lowerCamelCase__,		query_images=lowerCamelCase__		)

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

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






    def       _lowerCAmelCase (	self		):
        A			:	Any										=       self.get_image_processor()
        A			:	Optional[Any]										=       self.get_tokenizer()

        A			:	List[str]										=       OwlViTProcessor(tokenizer=lowerCamelCase__,		image_processor=lowerCamelCase__		)

        A			:	Any										=       [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

        A			:	Optional[Any]										=       processor.batch_decode(lowerCamelCase__		)
        A			:	Union[str, Any]										=       tokenizer.batch_decode(lowerCamelCase__		)

        self.assertListEqual(lowerCamelCase__,		lowerCamelCase__		)


