



import json
import os
import tempfile

import datasets
from utils import generate_example_dataset, get_duration


lowerCAmelCase__		:					str							=50000
lowerCAmelCase__		:					List[Any]							=5000

lowerCAmelCase__		:					Optional[Any]							=os.path.split(__file__)
lowerCAmelCase__		:					List[str]							=os.path.join(RESULTS_BASEPATH, '''results''', RESULTS_FILENAME.replace('''.py''', '''.json'''))






@get_duration
def      __lowercase (			a__							,       a__       )    ->			str:


							for i in range(lowerCAmelCase__       ):
														__SCREAMING_SNAKE_CASE									= dataset[i]






@get_duration
def      __lowercase (			a__							,       a__							,       a__       )    ->			List[str]:


							for i in range(0							,       len(lowerCAmelCase__       )							,       lowerCAmelCase__       ):
														__SCREAMING_SNAKE_CASE									= dataset[i : i + batch_size]






@get_duration
def      __lowercase (			a__							,       a__							,       a__       )    ->			Dict:


							with dataset.formatted_as(type=lowerCAmelCase__       ):
														for i in range(lowerCAmelCase__       ):
																					__SCREAMING_SNAKE_CASE									= dataset[i]






@get_duration
def      __lowercase (			a__							,       a__							,       a__							,       a__       )    ->			int:


							with dataset.formatted_as(type=lowerCAmelCase__       ):
														for i in range(0							,       lowerCAmelCase__							,       lowerCAmelCase__       ):
																					__SCREAMING_SNAKE_CASE									= dataset[i : i + batch_size]






def      __lowercase (			)    ->			Dict:
							__SCREAMING_SNAKE_CASE									= {'num examples': SPEED_TEST_N_EXAMPLES}
							__SCREAMING_SNAKE_CASE									= [
							    (read, {'length': SMALL_TEST}),
							    (read, {'length': SPEED_TEST_N_EXAMPLES}),
							    (read_batch, {'length': SPEED_TEST_N_EXAMPLES, 'batch_size': 10}),
							    (read_batch, {'length': SPEED_TEST_N_EXAMPLES, 'batch_size': 1_00}),
							    (read_batch, {'length': SPEED_TEST_N_EXAMPLES, 'batch_size': 10_00}),
							    (read_formatted, {'type': 'numpy', 'length': SMALL_TEST}),
							    (read_formatted, {'type': 'pandas', 'length': SMALL_TEST}),
							    (read_formatted, {'type': 'torch', 'length': SMALL_TEST}),
							    (read_formatted, {'type': 'tensorflow', 'length': SMALL_TEST}),
							    (read_formatted_batch, {'type': 'numpy', 'length': SMALL_TEST, 'batch_size': 10}),
							    (read_formatted_batch, {'type': 'numpy', 'length': SMALL_TEST, 'batch_size': 10_00}),
							]

							__SCREAMING_SNAKE_CASE									= [
							    (read, {'length': SMALL_TEST}),
							    (read, {'length': SPEED_TEST_N_EXAMPLES}),
							    (read_batch, {'length': SPEED_TEST_N_EXAMPLES, 'batch_size': 10}),
							    (read_batch, {'length': SPEED_TEST_N_EXAMPLES, 'batch_size': 1_00}),
							    (read_batch, {'length': SPEED_TEST_N_EXAMPLES, 'batch_size': 10_00}),
							    (read_formatted, {'type': 'numpy', 'length': SMALL_TEST}),
							    (read_formatted_batch, {'type': 'numpy', 'length': SMALL_TEST, 'batch_size': 10}),
							    (read_formatted_batch, {'type': 'numpy', 'length': SMALL_TEST, 'batch_size': 10_00}),
							]
							with tempfile.TemporaryDirectory() as tmp_dir:
														print('generating dataset'       )
														__SCREAMING_SNAKE_CASE									= datasets.Features(
														    {'list': datasets.Sequence(datasets.Value('float32'       )       ), 'numbers': datasets.Value('float32'       )}       )
														__SCREAMING_SNAKE_CASE									= generate_example_dataset(
														    os.path.join(lowerCAmelCase__							,       'dataset.arrow'       )							,       lowerCAmelCase__							,       num_examples=lowerCAmelCase__							,       seq_shapes={'list': (1_00,)}							,       )
														print('first set of iterations'       )
														for func, kwargs in functions:
																					print(func.__name__							,       str(lowerCAmelCase__       )       )
																					__SCREAMING_SNAKE_CASE									= func(lowerCAmelCase__							,       **lowerCAmelCase__       )

														print('shuffling dataset'       )
														__SCREAMING_SNAKE_CASE									= dataset.shuffle()
														print('Second set of iterations (after shuffling'       )
														for func, kwargs in functions_shuffled:
																					print('shuffled '							,       func.__name__							,       str(lowerCAmelCase__       )       )
																					__SCREAMING_SNAKE_CASE									= func(
																					    lowerCAmelCase__							,       **lowerCAmelCase__       )

							with open(lowerCAmelCase__							,       'wb'       ) as f:
														f.write(json.dumps(lowerCAmelCase__       ).encode('utf-8'       )       )


if __name__ == "__main__":  # useful to run the profiler
			benchmark_iterating()











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

						'''simple docstring'''




						def __init__(   self				,							_A				,							_A=12				,							_A=7				,							_A=True				,							_A=True				,							_A=True				,							_A=99				,							_A=32				,							_A=32				,							_A=2				,							_A=4				,							_A=37				,							_A=0.1				,							_A=0.1				,							_A=512				,							_A=0.0_2				,							_A=0				,							_A=None				,							):







													'''simple docstring'''

													__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 	_A     (   self		):







													'''simple docstring'''

													__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(_A		):
																											__SCREAMING_SNAKE_CASE									= 1
																											__SCREAMING_SNAKE_CASE									= 0

													__SCREAMING_SNAKE_CASE									= self.get_config()

													return config, input_ids, tf.convert_to_tensor(_A		)






						def 	_A     (   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 	_A     (   self				,							_A				,							_A				,							_A		):







													'''simple docstring'''

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







													'''simple docstring'''

													__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        UpperCAmelCase_  (       UpperCamelCase_   ,   unittest.TestCase	):

						'''simple docstring'''




						UpperCamelCase__      :			str      =  (TFBlipTextModel,) if is_tf_available() else ()
						UpperCamelCase__      :			int      =  False
						UpperCamelCase__      :			Optional[Any]      =  False
						UpperCamelCase__      :			Tuple      =  False






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= BlipTextModelTester(self		)
													__SCREAMING_SNAKE_CASE									= ConfigTester(self				,							config_class=_A				,							hidden_size=37		)






						def 	_A     (   self		):







													'''simple docstring'''

													self.config_tester.run_common_tests()






						def 	_A     (   self		):







													'''simple docstring'''

													__SCREAMING_SNAKE_CASE									= self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*_A		)






						def 	_A     (   self		):







													'''simple docstring'''

													pass






						def 	_A     (   self		):







													'''simple docstring'''

													pass






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







													'''simple docstring'''

													pass






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







													'''simple docstring'''

													pass






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







													'''simple docstring'''

													pass






						@slow
						def 	_A     (   self		):







													'''simple docstring'''

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






						def 	_A     (   self				,							_A=True		):







													'''simple docstring'''

													super().test_pt_tf_model_equivalence(allow_missing_keys=_A		)






