

import json
import os
import tempfile

import datasets
from datasets.arrow_writer import ArrowWriter
from datasets.features import ArrayaD
from utils import generate_examples, get_duration


_UpperCamelCase			       =			(30, 487)
_UpperCamelCase			       =			(36, 1024)
_UpperCamelCase			       =			(100, 100)
_UpperCamelCase			       =			100

_UpperCamelCase			       =			datasets.Features(
    {'''text''': ArrayaD(SHAPE_TEST_1, dtype='''float32'''), '''image''': ArrayaD(SHAPE_TEST_2, dtype='''float32''')}
)

_UpperCamelCase			       =			os.path.split(__file__)
_UpperCamelCase			       =			os.path.join(RESULTS_BASEPATH, '''results''', RESULTS_FILENAME.replace('''.py''', '''.json'''))
@get_duration
def 						UpperCamelCase_(			snake_case__:  Optional[Any]  ,    snake_case__:  List[str]  ,    snake_case__:  Optional[int]   )       -> Union[str, Any]:
 with ArrowWriter(features=UpperCamelCase_  ,    path=os.path.join(UpperCamelCase_  ,    'beta.arrow'   )   ) as writer:
  for key, record in dummy_data:
   UpperCAmelCase__							=				my_features.encode_example(UpperCamelCase_   )
   writer.write(UpperCamelCase_   )
  UpperCAmelCase__							=				writer.finalize()
@get_duration
def 						UpperCamelCase_(			snake_case__:  List[Any]  ,    snake_case__:  List[Any]   )       -> Union[str, Any]:
 UpperCAmelCase__							=				datasets.Dataset.from_file(
     filename=os.path.join(UpperCamelCase_  ,    'beta.arrow'   )  ,    info=datasets.DatasetInfo(features=UpperCamelCase_   )   )
 for _ in dataset:
  pass
@get_duration
def 						UpperCamelCase_(			snake_case__:  int  ,    snake_case__:  List[Any]   )       -> str:
 UpperCAmelCase__							=				datasets.Dataset.from_file(
     filename=os.path.join(UpperCamelCase_  ,    'beta.arrow'   )  ,    info=datasets.DatasetInfo(features=UpperCamelCase_   )   )
 dataset.set_format('numpy'   )
 for _ in dataset:
  pass
@get_duration
def 						UpperCamelCase_(			snake_case__:  Union[str, Any]  ,    snake_case__:  List[str]   )       -> Any:
 UpperCAmelCase__							=				10
 UpperCAmelCase__							=				datasets.Dataset.from_file(
     filename=os.path.join(UpperCamelCase_  ,    'beta.arrow'   )  ,    info=datasets.DatasetInfo(features=UpperCamelCase_   )   )
 for i in range(0  ,    len(UpperCamelCase_   )  ,    UpperCamelCase_   ):
  UpperCAmelCase__							=				dataset[i : i + batch_size]
@get_duration
def 						UpperCamelCase_(			snake_case__:  Optional[Any]  ,    snake_case__:  int   )       -> Optional[int]:
 UpperCAmelCase__							=				10
 UpperCAmelCase__							=				datasets.Dataset.from_file(
     filename=os.path.join(UpperCamelCase_  ,    'beta.arrow'   )  ,    info=datasets.DatasetInfo(features=UpperCamelCase_   )   )
 dataset.set_format('numpy'   )
 for i in range(0  ,    len(UpperCamelCase_   )  ,    UpperCamelCase_   ):
  UpperCAmelCase__							=				dataset[i : i + batch_size]
@get_duration
def 						UpperCamelCase_(			snake_case__:  Dict  ,    snake_case__:  List[Any]   )       -> str:
 UpperCAmelCase__							=				datasets.Dataset.from_file(
     filename=os.path.join(UpperCamelCase_  ,    'beta.arrow'   )  ,    info=datasets.DatasetInfo(features=UpperCamelCase_   )   )
 for col in feats:
  UpperCAmelCase__							=				dataset[col]
@get_duration
def 						UpperCamelCase_(			snake_case__:  int  ,    snake_case__:  Optional[int]   )       -> Optional[int]:
 UpperCAmelCase__							=				datasets.Dataset.from_file(
     filename=os.path.join(UpperCamelCase_  ,    'beta.arrow'   )  ,    info=datasets.DatasetInfo(features=UpperCamelCase_   )   )
 dataset.set_format('numpy'   )
 for col in feats:
  UpperCAmelCase__							=				dataset[col]
def 						UpperCamelCase_(			)       -> Union[str, Any]:
 UpperCAmelCase__							=				{}
 UpperCAmelCase__							=				(
     read_unformated,
     read_formatted_as_numpy,
     read_batch_unformated,
     read_batch_formatted_as_numpy,
     read_col_unformated,
     read_col_formatted_as_numpy,
 )
 with tempfile.TemporaryDirectory() as tmp_dir:
  UpperCAmelCase__							=				datasets.Features({'image': ArrayaD(UpperCamelCase_  ,    dtype='float32'   )}   )
  UpperCAmelCase__							=				generate_examples(features=UpperCamelCase_  ,    num_examples=UpperCamelCase_   )
  UpperCAmelCase__							=				write(UpperCamelCase_  ,    UpperCamelCase_  ,    UpperCamelCase_   )
  for read_func in read_functions:
   UpperCAmelCase__							=				read_func(UpperCamelCase_  ,    UpperCamelCase_   )

 with tempfile.TemporaryDirectory() as tmp_dir:
  # don't use fixed length for fair comparison
  # feats = datasets.Features(
  #     {"image": datasets.Sequence(datasets.Sequence(datasets.Value("float32"), SPEED_TEST_SHAPE[1]), SPEED_TEST_SHAPE[0])}
  # )
  UpperCAmelCase__							=				datasets.Features({'image': datasets.Sequence(datasets.Sequence(datasets.Value('float32'   )   )   )}   )
  UpperCAmelCase__							=				generate_examples(
      features=UpperCamelCase_  ,    num_examples=UpperCamelCase_  ,    seq_shapes={'image': SPEED_TEST_SHAPE}   )
  UpperCAmelCase__							=				write(UpperCamelCase_  ,    UpperCamelCase_  ,    UpperCamelCase_   )
  for read_func in read_functions:
   UpperCAmelCase__							=				read_func(UpperCamelCase_  ,    UpperCamelCase_   )

 with tempfile.TemporaryDirectory() as tmp_dir:
  # don't use fixed length for fair comparison
  # feats = datasets.Features(
  #     {"image": datasets.Sequence(datasets.Value("float32"), SPEED_TEST_SHAPE[0] * SPEED_TEST_SHAPE[1])}
  # )
  UpperCAmelCase__							=				datasets.Features({'image': datasets.Sequence(datasets.Value('float32'   )   )}   )
  UpperCAmelCase__							=				generate_examples(
      features=UpperCamelCase_  ,    num_examples=UpperCamelCase_  ,    seq_shapes={'image': [SPEED_TEST_SHAPE[0] * SPEED_TEST_SHAPE[1]]}  ,    )
  UpperCAmelCase__							=				write(UpperCamelCase_  ,    UpperCamelCase_  ,    UpperCamelCase_   )
  for read_func in read_functions:
   UpperCAmelCase__							=				read_func(UpperCamelCase_  ,    UpperCamelCase_   )

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


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









import inspect
import json
import os
import tempfile
import unittest

from transformers.models.speech_to_text_a import SpeechaTextaTokenizer
from transformers.models.speech_to_text_a.tokenization_speech_to_text_a import VOCAB_FILES_NAMES

from ...test_tokenization_common import TokenizerTesterMixin
class 				__snake_case     (_a  ,		unittest.TestCase						):
				lowerCAmelCase__			=   SpeechaTextaTokenizer
				lowerCAmelCase__			=   False



				def 					SCREAMING_SNAKE_CASE				(					self					:						Optional[int]					)     ->			Optional[int]:





								'''simple docstring'''




								super().setUp()

								_lowerCAmelCase			:			Optional[int]        =					"""<s> <pad> </s> <unk> here@@ a couple of@@ words for the he@@ re@@ vocab""".split(""" """					)
								_lowerCAmelCase			:			Optional[int]        =					["""he re</w> 123""", """here a 1456"""]
								_lowerCAmelCase			:			Union[str, Any]        =					dict(zip(_UpperCAmelCase						,		range(len(_UpperCAmelCase					)					)					)					)

								_lowerCAmelCase			:			Any        =					{"""pad_token""": """<pad>""", """unk_token""": """<unk>""", """bos_token""": """<s>""", """eos_token""": """</s>"""}

								_lowerCAmelCase			:			Union[str, Any]        =					tempfile.mkdtemp()
								_lowerCAmelCase			:			Tuple        =					os.path.join(self.tmpdirname						,		VOCAB_FILES_NAMES["""vocab_file"""]					)
								_lowerCAmelCase			:			Any        =					os.path.join(self.tmpdirname						,		VOCAB_FILES_NAMES["""merges_file"""]					)

								with open(self.vocab_file						,		"""w"""						,		encoding="""utf-8"""					) as fp:
												fp.write(json.dumps(_UpperCAmelCase					) + """\n"""					)

								with open(self.merges_file						,		"""w"""					) as fp:
												fp.write("""\n""".join(_UpperCAmelCase					)					)



				def 					SCREAMING_SNAKE_CASE				(					self					:						int					)     ->			Any:





								'''simple docstring'''




								_lowerCAmelCase			:			List[str]        =					list(self.get_tokenizer().get_vocab().keys()					)

								self.assertEqual(vocab_keys[0]						,		"""<s>"""					)
								self.assertEqual(vocab_keys[1]						,		"""<pad>"""					)
								self.assertEqual(vocab_keys[-1]						,		"""vocab"""					)
								self.assertEqual(len(_UpperCAmelCase					)						,		14					)



				def 					SCREAMING_SNAKE_CASE				(					self					:						Any					)     ->			int:





								'''simple docstring'''




								self.assertEqual(self.get_tokenizer().vocab_size						,		14					)



				def 					SCREAMING_SNAKE_CASE				(					self					:						str					)     ->			Tuple:





								'''simple docstring'''




								_lowerCAmelCase			:			List[Any]        =					SpeechaTextaTokenizer.from_pretrained(self.tmpdirname					)

								# make sure @@ is correctly concatenated
								_lowerCAmelCase			:			int        =					[4, 6, 8, 7, 10]  # ["here@@", "couple", "words", "of@@", "the"]
								_lowerCAmelCase			:			List[Any]        =					tokenizer.decode(_UpperCAmelCase					)

								self.assertTrue(output_string == """herecouple words ofthe"""					)



				def 					SCREAMING_SNAKE_CASE				(					self					:						Dict					)     ->			Any:





								'''simple docstring'''




								_lowerCAmelCase			:			str        =					SpeechaTextaTokenizer.from_pretrained(self.tmpdirname					)

								with tempfile.TemporaryDirectory() as tmp_dirname:
												tokenizer.save_pretrained(_UpperCAmelCase					)
												os.remove(os.path.join(_UpperCAmelCase						,		"""merges.txt"""					)					)

												# load tokenizer without merges file should not throw an error
												_lowerCAmelCase			:			Any        =					SpeechaTextaTokenizer.from_pretrained(_UpperCAmelCase					)

								with tempfile.TemporaryDirectory() as tmp_dirname:
												# save tokenizer and load again
												tokenizer.save_pretrained(_UpperCAmelCase					)
												_lowerCAmelCase			:			int        =					SpeechaTextaTokenizer.from_pretrained(_UpperCAmelCase					)

								self.assertIsNotNone(_UpperCAmelCase					)



				def 					SCREAMING_SNAKE_CASE				(					self					:						Optional[Any]					)     ->			Optional[Any]:





								'''simple docstring'''




								if not self.test_slow_tokenizer:
												return

								_lowerCAmelCase			:			Dict        =					inspect.signature(self.tokenizer_class.__init__					)
								_lowerCAmelCase			:			Union[str, Any]        =					self.get_tokenizer()

								for parameter_name, parameter in signature.parameters.items():
												if parameter.default != inspect.Parameter.empty and parameter_name != "merges_file":
																self.assertIn(_UpperCAmelCase						,		tokenizer.init_kwargs					)

