


"""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   snake_case						(				_a:     str		,   _a:     Union[str, Any]							)->		Tuple:
      '''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   snake_case						(				_a:     Tuple		,   _a:     int		,   _a:     Optional[int]							)->		Dict:
      '''simple docstring'''


      lowerCamelCase__           =		tmp_path / 'cache'
      lowerCamelCase__           =		{'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
      with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
            lowerCamelCase__           =		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   snake_case						(				_a:     List[Any]		,   _a:     int		,   _a:     Dict							)->		List[str]:
      '''simple docstring'''


      lowerCamelCase__           =		tmp_path / 'cache'
      lowerCamelCase__           =		{'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
      lowerCamelCase__           =		features.copy() if features else default_expected_features
      lowerCamelCase__           =		(
          Features({feature: Value(_a							) for feature, dtype in features.items()}							) if features is not None else None
      )
      lowerCamelCase__           =		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   snake_case						(				_a:     List[Any]		,   _a:     Optional[int]		,   _a:     Any							)->		int:
      '''simple docstring'''


      lowerCamelCase__           =		tmp_path / 'cache'
      lowerCamelCase__           =		{'col_3': 'float64', 'col_1': 'string', 'col_2': 'int64'}
      lowerCamelCase__           =		features.copy() if features else default_expected_features
      lowerCamelCase__           =		(
          Features({feature: Value(_a							) for feature, dtype in features.items()}							) if features is not None else None
      )
      lowerCamelCase__           =		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   snake_case						(				_a:     Any		,   _a:     Optional[int]							)->		Union[str, Any]:
      '''simple docstring'''


      lowerCamelCase__           =		{'col_2': 'int64', 'col_3': 'float64', 'col_1': 'string'}
      lowerCamelCase__           =		features.copy()
      lowerCamelCase__           =		(
          Features({feature: Value(_a							) for feature, dtype in features.items()}							) if features is not None else None
      )
      lowerCamelCase__           =		tmp_path / 'cache'
      lowerCamelCase__           =		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   snake_case						(				_a:     List[Any]		,   _a:     Optional[Any]		,   _a:     Union[str, Any]							)->		int:
      '''simple docstring'''


      lowerCamelCase__           =		tmp_path / 'cache'
      lowerCamelCase__           =		{'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
      lowerCamelCase__           =		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   snake_case						(				_a:     Any		,   _a:     List[str]		,   _a:     Optional[int]							)->		Optional[int]:
      '''simple docstring'''


      if issubclass(_a		,   _a							):
            lowerCamelCase__           =		jsonl_path
      elif issubclass(_a		,   _a							):
            lowerCamelCase__           =		[jsonl_path]
      lowerCamelCase__           =		tmp_path / 'cache'
      lowerCamelCase__           =		{'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
      lowerCamelCase__           =		JsonDatasetReader(_a		,   cache_dir=_a							).read()
      _check_json_dataset(_a		,   _a							)
def   snake_case						(				_a:     Optional[int]		,   _a:     int		,   _a:     str=("train",)							)->		Union[str, Any]:
      '''simple docstring'''


      assert isinstance(_a		,   _a							)
      for split in splits:
            lowerCamelCase__           =		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   snake_case						(				_a:     Dict		,   _a:     int		,   _a:     List[str]							)->		List[str]:
      '''simple docstring'''


      lowerCamelCase__           =		tmp_path / 'cache'
      lowerCamelCase__           =		{'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
      with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
            lowerCamelCase__           =		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   snake_case						(				_a:     int		,   _a:     Any		,   _a:     Dict							)->		Union[str, Any]:
      '''simple docstring'''


      lowerCamelCase__           =		tmp_path / 'cache'
      lowerCamelCase__           =		{'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
      lowerCamelCase__           =		features.copy() if features else default_expected_features
      lowerCamelCase__           =		(
          Features({feature: Value(_a							) for feature, dtype in features.items()}							) if features is not None else None
      )
      lowerCamelCase__           =		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   snake_case						(				_a:     int		,   _a:     Optional[int]		,   _a:     int							)->		Dict:
      '''simple docstring'''


      if split:
            lowerCamelCase__           =		{split: jsonl_path}
      else:
            lowerCamelCase__           =		'train'
            lowerCamelCase__           =		{'train': jsonl_path, 'test': jsonl_path}
      lowerCamelCase__           =		tmp_path / 'cache'
      lowerCamelCase__           =		{'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
      lowerCamelCase__           =		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   snake_case						(				_a:     Optional[Any]							)->		List[Any]:
      '''simple docstring'''


      return json.load(_a							)
def   snake_case						(				_a:     Tuple							)->		Dict:
      '''simple docstring'''


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






class   _a      :
    @pytest.mark.parametrize('lines, load_json_function'							,					[(True, load_json_lines), (False, load_json)]				)
    def      _UpperCamelCase       ( self							:					Optional[Any]							,					SCREAMING_SNAKE_CASE__							:					Optional[int]							,					SCREAMING_SNAKE_CASE__							:					Tuple							,					SCREAMING_SNAKE_CASE__							:					Optional[Any]				):
          with io.BytesIO() as buffer:
                JsonDatasetWriter(SCREAMING_SNAKE_CASE__							,					SCREAMING_SNAKE_CASE__							,					lines=SCREAMING_SNAKE_CASE__				).write()
                buffer.seek(0				)
                lowerCamelCase__           =		load_json_function(SCREAMING_SNAKE_CASE__				)
          assert isinstance(SCREAMING_SNAKE_CASE__							,					SCREAMING_SNAKE_CASE__				)
          assert isinstance(exported_content[0]							,					SCREAMING_SNAKE_CASE__				)
          assert len(SCREAMING_SNAKE_CASE__				) == 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      _UpperCamelCase       ( self							:					str							,					SCREAMING_SNAKE_CASE__							:					Optional[int]							,					SCREAMING_SNAKE_CASE__							:					List[str]							,					SCREAMING_SNAKE_CASE__							:					List[Any]							,					SCREAMING_SNAKE_CASE__							:					Dict							,					SCREAMING_SNAKE_CASE__							:					Any				):
          with io.BytesIO() as buffer:
                JsonDatasetWriter(SCREAMING_SNAKE_CASE__							,					SCREAMING_SNAKE_CASE__							,					lines=SCREAMING_SNAKE_CASE__							,					orient=SCREAMING_SNAKE_CASE__				).write()
                buffer.seek(0				)
                lowerCamelCase__           =		load_json(SCREAMING_SNAKE_CASE__				)
          assert isinstance(SCREAMING_SNAKE_CASE__							,					SCREAMING_SNAKE_CASE__				)
          if keys:
                if container is dict:
                      assert exported_content.keys() == keys
                else:
                      assert exported_content[0].keys() == keys
          else:
                assert not hasattr(SCREAMING_SNAKE_CASE__							,					'keys'				) and not hasattr(exported_content[0]							,					'keys'				)
          if len_at:
                assert len(exported_content[len_at]				) == 10
          else:
                assert len(SCREAMING_SNAKE_CASE__				) == 10
    @pytest.mark.parametrize('lines, load_json_function'							,					[(True, load_json_lines), (False, load_json)]				)
    def      _UpperCamelCase       ( self							:					List[Any]							,					SCREAMING_SNAKE_CASE__							:					str							,					SCREAMING_SNAKE_CASE__							:					str							,					SCREAMING_SNAKE_CASE__							:					Tuple				):
          with io.BytesIO() as buffer:
                JsonDatasetWriter(SCREAMING_SNAKE_CASE__							,					SCREAMING_SNAKE_CASE__							,					lines=SCREAMING_SNAKE_CASE__							,					num_proc=2				).write()
                buffer.seek(0				)
                lowerCamelCase__           =		load_json_function(SCREAMING_SNAKE_CASE__				)
          assert isinstance(SCREAMING_SNAKE_CASE__							,					SCREAMING_SNAKE_CASE__				)
          assert isinstance(exported_content[0]							,					SCREAMING_SNAKE_CASE__				)
          assert len(SCREAMING_SNAKE_CASE__				) == 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      _UpperCamelCase       ( self							:					List[Any]							,					SCREAMING_SNAKE_CASE__							:					List[str]							,					SCREAMING_SNAKE_CASE__							:					List[str]							,					SCREAMING_SNAKE_CASE__							:					List[Any]							,					SCREAMING_SNAKE_CASE__							:					Dict							,					SCREAMING_SNAKE_CASE__							:					str				):
          with io.BytesIO() as buffer:
                JsonDatasetWriter(SCREAMING_SNAKE_CASE__							,					SCREAMING_SNAKE_CASE__							,					lines=SCREAMING_SNAKE_CASE__							,					orient=SCREAMING_SNAKE_CASE__							,					num_proc=2				).write()
                buffer.seek(0				)
                lowerCamelCase__           =		load_json(SCREAMING_SNAKE_CASE__				)
          assert isinstance(SCREAMING_SNAKE_CASE__							,					SCREAMING_SNAKE_CASE__				)
          if keys:
                if container is dict:
                      assert exported_content.keys() == keys
                else:
                      assert exported_content[0].keys() == keys
          else:
                assert not hasattr(SCREAMING_SNAKE_CASE__							,					'keys'				) and not hasattr(exported_content[0]							,					'keys'				)
          if len_at:
                assert len(exported_content[len_at]				) == 10
          else:
                assert len(SCREAMING_SNAKE_CASE__				) == 10
    def      _UpperCamelCase       ( self							:					Union[str, Any]							,					SCREAMING_SNAKE_CASE__							:					str				):
          with pytest.raises(SCREAMING_SNAKE_CASE__				):
                with io.BytesIO() as buffer:
                      JsonDatasetWriter(SCREAMING_SNAKE_CASE__							,					SCREAMING_SNAKE_CASE__							,					num_proc=0				)


    @pytest.mark.parametrize('compression, extension'							,					[('gzip', 'gz'), ('bz2', 'bz2'), ('xz', 'xz')]				)
    def      _UpperCamelCase       ( self							:					Tuple							,					SCREAMING_SNAKE_CASE__							:					Union[str, Any]							,					SCREAMING_SNAKE_CASE__							:					Union[str, Any]							,					SCREAMING_SNAKE_CASE__							:					Any							,					SCREAMING_SNAKE_CASE__							:					Union[str, Any]							,					SCREAMING_SNAKE_CASE__							:					int				):
          lowerCamelCase__           =		tmp_path_factory.mktemp('data'				) / F'test.json.{extension}'
          lowerCamelCase__           =		str(shared_datadir / F'test_file.json.{extension}'				)
          JsonDatasetWriter(SCREAMING_SNAKE_CASE__							,					SCREAMING_SNAKE_CASE__							,					compression=SCREAMING_SNAKE_CASE__				).write()

          with fsspec.open(SCREAMING_SNAKE_CASE__							,					'rb'							,					compression='infer'				) as f:
                lowerCamelCase__           =		f.read()
          with fsspec.open(SCREAMING_SNAKE_CASE__							,					'rb'							,					compression='infer'				) as f:
                lowerCamelCase__           =		f.read()
          assert exported_content == original_content







"""simple docstring"""


from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

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


if is_tf_available():
   import tensorflow as tf

   from transformers import TFBlipTextModel
   from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST



class   _a      :
    def __init__( self							:					List[str]							,					SCREAMING_SNAKE_CASE__							:					List[str]							,					SCREAMING_SNAKE_CASE__							:					Union[str, Any]=12							,					SCREAMING_SNAKE_CASE__							:					Union[str, Any]=7							,					SCREAMING_SNAKE_CASE__							:					Union[str, Any]=True							,					SCREAMING_SNAKE_CASE__							:					Optional[Any]=True							,					SCREAMING_SNAKE_CASE__							:					str=True							,					SCREAMING_SNAKE_CASE__							:					Any=99							,					SCREAMING_SNAKE_CASE__							:					Optional[Any]=32							,					SCREAMING_SNAKE_CASE__							:					str=32							,					SCREAMING_SNAKE_CASE__							:					Tuple=2							,					SCREAMING_SNAKE_CASE__							:					str=4							,					SCREAMING_SNAKE_CASE__							:					Optional[Any]=37							,					SCREAMING_SNAKE_CASE__							:					Union[str, Any]=0.1							,					SCREAMING_SNAKE_CASE__							:					Optional[Any]=0.1							,					SCREAMING_SNAKE_CASE__							:					str=5_12							,					SCREAMING_SNAKE_CASE__							:					str=0.02							,					SCREAMING_SNAKE_CASE__							:					Union[str, Any]=0							,					SCREAMING_SNAKE_CASE__							:					Any=None							,					):
          lowerCamelCase__           =		parent
          lowerCamelCase__           =		batch_size
          lowerCamelCase__           =		seq_length
          lowerCamelCase__           =		is_training
          lowerCamelCase__           =		use_input_mask
          lowerCamelCase__           =		use_labels
          lowerCamelCase__           =		vocab_size
          lowerCamelCase__           =		hidden_size
          lowerCamelCase__           =		projection_dim
          lowerCamelCase__           =		num_hidden_layers
          lowerCamelCase__           =		num_attention_heads
          lowerCamelCase__           =		intermediate_size
          lowerCamelCase__           =		dropout
          lowerCamelCase__           =		attention_dropout
          lowerCamelCase__           =		max_position_embeddings
          lowerCamelCase__           =		initializer_range
          lowerCamelCase__           =		scope
          lowerCamelCase__           =		bos_token_id
    def      _UpperCamelCase       ( self							:					int				):
          lowerCamelCase__           =		ids_tensor([self.batch_size, self.seq_length]							,					self.vocab_size				)

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

          if input_mask is not None:
                lowerCamelCase__           =		input_mask.numpy()
                lowerCamelCase__    ,  lowerCamelCase__           =		input_mask.shape
                lowerCamelCase__           =		np.random.randint(1							,					seq_length - 1							,					size=(batch_size,)				)
                for batch_idx, start_index in enumerate(SCREAMING_SNAKE_CASE__				):
                      lowerCamelCase__           =		1
                      lowerCamelCase__           =		0

          lowerCamelCase__           =		self.get_config()

          return config, input_ids, tf.convert_to_tensor(SCREAMING_SNAKE_CASE__				)
    def      _UpperCamelCase       ( self							:					Any				):
          return BlipTextConfig(
              vocab_size=self.vocab_size							,					hidden_size=self.hidden_size							,					projection_dim=self.projection_dim							,					num_hidden_layers=self.num_hidden_layers							,					num_attention_heads=self.num_attention_heads							,					intermediate_size=self.intermediate_size							,					dropout=self.dropout							,					attention_dropout=self.attention_dropout							,					max_position_embeddings=self.max_position_embeddings							,					initializer_range=self.initializer_range							,					bos_token_id=self.bos_token_id							,					)
    def      _UpperCamelCase       ( self							:					Any							,					SCREAMING_SNAKE_CASE__							:					Any							,					SCREAMING_SNAKE_CASE__							:					List[str]							,					SCREAMING_SNAKE_CASE__							:					List[str]				):
          lowerCamelCase__           =		TFBlipTextModel(config=SCREAMING_SNAKE_CASE__				)
          lowerCamelCase__           =		model(SCREAMING_SNAKE_CASE__							,					attention_mask=SCREAMING_SNAKE_CASE__							,					training=SCREAMING_SNAKE_CASE__				)
          lowerCamelCase__           =		model(SCREAMING_SNAKE_CASE__							,					training=SCREAMING_SNAKE_CASE__				)
          self.parent.assertEqual(result.last_hidden_state.shape							,					(self.batch_size, self.seq_length, self.hidden_size)				)
          self.parent.assertEqual(result.pooler_output.shape							,					(self.batch_size, self.hidden_size)				)


    def      _UpperCamelCase       ( self							:					Dict				):
          lowerCamelCase__           =		self.prepare_config_and_inputs()
          lowerCamelCase__    ,  lowerCamelCase__    ,  lowerCamelCase__           =		config_and_inputs
          lowerCamelCase__           =		{'input_ids': input_ids, 'attention_mask': input_mask}
          return config, inputs_dict





@require_tf
class   _a      (      SCREAMING_SNAKE_CASE_		,		unittest.TestCase ):
    a_    :				str             =    (TFBlipTextModel,) if is_tf_available() else ()
    a_    :				List[str]             =    False
    a_    :				Optional[Any]             =    False
    a_    :				Union[str, Any]             =    False
    def      _UpperCamelCase       ( self							:					str				):
          lowerCamelCase__           =		BlipTextModelTester(self				)
          lowerCamelCase__           =		ConfigTester(self							,					config_class=SCREAMING_SNAKE_CASE__							,					hidden_size=37				)
    def      _UpperCamelCase       ( self							:					Tuple				):
          self.config_tester.run_common_tests()
    def      _UpperCamelCase       ( self							:					str				):
          lowerCamelCase__           =		self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE__				)
    def      _UpperCamelCase       ( self							:					Tuple				):
          pass
    def      _UpperCamelCase       ( self							:					Tuple				):
          pass
    @unittest.skip(reason='Blip does not use inputs_embeds'				)
    def      _UpperCamelCase       ( self							:					List[str]				):
          pass
    @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING'				)
    def      _UpperCamelCase       ( self							:					Dict				):
          pass
    @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING'				)
    def      _UpperCamelCase       ( self							:					List[Any]				):
          pass
    @slow
    def      _UpperCamelCase       ( self							:					str				):
          for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                lowerCamelCase__           =		TFBlipTextModel.from_pretrained(SCREAMING_SNAKE_CASE__				)
                self.assertIsNotNone(SCREAMING_SNAKE_CASE__				)


    def      _UpperCamelCase       ( self							:					Optional[int]							,					SCREAMING_SNAKE_CASE__							:					List[Any]=True				):
          super().test_pt_tf_model_equivalence(allow_missing_keys=SCREAMING_SNAKE_CASE__				)



