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   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_=12   ,				SCREAMING_SNAKE_CASE_=7   ,				SCREAMING_SNAKE_CASE_=True   ,				SCREAMING_SNAKE_CASE_=True   ,				SCREAMING_SNAKE_CASE_=True   ,				SCREAMING_SNAKE_CASE_=99   ,				SCREAMING_SNAKE_CASE_=32   ,				SCREAMING_SNAKE_CASE_=32   ,				SCREAMING_SNAKE_CASE_=2   ,				SCREAMING_SNAKE_CASE_=4   ,				SCREAMING_SNAKE_CASE_=37   ,				SCREAMING_SNAKE_CASE_=0.1   ,				SCREAMING_SNAKE_CASE_=0.1   ,				SCREAMING_SNAKE_CASE_=512   ,				SCREAMING_SNAKE_CASE_=0.02   ,				SCREAMING_SNAKE_CASE_=0   ,				SCREAMING_SNAKE_CASE_=None   ,				)		->      List[Any]:





       '''simple docstring'''





       UpperCamelCase__										=      parent
       UpperCamelCase__										=      batch_size
       UpperCamelCase__										=      seq_length
       UpperCamelCase__										=      is_training
       UpperCamelCase__										=      use_input_mask
       UpperCamelCase__										=      use_labels
       UpperCamelCase__										=      vocab_size
       UpperCamelCase__										=      hidden_size
       UpperCamelCase__										=      projection_dim
       UpperCamelCase__										=      num_hidden_layers
       UpperCamelCase__										=      num_attention_heads
       UpperCamelCase__										=      intermediate_size
       UpperCamelCase__										=      dropout
       UpperCamelCase__										=      attention_dropout
       UpperCamelCase__										=      max_position_embeddings
       UpperCamelCase__										=      initializer_range
       UpperCamelCase__										=      scope
       UpperCamelCase__										=      bos_token_id






  def 		_a					(self	)		->      int:





       '''simple docstring'''





       UpperCamelCase__										=      ids_tensor([self.batch_size, self.seq_length]   ,				self.vocab_size	)

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

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

       UpperCamelCase__										=      self.get_config()

       return config, input_ids, tf.convert_to_tensor(SCREAMING_SNAKE_CASE_	)






  def 		_a					(self	)		->      Optional[int]:





       '''simple docstring'''





       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 		_a					(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_	)		->      Optional[int]:





       '''simple docstring'''





       UpperCamelCase__										=      TFBlipTextModel(config=SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      model(SCREAMING_SNAKE_CASE_   ,				attention_mask=SCREAMING_SNAKE_CASE_   ,				training=SCREAMING_SNAKE_CASE_	)
       UpperCamelCase__										=      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 		_a					(self	)		->      Tuple:





       '''simple docstring'''





       UpperCamelCase__										=      self.prepare_config_and_inputs()
       UpperCamelCase__						,       UpperCamelCase__						,       UpperCamelCase__										=      config_and_inputs
       UpperCamelCase__										=      {'''input_ids''': input_ids, '''attention_mask''': input_mask}
       return config, inputs_dict



@require_tf
class     _A   (    __UpperCamelCase	, unittest.TestCase							):
  SCREAMING_SNAKE_CASE_	:	Optional[int]						=(TFBlipTextModel,) if is_tf_available() else ()
  SCREAMING_SNAKE_CASE_	:	str						=False
  SCREAMING_SNAKE_CASE_	:	Optional[Any]						=False
  SCREAMING_SNAKE_CASE_	:	List[Any]						=False






  def 		_a					(self	)		->      Any:





       '''simple docstring'''





       UpperCamelCase__										=      BlipTextModelTester(self	)
       UpperCamelCase__										=      ConfigTester(self   ,				config_class=SCREAMING_SNAKE_CASE_   ,				hidden_size=37	)






  def 		_a					(self	)		->      Optional[int]:





       '''simple docstring'''





       self.config_tester.run_common_tests()






  def 		_a					(self	)		->      Tuple:





       '''simple docstring'''





       UpperCamelCase__										=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_	)






  def 		_a					(self	)		->      Optional[int]:





       '''simple docstring'''





       pass






  def 		_a					(self	)		->      str:





       '''simple docstring'''





       pass






  @unittest.skip(reason='''Blip does not use inputs_embeds'''	)
  def 		_a					(self	)		->      str:





       '''simple docstring'''





       pass






  @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING'''	)
  def 		_a					(self	)		->      Tuple:





       '''simple docstring'''





       pass






  @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING'''	)
  def 		_a					(self	)		->      Any:





       '''simple docstring'''





       pass






  @slow
  def 		_a					(self	)		->      Dict:





       '''simple docstring'''





       for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            UpperCamelCase__										=      TFBlipTextModel.from_pretrained(SCREAMING_SNAKE_CASE_	)
            self.assertIsNotNone(SCREAMING_SNAKE_CASE_	)






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_=True	)		->      str:





       '''simple docstring'''





       super().test_pt_tf_model_equivalence(allow_missing_keys=SCREAMING_SNAKE_CASE_	)




import os
from typing import Optional

import fsspec
from fsspec.archive import AbstractArchiveFileSystem
from fsspec.utils import DEFAULT_BLOCK_SIZE



class     _A   (    __UpperCamelCase							):
  SCREAMING_SNAKE_CASE_	:	List[str]						=""
  SCREAMING_SNAKE_CASE_	:	str						=(
      None  # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz
  )
  SCREAMING_SNAKE_CASE_	:	str						=None  # compression type in fsspec. ex: "gzip"
  SCREAMING_SNAKE_CASE_	:	str						=None  # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz






  def __init__(self   ,				SCREAMING_SNAKE_CASE_ = ""   ,				SCREAMING_SNAKE_CASE_ = None   ,				SCREAMING_SNAKE_CASE_ = None   ,				**SCREAMING_SNAKE_CASE_	)		->      List[str]:





       '''simple docstring'''





       super().__init__(self   ,				**SCREAMING_SNAKE_CASE_	)
       # always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode
       UpperCamelCase__										=      fsspec.open(
           SCREAMING_SNAKE_CASE_   ,				mode='''rb'''   ,				protocol=SCREAMING_SNAKE_CASE_   ,				compression=self.compression   ,				client_kwargs={
               '''requote_redirect_url''': False,  # see https://github.com/huggingface/datasets/pull/5459
               '''trust_env''': True,  # Enable reading proxy env variables.
               **(target_options or {}).pop('''client_kwargs'''   ,				{}	),  # To avoid issues if it was already passed.
           }   ,				**(target_options or {})   ,				)
       UpperCamelCase__										=      os.path.basename(self.file.path.split('''::'''	)[0]	)
       UpperCamelCase__										=      (
           self.compressed_name[: self.compressed_name.rindex('''.'''	)]
           if '''.''' in self.compressed_name
           else self.compressed_name
       )
       UpperCamelCase__										=      None






  @classmethod
  def 		_a					(cls   ,				SCREAMING_SNAKE_CASE_	)		->      Optional[Any]:





       '''simple docstring'''





       return super()._strip_protocol(SCREAMING_SNAKE_CASE_	).lstrip('''/'''	)






  def 		_a					(self	)		->      Union[str, Any]:





       '''simple docstring'''





       if self.dir_cache is None:
            UpperCamelCase__										=      {**self.file.fs.info(self.file.path	), '''name''': self.uncompressed_name}
            UpperCamelCase__										=      {f['''name''']: f}






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_	)		->      List[Any]:





       '''simple docstring'''





       return self.file.open().read()






  def 		_a					(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_ = "rb"   ,				SCREAMING_SNAKE_CASE_=None   ,				SCREAMING_SNAKE_CASE_=True   ,				SCREAMING_SNAKE_CASE_=None   ,				**SCREAMING_SNAKE_CASE_   ,				)		->      List[Any]:





       '''simple docstring'''





       UpperCamelCase__										=      self._strip_protocol(SCREAMING_SNAKE_CASE_	)
       if mode != "rb":
            raise ValueError(F"Tried to read with mode {mode} on file {self.file.path} opened with mode 'rb'"	)
       return self.file.open()



class     _A   (    __UpperCamelCase							):
  SCREAMING_SNAKE_CASE_	:	Tuple						="bz2"
  SCREAMING_SNAKE_CASE_	:	List[str]						="bz2"
  SCREAMING_SNAKE_CASE_	:	List[Any]						=".bz2"



class     _A   (    __UpperCamelCase							):
  SCREAMING_SNAKE_CASE_	:	Tuple						="gzip"
  SCREAMING_SNAKE_CASE_	:	Dict						="gzip"
  SCREAMING_SNAKE_CASE_	:	List[Any]						=".gz"



class     _A   (    __UpperCamelCase							):
  SCREAMING_SNAKE_CASE_	:	List[Any]						="lz4"
  SCREAMING_SNAKE_CASE_	:	str						="lz4"
  SCREAMING_SNAKE_CASE_	:	Any						=".lz4"



class     _A   (    __UpperCamelCase							):
  SCREAMING_SNAKE_CASE_	:	Dict						="xz"
  SCREAMING_SNAKE_CASE_	:	List[str]						="xz"
  SCREAMING_SNAKE_CASE_	:	Optional[int]						=".xz"



class     _A   (    __UpperCamelCase							):
  SCREAMING_SNAKE_CASE_	:	Optional[Any]						="zstd"
  SCREAMING_SNAKE_CASE_	:	Optional[int]						="zstd"
  SCREAMING_SNAKE_CASE_	:	Optional[int]						=".zst"






  def __init__(self   ,				SCREAMING_SNAKE_CASE_   ,				SCREAMING_SNAKE_CASE_ = "rb"   ,				SCREAMING_SNAKE_CASE_ = None   ,				SCREAMING_SNAKE_CASE_ = None   ,				SCREAMING_SNAKE_CASE_ = DEFAULT_BLOCK_SIZE   ,				**SCREAMING_SNAKE_CASE_   ,				)		->      List[Any]:





       '''simple docstring'''





       super().__init__(
           fo=SCREAMING_SNAKE_CASE_   ,				mode=SCREAMING_SNAKE_CASE_   ,				target_protocol=SCREAMING_SNAKE_CASE_   ,				target_options=SCREAMING_SNAKE_CASE_   ,				block_size=SCREAMING_SNAKE_CASE_   ,				**SCREAMING_SNAKE_CASE_   ,				)
       # We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2:
       #
       # File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open
       #     out.close = close
       # AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only
       #
       # see https://github.com/intake/filesystem_spec/issues/725
       UpperCamelCase__										=      self.file.__enter__



       class     _A   :






         def __init__(self   ,				SCREAMING_SNAKE_CASE_	)		->      int:





              '''simple docstring'''





              UpperCamelCase__										=      file_






         def __enter__(self	)		->      Union[str, Any]:





              '''simple docstring'''





              self._file.__enter__()
              return self






         def __exit__(self   ,				*SCREAMING_SNAKE_CASE_   ,				**SCREAMING_SNAKE_CASE_	)		->      List[Any]:





              '''simple docstring'''





              self._file.__exit__(*SCREAMING_SNAKE_CASE_   ,				**SCREAMING_SNAKE_CASE_	)






         def __iter__(self	)		->      Dict:





              '''simple docstring'''





              return iter(self._file	)






         def 		_a					(self	)		->      Dict:





              '''simple docstring'''





              return next(self._file	)






         def __getattr__(self   ,				SCREAMING_SNAKE_CASE_	)		->      str:





              '''simple docstring'''





              return getattr(self._file   ,				SCREAMING_SNAKE_CASE_	)

       def fixed_enter(*SCREAMING_SNAKE_CASE_   ,				**SCREAMING_SNAKE_CASE_	):
            return WrappedFile(_enter(*SCREAMING_SNAKE_CASE_   ,				**SCREAMING_SNAKE_CASE_	)	)

       UpperCamelCase__										=      fixed_enter



