|
6 | 6 | # Licensed under the Apache License, Version 2.0 (the "License"). |
7 | 7 |
|
8 | 8 | # pylint: disable=too-many-lines |
9 | | -"""Input generator based on tf.data.""" |
| 9 | +"""Input generator based on tf.data. |
| 10 | +
|
| 11 | +This module provides building blocks for constructing input pipelines using tf.data.Dataset. |
| 12 | +
|
| 13 | +It defines two types: |
| 14 | +
|
| 15 | +1. BuildDatasetFn() -> dataset: A function to create a tf.data.Dataset instance. |
| 16 | +2. DatasetToDatasetFn(dataset) -> dataset: A function to create a tf.data.Dataset instance |
| 17 | + from the given dataset. |
| 18 | +
|
| 19 | +
|
| 20 | +The input pipeline is composed of three stages. |
| 21 | +
|
| 22 | +1. Source: BuildDatasetFn - Creates raw tf.data.Dataset |
| 23 | + Examples: tfds_dataset(), tfrecord_dataset(), sample_from_datasets() |
| 24 | +
|
| 25 | +2. Processor: DatasetToDatasetFn - Transforms examples |
| 26 | + Examples: rekey(), shuffle(), select_fields(), chain() |
| 27 | +
|
| 28 | +3. Batcher: DatasetToDatasetFn - Batches examples |
| 29 | + Examples: batch(), per_feed_batch(), pack_to_batch() |
| 30 | +
|
| 31 | +
|
| 32 | +The class Input composes these three stages into a single module. |
| 33 | +
|
| 34 | + # Configure the input pipeline |
| 35 | + input_cfg = Input.default_config().set( |
| 36 | + is_training=True, |
| 37 | + source=config_for_function(tfds_dataset).set( |
| 38 | + dataset_name="mnist", |
| 39 | + split="train", |
| 40 | + train_shuffle_buffer_size=10000, |
| 41 | + ), |
| 42 | + processor=config_for_function(chain).set( |
| 43 | + config_for_function(rekey).set(key_map={"x": "image", "y": "label"}), |
| 44 | + config_for_function(shuffle).set(shuffle_buffer_size=1000), |
| 45 | + ), |
| 46 | + batcher=config_for_function(batch).set( |
| 47 | + global_batch_size=256, |
| 48 | + pad_example_fn=default_pad_example_fn, |
| 49 | + ), |
| 50 | + ) |
| 51 | +
|
| 52 | + # Instantiate and get dataset |
| 53 | + input_module = input_cfg.instantiate() |
| 54 | + dataset = input_module.dataset() |
| 55 | +
|
| 56 | + # Iterate over batches |
| 57 | + for batch in dataset: |
| 58 | + train_step(batch) |
| 59 | +
|
| 60 | +
|
| 61 | +The module is designed for multi-host JAX training: |
| 62 | +
|
| 63 | +- Automatic data sharding across processes via tfds_read_config() |
| 64 | +- Evaluation padding ensures consistent batch counts across hosts |
| 65 | +- Physical/logical batching supports advanced dispatch patterns |
| 66 | +- All dataset operations are multi-host aware (jax.process_count/index) |
| 67 | +
|
| 68 | +
|
| 69 | +See Also: |
| 70 | +
|
| 71 | +- input_base.Input: Base class |
| 72 | +- input_grain.py: PyGrain-based alternative (preferred for new code) |
| 73 | +
|
| 74 | +""" |
10 | 75 |
|
11 | 76 | from collections.abc import Mapping, Sequence |
12 | 77 | from typing import Any, Callable, Optional, Union |
|
17 | 82 | import tensorflow_datasets as tfds |
18 | 83 |
|
19 | 84 | try: |
20 | | - # Necessary for S3 access. See, e.g: https://github.com/tensorflow/tensorflow/issues/51583 |
| 85 | + # Import tensorflow_io for its side effects of supporting S3. The following will error |
| 86 | + # if tensorflow_io is not installed. |
| 87 | + # ds = tf.data.TFRecordDataset("s3://my-bucket/data.tfrecord") |
| 88 | + # For more details, see https://github.com/tensorflow/tensorflow/issues/51583 |
21 | 89 | # pytype: disable=import-error |
22 | 90 | import tensorflow_io as tfio # pylint: disable=unused-import |
23 | 91 | except ImportError: |
|
0 commit comments