import unittest

import numpy as np

from transformers import is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
				import tensorflow as tf

				from transformers.activations_tf import get_tf_activation



@require_tf
class    lowercase_    (     unittest.TestCase		):



							def        UpperCamelCase       (						self				):
													_snake_case						:     int				=    tf.constant([-100, -1.0, -0.1, 0, 0.1, 1.0, 100.0]				)
													_snake_case						:     List[Any]				=    get_tf_activation("gelu"				)
													_snake_case						:     Any				=    get_tf_activation("gelu_10"				)

													_snake_case						:     Dict				=    gelu(lowercase_				)
													_snake_case						:     Optional[int]				=    geluaa(lowercase_				)

													_snake_case						:     Tuple				=    tf.where(y_gelu_aa < 10.0      ,				1.0      ,				0.0				)

													self.assertEqual(tf.math.reduce_max(lowercase_				).numpy().item()      ,				10.0				)
													self.assertTrue(np.allclose(y_gelu * clipped_mask      ,				y_gelu_aa * clipped_mask				)				)



							def        UpperCamelCase       (						self				):
													get_tf_activation("gelu"				)
													get_tf_activation("gelu_10"				)
													get_tf_activation("gelu_fast"				)
													get_tf_activation("gelu_new"				)
													get_tf_activation("glu"				)
													get_tf_activation("mish"				)
													get_tf_activation("quick_gelu"				)
													get_tf_activation("relu"				)
													get_tf_activation("sigmoid"				)
													get_tf_activation("silu"				)
													get_tf_activation("swish"				)
													get_tf_activation("tanh"				)
													with self.assertRaises(lowercase_				):
																			get_tf_activation("bogus"				)
													with self.assertRaises(lowercase_				):
																			get_tf_activation(lowercase_				)
import os
import sys
from pathlib import Path

import pytest

from datasets import Dataset, IterableDataset
from datasets.distributed import split_dataset_by_node

from .utils import execute_subprocess_async, get_torch_dist_unique_port, require_torch
def 					snake_case						()       ->       int:

						'''simple docstring'''



						_snake_case						:     List[Any]				=    Dataset.from_dict({"i": range(17						)}						)
						_snake_case						:     Tuple				=    len(__lowercase						)
						_snake_case						:     Any				=    3
						_snake_case						:     List[str]				=    [
						    split_dataset_by_node(__lowercase					,			rank=__lowercase					,			world_size=__lowercase						) for rank in range(__lowercase						)
						]
						assert sum(len(__lowercase						) for ds in datasets_per_rank						) == full_size
						assert len({tuple(x.values()						) for ds in datasets_per_rank for x in ds}						) == full_size
def 					snake_case						()       ->       List[Any]:

						'''simple docstring'''



						def gen():
												return ({"i": i} for i in range(17						))

						_snake_case						:     List[Any]				=    3
						_snake_case						:     Dict				=    IterableDataset.from_generator(__lowercase						)
						_snake_case						:     List[Any]				=    len(list(__lowercase						)						)
						_snake_case						:     str				=    [
						    split_dataset_by_node(__lowercase					,			rank=__lowercase					,			world_size=__lowercase						) for rank in range(__lowercase						)
						]
						assert sum(len(list(__lowercase						)						) for ds in datasets_per_rank						) == full_size
						assert len({tuple(x.values()						) for ds in datasets_per_rank for x in ds}						) == full_size
@pytest.mark.parametrize("shards_per_node"					,			[1, 2, 3]						)
def 					snake_case						(__lowercase						)       ->       List[Any]:

						'''simple docstring'''



						def gen(__lowercase						):
												for shard in shards:
																		yield from ({"i": i, "shard": shard} for i in range(17						))

						_snake_case						:     str				=    3
						_snake_case						:     List[str]				=    shards_per_node * world_size
						_snake_case						:     List[Any]				=    {"shards": [F"""shard_{shard_idx}.txt""" for shard_idx in range(__lowercase						)]}
						_snake_case						:     Optional[int]				=    IterableDataset.from_generator(__lowercase					,			gen_kwargs=__lowercase						)
						_snake_case						:     Tuple				=    len(list(__lowercase						)						)
						assert full_ds.n_shards == world_size * shards_per_node
						_snake_case						:     List[Any]				=    [
						    split_dataset_by_node(__lowercase					,			rank=__lowercase					,			world_size=__lowercase						) for rank in range(__lowercase						)
						]
						assert [ds.n_shards for ds in datasets_per_rank] == [shards_per_node] * world_size
						assert sum(len(list(__lowercase						)						) for ds in datasets_per_rank						) == full_size
						assert len({tuple(x.values()						) for ds in datasets_per_rank for x in ds}						) == full_size
def 					snake_case						()       ->       Dict:

						'''simple docstring'''



						def gen():
												return ({"i": i} for i in range(17						))

						_snake_case						:     Dict				=    2
						_snake_case						:     str				=    IterableDataset.from_generator(__lowercase						)
						_snake_case						:     Dict				=    len(list(__lowercase						)						)

						_snake_case						:     Optional[int]				=    split_dataset_by_node(__lowercase					,			rank=0					,			world_size=__lowercase						).shuffle(seed=42						)
						assert len(list(__lowercase						)						) == 1 + full_size // world_size
						with pytest.raises(__lowercase						):
												split_dataset_by_node(__lowercase					,			rank=0					,			world_size=__lowercase						).shuffle()

						_snake_case						:     Optional[int]				=    split_dataset_by_node(full_ds.shuffle(seed=42						)					,			rank=0					,			world_size=__lowercase						)
						assert len(list(__lowercase						)						) == 1 + full_size // world_size
						with pytest.raises(__lowercase						):
												split_dataset_by_node(full_ds.shuffle()					,			rank=0					,			world_size=__lowercase						)
@pytest.mark.parametrize("streaming"					,			[False, True]						)
@require_torch
@pytest.mark.skipif(os.name == "nt"					,			reason="execute_subprocess_async doesn't support windows"						)
@pytest.mark.integration
def 					snake_case						(__lowercase						)       ->       Any:

						'''simple docstring'''



						_snake_case						:     Union[str, Any]				=    2
						_snake_case						:     Optional[Any]				=    get_torch_dist_unique_port()
						_snake_case						:     Optional[int]				=    Path(__lowercase						).resolve().parent / "distributed_scripts" / "launch_torch_distributed.py"
						_snake_case						:     Any				=    F"""
        -m torch.distributed.launch
        --nproc_per_node={nproc_per_node}
        --master_port={master_port}
        {test_script}
    """.split()
						_snake_case						:     str				=    F"""
        --streaming={streaming}
    """.split()
						_snake_case						:     Optional[Any]				=    [sys.executable] + distributed_args + args
						execute_subprocess_async(__lowercase					,			env=os.environ.copy()						)

@pytest.mark.parametrize(
    "nproc_per_node, num_workers"					,			[
        (2, 2),  # each node has 2 shards and each worker has 1 shards
        (3, 2),  # each node uses all the shards but skips examples, and each worker has 2 shards
    ]					,			)
@require_torch
@pytest.mark.skipif(os.name == "nt"					,			reason="execute_subprocess_async doesn't support windows"						)
@pytest.mark.integration
def 					snake_case						(__lowercase					,			__lowercase						)       ->       Optional[Any]:

						'''simple docstring'''



						_snake_case						:     Dict				=    True
						_snake_case						:     Optional[int]				=    get_torch_dist_unique_port()
						_snake_case						:     List[str]				=    Path(__lowercase						).resolve().parent / "distributed_scripts" / "launch_torch_distributed.py"
						_snake_case						:     List[str]				=    F"""
        -m torch.distributed.launch
        --nproc_per_node={nproc_per_node}
        --master_port={master_port}
        {test_script}
    """.split()
						_snake_case						:     Tuple				=    F"""
        --streaming={streaming}
        --num_workers={num_workers}
    """.split()
						_snake_case						:     Any				=    [sys.executable] + distributed_args + args
						execute_subprocess_async(__lowercase					,			env=os.environ.copy()						)