




import os
from typing import Optional

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







class        __lowerCAmelCase					(			__magic_name__					):

			"""simple docstring"""







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


			def __init__( self     ,		lowerCamelCase__ = ""     ,		lowerCamelCase__ = None     ,		lowerCamelCase__ = None     ,		**lowerCamelCase__				)       -> Dict:



							'''simple docstring'''
							super().__init__(self     ,		**lowerCamelCase__				)
							# always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode
							__lowerCamelCase									=       fsspec.open(
							    lowerCamelCase__     ,		mode='rb'     ,		protocol=lowerCamelCase__     ,		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  lowercase_       ( cls     ,		lowerCamelCase__				)       -> Optional[Any]:



							'''simple docstring'''
							# compressed file paths are always relative to the archive root
							return super()._strip_protocol(lowerCamelCase__				).lstrip('/'				)


			def  lowercase_       ( self				)       -> List[str]:



							'''simple docstring'''
							if self.dir_cache is None:
											__lowerCamelCase									=       {**self.file.fs.info(self.file.path				), 'name': self.uncompressed_name}
											__lowerCamelCase									=       {f['name']: f}


			def  lowercase_       ( self     ,		lowerCamelCase__				)       -> Tuple:



							'''simple docstring'''
							return self.file.open().read()






			def  lowercase_       ( self     ,		lowerCamelCase__     ,		lowerCamelCase__ = "rb"     ,		lowerCamelCase__=None     ,		lowerCamelCase__=True     ,		lowerCamelCase__=None     ,		**lowerCamelCase__     ,		)       -> Optional[Any]:



							'''simple docstring'''
							__lowerCamelCase									=       self._strip_protocol(lowerCamelCase__				)
							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        __lowerCAmelCase					(			__magic_name__					):

			"""simple docstring"""







			snake_case_  						=			'''bz2'''
			snake_case_  						=			'''bz2'''
			snake_case_  						=			'''.bz2'''







class        __lowerCAmelCase					(			__magic_name__					):

			"""simple docstring"""







			snake_case_  						=			'''gzip'''
			snake_case_  						=			'''gzip'''
			snake_case_  						=			'''.gz'''







class        __lowerCAmelCase					(			__magic_name__					):

			"""simple docstring"""







			snake_case_  						=			'''lz4'''
			snake_case_  						=			'''lz4'''
			snake_case_  						=			'''.lz4'''







class        __lowerCAmelCase					(			__magic_name__					):

			"""simple docstring"""







			snake_case_  						=			'''xz'''
			snake_case_  						=			'''xz'''
			snake_case_  						=			'''.xz'''







class        __lowerCAmelCase					(			__magic_name__					):

			"""simple docstring"""







			snake_case_  						=			'''zstd'''
			snake_case_  						=			'''zstd'''
			snake_case_  						=			'''.zst'''


			def __init__( self     ,		lowerCamelCase__     ,		lowerCamelCase__ = "rb"     ,		lowerCamelCase__ = None     ,		lowerCamelCase__ = None     ,		lowerCamelCase__ = DEFAULT_BLOCK_SIZE     ,		**lowerCamelCase__     ,		)       -> Dict:



							'''simple docstring'''
							super().__init__(
							    fo=lowerCamelCase__     ,		mode=lowerCamelCase__     ,		target_protocol=lowerCamelCase__     ,		target_options=lowerCamelCase__     ,		block_size=lowerCamelCase__     ,		**lowerCamelCase__     ,		)
							# 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        __lowerCAmelCase					:

										"""simple docstring"""







										def __init__( self     ,		lowerCamelCase__				)       -> str:



														'''simple docstring'''
														__lowerCamelCase									=       file_


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



														'''simple docstring'''
														self._file.__enter__()
														return self


										def __exit__( self     ,		*lowerCamelCase__     ,		**lowerCamelCase__				)       -> int:



														'''simple docstring'''
														self._file.__exit__(*lowerCamelCase__     ,		**lowerCamelCase__				)


										def __iter__( self				)       -> List[str]:



														'''simple docstring'''
														return iter(self._file				)


										def  lowercase_       ( self				)       -> List[Any]:



														'''simple docstring'''
														return next(self._file				)






										def __getattr__( self     ,		lowerCamelCase__				)       -> Union[str, Any]:



														'''simple docstring'''
														return getattr(self._file     ,		lowerCamelCase__				)

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

							__lowerCamelCase									=       fixed_enter








import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
					import torch

					from transformers.activations import gelu_new, gelu_python, get_activation







@require_torch
class        __lowerCAmelCase					(			unittest.TestCase					):

			"""simple docstring"""







			def  lowercase_       ( self				)       -> int:



							'''simple docstring'''
							__lowerCamelCase									=       torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]				)
							__lowerCamelCase									=       get_activation('gelu'				)
							self.assertTrue(torch.allclose(gelu_python(lowerCamelCase__				)     ,		torch_builtin(lowerCamelCase__				)				)				)
							self.assertFalse(torch.allclose(gelu_python(lowerCamelCase__				)     ,		gelu_new(lowerCamelCase__				)				)				)


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



							'''simple docstring'''
							__lowerCamelCase									=       torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]				)
							__lowerCamelCase									=       get_activation('gelu'				)
							__lowerCamelCase									=       get_activation('gelu_10'				)

							__lowerCamelCase									=       torch_builtin(lowerCamelCase__				)
							__lowerCamelCase									=       geluaa(lowerCamelCase__				)

							__lowerCamelCase									=       torch.where(y_gelu_aa < 10.0     ,		1     ,		0				)

							self.assertTrue(torch.max(lowerCamelCase__				).item() == 10.0				)
							self.assertTrue(torch.allclose(y_gelu * clipped_mask     ,		y_gelu_aa * clipped_mask				)				)


			def  lowercase_       ( self				)       -> Any:



							'''simple docstring'''
							get_activation('gelu'				)
							get_activation('gelu_10'				)
							get_activation('gelu_fast'				)
							get_activation('gelu_new'				)
							get_activation('gelu_python'				)
							get_activation('gelu_pytorch_tanh'				)
							get_activation('linear'				)
							get_activation('mish'				)
							get_activation('quick_gelu'				)
							get_activation('relu'				)
							get_activation('sigmoid'				)
							get_activation('silu'				)
							get_activation('swish'				)
							get_activation('tanh'				)
							with self.assertRaises(lowerCamelCase__				):
											get_activation('bogus'				)
							with self.assertRaises(lowerCamelCase__				):
											get_activation(lowerCamelCase__				)






			def  lowercase_       ( self				)       -> Dict:



							'''simple docstring'''
							__lowerCamelCase									=       get_activation('gelu'				)
							__lowerCamelCase									=       1
							__lowerCamelCase									=       get_activation('gelu'				)
							self.assertEqual(acta.a     ,		1				)
							with self.assertRaises(lowerCamelCase__				):
											__lowerCamelCase									=       acta.a


