# Path: init2winit/dataset_lib/data_utils.py
# def iterator_as_numpy(iterator):
# def image_iterator(data,
#                    rescale,
#                    output_shape,
#                    is_one_hot,
#                    autoencoder,
#                    shuffle_rng=None,
#                    augment_fn=None,
#                    include_example_keys=False):
# def maybe_pad_batch(batch,
#                     desired_batch_size,
#                     data_format=None,
#                     mask_key=None):
#   def zero_pad(ar, pad_axis):
# def shard(batch, n_devices=None):
#   def _shard_array(array):
# def tf_to_numpy(tfds_data):
# 
# Path: init2winit/dataset_lib/image_preprocessing.py
# def _crop(key, image, hps):
# def crop(key, images, hps):
# def mixup_general(key,
#                   *args_to_mix,
#                   alpha=0.1,
#                   fold_in=None,
#                   n=2,
#                   **kwargs_to_mix):
#   def mix(batch):
#     def mul(a, b):  # B * BHWC -> B111 * BHWC
# def mixup(key, alpha, images, labels):
# def augment_cifar10(key, images, labels, hps):
# def identity_augment(key, images, labels):
# 
# Path: init2winit/dataset_lib/data_utils.py
# def iterator_as_numpy(iterator):
# def image_iterator(data,
#                    rescale,
#                    output_shape,
#                    is_one_hot,
#                    autoencoder,
#                    shuffle_rng=None,
#                    augment_fn=None,
#                    include_example_keys=False):
# def maybe_pad_batch(batch,
#                     desired_batch_size,
#                     data_format=None,
#                     mask_key=None):
#   def zero_pad(ar, pad_axis):
# def shard(batch, n_devices=None):
#   def _shard_array(array):
# def tf_to_numpy(tfds_data):


import functools
import itertools
import jax.numpy as jnp
import jax.random
import numpy as np
import tensorflow.compat.v2 as tf
import tensorflow_datasets as tfds
from absl import logging
from init2winit.dataset_lib import data_utils
from init2winit.dataset_lib import image_preprocessing
from init2winit.dataset_lib.data_utils import Dataset
from ml_collections.config_dict import config_dict
  Returns:
    train_iterator_fn, valid_epoch, test_epoch: three generators.
  """
  augment_fn = jax.jit(augment_fn)

  if include_example_keys:
    read_config = tfds.ReadConfig(add_tfds_id=True)
  else:
    read_config = None

  train_ds = tfds.load(
      dataset_name,
      split=tfds.core.ReadInstruction(
          'train', from_=0, to=train_size, unit='abs'))

  data_train = train_ds.shuffle(
      train_size,
      reshuffle_each_iteration=True,
      seed=int(jax.random.randint(shuffle_rng, [1], 0, 1000)))
  data_train = data_train.repeat()
  data_train = data_train.batch(per_host_batch_size)

  if valid_size > 0:
    valid_ds = tfds.load(
        dataset_name,
        read_config=read_config,
        split=tfds.core.ReadInstruction(
            'train', from_=train_size, to=train_size + valid_size, unit='abs'))
    data_valid = valid_ds.batch(valid_size)
  else:

# Path: init2winit/dataset_lib/data_utils.py
# def iterator_as_numpy(iterator):
# def image_iterator(data,
#                    rescale,
#                    output_shape,
#                    is_one_hot,
#                    autoencoder,
#                    shuffle_rng=None,
#                    augment_fn=None,
#                    include_example_keys=False):
# def maybe_pad_batch(batch,
#                     desired_batch_size,
#                     data_format=None,
#                     mask_key=None):
#   def zero_pad(ar, pad_axis):
# def shard(batch, n_devices=None):
#   def _shard_array(array):
# def tf_to_numpy(tfds_data):
# 
# Path: init2winit/dataset_lib/image_preprocessing.py
# def _crop(key, image, hps):
# def crop(key, images, hps):
# def mixup_general(key,
#                   *args_to_mix,
#                   alpha=0.1,
#                   fold_in=None,
#                   n=2,
#                   **kwargs_to_mix):
#   def mix(batch):
#     def mul(a, b):  # B * BHWC -> B111 * BHWC
# def mixup(key, alpha, images, labels):
# def augment_cifar10(key, images, labels, hps):
# def identity_augment(key, images, labels):
# 
# Path: init2winit/dataset_lib/data_utils.py
# def iterator_as_numpy(iterator):
# def image_iterator(data,
#                    rescale,
#                    output_shape,
#                    is_one_hot,
#                    autoencoder,
#                    shuffle_rng=None,
#                    augment_fn=None,
#                    include_example_keys=False):
# def maybe_pad_batch(batch,
#                     desired_batch_size,
#                     data_format=None,
#                     mask_key=None):
#   def zero_pad(ar, pad_axis):
# def shard(batch, n_devices=None):
#   def _shard_array(array):
# def tf_to_numpy(tfds_data):


# Path: init2winit/dataset_lib/small_image_datasets.py
import functools
import itertools
import jax.numpy as jnp
import jax.random
import numpy as np
import tensorflow.compat.v2 as tf
import tensorflow_datasets as tfds
from absl import logging
from init2winit.dataset_lib import data_utils
from init2winit.dataset_lib import image_preprocessing
from init2winit.dataset_lib.data_utils import Dataset
from ml_collections.config_dict import config_dict

  Returns:
    train_iterator_fn, valid_epoch, test_epoch: three generators.
  """
  augment_fn = jax.jit(augment_fn)

  if include_example_keys:
    read_config = tfds.ReadConfig(add_tfds_id=True)
  else:
    read_config = None

  train_ds = tfds.load(
      dataset_name,
      split=tfds.core.ReadInstruction(
          'train', from_=0, to=train_size, unit='abs'))

  data_train = train_ds.shuffle(
      train_size,
      reshuffle_each_iteration=True,
      seed=int(jax.random.randint(shuffle_rng, [1], 0, 1000)))
  data_train = data_train.repeat()
  data_train = data_train.batch(per_host_batch_size)

  if valid_size > 0:
    valid_ds = tfds.load(
        dataset_name,
        read_config=read_config,
        split=tfds.core.ReadInstruction(
            'train', from_=train_size, to=train_size + valid_size, unit='abs'))
    data_valid = valid_ds.batch(valid_size)
  else:

    valid_ds = tf.data.Dataset.from_tensor_slices([])