





import unittest

from datasets import load_dataset

from transformers import BloomTokenizerFast
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin







@require_tokenizers
class     __snake_case       (							__lowerCamelCase   ,			unittest.TestCase			):





       '''simple docstring'''







       lowerCAmelCase__      		=     None
       lowerCAmelCase__      		=     BloomTokenizerFast
       lowerCAmelCase__      		=     BloomTokenizerFast
       lowerCAmelCase__      		=     True
       lowerCAmelCase__      		=     False
       lowerCAmelCase__      		=     """tokenizer_file"""
       lowerCAmelCase__      		=     {"""bos_token""": """<s>""", """eos_token""": """</s>""", """unk_token""": """<unk>""", """pad_token""": """<pad>"""}




       def 							UpperCAmelCase__ (  self :    Optional[Any]		):
         super().setUp()
         __snake_case:						Tuple   =						BloomTokenizerFast.from_pretrained("""bigscience/tokenizer"""		)
         tokenizer.save_pretrained(self.tmpdirname		)




       def 							UpperCAmelCase__ (  self :    str							,				**A :    Dict		):
         kwargs.update(self.special_tokens_map		)
         return BloomTokenizerFast.from_pretrained(self.tmpdirname							,				**A		)




       def 							UpperCAmelCase__ (  self :    Any		):
         __snake_case:						str   =						self.get_rust_tokenizer()

         __snake_case:						str   =						["""The quick brown fox</s>""", """jumps over the lazy dog</s>"""]
         __snake_case:						Optional[int]   =						[[2_175, 23_714, 73_173, 144_252, 2], [77, 132_619, 3_478, 368, 109_586, 35_433, 2]]

         __snake_case:						int   =						tokenizer.batch_encode_plus(A		)["""input_ids"""]
         self.assertListEqual(A							,				A		)

         __snake_case:						Tuple   =						tokenizer.batch_decode(A		)
         self.assertListEqual(A							,				A		)




       def 							UpperCAmelCase__ (  self :    Optional[Any]							,				A :    Optional[Any]=6		):
         for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
           with self.subTest(f'''{tokenizer.__class__.__name__} ({pretrained_name})'''		):
             __snake_case:						Union[str, Any]   =						self.rust_tokenizer_class.from_pretrained(A							,				**A		)
             # tokenizer_r.pad_token = None # Hotfixing padding = None
             # Simple input
             __snake_case:						Any   =						"""This is a simple input"""
             __snake_case:						Any   =						["""This is a simple input 1""", """This is a simple input 2"""]
             __snake_case:						Tuple   =						("""This is a simple input""", """This is a pair""")
             __snake_case:						Optional[Any]   =						[
                 ("""This is a simple input 1""", """This is a simple input 2"""),
                 ("""This is a simple pair 1""", """This is a simple pair 2"""),
             ]

             # Simple input tests
             try:
               tokenizer_r.encode(A							,				max_length=A		)
               tokenizer_r.encode_plus(A							,				max_length=A		)

               tokenizer_r.batch_encode_plus(A							,				max_length=A		)
               tokenizer_r.encode(A							,				max_length=A		)
               tokenizer_r.batch_encode_plus(A							,				max_length=A		)
             except ValueError:
               self.fail("""Bloom Tokenizer should be able to deal with padding"""		)

             __snake_case:						str   =						None  # Hotfixing padding = None
             self.assertRaises(A							,				tokenizer_r.encode							,				A							,				max_length=A							,				padding="""max_length"""		)

             # Simple input
             self.assertRaises(A							,				tokenizer_r.encode_plus							,				A							,				max_length=A							,				padding="""max_length"""		)

             # Simple input
             self.assertRaises(
                 A							,				tokenizer_r.batch_encode_plus							,				A							,				max_length=A							,				padding="""max_length"""							,				)

             # Pair input
             self.assertRaises(A							,				tokenizer_r.encode							,				A							,				max_length=A							,				padding="""max_length"""		)

             # Pair input
             self.assertRaises(A							,				tokenizer_r.encode_plus							,				A							,				max_length=A							,				padding="""max_length"""		)

             # Pair input
             self.assertRaises(
                 A							,				tokenizer_r.batch_encode_plus							,				A							,				max_length=A							,				padding="""max_length"""							,				)




       def 							UpperCAmelCase__ (  self :    List[Any]		):
         __snake_case:						Dict   =						self.get_rust_tokenizer()
         __snake_case:						Union[str, Any]   =						load_dataset("""xnli"""							,				"""all_languages"""							,				split="""test"""							,				streaming=A		)

         __snake_case:						Dict   =						next(iter(A		)		)["""premise"""]  # pick up one data
         __snake_case:						List[str]   =						list(sample_data.values()		)

         __snake_case:						Tuple   =						list(map(tokenizer.encode							,				A		)		)
         __snake_case:						Any   =						[tokenizer.decode(A							,				clean_up_tokenization_spaces=A		) for x in output_tokens]
         self.assertListEqual(A							,				A		)






       def 							UpperCAmelCase__ (  self :    Any		):
         # The test has to be overriden because BLOOM uses ALiBi positional embeddings that does not have
         # any sequence length constraints. This test of the parent class will fail since it relies on the
         # maximum sequence length of the positoonal embeddings.
         self.assertGreaterEqual(len(self.tokenizer_class.pretrained_vocab_files_map		)							,				1		)
         self.assertGreaterEqual(len(list(self.tokenizer_class.pretrained_vocab_files_map.values()		)[0]		)							,				1		)













import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
      import tensorflow as tf

      from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments







@require_tf
class     __snake_case       (							unittest.TestCase			):





       '''simple docstring'''







       def 							UpperCAmelCase__ (  self :    Any							,				A :    Union[str, Any]		):
         for model_result in results.values():
           for batch_size, sequence_length in zip(model_result["""bs"""]							,				model_result["""ss"""]		):
             __snake_case:						Dict   =						model_result["""result"""][batch_size][sequence_length]
             self.assertIsNotNone(A		)




       def 							UpperCAmelCase__ (  self :    List[Any]		):
         __snake_case:						List[Any]   =						"""sshleifer/tiny-gpt2"""
         __snake_case:						Union[str, Any]   =						TensorFlowBenchmarkArguments(
             models=[MODEL_ID]							,				training=A							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				eager_mode=A							,				multi_process=A							,				)
         __snake_case:						Dict   =						TensorFlowBenchmark(A		)
         __snake_case:						List[str]   =						benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result		)
         self.check_results_dict_not_empty(results.memory_inference_result		)




       def 							UpperCAmelCase__ (  self :    Optional[int]		):
         __snake_case:						Optional[int]   =						"""sgugger/tiny-distilbert-classification"""
         __snake_case:						str   =						TensorFlowBenchmarkArguments(
             models=[MODEL_ID]							,				training=A							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				multi_process=A							,				only_pretrain_model=A							,				)
         __snake_case:						Any   =						TensorFlowBenchmark(A		)
         __snake_case:						List[Any]   =						benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result		)
         self.check_results_dict_not_empty(results.memory_inference_result		)




       def 							UpperCAmelCase__ (  self :    int		):
         __snake_case:						Dict   =						"""sshleifer/tiny-gpt2"""
         __snake_case:						Union[str, Any]   =						TensorFlowBenchmarkArguments(
             models=[MODEL_ID]							,				training=A							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				multi_process=A							,				)
         __snake_case:						int   =						TensorFlowBenchmark(A		)
         __snake_case:						str   =						benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result		)
         self.check_results_dict_not_empty(results.memory_inference_result		)




       def 							UpperCAmelCase__ (  self :    List[Any]		):
         __snake_case:						List[Any]   =						"""sshleifer/tiny-gpt2"""
         __snake_case:						int   =						AutoConfig.from_pretrained(A		)
         __snake_case:						Optional[Any]   =						TensorFlowBenchmarkArguments(
             models=[MODEL_ID]							,				training=A							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				eager_mode=A							,				multi_process=A							,				)
         __snake_case:						Union[str, Any]   =						TensorFlowBenchmark(A							,				[config]		)
         __snake_case:						Tuple   =						benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result		)
         self.check_results_dict_not_empty(results.memory_inference_result		)




       def 							UpperCAmelCase__ (  self :    Any		):
         __snake_case:						Optional[int]   =						"""sshleifer/tiny-gpt2"""
         __snake_case:						Tuple   =						AutoConfig.from_pretrained(A		)
         __snake_case:						Union[str, Any]   =						TensorFlowBenchmarkArguments(
             models=[MODEL_ID]							,				training=A							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				multi_process=A							,				)
         __snake_case:						str   =						TensorFlowBenchmark(A							,				[config]		)
         __snake_case:						str   =						benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result		)
         self.check_results_dict_not_empty(results.memory_inference_result		)




       def 							UpperCAmelCase__ (  self :    Any		):
         __snake_case:						Tuple   =						"""sshleifer/tiny-gpt2"""
         __snake_case:						str   =						TensorFlowBenchmarkArguments(
             models=[MODEL_ID]							,				training=A							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				multi_process=A							,				)
         __snake_case:						Tuple   =						TensorFlowBenchmark(A		)
         __snake_case:						Tuple   =						benchmark.run()
         self.check_results_dict_not_empty(results.time_train_result		)
         self.check_results_dict_not_empty(results.memory_train_result		)




       def 							UpperCAmelCase__ (  self :    int		):
         __snake_case:						Dict   =						"""sshleifer/tiny-gpt2"""
         __snake_case:						Union[str, Any]   =						AutoConfig.from_pretrained(A		)
         __snake_case:						Optional[int]   =						TensorFlowBenchmarkArguments(
             models=[MODEL_ID]							,				training=A							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				multi_process=A							,				)
         __snake_case:						List[str]   =						TensorFlowBenchmark(A							,				[config]		)
         __snake_case:						Dict   =						benchmark.run()
         self.check_results_dict_not_empty(results.time_train_result		)
         self.check_results_dict_not_empty(results.memory_train_result		)




       def 							UpperCAmelCase__ (  self :    List[str]		):
         __snake_case:						List[Any]   =						"""patrickvonplaten/t5-tiny-random"""
         __snake_case:						List[str]   =						AutoConfig.from_pretrained(A		)
         __snake_case:						List[str]   =						TensorFlowBenchmarkArguments(
             models=[MODEL_ID]							,				training=A							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				multi_process=A							,				)
         __snake_case:						Optional[int]   =						TensorFlowBenchmark(A							,				configs=[config]		)
         __snake_case:						Tuple   =						benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result		)
         self.check_results_dict_not_empty(results.memory_inference_result		)




       @unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices("""GPU"""		)		) == 0							,				"""Cannot do xla on CPU."""		)
       def 							UpperCAmelCase__ (  self :    Optional[Any]		):
         __snake_case:						Optional[Any]   =						"""sshleifer/tiny-gpt2"""
         __snake_case:						List[Any]   =						TensorFlowBenchmarkArguments(
             models=[MODEL_ID]							,				training=A							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				use_xla=A							,				multi_process=A							,				)
         __snake_case:						Union[str, Any]   =						TensorFlowBenchmark(A		)
         __snake_case:						List[Any]   =						benchmark.run()
         self.check_results_dict_not_empty(results.time_inference_result		)
         self.check_results_dict_not_empty(results.memory_inference_result		)




       def 							UpperCAmelCase__ (  self :    Union[str, Any]		):
         __snake_case:						List[str]   =						"""sshleifer/tiny-gpt2"""
         with tempfile.TemporaryDirectory() as tmp_dir:
           __snake_case:						int   =						TensorFlowBenchmarkArguments(
               models=[MODEL_ID]							,				inference=A							,				save_to_csv=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				inference_time_csv_file=os.path.join(A							,				"""inf_time.csv"""		)							,				inference_memory_csv_file=os.path.join(A							,				"""inf_mem.csv"""		)							,				env_info_csv_file=os.path.join(A							,				"""env.csv"""		)							,				multi_process=A							,				)
           __snake_case:						Tuple   =						TensorFlowBenchmark(A		)
           benchmark.run()
           self.assertTrue(Path(os.path.join(A							,				"""inf_time.csv"""		)		).exists()		)
           self.assertTrue(Path(os.path.join(A							,				"""inf_mem.csv"""		)		).exists()		)
           self.assertTrue(Path(os.path.join(A							,				"""env.csv"""		)		).exists()		)






       def 							UpperCAmelCase__ (  self :    Optional[int]		):
         __snake_case:						Any   =						"""sshleifer/tiny-gpt2"""

         def _check_summary_is_not_empty(A :    Union[str, Any]		):
           self.assertTrue(hasattr(A							,				"""sequential"""		)		)
           self.assertTrue(hasattr(A							,				"""cumulative"""		)		)
           self.assertTrue(hasattr(A							,				"""current"""		)		)
           self.assertTrue(hasattr(A							,				"""total"""		)		)

         with tempfile.TemporaryDirectory() as tmp_dir:
           __snake_case:						Union[str, Any]   =						TensorFlowBenchmarkArguments(
               models=[MODEL_ID]							,				inference=A							,				sequence_lengths=[8]							,				batch_sizes=[1]							,				log_filename=os.path.join(A							,				"""log.txt"""		)							,				log_print=A							,				trace_memory_line_by_line=A							,				eager_mode=A							,				multi_process=A							,				)
           __snake_case:						Dict   =						TensorFlowBenchmark(A		)
           __snake_case:						List[str]   =						benchmark.run()
           _check_summary_is_not_empty(result.inference_summary		)
           self.assertTrue(Path(os.path.join(A							,				"""log.txt"""		)		).exists()		)






