



'''simple docstring'''






import pyarrow.parquet as pq
import pytest

from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config
from datasets.features.image import Image
from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases


def 				snake_case_  (      _lowerCAmelCase						:		List[Any]     ,   _lowerCAmelCase						:		Optional[Any]						)   ->     str:
							assert isinstance(_lowerCamelCase     ,   _lowerCamelCase						)
							assert dataset.num_rows == 4
							assert dataset.num_columns == 3
							assert dataset.column_names == ["col_1", "col_2", "col_3"]
							for feature, expected_dtype in expected_features.items():
														assert dataset.features[feature].dtype == expected_dtype


@pytest.mark.parametrize('''keep_in_memory'''     ,   [False, True]						)
def 				snake_case_  (      _lowerCAmelCase						:		Any     ,   _lowerCAmelCase						:		Dict     ,   _lowerCAmelCase						:		int						)   ->     Optional[int]:
							UpperCAmelCase				: Union[str, Any]											=	tmp_path / """cache"""
							UpperCAmelCase				: int											=	{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
							with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
														UpperCAmelCase				: Optional[Any]											=	ParquetDatasetReader(_lowerCamelCase     ,   cache_dir=_lowerCamelCase     ,   keep_in_memory=_lowerCamelCase						).read()
							_check_parquet_dataset(_lowerCamelCase     ,   _lowerCamelCase						)


@pytest.mark.parametrize(
    '''features'''     ,   [
        None,
        {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''},
        {'''col_1''': '''string''', '''col_2''': '''string''', '''col_3''': '''string'''},
        {'''col_1''': '''int32''', '''col_2''': '''int32''', '''col_3''': '''int32'''},
        {'''col_1''': '''float32''', '''col_2''': '''float32''', '''col_3''': '''float32'''},
    ]     ,   )
def 				snake_case_  (      _lowerCAmelCase						:		Optional[Any]     ,   _lowerCAmelCase						:		Any     ,   _lowerCAmelCase						:		Tuple						)   ->     List[str]:
							UpperCAmelCase				: Optional[Any]											=	tmp_path / """cache"""
							UpperCAmelCase				: Optional[Any]											=	{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
							UpperCAmelCase				: Optional[Any]											=	features.copy() if features else default_expected_features
							UpperCAmelCase				: int											=	(
							    Features({feature: Value(_lowerCamelCase						) for feature, dtype in features.items()}						) if features is not None else None
							)
							UpperCAmelCase				: Tuple											=	ParquetDatasetReader(_lowerCamelCase     ,   features=_lowerCamelCase     ,   cache_dir=_lowerCamelCase						).read()
							_check_parquet_dataset(_lowerCamelCase     ,   _lowerCamelCase						)


@pytest.mark.parametrize('''split'''     ,   [None, NamedSplit('''train'''						), '''train''', '''test''']						)
def 				snake_case_  (      _lowerCAmelCase						:		int     ,   _lowerCAmelCase						:		Dict     ,   _lowerCAmelCase						:		Union[str, Any]						)   ->     Union[str, Any]:
							UpperCAmelCase				: Optional[int]											=	tmp_path / """cache"""
							UpperCAmelCase				: Optional[Any]											=	{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
							UpperCAmelCase				: Optional[int]											=	ParquetDatasetReader(_lowerCamelCase     ,   cache_dir=_lowerCamelCase     ,   split=_lowerCamelCase						).read()
							_check_parquet_dataset(_lowerCamelCase     ,   _lowerCamelCase						)
							assert dataset.split == split if split else "train"


@pytest.mark.parametrize('''path_type'''     ,   [str, list]						)
def 				snake_case_  (      _lowerCAmelCase						:		List[str]     ,   _lowerCAmelCase						:		str     ,   _lowerCAmelCase						:		List[Any]						)   ->     Any:
							if issubclass(_lowerCamelCase     ,   _lowerCamelCase						):
														UpperCAmelCase				: List[Any]											=	parquet_path
							elif issubclass(_lowerCamelCase     ,   _lowerCamelCase						):
														UpperCAmelCase				: int											=	[parquet_path]
							UpperCAmelCase				: Union[str, Any]											=	tmp_path / """cache"""
							UpperCAmelCase				: int											=	{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
							UpperCAmelCase				: Union[str, Any]											=	ParquetDatasetReader(_lowerCamelCase     ,   cache_dir=_lowerCamelCase						).read()
							_check_parquet_dataset(_lowerCamelCase     ,   _lowerCamelCase						)


def 				snake_case_  (      _lowerCAmelCase						:		Union[str, Any]     ,   _lowerCAmelCase						:		Tuple     ,   _lowerCAmelCase						:		Optional[int]=("train",)						)   ->     Optional[Any]:
							assert isinstance(_lowerCamelCase     ,   _lowerCamelCase						)
							for split in splits:
														UpperCAmelCase				: Union[str, Any]											=	dataset_dict[split]
														assert dataset.num_rows == 4
														assert dataset.num_columns == 3
														assert dataset.column_names == ["col_1", "col_2", "col_3"]
														for feature, expected_dtype in expected_features.items():
																					assert dataset.features[feature].dtype == expected_dtype


@pytest.mark.parametrize('''keep_in_memory'''     ,   [False, True]						)
def 				snake_case_  (      _lowerCAmelCase						:		int     ,   _lowerCAmelCase						:		Optional[Any]     ,   _lowerCAmelCase						:		int						)   ->     Tuple:
							UpperCAmelCase				: Any											=	tmp_path / """cache"""
							UpperCAmelCase				: Any											=	{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
							with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
														UpperCAmelCase				: Any											=	ParquetDatasetReader(
														    {'''train''': parquet_path}     ,   cache_dir=_lowerCamelCase     ,   keep_in_memory=_lowerCamelCase						).read()
							_check_parquet_datasetdict(_lowerCamelCase     ,   _lowerCamelCase						)


@pytest.mark.parametrize(
    '''features'''     ,   [
        None,
        {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''},
        {'''col_1''': '''string''', '''col_2''': '''string''', '''col_3''': '''string'''},
        {'''col_1''': '''int32''', '''col_2''': '''int32''', '''col_3''': '''int32'''},
        {'''col_1''': '''float32''', '''col_2''': '''float32''', '''col_3''': '''float32'''},
    ]     ,   )
def 				snake_case_  (      _lowerCAmelCase						:		Union[str, Any]     ,   _lowerCAmelCase						:		List[str]     ,   _lowerCAmelCase						:		int						)   ->     Union[str, Any]:
							UpperCAmelCase				: Tuple											=	tmp_path / """cache"""
							UpperCAmelCase				: Dict											=	{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
							UpperCAmelCase				: Optional[int]											=	features.copy() if features else default_expected_features
							UpperCAmelCase				: str											=	(
							    Features({feature: Value(_lowerCamelCase						) for feature, dtype in features.items()}						) if features is not None else None
							)
							UpperCAmelCase				: List[str]											=	ParquetDatasetReader({'''train''': parquet_path}     ,   features=_lowerCamelCase     ,   cache_dir=_lowerCamelCase						).read()
							_check_parquet_datasetdict(_lowerCamelCase     ,   _lowerCamelCase						)


@pytest.mark.parametrize('''split'''     ,   [None, NamedSplit('''train'''						), '''train''', '''test''']						)
def 				snake_case_  (      _lowerCAmelCase						:		Tuple     ,   _lowerCAmelCase						:		int     ,   _lowerCAmelCase						:		List[str]						)   ->     Union[str, Any]:
							if split:
														UpperCAmelCase				: List[Any]											=	{split: parquet_path}
							else:
														UpperCAmelCase				: List[str]											=	"""train"""
														UpperCAmelCase				: Optional[Any]											=	{"""train""": parquet_path, """test""": parquet_path}
							UpperCAmelCase				: List[str]											=	tmp_path / """cache"""
							UpperCAmelCase				: Dict											=	{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
							UpperCAmelCase				: List[str]											=	ParquetDatasetReader(_lowerCamelCase     ,   cache_dir=_lowerCamelCase						).read()
							_check_parquet_datasetdict(_lowerCamelCase     ,   _lowerCamelCase     ,   splits=list(path.keys()						)						)
							assert all(dataset[split].split == split for split in path.keys()						)


def 				snake_case_  (      _lowerCAmelCase						:		Union[str, Any]     ,   _lowerCAmelCase						:		str						)   ->     List[str]:
							UpperCAmelCase				: List[str]											=	ParquetDatasetWriter(_lowerCamelCase     ,   tmp_path / '''foo.parquet'''						)
							assert writer.write() > 0
							UpperCAmelCase				: Any											=	pq.ParquetFile(tmp_path / '''foo.parquet'''						)
							UpperCAmelCase				: Dict											=	pf.read()
							assert dataset.data.table == output_table


def 				snake_case_  (      _lowerCAmelCase						:		List[Any]     ,   _lowerCAmelCase						:		Dict						)   ->     Union[str, Any]:
							UpperCAmelCase				: Optional[int]											=	str(shared_datadir / '''test_image_rgb.jpg'''						)
							UpperCAmelCase				: Optional[Any]											=	{"""image""": [image_path]}
							UpperCAmelCase				: List[str]											=	Features({'''image''': Image()}						)
							UpperCAmelCase				: int											=	Dataset.from_dict(_lowerCamelCase     ,   features=_lowerCamelCase						)
							UpperCAmelCase				: Tuple											=	ParquetDatasetWriter(_lowerCamelCase     ,   tmp_path / '''foo.parquet'''						)
							assert writer.write() > 0

							UpperCAmelCase				: List[str]											=	Dataset.from_parquet(str(tmp_path / '''foo.parquet'''						)						)
							assert dataset.features == reloaded_dataset.features

							UpperCAmelCase				: List[str]											=	ParquetDatasetReader(str(tmp_path / '''foo.parquet'''						)     ,   streaming=_lowerCamelCase						).read()
							assert dataset.features == reloaded_iterable_dataset.features




@pytest.mark.parametrize(
    '''feature, expected'''     ,   [
        (Features({'''foo''': Value('''int32'''						)}						), None),
        (Features({'''image''': Image(), '''foo''': Value('''int32'''						)}						), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS),
        (Features({'''nested''': Sequence(Audio()						)}						), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS),
    ]     ,   )
def 				snake_case_  (      _lowerCAmelCase						:		int     ,   _lowerCAmelCase						:		int						)   ->     Dict:
							assert get_writer_batch_size(_lowerCamelCase						) == expected










'''simple docstring'''






import math

import tensorflow as tf
from packaging import version


def 				snake_case_  (      _lowerCAmelCase						:		List[str]						)   ->     Dict:
							UpperCAmelCase				: Tuple											=	tf.convert_to_tensor(_lowerCAmelCase						)
							UpperCAmelCase				: Dict											=	0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0						)     ,   x.dtype						)						))

							return x * cdf


def 				snake_case_  (      _lowerCAmelCase						:		int						)   ->     List[Any]:
							UpperCAmelCase				: str											=	tf.convert_to_tensor(_lowerCAmelCase						)
							UpperCAmelCase				: Union[str, Any]											=	tf.cast(math.pi     ,   x.dtype						)
							UpperCAmelCase				: Tuple											=	tf.cast(0.0_4_4_7_1_5     ,   x.dtype						)
							UpperCAmelCase				: List[Any]											=	0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi						) * (x + coeff * tf.pow(_lowerCAmelCase     ,   3						))						))

							return x * cdf


def 				snake_case_  (      _lowerCAmelCase						:		Optional[Any]						)   ->     Any:
							UpperCAmelCase				: List[Any]											=	tf.convert_to_tensor(_lowerCAmelCase						)

							return x * tf.tanh(tf.math.softplus(_lowerCAmelCase						)						)


def 				snake_case_  (      _lowerCAmelCase						:		str						)   ->     Tuple:
							UpperCAmelCase				: Union[str, Any]											=	tf.convert_to_tensor(_lowerCAmelCase						)
							UpperCAmelCase				: Union[str, Any]											=	tf.cast(0.0_4_4_7_1_5     ,   x.dtype						)
							UpperCAmelCase				: Tuple											=	tf.cast(0.7_9_7_8_8_4_5_6_0_8     ,   x.dtype						)

							return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x)						))


def 				snake_case_  (      _lowerCAmelCase						:		Optional[Any]						)   ->     Optional[int]:
							UpperCAmelCase				: str											=	tf.convert_to_tensor(_lowerCAmelCase						)
							UpperCAmelCase				: Optional[int]											=	tf.cast(1.7_0_2     ,   x.dtype						)
							return x * tf.math.sigmoid(coeff * x						)


def 				snake_case_  (      _lowerCAmelCase						:		Any						)   ->     Any:
							return tf.clip_by_value(_gelu(_lowerCAmelCase						)     ,   -10     ,   10						)


def 				snake_case_  (      _lowerCAmelCase						:		Dict     ,   _lowerCAmelCase						:		Optional[Any]=-1						)   ->     Union[str, Any]:
							UpperCAmelCase							,	UpperCAmelCase				: Optional[Any]											=	tf.split(_lowerCAmelCase     ,   2     ,   axis=_lowerCAmelCase						)
							return a * tf.math.sigmoid(_lowerCAmelCase						)


if version.parse(tf.version.VERSION) >= version.parse("2.4"):


						def 				snake_case_  (      _lowerCAmelCase						:		Tuple						)   ->     Any:
													return tf.keras.activations.gelu(_lowerCAmelCase     ,   approximate=_lowerCAmelCase						)

						UpperCamelCase__:     List[Any]										=    tf.keras.activations.gelu




						UpperCamelCase__:     List[Any]										=    approximate_gelu_wrap
else:
						UpperCamelCase__:     List[str]										=    _gelu
						UpperCamelCase__:     Tuple										=    _gelu_new


UpperCamelCase__:     List[Any]										=    {
    "gelu": gelu,
    "gelu_10": gelu_aa,
    "gelu_fast": gelu_fast,
    "gelu_new": gelu_new,
    "glu": glu,
    "mish": mish,
    "quick_gelu": quick_gelu,
    "relu": tf.keras.activations.relu,
    "sigmoid": tf.keras.activations.sigmoid,
    "silu": tf.keras.activations.swish,
    "swish": tf.keras.activations.swish,
    "tanh": tf.keras.activations.tanh,
}




def 				snake_case_  (      _lowerCAmelCase						:		Dict						)   ->     List[Any]:
							if activation_string in ACTaFN:
														return ACTaFN[activation_string]
							else:
														raise KeyError(f"""function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()						)}"""						)





