






"""simple docstring"""



def 						_SCREAMING_SNAKE_CASE (    __snake_case			:  bytes    ):





  '''simple docstring'''





  return "".join([hex(__snake_case    )[2:].zfill(2    ).upper() for byte in list(__snake_case    )]    )
def 						_SCREAMING_SNAKE_CASE (    __snake_case			:  str    ):





  '''simple docstring'''





  if (len(__snake_case    ) % 2) != 0:
    raise ValueError(
        'Base16 encoded data is invalid:\nData does not have an even number of hex digits.'    )
  # Check the character set - the standard base16 alphabet
  # is uppercase according to RFC3548 section 6
  if not set(__snake_case    ) <= set('0123456789ABCDEF'    ):
    raise ValueError(
        'Base16 encoded data is invalid:\nData is not uppercase hex or it contains invalid characters.'    )
  # For every two hexadecimal digits (= a byte), turn it into an integer.
  # Then, string the result together into bytes, and return it.
  return bytes(int(data[i] + data[i + 1]		,					16    ) for i in range(0		,					len(__snake_case    )		,					2    )    )


if __name__ == "__main__":
    import doctest

    doctest.testmod()










"""simple docstring"""



import os
from argparse import ArgumentParser
from typing import List

import torch.utils.data

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


_UpperCamelCase		:  List[Any]						=		4
_UpperCamelCase		:  Optional[Any]						=		3

class        a     (   a_	):
    pass


def 						_SCREAMING_SNAKE_CASE (    __snake_case			:  List[str]    ):





  '''simple docstring'''





  for shard in shards:
    for i in range(__snake_case    ):
      yield {"i": i, "shard": shard}
def 						_SCREAMING_SNAKE_CASE (    ):





  '''simple docstring'''





  lowercase							    =       int(os.environ['RANK']    )
  lowercase							    =       int(os.environ['WORLD_SIZE']    )

  lowercase							    =       ArgumentParser()
  parser.add_argument('--streaming'		,					type=__snake_case    )
  parser.add_argument('--local_rank'		,					type=__snake_case    )
  parser.add_argument('--num_workers'		,					type=__snake_case		,					default=0    )
  lowercase							    =       parser.parse_args()
  lowercase							    =       args.streaming
  lowercase							    =       args.num_workers

  lowercase							    =       {'shards': [f'shard_{shard_idx}' for shard_idx in range(__snake_case    )]}
  lowercase							    =       IterableDataset.from_generator(__snake_case		,					gen_kwargs=__snake_case    )
  if not streaming:
    lowercase							    =       Dataset.from_list(list(__snake_case    )    )

  lowercase							    =       split_dataset_by_node(__snake_case		,					rank=__snake_case		,					world_size=__snake_case    )
  lowercase							    =       torch.utils.data.DataLoader(__snake_case		,					num_workers=__snake_case    )

  lowercase							    =       NUM_SHARDS * NUM_ITEMS_PER_SHARD
  lowercase							    =       full_size // world_size
  expected_local_size += int(rank < (full_size % world_size)    )

  lowercase							    =       sum(1 for _ in dataloader    )
  if local_size != expected_local_size:
    raise FailedTestError(f'local_size {local_size} != expected_local_size {expected_local_size}'    )


if __name__ == "__main__":
    main()


