Skip to content

Commit b5d0fff

Browse files
wangkuiyichanglan
authored andcommitted
Docstring input_tf_data
GitOrigin-RevId: d66bdc9
1 parent 6367b36 commit b5d0fff

1 file changed

Lines changed: 70 additions & 2 deletions

File tree

axlearn/common/input_tf_data.py

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,72 @@
66
# Licensed under the Apache License, Version 2.0 (the "License").
77

88
# 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+
"""
1075

1176
from collections.abc import Mapping, Sequence
1277
from typing import Any, Callable, Optional, Union
@@ -17,7 +82,10 @@
1782
import tensorflow_datasets as tfds
1883

1984
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
2189
# pytype: disable=import-error
2290
import tensorflow_io as tfio # pylint: disable=unused-import
2391
except ImportError:

0 commit comments

Comments
 (0)