Skip to content

[Train] [Data] Add inter-execution file shuffling to enable deterministic multi-epoch training - #59528

Merged
alexeykudinkin merged 15 commits into
ray-project:masterfrom
xinyuangui2:cp-inter-execution
Dec 18, 2025
Merged

alexeykudinkin merged 15 commits into
ray-project:masterfrom
xinyuangui2:cp-inter-execution

Conversation

@xinyuangui2

Copy link
Copy Markdown
Contributor

This PR renamed #59044

Description

This PR adds execution-aware shuffling to Ray Data's file-based datasources, enabling different file orders across different executions while maintaining determinism.

Changes

Core functionality:

  • Added execution_idx field to DataContext to track the current epoch
  • FileShuffleConfig can receive base_seed to automatically increment seed after each execution
  • If FileShuffleConfig still uses seed, the random seed is still the same for each execution.
  • Modified FileBasedDatasource.get_read_tasks() to accept execution_idx parameter and pass it through the shuffle logic

Benefits:

  • No breaking API change.
  • Each epoch produces a different but deterministic shuffle when base_seed is provided
  • Ensures that multiple datasets with the same shuffle config produce identical results within each epoch

@xinyuangui2
xinyuangui2 requested review from a team as code owners December 18, 2025 02:53

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a valuable feature for deterministic multi-epoch training by enabling execution-aware file shuffling. The implementation is well-structured, with the core logic encapsulated in FileShuffleConfig and a new _shuffle_file_metadata helper function. The use of an ExecutionCallback to manage the execution index is a clean approach. The changes are propagated consistently across various datasources. I have a couple of suggestions to improve the robustness of seed generation and to align an implementation with its type hint. Overall, this is a solid contribution.

@ray-gardener ray-gardener Bot added train Ray Train Related Issue data Ray Data-related issues labels Dec 18, 2025
@xinyuangui2 xinyuangui2 added the go add ONLY when ready to merge, run all tests label Dec 18, 2025
@alexeykudinkin
alexeykudinkin merged commit 4aa3a5b into ray-project:master Dec 18, 2025
richardliaw pushed a commit that referenced this pull request Mar 24, 2026
## Description

Adding two new expressions:
* `random()` for generating a column of uniform random number.
* `uuid()` for generating UUID (a column of unique strings)

`random()` accepts two arguments, related to reproducibility:
* `seed`: a "base" random seed. Default is `None`.
* `reseed_after_execution`: This is a flag that affects the random
number generation across different iteration of `iter_batches` (i.e.,
across different epochs in the context of epochs). See also,
#59528. It only matters when the
`seed` is set. If False, each dataset execution will generate the same
set of random number. If True, each dataset execution (i.e., training
epoch) will give a new but reproducible set of random number.

`uuid()` takes no argument.

## Related issues
Related to #55275

Related to #59662 as well. I did this PR first...so probably I could use
the same `RandomSeedConfig` in that PR.

## Additional information

[Random seed business] The actual seed sequence used to initialize the
random number generator is `[block_idx, task_idx, execution_idx,
base_seed]`. The block idx is tracked by a counter (like in
`monotonically_increasing_id()`). The task and execution indices are
coming from `TaskContext` and `DataContext`, respectively. The way I
used `execution_idx` is slightly different to what `FileShuffleConfig`
is doing, but I think all these random seed business could be
consolidated in the future (e.g., random_sample)

[Independent visit functions] Following the discussion in
#59290, there are separate
`visit_uuid()` and `visit_random` for these expressions.

Signed-off-by: Kit Lee <[email protected]>
ryanaoleary pushed a commit to ryanaoleary/ray that referenced this pull request Mar 25, 2026
## Description

Adding two new expressions:
* `random()` for generating a column of uniform random number.
* `uuid()` for generating UUID (a column of unique strings)

`random()` accepts two arguments, related to reproducibility:
* `seed`: a "base" random seed. Default is `None`.
* `reseed_after_execution`: This is a flag that affects the random
number generation across different iteration of `iter_batches` (i.e.,
across different epochs in the context of epochs). See also,
ray-project#59528. It only matters when the
`seed` is set. If False, each dataset execution will generate the same
set of random number. If True, each dataset execution (i.e., training
epoch) will give a new but reproducible set of random number.

`uuid()` takes no argument.

## Related issues
Related to ray-project#55275

Related to ray-project#59662 as well. I did this PR first...so probably I could use
the same `RandomSeedConfig` in that PR.

## Additional information

[Random seed business] The actual seed sequence used to initialize the
random number generator is `[block_idx, task_idx, execution_idx,
base_seed]`. The block idx is tracked by a counter (like in
`monotonically_increasing_id()`). The task and execution indices are
coming from `TaskContext` and `DataContext`, respectively. The way I
used `execution_idx` is slightly different to what `FileShuffleConfig`
is doing, but I think all these random seed business could be
consolidated in the future (e.g., random_sample)

[Independent visit functions] Following the discussion in
ray-project#59290, there are separate
`visit_uuid()` and `visit_random` for these expressions.

Signed-off-by: Kit Lee <[email protected]>
Lucas61000 pushed a commit to Lucas61000/ray that referenced this pull request May 15, 2026
## Description

Adding two new expressions:
* `random()` for generating a column of uniform random number.
* `uuid()` for generating UUID (a column of unique strings)

`random()` accepts two arguments, related to reproducibility:
* `seed`: a "base" random seed. Default is `None`.
* `reseed_after_execution`: This is a flag that affects the random
number generation across different iteration of `iter_batches` (i.e.,
across different epochs in the context of epochs). See also,
ray-project#59528. It only matters when the
`seed` is set. If False, each dataset execution will generate the same
set of random number. If True, each dataset execution (i.e., training
epoch) will give a new but reproducible set of random number.

`uuid()` takes no argument.

## Related issues
Related to ray-project#55275

Related to ray-project#59662 as well. I did this PR first...so probably I could use
the same `RandomSeedConfig` in that PR.

## Additional information

[Random seed business] The actual seed sequence used to initialize the
random number generator is `[block_idx, task_idx, execution_idx,
base_seed]`. The block idx is tracked by a counter (like in
`monotonically_increasing_id()`). The task and execution indices are
coming from `TaskContext` and `DataContext`, respectively. The way I
used `execution_idx` is slightly different to what `FileShuffleConfig`
is doing, but I think all these random seed business could be
consolidated in the future (e.g., random_sample)

[Independent visit functions] Following the discussion in
ray-project#59290, there are separate
`visit_uuid()` and `visit_random` for these expressions.

Signed-off-by: Kit Lee <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data Ray Data-related issues go add ONLY when ready to merge, run all tests train Ray Train Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants