





"""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 __magic_name__   :
       def __init__(     self   ,  __magic_name__   ,  __magic_name__=1_2   ,  __magic_name__=7   ,  __magic_name__=True   ,  __magic_name__=True   ,  __magic_name__=True   ,  __magic_name__=9_9   ,  __magic_name__=3_2   ,  __magic_name__=3_2   ,  __magic_name__=2   ,  __magic_name__=4   ,  __magic_name__=3_7   ,  __magic_name__=0.1   ,  __magic_name__=0.1   ,  __magic_name__=5_1_2   ,  __magic_name__=0.02   ,  __magic_name__=0   ,  __magic_name__=None   ,  ):





         """simple docstring"""


         _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 		_lowerCamelCase   (     self	):





         """simple docstring"""


         _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(__magic_name__	):
             _lowerCAmelCase	      =					1
             _lowerCAmelCase	      =					0

         _lowerCAmelCase	      =					self.get_config()

         return config, input_ids, tf.convert_to_tensor(__magic_name__	)
       def 		_lowerCamelCase   (     self	):





         """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 		_lowerCamelCase   (     self   ,  __magic_name__   ,  __magic_name__   ,  __magic_name__	):





         """simple docstring"""


         _lowerCAmelCase	      =					TFBlipTextModel(config=__magic_name__	)
         _lowerCAmelCase	      =					model(__magic_name__   ,  attention_mask=__magic_name__   ,  training=__magic_name__	)
         _lowerCAmelCase	      =					model(__magic_name__   ,  training=__magic_name__	)
         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	):





         """simple docstring"""


         _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 __magic_name__   (       A__    ,unittest.TestCase		):
       UpperCamelCase    :						Tuple      =	(TFBlipTextModel,) if is_tf_available() else ()
       UpperCamelCase    :						str      =	False
       UpperCamelCase    :						Optional[int]      =	False
       UpperCamelCase    :						Union[str, Any]      =	False
       def 		_lowerCamelCase   (     self	):





         """simple docstring"""


         _lowerCAmelCase	      =					BlipTextModelTester(self	)
         _lowerCAmelCase	      =					ConfigTester(self   ,  config_class=__magic_name__   ,  hidden_size=3_7	)
       def 		_lowerCamelCase   (     self	):





         """simple docstring"""


         self.config_tester.run_common_tests()
       def 		_lowerCamelCase   (     self	):





         """simple docstring"""


         _lowerCAmelCase	      =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__magic_name__	)
       def 		_lowerCamelCase   (     self	):





         """simple docstring"""


         pass
       def 		_lowerCamelCase   (     self	):





         """simple docstring"""


         pass
       @unittest.skip(reason='Blip does not use inputs_embeds'	)
       def 		_lowerCamelCase   (     self	):





         """simple docstring"""


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





         """simple docstring"""


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





         """simple docstring"""


         pass
       @slow
       def 		_lowerCamelCase   (     self	):





         """simple docstring"""


         for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
           _lowerCAmelCase	      =					TFBlipTextModel.from_pretrained(__magic_name__	)
           self.assertIsNotNone(__magic_name__	)
       def 		_lowerCamelCase   (     self   ,  __magic_name__=True	):





         """simple docstring"""


         super().test_pt_tf_model_equivalence(allow_missing_keys=__magic_name__	)











# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os

from accelerate.test_utils import execute_subprocess_async






def     a     (		lowerCamelCase_=None							):






				'''simple docstring'''
				if subparsers is not None:
								lowercase__  					=     subparsers.add_parser('''test'''							)
				else:
								lowercase__  					=     argparse.ArgumentParser('''Accelerate test command'''							)

				parser.add_argument(
				    '''--config_file'''						,  default=lowerCamelCase_						,  help=(
				        '''The path to use to store the config file. Will default to a file named default_config.yaml in the cache '''
				        '''location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have '''
				        '''such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed '''
				        '''with \'huggingface\'.'''
				    )						,  )

				if subparsers is not None:
								parser.set_defaults(func=lowerCamelCase_							)
				return parser






def     a     (		lowerCamelCase_							):






				'''simple docstring'''
				lowercase__  					=     os.path.sep.join(__file__.split(os.path.sep							)[:-2] + ['''test_utils''', '''scripts''', '''test_script.py''']							)

				if args.config_file is None:
								lowercase__  					=     script_name
				else:
								lowercase__  					=     F"""--config_file={args.config_file} {script_name}"""

				lowercase__  					=     ['''accelerate-launch'''] + test_args.split()
				lowercase__  					=     execute_subprocess_async(lowerCamelCase_						,  env=os.environ.copy()							)
				if result.returncode == 0:
								print('''Test is a success! You are ready for your distributed training!'''							)






def     a     (		):






				'''simple docstring'''
				lowercase__  					=     test_command_parser()
				lowercase__  					=     parser.parse_args()
				test_command(lowerCamelCase_							)


if __name__ == "__main__":
							main()




