






'''simple docstring'''







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 _UpperCamelCase	(					)							->		Dict:


      '''simple docstring'''



      UpperCamelCase__  =    Dataset.from_dict({"i": range(17   )}   )
      UpperCamelCase__  =    len(_SCREAMING_SNAKE_CASE   )
      UpperCamelCase__  =    3
      UpperCamelCase__  =    [
          split_dataset_by_node(_SCREAMING_SNAKE_CASE				,      rank=_SCREAMING_SNAKE_CASE				,      world_size=_SCREAMING_SNAKE_CASE   ) for rank in range(_SCREAMING_SNAKE_CASE   )
      ]
      assert sum(len(_SCREAMING_SNAKE_CASE   ) 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 _UpperCamelCase	(					)							->		Optional[Any]:


      '''simple docstring'''



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

      UpperCamelCase__  =    3
      UpperCamelCase__  =    IterableDataset.from_generator(_SCREAMING_SNAKE_CASE   )
      UpperCamelCase__  =    len(list(_SCREAMING_SNAKE_CASE   )   )
      UpperCamelCase__  =    [
          split_dataset_by_node(_SCREAMING_SNAKE_CASE				,      rank=_SCREAMING_SNAKE_CASE				,      world_size=_SCREAMING_SNAKE_CASE   ) for rank in range(_SCREAMING_SNAKE_CASE   )
      ]
      assert sum(len(list(_SCREAMING_SNAKE_CASE   )   ) 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 _UpperCamelCase	(					__A   )							->		Optional[Any]:


      '''simple docstring'''



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

      UpperCamelCase__  =    3
      UpperCamelCase__  =    shards_per_node * world_size
      UpperCamelCase__  =    {"shards": [F'''shard_{shard_idx}.txt''' for shard_idx in range(_SCREAMING_SNAKE_CASE   )]}
      UpperCamelCase__  =    IterableDataset.from_generator(_SCREAMING_SNAKE_CASE				,      gen_kwargs=_SCREAMING_SNAKE_CASE   )
      UpperCamelCase__  =    len(list(_SCREAMING_SNAKE_CASE   )   )
      assert full_ds.n_shards == world_size * shards_per_node
      UpperCamelCase__  =    [
          split_dataset_by_node(_SCREAMING_SNAKE_CASE				,      rank=_SCREAMING_SNAKE_CASE				,      world_size=_SCREAMING_SNAKE_CASE   ) for rank in range(_SCREAMING_SNAKE_CASE   )
      ]
      assert [ds.n_shards for ds in datasets_per_rank] == [shards_per_node] * world_size
      assert sum(len(list(_SCREAMING_SNAKE_CASE   )   ) 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 _UpperCamelCase	(					)							->		List[str]:


      '''simple docstring'''



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

      UpperCamelCase__  =    2
      UpperCamelCase__  =    IterableDataset.from_generator(_SCREAMING_SNAKE_CASE   )
      UpperCamelCase__  =    len(list(_SCREAMING_SNAKE_CASE   )   )

      UpperCamelCase__  =    split_dataset_by_node(_SCREAMING_SNAKE_CASE				,      rank=0				,      world_size=_SCREAMING_SNAKE_CASE   ).shuffle(seed=42   )
      assert len(list(_SCREAMING_SNAKE_CASE   )   ) == 1 + full_size // world_size
      with pytest.raises(_SCREAMING_SNAKE_CASE   ):
            split_dataset_by_node(_SCREAMING_SNAKE_CASE				,      rank=0				,      world_size=_SCREAMING_SNAKE_CASE   ).shuffle()

      UpperCamelCase__  =    split_dataset_by_node(full_ds.shuffle(seed=42   )				,      rank=0				,      world_size=_SCREAMING_SNAKE_CASE   )
      assert len(list(_SCREAMING_SNAKE_CASE   )   ) == 1 + full_size // world_size
      with pytest.raises(_SCREAMING_SNAKE_CASE   ):
            split_dataset_by_node(full_ds.shuffle()				,      rank=0				,      world_size=_SCREAMING_SNAKE_CASE   )







@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 _UpperCamelCase	(					__A   )							->		Union[str, Any]:


      '''simple docstring'''



      UpperCamelCase__  =    2
      UpperCamelCase__  =    get_torch_dist_unique_port()
      UpperCamelCase__  =    Path(_SCREAMING_SNAKE_CASE   ).resolve().parent / "distributed_scripts" / "launch_torch_distributed.py"
      UpperCamelCase__  =    F'''
        -m torch.distributed.launch
        --nproc_per_node={nproc_per_node}
        --master_port={master_port}
        {test_script}
    '''.split()
      UpperCamelCase__  =    F'''
        --streaming={streaming}
    '''.split()
      UpperCamelCase__  =    [sys.executable] + distributed_args + args
      execute_subprocess_async(_SCREAMING_SNAKE_CASE				,      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 _UpperCamelCase	(					__A				,      __A   )							->		Union[str, Any]:


      '''simple docstring'''



      UpperCamelCase__  =    True
      UpperCamelCase__  =    get_torch_dist_unique_port()
      UpperCamelCase__  =    Path(_SCREAMING_SNAKE_CASE   ).resolve().parent / "distributed_scripts" / "launch_torch_distributed.py"
      UpperCamelCase__  =    F'''
        -m torch.distributed.launch
        --nproc_per_node={nproc_per_node}
        --master_port={master_port}
        {test_script}
    '''.split()
      UpperCamelCase__  =    F'''
        --streaming={streaming}
        --num_workers={num_workers}
    '''.split()
      UpperCamelCase__  =    [sys.executable] + distributed_args + args
      execute_subprocess_async(_SCREAMING_SNAKE_CASE				,      env=os.environ.copy()   )









'''simple docstring'''







import math







def _UpperCamelCase	(					__A   )							->		bool:


      '''simple docstring'''



      if 1 < number < 4:
            # 2 and 3 are primes
            return True
      elif number < 2 or number % 2 == 0 or number % 3 == 0:
            # Negatives, 0, 1, all even numbers, all multiples of 3 are not primes
            return False

      # All primes number are in format of 6k +/- 1
      for i in range(5				,      int(math.sqrt(__A   ) + 1   )				,      6   ):
            if number % i == 0 or number % (i + 2) == 0:
                  return False
      return True







def _UpperCamelCase	(					__A = 1000				,      __A = 1000   )							->		int:


      '''simple docstring'''



      UpperCamelCase__  =    [0, 0, 0]  # length, a, b
      for a in range((a_limit * -1) + 1				,      __A   ):
            for b in range(2				,      __A   ):
                  if is_prime(__A   ):
                        UpperCamelCase__  =    0
                        UpperCamelCase__  =    0
                        while is_prime((n**2) + (a * n) + b   ):
                              count += 1
                              n += 1
                        if count > longest[0]:
                              UpperCamelCase__  =    [count, a, b]
      UpperCamelCase__  =    longest[1] * longest[2]
      return ans


if __name__ == "__main__":
  print(solution(1_0_0_0, 1_0_0_0))





