


'''simple docstring'''
import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
from ...utils import logging
from ..auto import CONFIG_MAPPING


__snake_case							      =     logging.get_logger(__name__)

__snake_case							      =     {
    '''salesforce/blip2-opt-2.7b''': '''https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json''',
}
class    lowercase	(							A__					):

		"""simple docstring"""

		_a     =				'blip_2_vision_model'




		def __init__(		self    ,   UpperCamelCase_=1408    ,   UpperCamelCase_=6144    ,   UpperCamelCase_=39    ,   UpperCamelCase_=16    ,   UpperCamelCase_=224    ,   UpperCamelCase_=14    ,   UpperCamelCase_="gelu"    ,   UpperCamelCase_=0.00001    ,   UpperCamelCase_=0.0    ,   UpperCamelCase_=1e-10    ,   UpperCamelCase_=True    ,   **UpperCamelCase_    ,   ):

							'''simple docstring'''


							super().__init__(**UpperCamelCase_							)

							UpperCamelCase__   :List[Any]												= hidden_size
							UpperCamelCase__   :int												= intermediate_size
							UpperCamelCase__   :Tuple												= num_hidden_layers
							UpperCamelCase__   :Dict												= num_attention_heads
							UpperCamelCase__   :List[str]												= patch_size
							UpperCamelCase__   :Optional[Any]												= image_size
							UpperCamelCase__   :Any												= initializer_range
							UpperCamelCase__   :int												= attention_dropout
							UpperCamelCase__   :str												= layer_norm_eps
							UpperCamelCase__   :Optional[int]												= hidden_act
							UpperCamelCase__   :List[str]												= qkv_bias




		@classmethod
		def     lowerCAmelCase__				(		cls    ,   UpperCamelCase_    ,   **UpperCamelCase_							):

							'''simple docstring'''


							cls._set_token_in_kwargs(UpperCamelCase_							)

							UpperCamelCase__					,				UpperCamelCase__   :List[Any]												= cls.get_config_dict(UpperCamelCase_    ,   **UpperCamelCase_							)

							# get the vision config dict if we are loading from Blip2Config
							if config_dict.get('''model_type'''							) == "blip-2":
												UpperCamelCase__   :Tuple												= config_dict['''vision_config''']

							if "model_type" in config_dict and hasattr(cls    ,   '''model_type'''							) and config_dict["model_type"] != cls.model_type:
												logger.warning(
												    F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
												    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''							)

							return cls.from_dict(UpperCamelCase_    ,   **UpperCamelCase_							)



class    lowercase	(							A__					):

		"""simple docstring"""

		_a     =				'blip_2_qformer'




		def __init__(		self    ,   UpperCamelCase_=30522    ,   UpperCamelCase_=768    ,   UpperCamelCase_=12    ,   UpperCamelCase_=12    ,   UpperCamelCase_=3072    ,   UpperCamelCase_="gelu"    ,   UpperCamelCase_=0.1    ,   UpperCamelCase_=0.1    ,   UpperCamelCase_=512    ,   UpperCamelCase_=0.02    ,   UpperCamelCase_=1e-12    ,   UpperCamelCase_=0    ,   UpperCamelCase_="absolute"    ,   UpperCamelCase_=2    ,   UpperCamelCase_=1408    ,   **UpperCamelCase_    ,   ):

							'''simple docstring'''


							super().__init__(pad_token_id=UpperCamelCase_    ,   **UpperCamelCase_							)

							UpperCamelCase__   :str												= vocab_size
							UpperCamelCase__   :Tuple												= hidden_size
							UpperCamelCase__   :List[Any]												= num_hidden_layers
							UpperCamelCase__   :List[str]												= num_attention_heads
							UpperCamelCase__   :Dict												= hidden_act
							UpperCamelCase__   :Any												= intermediate_size
							UpperCamelCase__   :Optional[Any]												= hidden_dropout_prob
							UpperCamelCase__   :List[str]												= attention_probs_dropout_prob
							UpperCamelCase__   :str												= max_position_embeddings
							UpperCamelCase__   :Union[str, Any]												= initializer_range
							UpperCamelCase__   :Dict												= layer_norm_eps
							UpperCamelCase__   :Union[str, Any]												= position_embedding_type
							UpperCamelCase__   :str												= cross_attention_frequency
							UpperCamelCase__   :List[str]												= encoder_hidden_size




		@classmethod
		def     lowerCAmelCase__				(		cls    ,   UpperCamelCase_    ,   **UpperCamelCase_							):

							'''simple docstring'''


							cls._set_token_in_kwargs(UpperCamelCase_							)

							UpperCamelCase__					,				UpperCamelCase__   :str												= cls.get_config_dict(UpperCamelCase_    ,   **UpperCamelCase_							)

							# get the qformer config dict if we are loading from Blip2Config
							if config_dict.get('''model_type'''							) == "blip-2":
												UpperCamelCase__   :Any												= config_dict['''qformer_config''']

							if "model_type" in config_dict and hasattr(cls    ,   '''model_type'''							) and config_dict["model_type"] != cls.model_type:
												logger.warning(
												    F'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
												    F'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''							)

							return cls.from_dict(UpperCamelCase_    ,   **UpperCamelCase_							)



class    lowercase	(							A__					):

		"""simple docstring"""

		_a     =				'blip-2'
		_a     =				True




		def __init__(		self    ,   UpperCamelCase_=None    ,   UpperCamelCase_=None    ,   UpperCamelCase_=None    ,   UpperCamelCase_=32    ,   **UpperCamelCase_							):

							'''simple docstring'''


							super().__init__(**UpperCamelCase_							)

							if vision_config is None:
												UpperCamelCase__   :Optional[int]												= {}
												logger.info('''vision_config is None. initializing the Blip2VisionConfig with default values.'''							)

							if qformer_config is None:
												UpperCamelCase__   :int												= {}
												logger.info('''qformer_config is None. Initializing the Blip2QFormerConfig with default values.'''							)

							if text_config is None:
												UpperCamelCase__   :List[str]												= {}
												logger.info('''text_config is None. Initializing the text config with default values (`OPTConfig`).'''							)

							UpperCamelCase__   :Any												= BlipaVisionConfig(**UpperCamelCase_							)
							UpperCamelCase__   :List[str]												= BlipaQFormerConfig(**UpperCamelCase_							)
							UpperCamelCase__   :Optional[int]												= text_config['''model_type'''] if '''model_type''' in text_config else '''opt'''
							UpperCamelCase__   :Any												= CONFIG_MAPPING[text_model_type](**UpperCamelCase_							)

							UpperCamelCase__   :List[str]												= self.text_config.tie_word_embeddings
							UpperCamelCase__   :int												= self.text_config.is_encoder_decoder

							UpperCamelCase__   :List[str]												= num_query_tokens
							UpperCamelCase__   :Optional[int]												= self.vision_config.hidden_size
							UpperCamelCase__   :Any												= self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
							UpperCamelCase__   :Optional[Any]												= 1.0
							UpperCamelCase__   :List[Any]												= 0.02




		@classmethod
		def     lowerCAmelCase__				(		cls    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   **UpperCamelCase_    ,   ):

							'''simple docstring'''


							return cls(
							    vision_config=vision_config.to_dict()    ,   qformer_config=qformer_config.to_dict()    ,   text_config=text_config.to_dict()    ,   **UpperCamelCase_    ,   )




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :str												= copy.deepcopy(self.__dict__							)
							UpperCamelCase__   :List[str]												= self.vision_config.to_dict()
							UpperCamelCase__   :Dict												= self.qformer_config.to_dict()
							UpperCamelCase__   :List[Any]												= self.text_config.to_dict()
							UpperCamelCase__   :Dict												= self.__class__.model_type
							return output



'''simple docstring'''
import io
import json

import fsspec
import pytest

from datasets import Dataset, DatasetDict, Features, NamedSplit, Value
from datasets.io.json import JsonDatasetReader, JsonDatasetWriter

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases


def 			a      (			__a ,					__a   )  ->	Optional[int]:



					'''simple docstring'''






					assert isinstance(__a ,					__a   )
					assert dataset.num_rows == 4
					assert dataset.num_columns == 3
					assert dataset.column_names == ["col_1", "col_2", "col_3"]
					for feature, expected_dtype in expected_features.items():
										assert dataset.features[feature].dtype == expected_dtype


@pytest.mark.parametrize('''keep_in_memory''' ,					[False, True]   )
def 			a      (			__a ,					__a ,					__a   )  ->	Optional[Any]:



					'''simple docstring'''






					UpperCamelCase__   :Union[str, Any]												= tmp_path / '''cache'''
					UpperCamelCase__   :Dict												= {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
					with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
										UpperCamelCase__   :Tuple												= JsonDatasetReader(__a ,					cache_dir=__a ,					keep_in_memory=__a   ).read()
					_check_json_dataset(__a ,					__a   )


@pytest.mark.parametrize(
    '''features''' ,					[
        None,
        {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''},
        {'''col_1''': '''string''', '''col_2''': '''string''', '''col_3''': '''string'''},
        {'''col_1''': '''int32''', '''col_2''': '''int32''', '''col_3''': '''int32'''},
        {'''col_1''': '''float32''', '''col_2''': '''float32''', '''col_3''': '''float32'''},
    ] ,					)
def 			a      (			__a ,					__a ,					__a   )  ->	Any:



					'''simple docstring'''






					UpperCamelCase__   :Union[str, Any]												= tmp_path / '''cache'''
					UpperCamelCase__   :Optional[Any]												= {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
					UpperCamelCase__   :Optional[Any]												= features.copy() if features else default_expected_features
					UpperCamelCase__   :Tuple												= (
					    Features({feature: Value(__a   ) for feature, dtype in features.items()}   ) if features is not None else None
					)
					UpperCamelCase__   :int												= JsonDatasetReader(__a ,					features=__a ,					cache_dir=__a   ).read()
					_check_json_dataset(__a ,					__a   )


@pytest.mark.parametrize(
    '''features''' ,					[
        None,
        {'''col_3''': '''float64''', '''col_1''': '''string''', '''col_2''': '''int64'''},
    ] ,					)
def 			a      (			__a ,					__a ,					__a   )  ->	Tuple:



					'''simple docstring'''






					UpperCamelCase__   :int												= tmp_path / '''cache'''
					UpperCamelCase__   :str												= {'''col_3''': '''float64''', '''col_1''': '''string''', '''col_2''': '''int64'''}
					UpperCamelCase__   :Any												= features.copy() if features else default_expected_features
					UpperCamelCase__   :Union[str, Any]												= (
					    Features({feature: Value(__a   ) for feature, dtype in features.items()}   ) if features is not None else None
					)
					UpperCamelCase__   :Any												= JsonDatasetReader(__a ,					features=__a ,					cache_dir=__a   ).read()
					assert isinstance(__a ,					__a   )
					assert dataset.num_rows == 2
					assert dataset.num_columns == 3
					assert dataset.column_names == ["col_3", "col_1", "col_2"]
					for feature, expected_dtype in expected_features.items():
										assert dataset.features[feature].dtype == expected_dtype


def 			a      (			__a ,					__a   )  ->	List[Any]:



					'''simple docstring'''






					UpperCamelCase__   :Any												= {'''col_2''': '''int64''', '''col_3''': '''float64''', '''col_1''': '''string'''}
					UpperCamelCase__   :int												= features.copy()
					UpperCamelCase__   :List[Any]												= (
					    Features({feature: Value(__a   ) for feature, dtype in features.items()}   ) if features is not None else None
					)
					UpperCamelCase__   :Optional[int]												= tmp_path / '''cache'''
					UpperCamelCase__   :Dict												= JsonDatasetReader(__a ,					features=__a ,					cache_dir=__a   ).read()
					assert isinstance(__a ,					__a   )
					assert dataset.num_rows == 2
					assert dataset.num_columns == 3
					assert dataset.column_names == ["col_2", "col_3", "col_1"]
					for feature, expected_dtype in expected_features.items():
										assert dataset.features[feature].dtype == expected_dtype


@pytest.mark.parametrize('''split''' ,					[None, NamedSplit('''train'''   ), '''train''', '''test''']   )
def 			a      (			__a ,					__a ,					__a   )  ->	List[Any]:



					'''simple docstring'''






					UpperCamelCase__   :Union[str, Any]												= tmp_path / '''cache'''
					UpperCamelCase__   :Optional[Any]												= {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
					UpperCamelCase__   :List[Any]												= JsonDatasetReader(__a ,					cache_dir=__a ,					split=__a   ).read()
					_check_json_dataset(__a ,					__a   )
					assert dataset.split == split if split else "train"


@pytest.mark.parametrize('''path_type''' ,					[str, list]   )
def 			a      (			__a ,					__a ,					__a   )  ->	Any:



					'''simple docstring'''






					if issubclass(__a ,					__a   ):
										UpperCamelCase__   :Union[str, Any]												= jsonl_path
					elif issubclass(__a ,					__a   ):
										UpperCamelCase__   :int												= [jsonl_path]
					UpperCamelCase__   :Dict												= tmp_path / '''cache'''
					UpperCamelCase__   :Any												= {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
					UpperCamelCase__   :List[str]												= JsonDatasetReader(__a ,					cache_dir=__a   ).read()
					_check_json_dataset(__a ,					__a   )


def 			a      (			__a ,					__a ,					__a=("train",)   )  ->	Optional[Any]:



					'''simple docstring'''






					assert isinstance(__a ,					__a   )
					for split in splits:
										UpperCamelCase__   :Optional[int]												= dataset_dict[split]
										assert dataset.num_rows == 4
										assert dataset.num_columns == 3
										assert dataset.column_names == ["col_1", "col_2", "col_3"]
										for feature, expected_dtype in expected_features.items():
															assert dataset.features[feature].dtype == expected_dtype


@pytest.mark.parametrize('''keep_in_memory''' ,					[False, True]   )
def 			a      (			__a ,					__a ,					__a   )  ->	List[str]:



					'''simple docstring'''






					UpperCamelCase__   :List[str]												= tmp_path / '''cache'''
					UpperCamelCase__   :Dict												= {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
					with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
										UpperCamelCase__   :str												= JsonDatasetReader({'''train''': jsonl_path} ,					cache_dir=__a ,					keep_in_memory=__a   ).read()
					_check_json_datasetdict(__a ,					__a   )


@pytest.mark.parametrize(
    '''features''' ,					[
        None,
        {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''},
        {'''col_1''': '''string''', '''col_2''': '''string''', '''col_3''': '''string'''},
        {'''col_1''': '''int32''', '''col_2''': '''int32''', '''col_3''': '''int32'''},
        {'''col_1''': '''float32''', '''col_2''': '''float32''', '''col_3''': '''float32'''},
    ] ,					)
def 			a      (			__a ,					__a ,					__a   )  ->	int:



					'''simple docstring'''






					UpperCamelCase__   :Tuple												= tmp_path / '''cache'''
					UpperCamelCase__   :Any												= {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
					UpperCamelCase__   :Optional[int]												= features.copy() if features else default_expected_features
					UpperCamelCase__   :str												= (
					    Features({feature: Value(__a   ) for feature, dtype in features.items()}   ) if features is not None else None
					)
					UpperCamelCase__   :Dict												= JsonDatasetReader({'''train''': jsonl_path} ,					features=__a ,					cache_dir=__a   ).read()
					_check_json_datasetdict(__a ,					__a   )


@pytest.mark.parametrize('''split''' ,					[None, NamedSplit('''train'''   ), '''train''', '''test''']   )
def 			a      (			__a ,					__a ,					__a   )  ->	str:



					'''simple docstring'''






					if split:
										UpperCamelCase__   :List[str]												= {split: jsonl_path}
					else:
										UpperCamelCase__   :int												= '''train'''
										UpperCamelCase__   :int												= {'''train''': jsonl_path, '''test''': jsonl_path}
					UpperCamelCase__   :Any												= tmp_path / '''cache'''
					UpperCamelCase__   :Union[str, Any]												= {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
					UpperCamelCase__   :Any												= JsonDatasetReader(__a ,					cache_dir=__a   ).read()
					_check_json_datasetdict(__a ,					__a ,					splits=list(path.keys()   )   )
					assert all(dataset[split].split == split for split in path.keys()   )


def 			a      (			__a   )  ->	Union[str, Any]:



					'''simple docstring'''






					return json.load(__a   )


def 			a      (			__a   )  ->	int:



					'''simple docstring'''






					return [json.loads(__a   ) for line in buffer]







class    lowercase	:

		"""simple docstring"""

		@pytest.mark.parametrize('''lines, load_json_function'''    ,   [(True, load_json_lines), (False, load_json)]							)
		def     lowerCAmelCase__				(		self    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_							):

							'''simple docstring'''


							with io.BytesIO() as buffer:
												JsonDatasetWriter(UpperCamelCase_    ,   UpperCamelCase_    ,   lines=UpperCamelCase_							).write()
												buffer.seek(0							)
												UpperCamelCase__   :List[Any]												= load_json_function(UpperCamelCase_							)
							assert isinstance(UpperCamelCase_    ,   UpperCamelCase_							)
							assert isinstance(exported_content[0]    ,   UpperCamelCase_							)
							assert len(UpperCamelCase_							) == 10




		@pytest.mark.parametrize(
		    '''orient, container, keys, len_at'''    ,   [
		        ('''records''', list, {'''tokens''', '''labels''', '''answers''', '''id'''}, None),
		        ('''split''', dict, {'''columns''', '''data'''}, '''data'''),
		        ('''index''', dict, set('''0123456789'''							), None),
		        ('''columns''', dict, {'''tokens''', '''labels''', '''answers''', '''id'''}, '''tokens'''),
		        ('''values''', list, None, None),
		        ('''table''', dict, {'''schema''', '''data'''}, '''data'''),
		    ]    ,   )
		def     lowerCAmelCase__				(		self    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_							):

							'''simple docstring'''


							with io.BytesIO() as buffer:
												JsonDatasetWriter(UpperCamelCase_    ,   UpperCamelCase_    ,   lines=UpperCamelCase_    ,   orient=UpperCamelCase_							).write()
												buffer.seek(0							)
												UpperCamelCase__   :Optional[int]												= load_json(UpperCamelCase_							)
							assert isinstance(UpperCamelCase_    ,   UpperCamelCase_							)
							if keys:
												if container is dict:
																	assert exported_content.keys() == keys
												else:
																	assert exported_content[0].keys() == keys
							else:
												assert not hasattr(UpperCamelCase_    ,   '''keys'''							) and not hasattr(exported_content[0]    ,   '''keys'''							)
							if len_at:
												assert len(exported_content[len_at]							) == 10
							else:
												assert len(UpperCamelCase_							) == 10




		@pytest.mark.parametrize('''lines, load_json_function'''    ,   [(True, load_json_lines), (False, load_json)]							)
		def     lowerCAmelCase__				(		self    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_							):

							'''simple docstring'''


							with io.BytesIO() as buffer:
												JsonDatasetWriter(UpperCamelCase_    ,   UpperCamelCase_    ,   lines=UpperCamelCase_    ,   num_proc=2							).write()
												buffer.seek(0							)
												UpperCamelCase__   :Union[str, Any]												= load_json_function(UpperCamelCase_							)
							assert isinstance(UpperCamelCase_    ,   UpperCamelCase_							)
							assert isinstance(exported_content[0]    ,   UpperCamelCase_							)
							assert len(UpperCamelCase_							) == 10




		@pytest.mark.parametrize(
		    '''orient, container, keys, len_at'''    ,   [
		        ('''records''', list, {'''tokens''', '''labels''', '''answers''', '''id'''}, None),
		        ('''split''', dict, {'''columns''', '''data'''}, '''data'''),
		        ('''index''', dict, set('''0123456789'''							), None),
		        ('''columns''', dict, {'''tokens''', '''labels''', '''answers''', '''id'''}, '''tokens'''),
		        ('''values''', list, None, None),
		        ('''table''', dict, {'''schema''', '''data'''}, '''data'''),
		    ]    ,   )
		def     lowerCAmelCase__				(		self    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_							):

							'''simple docstring'''


							with io.BytesIO() as buffer:
												JsonDatasetWriter(UpperCamelCase_    ,   UpperCamelCase_    ,   lines=UpperCamelCase_    ,   orient=UpperCamelCase_    ,   num_proc=2							).write()
												buffer.seek(0							)
												UpperCamelCase__   :int												= load_json(UpperCamelCase_							)
							assert isinstance(UpperCamelCase_    ,   UpperCamelCase_							)
							if keys:
												if container is dict:
																	assert exported_content.keys() == keys
												else:
																	assert exported_content[0].keys() == keys
							else:
												assert not hasattr(UpperCamelCase_    ,   '''keys'''							) and not hasattr(exported_content[0]    ,   '''keys'''							)
							if len_at:
												assert len(exported_content[len_at]							) == 10
							else:
												assert len(UpperCamelCase_							) == 10




		def     lowerCAmelCase__				(		self    ,   UpperCamelCase_							):

							'''simple docstring'''


							with pytest.raises(UpperCamelCase_							):
												with io.BytesIO() as buffer:
																	JsonDatasetWriter(UpperCamelCase_    ,   UpperCamelCase_    ,   num_proc=0							)




		@pytest.mark.parametrize('''compression, extension'''    ,   [('''gzip''', '''gz'''), ('''bz2''', '''bz2'''), ('''xz''', '''xz''')]							)
		def     lowerCAmelCase__				(		self    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__   :Tuple												= tmp_path_factory.mktemp('''data'''							) / F'''test.json.{extension}'''
							UpperCamelCase__   :Union[str, Any]												= str(shared_datadir / F'''test_file.json.{extension}'''							)
							JsonDatasetWriter(UpperCamelCase_    ,   UpperCamelCase_    ,   compression=UpperCamelCase_							).write()

							with fsspec.open(UpperCamelCase_    ,   '''rb'''    ,   compression='''infer'''							) as f:
												UpperCamelCase__   :Dict												= f.read()
							with fsspec.open(UpperCamelCase_    ,   '''rb'''    ,   compression='''infer'''							) as f:
												UpperCamelCase__   :int												= f.read()
							assert exported_content == original_content