


"""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 			lowerCamelCase_		:





				"""simple docstring"""







				def __init__(						self       ,		UpperCAmelCase__       ,		UpperCAmelCase__=12       ,		UpperCAmelCase__=7       ,		UpperCAmelCase__=True       ,		UpperCAmelCase__=True       ,		UpperCAmelCase__=True       ,		UpperCAmelCase__=99       ,		UpperCAmelCase__=32       ,		UpperCAmelCase__=32       ,		UpperCAmelCase__=2       ,		UpperCAmelCase__=4       ,		UpperCAmelCase__=37       ,		UpperCAmelCase__=0.1       ,		UpperCAmelCase__=0.1       ,		UpperCAmelCase__=512       ,		UpperCAmelCase__=0.02       ,		UpperCAmelCase__=0       ,		UpperCAmelCase__=None       ,		):
								SCREAMING_SNAKE_CASE__     							=				parent
								SCREAMING_SNAKE_CASE__     							=				batch_size
								SCREAMING_SNAKE_CASE__     							=				seq_length
								SCREAMING_SNAKE_CASE__     							=				is_training
								SCREAMING_SNAKE_CASE__     							=				use_input_mask
								SCREAMING_SNAKE_CASE__     							=				use_labels
								SCREAMING_SNAKE_CASE__     							=				vocab_size
								SCREAMING_SNAKE_CASE__     							=				hidden_size
								SCREAMING_SNAKE_CASE__     							=				projection_dim
								SCREAMING_SNAKE_CASE__     							=				num_hidden_layers
								SCREAMING_SNAKE_CASE__     							=				num_attention_heads
								SCREAMING_SNAKE_CASE__     							=				intermediate_size
								SCREAMING_SNAKE_CASE__     							=				dropout
								SCREAMING_SNAKE_CASE__     							=				attention_dropout
								SCREAMING_SNAKE_CASE__     							=				max_position_embeddings
								SCREAMING_SNAKE_CASE__     							=				initializer_range
								SCREAMING_SNAKE_CASE__     							=				scope
								SCREAMING_SNAKE_CASE__     							=				bos_token_id



				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				ids_tensor([self.batch_size, self.seq_length]       ,		self.vocab_size     )

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

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

								SCREAMING_SNAKE_CASE__     							=				self.get_config()

								return config, input_ids, tf.convert_to_tensor(UpperCAmelCase__     )



				def   lowerCAmelCase__		(						self     ):
								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       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__     ):
								SCREAMING_SNAKE_CASE__     							=				TFBlipTextModel(config=UpperCAmelCase__     )
								SCREAMING_SNAKE_CASE__     							=				model(UpperCAmelCase__       ,		attention_mask=UpperCAmelCase__       ,		training=UpperCAmelCase__     )
								SCREAMING_SNAKE_CASE__     							=				model(UpperCAmelCase__       ,		training=UpperCAmelCase__     )
								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     ):
								SCREAMING_SNAKE_CASE__     							=				self.prepare_config_and_inputs()
								SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__							,      SCREAMING_SNAKE_CASE__     							=				config_and_inputs
								SCREAMING_SNAKE_CASE__     							=				{"input_ids": input_ids, "attention_mask": input_mask}
								return config, inputs_dict


@require_tf
class 			lowerCamelCase_		(			lowercase       ,  unittest.TestCase   ):





				"""simple docstring"""







				_lowerCAmelCase      :      Tuple      							=					(TFBlipTextModel,) if is_tf_available() else ()
				_lowerCAmelCase      :      str      							=					False
				_lowerCAmelCase      :      List[Any]      							=					False
				_lowerCAmelCase      :      List[Any]      							=					False



				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				BlipTextModelTester(self     )
								SCREAMING_SNAKE_CASE__     							=				ConfigTester(self       ,		config_class=UpperCAmelCase__       ,		hidden_size=37     )



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



				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*UpperCAmelCase__     )



				def   lowerCAmelCase__		(						self     ):
								pass



				def   lowerCAmelCase__		(						self     ):
								pass



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



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



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



				@slow
				def   lowerCAmelCase__		(						self     ):
								for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												SCREAMING_SNAKE_CASE__     							=				TFBlipTextModel.from_pretrained(UpperCAmelCase__     )
												self.assertIsNotNone(UpperCAmelCase__     )






				def   lowerCAmelCase__		(						self       ,		UpperCAmelCase__=True     ):
								super().test_pt_tf_model_equivalence(allow_missing_keys=UpperCAmelCase__     )




"""simple docstring"""






import ast
import os
import re
import shutil
import tempfile
import unittest
from unittest import mock

import torch

from accelerate.test_utils.examples import compare_against_test
from accelerate.test_utils.testing import TempDirTestCase, require_trackers, run_command, slow
from accelerate.utils import write_basic_config


# DataLoaders built from `test_samples/MRPC` for quick testing
# Should mock `{script_name}.get_dataloaders` via:
# @mock.patch("{script_name}.get_dataloaders", mocked_dataloaders)

_lowerCamelCase     =  [
    'cross_validation.py',
    'gradient_accumulation.py',
    'local_sgd.py',
    'multi_process_metrics.py',
    'memory.py',
    'automatic_gradient_accumulation.py',
    'fsdp_with_peak_mem_tracking.py',
    'deepspeed_with_config_support.py',
    'megatron_lm_gpt_pretraining.py',
]


class 			lowerCamelCase_		(			unittest.TestCase   ):





				"""simple docstring"""







				def   lowerCAmelCase__		(						self       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__ = None       ,		UpperCAmelCase__ = None     ):
								SCREAMING_SNAKE_CASE__     							=				None
								SCREAMING_SNAKE_CASE__     							=				os.path.abspath(os.path.join("examples"       ,		"by_feature"     )     )
								SCREAMING_SNAKE_CASE__     							=				os.path.abspath("examples"     )
								for item in os.listdir(UpperCAmelCase__     ):
												if item not in EXCLUDE_EXAMPLES:
																SCREAMING_SNAKE_CASE__     							=				os.path.join(UpperCAmelCase__       ,		UpperCAmelCase__     )
																if os.path.isfile(UpperCAmelCase__     ) and ".py" in item_path:
																				with self.subTest(
																				    tested_script=UpperCAmelCase__       ,		feature_script=UpperCAmelCase__       ,		tested_section="main()" if parser_only else "training_function()"       ,		):
																								SCREAMING_SNAKE_CASE__     							=				compare_against_test(
																								    os.path.join(UpperCAmelCase__       ,		UpperCAmelCase__     )       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__     )
																								SCREAMING_SNAKE_CASE__     							=				"\n".join(UpperCAmelCase__     )
																								if special_strings is not None:
																												for string in special_strings:
																																SCREAMING_SNAKE_CASE__     							=				diff.replace(UpperCAmelCase__       ,		""     )
																								self.assertEqual(UpperCAmelCase__       ,		""     )



				def   lowerCAmelCase__		(						self     ):
								self.one_complete_example("complete_nlp_example.py"       ,		UpperCAmelCase__     )
								self.one_complete_example("complete_nlp_example.py"       ,		UpperCAmelCase__     )






				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				os.path.abspath(os.path.join("examples"       ,		"cv_example.py"     )     )
								SCREAMING_SNAKE_CASE__     							=				[
								    " " * 16 + "{\n\n",
								    " " * 20 + "\"accuracy\": eval_metric[\"accuracy\"],\n\n",
								    " " * 20 + "\"f1\": eval_metric[\"f1\"],\n\n",
								    " " * 20 + "\"train_loss\": total_loss.item() / len(train_dataloader),\n\n",
								    " " * 20 + "\"epoch\": epoch,\n\n",
								    " " * 16 + "},\n\n",
								    " " * 16 + "step=epoch,\n",
								    " " * 12,
								    " " * 8 + "for step, batch in enumerate(active_dataloader):\n",
								]
								self.one_complete_example("complete_cv_example.py"       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__     )
								self.one_complete_example("complete_cv_example.py"       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__     )


@mock.patch.dict(os.environ       ,  {"TESTING_MOCKED_DATALOADERS": "1"}   )
class 			lowerCamelCase_		(			lowercase   ):





				"""simple docstring"""







				_lowerCAmelCase      :      Dict      							=					False



				@classmethod
				def   lowerCAmelCase__		(						cls     ):
								super().setUpClass()
								SCREAMING_SNAKE_CASE__     							=				tempfile.mkdtemp()
								SCREAMING_SNAKE_CASE__     							=				os.path.join(cls._tmpdir       ,		"default_config.yml"     )

								write_basic_config(save_location=cls.configPath     )
								SCREAMING_SNAKE_CASE__     							=				["accelerate", "launch", "--config_file", cls.configPath]



				@classmethod
				def   lowerCAmelCase__		(						cls     ):
								super().tearDownClass()
								shutil.rmtree(cls._tmpdir     )



				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				f'''
        examples/by_feature/checkpointing.py
        --checkpointing_steps epoch
        --output_dir {self.tmpdir}
        '''.split()
								run_command(self._launch_args + testargs     )
								self.assertTrue(os.path.exists(os.path.join(self.tmpdir       ,		"epoch_0"     )     )     )



				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				f'''
        examples/by_feature/checkpointing.py
        --checkpointing_steps 1
        --output_dir {self.tmpdir}
        '''.split()
								SCREAMING_SNAKE_CASE__     							=				run_command(self._launch_args + testargs     )
								self.assertTrue(os.path.exists(os.path.join(self.tmpdir       ,		"step_2"     )     )     )



				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				f'''
        examples/by_feature/checkpointing.py
        --resume_from_checkpoint {os.path.join(self.tmpdir       ,		"epoch_0"     )}
        '''.split()
								SCREAMING_SNAKE_CASE__     							=				run_command(self._launch_args + testargs       ,		return_stdout=UpperCAmelCase__     )
								self.assertNotIn("epoch 0:"       ,		UpperCAmelCase__     )
								self.assertIn("epoch 1:"       ,		UpperCAmelCase__     )



				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				f'''
        examples/by_feature/checkpointing.py
        --resume_from_checkpoint {os.path.join(self.tmpdir       ,		"step_2"     )}
        '''.split()
								SCREAMING_SNAKE_CASE__     							=				run_command(self._launch_args + testargs       ,		return_stdout=UpperCAmelCase__     )
								if torch.cuda.is_available():
												SCREAMING_SNAKE_CASE__     							=				torch.cuda.device_count()
								else:
												SCREAMING_SNAKE_CASE__     							=				1
								if num_processes > 1:
												self.assertNotIn("epoch 0:"       ,		UpperCAmelCase__     )
												self.assertIn("epoch 1:"       ,		UpperCAmelCase__     )
								else:
												self.assertIn("epoch 0:"       ,		UpperCAmelCase__     )
												self.assertIn("epoch 1:"       ,		UpperCAmelCase__     )



				@slow
				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				"\n        examples/by_feature/cross_validation.py\n        --num_folds 2\n        ".split()
								with mock.patch.dict(os.environ       ,		{"TESTING_MOCKED_DATALOADERS": "0"}     ):
												SCREAMING_SNAKE_CASE__     							=				run_command(self._launch_args + testargs       ,		return_stdout=UpperCAmelCase__     )
												SCREAMING_SNAKE_CASE__     							=				re.findall("({.+})"       ,		UpperCAmelCase__     )
												SCREAMING_SNAKE_CASE__     							=				[r for r in results if "accuracy" in r][-1]
												SCREAMING_SNAKE_CASE__     							=				ast.literal_eval(UpperCAmelCase__     )
												self.assertGreaterEqual(results["accuracy"]       ,		0.75     )



				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				["examples/by_feature/multi_process_metrics.py"]
								run_command(self._launch_args + testargs     )



				@require_trackers
				@mock.patch.dict(os.environ       ,		{"WANDB_MODE": "offline"}     )
				def   lowerCAmelCase__		(						self     ):
								with tempfile.TemporaryDirectory() as tmpdir:
												SCREAMING_SNAKE_CASE__     							=				f'''
            examples/by_feature/tracking.py
            --with_tracking
            --project_dir {tmpdir}
            '''.split()
												run_command(self._launch_args + testargs     )
												self.assertTrue(os.path.exists(os.path.join(UpperCAmelCase__       ,		"tracking"     )     )     )



				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				["examples/by_feature/gradient_accumulation.py"]
								run_command(self._launch_args + testargs     )






				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				["examples/by_feature/local_sgd.py"]
								run_command(self._launch_args + testargs     )
