Returns a function that generates a tf.data.Dataset object. Note: batch(is_training=True) requires sufficient number of examples per host. When your data is too small, you should add `ds = ds.repeat()` before your batch. Args: global_batch_size: The global physical batch si
(
global_batch_size: int,
*,
is_training: bool,
pad_example_fn: PadExampleFn,
global_logical_batch_size: Optional[int] = None,
logical_feed_indices: Optional[Sequence[int]] = None,
prefetch_buffer_size: Optional[int] = None,
post_batch_processor: Optional[ConfigOr[DatasetToDatasetFn]] = None,
repeat: Optional[int] = None,
)
| 837 | |
| 838 | |
| 839 | def batch( |
| 840 | global_batch_size: int, |
| 841 | *, |
| 842 | is_training: bool, |
| 843 | pad_example_fn: PadExampleFn, |
| 844 | global_logical_batch_size: Optional[int] = None, |
| 845 | logical_feed_indices: Optional[Sequence[int]] = None, |
| 846 | prefetch_buffer_size: Optional[int] = None, |
| 847 | post_batch_processor: Optional[ConfigOr[DatasetToDatasetFn]] = None, |
| 848 | repeat: Optional[int] = None, |
| 849 | ) -> DatasetToDatasetFn: |
| 850 | """Returns a function that generates a tf.data.Dataset object. |
| 851 | |
| 852 | Note: batch(is_training=True) requires sufficient number of examples |
| 853 | per host. When your data is too small, you should add `ds = ds.repeat()` |
| 854 | before your batch. |
| 855 | |
| 856 | Args: |
| 857 | global_batch_size: The global physical batch size across all replicas. |
| 858 | Must be divisible by the number of JAX processes and devices. |
| 859 | is_training: Whether the examples are used for training. |
| 860 | This parameter will be passed through to all input sources. |
| 861 | pad_example_fn: Create padded examples with the given function. |
| 862 | global_logical_batch_size: The global size of the logical batch, i.e. the physical batch |
| 863 | subset corresponding to elements drawn from the input dataset. |
| 864 | If None, assumed to be equal to the global physical batch size. |
| 865 | logical_feed_indices: The JAX process indices corresponding to feeds that provide logical |
| 866 | data after batching. Process indices that are not in this set will produce |
| 867 | physical-only (padded) batches, with no elements drawn from the input dataset. |
| 868 | If None, assumed to be the set of all JAX training processes. |
| 869 | prefetch_buffer_size: Size of prefetch buffer. This allows later |
| 870 | elements to be prepared while the current element is being |
| 871 | processed. If not set, `tf.data.experimental.AUTOTUNE` is used. |
| 872 | post_batch_processor: An optional processor (or config instantiating to a processor) that |
| 873 | applies batch-wise processing functions. |
| 874 | repeat: The number of times to repeat the batches from the dataset. |
| 875 | If None, repeat indefinitely if is_training=True and do not repeat otherwise. |
| 876 | Otherwise must be a positive integer. |
| 877 | |
| 878 | Returns: |
| 879 | A DatasetToDataset fn. |
| 880 | |
| 881 | Raises: |
| 882 | ValueError: If |
| 883 | - global_batch_size is not divisible by the number of JAX processes, or |
| 884 | - repeat is not a positive integer, or |
| 885 | - global_logical_batch_size and logical_feed_indices are not both set or both unset, or |
| 886 | - global_logical_batch_size is not divisible by the number of logical_feed_indices. |
| 887 | """ |
| 888 | num_data_feeds = jax.process_count() |
| 889 | if global_batch_size % num_data_feeds != 0: |
| 890 | raise ValueError( |
| 891 | f"global_batch_size ({global_batch_size}) must be divisible by " |
| 892 | f"number of JAX processes (data feeds) ({num_data_feeds})." |
| 893 | ) |
| 894 | per_feed_batch_size = global_batch_size // num_data_feeds |
| 895 | |
| 896 | if repeat is not None and (not isinstance(repeat, int) or repeat <= 0): |
no outgoing calls