


import torch

from accelerate import Accelerator, DistributedType







class 					lowerCAmelCase__	:




     '''simple docstring'''


     def __enter__(						self)      ->    Dict:
            if self.enabled:
                   _A				:	Tuple       =			self.model.no_sync()
                   self.model_sync_obj.__enter__()

            return self


     def __exit__(						self			,						__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase)      ->    Union[str, Any]:
            if self.enabled:
                   # Average all models on exit
                   self._sync_and_avg_model_params()
                   self.model_sync_obj.__exit__(__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase)


     def __init__(						self			,						__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase = True)      ->    Union[str, Any]:
            if accelerator.distributed_type not in [
                DistributedType.NO,
                DistributedType.MULTI_CPU,
                DistributedType.MULTI_GPU,
            ]:
                   raise NotImplementedError("LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)")
            _A				:	Union[str, Any]       =			enabled and accelerator.distributed_type != DistributedType.NO
            _A				:	List[str]       =			0
            if self.enabled:
                   _A				:	Dict       =			accelerator
                   _A				:	List[Any]       =			model
                   _A				:	Optional[int]       =			local_sgd_steps


     def 			_lowerCamelCase			(						self)      ->    Tuple:
            self.num_steps += 1
            if not self.enabled:
                   return

            if self.num_steps % self.local_sgd_steps == 0:
                   self._sync_and_avg_model_params()


     def 			_lowerCamelCase			(						self)      ->    Optional[int]:
            self.accelerator.wait_for_everyone()
            with self.accelerator.autocast():
                   for param in self.model.parameters():
                          _A				:	int       =			self.accelerator.reduce(param.data			,						reduction="mean")




import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import is_torch_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask


if is_torch_available():
    import torch

    from transformers import BlipTextModel
    from transformers.models.blip.modeling_blip import BLIP_PRETRAINED_MODEL_ARCHIVE_LIST







class 					lowerCAmelCase__	:




     '''simple docstring'''


     def __init__(						self			,						__lowerCamelCase			,						__lowerCamelCase=1_2			,						__lowerCamelCase=7			,						__lowerCamelCase=True			,						__lowerCamelCase=True			,						__lowerCamelCase=True			,						__lowerCamelCase=9_9			,						__lowerCamelCase=3_2			,						__lowerCamelCase=3_2			,						__lowerCamelCase=5			,						__lowerCamelCase=4			,						__lowerCamelCase=3_7			,						__lowerCamelCase=0.1			,						__lowerCamelCase=0.1			,						__lowerCamelCase=5_1_2			,						__lowerCamelCase=0.0_2			,						__lowerCamelCase=0			,						__lowerCamelCase=None			,						)      ->    str:
            _A				:	Optional[int]       =			parent
            _A				:	Optional[int]       =			batch_size
            _A				:	Optional[int]       =			seq_length
            _A				:	Dict       =			is_training
            _A				:	Union[str, Any]       =			use_input_mask
            _A				:	Optional[int]       =			use_labels
            _A				:	Any       =			vocab_size
            _A				:	Dict       =			hidden_size
            _A				:	Optional[Any]       =			projection_dim
            _A				:	Union[str, Any]       =			num_hidden_layers
            _A				:	Tuple       =			num_attention_heads
            _A				:	List[str]       =			intermediate_size
            _A				:	Any       =			dropout
            _A				:	List[str]       =			attention_dropout
            _A				:	str       =			max_position_embeddings
            _A				:	List[Any]       =			initializer_range
            _A				:	int       =			scope
            _A				:	List[str]       =			bos_token_id


     def 			_lowerCamelCase			(						self)      ->    List[str]:
            _A				:	Tuple       =			ids_tensor([self.batch_size, self.seq_length]			,						self.vocab_size)

            _A				:	str       =			None
            if self.use_input_mask:
                   _A				:	Union[str, Any]       =			random_attention_mask([self.batch_size, self.seq_length])

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

            _A				:	str       =			self.get_config()

            return config, input_ids, input_mask


     def 			_lowerCamelCase			(						self)      ->    Optional[int]:
            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 			_lowerCamelCase			(						self			,						__lowerCamelCase			,						__lowerCamelCase			,						__lowerCamelCase)      ->    List[str]:
            _A				:	int       =			BlipTextModel(config=__lowerCamelCase)
            model.to(__lowerCamelCase)
            model.eval()
            with torch.no_grad():
                   _A				:	int       =			model(__lowerCamelCase			,						attention_mask=__lowerCamelCase)
                   _A				:	List[str]       =			model(__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 			_lowerCamelCase			(						self)      ->    Any:
            _A				:	Dict       =			self.prepare_config_and_inputs()
            _A   ,      _A   ,      _A				:	str       =			config_and_inputs
            _A				:	List[str]       =			{"input_ids": input_ids, "attention_mask": input_mask}
            return config, inputs_dict







@require_torch
class 					lowerCAmelCase__	(    a					,   unittest.TestCase):




     '''simple docstring'''
     __SCREAMING_SNAKE_CASE        =			(BlipTextModel,) if is_torch_available() else ()
     __SCREAMING_SNAKE_CASE        =			False
     __SCREAMING_SNAKE_CASE        =			False
     __SCREAMING_SNAKE_CASE        =			False


     def 			_lowerCamelCase			(						self)      ->    Dict:
            _A				:	Optional[Any]       =			BlipTextModelTester(self)
            _A				:	List[str]       =			ConfigTester(self			,						config_class=__lowerCamelCase			,						hidden_size=3_7)


     def 			_lowerCamelCase			(						self)      ->    List[Any]:
            self.config_tester.run_common_tests()


     def 			_lowerCamelCase			(						self)      ->    Union[str, Any]:
            _A				:	Optional[Any]       =			self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*__lowerCamelCase)


     def 			_lowerCamelCase			(						self)      ->    Optional[Any]:
            pass


     def 			_lowerCamelCase			(						self)      ->    Dict:
            pass


     @unittest.skip(reason="Blip does not use inputs_embeds")
     def 			_lowerCamelCase			(						self)      ->    str:
            pass


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


     @unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING")
     def 			_lowerCamelCase			(						self)      ->    str:
            pass


     @slow
     def 			_lowerCamelCase			(						self)      ->    Optional[Any]:
            for model_name in BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   _A				:	Union[str, Any]       =			BlipTextModel.from_pretrained(__lowerCamelCase)
                   self.assertIsNotNone(__lowerCamelCase)


     def 			_lowerCamelCase			(						self)      ->    Optional[int]:
            super().test_pt_tf_model_equivalence(allow_missing_keys=__lowerCamelCase)
