


"""simple docstring"""


import os
from typing import Optional

import fsspec
from fsspec.archive import AbstractArchiveFileSystem
from fsspec.utils import DEFAULT_BLOCK_SIZE

class 						_UpperCamelCase						(     lowerCAmelCase__	):
							'''simple docstring'''







							__UpperCAmelCase		:					Tuple          =""""""
							__UpperCAmelCase		:					str          =(
							    None  # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz
							)
							__UpperCAmelCase		:					str          =None  # compression type in fsspec. ex: "gzip"
							__UpperCAmelCase		:					str          =None  # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz

							def __init__(  self					,     __a = ""					,     __a = None					,     __a = None					,     **__a			):
										super().__init__(self					,     **__a			)
										# always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode
										__lowerCAmelCase       					=    fsspec.open(
										    __a					,     mode="rb"					,     protocol=__a					,     compression=self.compression					,     client_kwargs={
										        "requote_redirect_url": False,  # see https://github.com/huggingface/datasets/pull/5459
										        "trust_env": True,  # Enable reading proxy env variables.
										        **(target_options or {}).pop("client_kwargs"					,     {}			),  # To avoid issues if it was already passed.
										    }					,     **(target_options or {})					,     )
										__lowerCAmelCase       					=    os.path.basename(self.file.path.split("::"			)[0]			)
										__lowerCAmelCase       					=    (
										    self.compressed_name[: self.compressed_name.rindex("."			)]
										    if "." in self.compressed_name
										    else self.compressed_name
										)
										__lowerCAmelCase       					=    None

							@classmethod
							def     snake_case       (  cls					,     __a			):
										# compressed file paths are always relative to the archive root
										return super()._strip_protocol(__a			).lstrip("/"			)

							def     snake_case       (  self			):
										if self.dir_cache is None:
													__lowerCAmelCase       					=    {**self.file.fs.info(self.file.path			), "name": self.uncompressed_name}
													__lowerCAmelCase       					=    {f["name"]: f}

							def     snake_case       (  self					,     __a			):
										return self.file.open().read()



							def     snake_case       (  self					,     __a					,     __a = "rb"					,     __a=None					,     __a=True					,     __a=None					,     **__a					,     ):
										__lowerCAmelCase       					=    self._strip_protocol(__a			)
										if mode != "rb":
													raise ValueError(f"Tried to read with mode {mode} on file {self.file.path} opened with mode 'rb'"			)
										return self.file.open()

class 						_UpperCamelCase						(     lowerCAmelCase__	):
							'''simple docstring'''







							__UpperCAmelCase		:					Any          ="""bz2"""
							__UpperCAmelCase		:					Any          ="""bz2"""
							__UpperCAmelCase		:					List[Any]          =""".bz2"""

class 						_UpperCamelCase						(     lowerCAmelCase__	):
							'''simple docstring'''







							__UpperCAmelCase		:					Tuple          ="""gzip"""
							__UpperCAmelCase		:					Union[str, Any]          ="""gzip"""
							__UpperCAmelCase		:					Dict          =""".gz"""

class 						_UpperCamelCase						(     lowerCAmelCase__	):
							'''simple docstring'''







							__UpperCAmelCase		:					Any          ="""lz4"""
							__UpperCAmelCase		:					Any          ="""lz4"""
							__UpperCAmelCase		:					List[str]          =""".lz4"""

class 						_UpperCamelCase						(     lowerCAmelCase__	):
							'''simple docstring'''







							__UpperCAmelCase		:					Optional[Any]          ="""xz"""
							__UpperCAmelCase		:					int          ="""xz"""
							__UpperCAmelCase		:					List[str]          =""".xz"""

class 						_UpperCamelCase						(     lowerCAmelCase__	):
							'''simple docstring'''







							__UpperCAmelCase		:					Optional[int]          ="""zstd"""
							__UpperCAmelCase		:					Optional[Any]          ="""zstd"""
							__UpperCAmelCase		:					str          =""".zst"""

							def __init__(  self					,     __a					,     __a = "rb"					,     __a = None					,     __a = None					,     __a = DEFAULT_BLOCK_SIZE					,     **__a					,     ):
										super().__init__(
										    fo=__a					,     mode=__a					,     target_protocol=__a					,     target_options=__a					,     block_size=__a					,     **__a					,     )
										# We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2:
										#
										# File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open
										#     out.close = close
										# AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only
										#
										# see https://github.com/intake/filesystem_spec/issues/725
										__lowerCAmelCase       					=    self.file.__enter__

										class 						_UpperCamelCase						:
																	'''simple docstring'''







																	def __init__(  self					,     __a			):
																				__lowerCAmelCase       					=    file_

																	def __enter__(  self			):
																				self._file.__enter__()
																				return self

																	def __exit__(  self					,     *__a					,     **__a			):
																				self._file.__exit__(*__a					,     **__a			)

																	def __iter__(  self			):
																				return iter(self._file			)

																	def     snake_case       (  self			):
																				return next(self._file			)



																	def __getattr__(  self					,     __a			):
																				return getattr(self._file					,     __a			)

										def fixed_enter(*__a					,     **__a			):
													return WrappedFile(_enter(*__a					,     **__a			)			)

										__lowerCAmelCase       					=    fixed_enter








"""simple docstring"""


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 						_UpperCamelCase						(     unittest.TestCase	):
							'''simple docstring'''







							def     snake_case       (  self					,     __a			):
										for model_result in results.values():
													for batch_size, sequence_length in zip(model_result["bs"]					,     model_result["ss"]			):
																__lowerCAmelCase       					=    model_result["result"][batch_size][sequence_length]
																self.assertIsNotNone(__a			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     eager_mode=__a					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sgugger/tiny-distilbert-classification"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     only_pretrain_model=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    AutoConfig.from_pretrained(__a			)
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     eager_mode=__a					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a					,     [config]			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    AutoConfig.from_pretrained(__a			)
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a					,     [config]			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_train_result			)
										self.check_results_dict_not_empty(results.memory_train_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    AutoConfig.from_pretrained(__a			)
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a					,     [config]			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_train_result			)
										self.check_results_dict_not_empty(results.memory_train_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "patrickvonplaten/t5-tiny-random"
										__lowerCAmelCase       					=    AutoConfig.from_pretrained(__a			)
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a					,     configs=[config]			)
										__lowerCAmelCase       					=    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     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										__lowerCAmelCase       					=    TensorFlowBenchmarkArguments(
										    models=[MODEL_ID]					,     training=__a					,     inference=__a					,     sequence_lengths=[8]					,     batch_sizes=[1]					,     use_xla=__a					,     multi_process=__a					,     )
										__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
										__lowerCAmelCase       					=    benchmark.run()
										self.check_results_dict_not_empty(results.time_inference_result			)
										self.check_results_dict_not_empty(results.memory_inference_result			)

							def     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"
										with tempfile.TemporaryDirectory() as tmp_dir:
													__lowerCAmelCase       					=    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					,     )
													__lowerCAmelCase       					=    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     snake_case       (  self			):
										__lowerCAmelCase       					=    "sshleifer/tiny-gpt2"

										def _check_summary_is_not_empty(__a			):
													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:
													__lowerCAmelCase       					=    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					,     )
													__lowerCAmelCase       					=    TensorFlowBenchmark(__a			)
													__lowerCAmelCase       					=    benchmark.run()
													_check_summary_is_not_empty(result.inference_summary			)
													self.assertTrue(Path(os.path.join(__a					,     "log.txt"			)			).exists()			)




