




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    __lowerCAmelCase       :






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






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]				):
         lowercase    :List[str]      	=						ids_tensor([self.batch_size, self.seq_length]       ,					self.vocab_size				)

         lowercase    :List[Any]      	=						None
         if self.use_input_mask:
           lowercase    :Optional[int]      	=						random_attention_mask([self.batch_size, self.seq_length]				)

         if input_mask is not None:
           lowercase    :Optional[Any]      	=						input_mask.numpy()
           lowercase	,    lowercase    :Optional[Any]      	=						input_mask.shape
           lowercase    :Optional[int]      	=						np.random.randint(1       ,					seq_length - 1       ,					size=(batch_size,)				)
           for batch_idx, start_index in enumerate(_lowerCAmelCase				):
             lowercase    :str      	=						1
             lowercase    :List[str]      	=						0

         lowercase    :List[Any]      	=						self.get_config()

         return config, input_ids, tf.convert_to_tensor(_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[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 					SCREAMING_SNAKE_CASE    (   self:					List[str]       ,					_lowerCAmelCase:					List[str]       ,					_lowerCAmelCase:					List[Any]       ,					_lowerCAmelCase:					Optional[int]				):
         lowercase    :Dict      	=						TFBlipTextModel(config=_lowerCAmelCase				)
         lowercase    :str      	=						model(_lowerCAmelCase       ,					attention_mask=_lowerCAmelCase       ,					training=_lowerCAmelCase				)
         lowercase    :str      	=						model(_lowerCAmelCase       ,					training=_lowerCAmelCase				)
         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 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         lowercase    :Any      	=						self.prepare_config_and_inputs()
         lowercase	,    lowercase	,    lowercase    :int      	=						config_and_inputs
         lowercase    :str      	=						{"input_ids": input_ids, "attention_mask": input_mask}
         return config, inputs_dict







@require_tf
class    __lowerCAmelCase       (							lowerCAmelCase      , unittest.TestCase):
       _a 					=						(TFBlipTextModel,) if is_tf_available() else ()
       _a 					=						False
       _a 					=						False
       _a 					=						False






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[int]				):
         lowercase    :Tuple      	=						BlipTextModelTester(self				)
         lowercase    :str      	=						ConfigTester(self       ,					config_class=_lowerCAmelCase       ,					hidden_size=37				)






       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         self.config_tester.run_common_tests()






       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]				):
         lowercase    :List[Any]      	=						self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[int]				):
         pass






       def 					SCREAMING_SNAKE_CASE    (   self:					int				):
         pass






       @unittest.skip(reason="Blip does not use inputs_embeds"				)
       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]				):
         pass






       @unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"				)
       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[int]				):
         pass






       @unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"				)
       def 					SCREAMING_SNAKE_CASE    (   self:					Union[str, Any]				):
         pass






       @slow
       def 					SCREAMING_SNAKE_CASE    (   self:					int				):
         for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           lowercase    :Dict      	=						TFBlipTextModel.from_pretrained(_lowerCAmelCase				)
           self.assertIsNotNone(_lowerCAmelCase				)






       def 					SCREAMING_SNAKE_CASE    (   self:					Dict       ,					_lowerCAmelCase:					Optional[int]=True				):
         super().test_pt_tf_model_equivalence(allow_missing_keys=_lowerCAmelCase				)









import inspect
import os
import unittest

import torch

import accelerate
from accelerate import debug_launcher
from accelerate.test_utils import (
    execute_subprocess_async,
    require_cpu,
    require_huggingface_suite,
    require_multi_gpu,
    require_single_gpu,
)
from accelerate.utils import patch_environment




@require_huggingface_suite
class    __lowerCAmelCase       (							unittest.TestCase):






       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]				):
         lowercase    :List[str]      	=						inspect.getfile(accelerate.test_utils				)
         lowercase    :List[Any]      	=						os.path.sep.join(
             mod_file.split(os.path.sep				)[:-1] + ["scripts", "external_deps", "test_metrics.py"]				)

         from accelerate.test_utils.scripts.external_deps import test_metrics  # noqa: F401

         lowercase    :str      	=						test_metrics






       @require_cpu
       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple				):
         debug_launcher(self.test_metrics.main       ,					num_processes=1				)






       @require_cpu
       def 					SCREAMING_SNAKE_CASE    (   self:					int				):
         debug_launcher(self.test_metrics.main				)






       @require_single_gpu
       def 					SCREAMING_SNAKE_CASE    (   self:					List[Any]				):
         self.test_metrics.main()






       @require_multi_gpu
       def 					SCREAMING_SNAKE_CASE    (   self:					Optional[Any]				):
         print(F"Found {torch.cuda.device_count()} devices."				)
         lowercase    :str      	=						["torchrun", F"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path]
         with patch_environment(omp_num_threads=1				):
           execute_subprocess_async(_lowerCAmelCase       ,					env=os.environ.copy()				)



