Skip to content

[Train] [Data] Add epoch-aware file shuffling to enable deterministic multi-epoch training - #59044

Merged
raulchen merged 28 commits into
ray-project:masterfrom
xinyuangui2:dt_context_epoch_idx
Dec 4, 2025
Merged

raulchen merged 28 commits into
ray-project:masterfrom
xinyuangui2:dt_context_epoch_idx

Conversation

@xinyuangui2

@xinyuangui2 xinyuangui2 commented Nov 27, 2025 •

Copy link
Copy Markdown
Contributor

Description

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

Changes

Core functionality:

  • Added epoch_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 epoch.
  • Modified FileBasedDatasource.get_read_tasks() to accept epoch_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 changed the title Dt context epoch idx [Train] [Data] Add epoch-aware file shuffling to enable deterministic multi-epoch training Nov 30, 2025
@xinyuangui2
xinyuangui2 marked this pull request as ready for review November 30, 2025 21:08
@xinyuangui2
xinyuangui2 requested review from a team as code owners November 30, 2025 21:08
Comment thread python/ray/data/tests/test_parquet.py Resolved
@ray-gardener ray-gardener Bot added the data Ray Data-related issues label Dec 1, 2025

@justinvyu justinvyu 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.

Cool stuff. Wondering if we need to update the get_read_tasks internal interface everywhere.

Comment on lines +71 to +72
EXECUTION_CALLBACKS_CONFIG_KEY,
[IssueDetectionExecutionCallback(), EpochIdxUpdateCallback()],

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.

Users can clear this list manually (and Ray Data emits some messages telling users how to do that if they want to disable the issue detectors), which will then break things. Should we always include this callback regardless of what the user set?

@justinvyu justinvyu Dec 3, 2025 •

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.

Oh wait nvm we don't ever tell the user to clear this list -- I was thinking of this:

"To disable issue detection, run DataContext.get_current().issue_detectors_config.detectors = []."

But it's still possible for users to do this:

ray.data.DataContext.get_current().set_config("execution_callbacks", [])

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.

this is fine. if users intentionally try to break things. there is nothing we can do.

Comment thread python/ray/data/context.py Resolved
Comment thread python/ray/data/read_api.py Resolved
self,
parallelism: int,
per_task_row_limit: Optional[int] = None,
epoch_idx: int = 0,

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.

does datasource not have access to the dataset's context? and then just use _epoch_idx

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

DataSource is one parameter to create Dataset.

@wrap_auto_init
def read_datasource(
datasource: Datasource,
*,
parallelism: int = -1,
num_cpus: Optional[float] = None,
num_gpus: Optional[float] = None,
memory: Optional[float] = None,
ray_remote_args: Dict[str, Any] = None,
concurrency: Optional[int] = None,
override_num_blocks: Optional[int] = None,
**read_args,
) -> Dataset:

So DataSource doesn't have access to dataset's context

@raulchen raulchen 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.

Looks good overall. Only a few small comments

Comment thread python/ray/data/tests/test_parquet.py Resolved
Comment thread python/ray/data/context.py Resolved
@xinyuangui2

Copy link
Copy Markdown
Contributor Author

Cool stuff. Wondering if we need to update the get_read_tasks internal interface everywhere.

I think so since the calling function is calling Datasource.get_read_task

reseed_after_epoch: If True, the random seed is ``seed + epoch_idx``, resulting
in different shuffling orders across epochs. If False, the random seed is
constantly ``seed``, resulting in the same shuffling order across epochs.
Only takes effect when ``seed`` is not None. Defaults to True.

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.

it should probably be default to false for backward compatibility.

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.

I think reseeding is a good default. The current fixed seed behavior is not a common pattern and is probably unexpected for them already.

Also, I think most users still use shuffle="files" which defaults to a random seed per epoch anyways.

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.

makes sense

reseed_after_epoch: If True, the random seed is ``seed + epoch_idx``, resulting
in different shuffling orders across epochs. If False, the random seed is
constantly ``seed``, resulting in the same shuffling order across epochs.
Only takes effect when ``seed`` is not None. Defaults to True.

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.

I think reseeding is a good default. The current fixed seed behavior is not a common pattern and is probably unexpected for them already.

Also, I think most users still use shuffle="files" which defaults to a random seed per epoch anyways.

Signed-off-by: xgui <[email protected]>
@raulchen
raulchen enabled auto-merge (squash) December 4, 2025 21:18
@github-actions github-actions Bot added the go add ONLY when ready to merge, run all tests label Dec 4, 2025
@github-actions
github-actions Bot disabled auto-merge December 4, 2025 22:09
Signed-off-by: xgui <[email protected]>
@raulchen
raulchen merged commit 5a757b8 into ray-project:master Dec 4, 2025
alexeykudinkin added a commit that referenced this pull request Dec 10, 2025
…ministic multi-epoch training (#59044)"

This reverts commit 5a757b8.

Signed-off-by: Alexey Kudinkin <[email protected]>
richardliaw pushed a commit that referenced this pull request Dec 11, 2025
…ministic multi-epoch training (#59044)" (#59364)

> Thank you for contributing to Ray! 🚀
> Please review the [Ray Contribution
Guide](https://docs.ray.io/en/master/ray-contribute/getting-involved.html)
before opening a pull request.

> ⚠️ Remove these instructions before submitting your PR.

> 💡 Tip: Mark as draft if you want early feedback, or ready for review
when it's complete.

## Description

Reverting to unblock release.

## Related issues
> Link related issues: "Fixes #1234", "Closes #1234", or "Related to
#1234".

## Additional information
> Optional: Add implementation details, API changes, usage examples,
screenshots, etc.

Signed-off-by: Alexey Kudinkin <[email protected]>
alexeykudinkin pushed a commit that referenced this pull request Dec 18, 2025
…stic multi-epoch training (#59528)

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

---------

Signed-off-by: Xinyuan <[email protected]>
Signed-off-by: xgui <[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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants