Skip to content

[Data] vary randomize_block_order and random_shuffle seed across executions - #61426

Closed
matthewdeng wants to merge 3 commits into
ray-project:masterfrom
matthewdeng:block-order-epoch
Closed

matthewdeng wants to merge 3 commits into
ray-project:masterfrom
matthewdeng:block-order-epoch

Conversation

@matthewdeng

Copy link
Copy Markdown
Contributor

Description

randomize_block_order(seed=X) produces the same block ordering every epoch because it always seeds with the fixed user seed.

This varies the seed per epoch by combining it with DataContext._execution_idx, matching the existing pattern in FileShuffleConfig.get_seed().

Extracts the shared formula into make_epoch_seed() in util.py.

@matthewdeng
matthewdeng requested a review from a team as a code owner March 2, 2026 19:29

@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 modifies randomize_block_order to use a different seed for each epoch, ensuring that block ordering varies across epochs even when a fixed seed is provided. This is achieved by combining the user-provided seed with the execution index. The logic for generating this per-epoch seed has been extracted into a new utility function, make_epoch_seed, which is also used to refactor FileShuffleConfig, improving code reuse. The changes are well-tested. I have one minor suggestion to improve code style by repositioning some imports.

Comment on lines +38 to +39
from ray.data._internal.util import make_epoch_seed
from ray.data.context import DataContext

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.

medium

For improved readability and consistency, it's generally better to place imports at the top of their scope. Consider moving these imports to the top of the fn function, alongside import random. This would make the code cleaner and align with common Python style practices.

@wingkitlee0

Copy link
Copy Markdown
Contributor

Hi @matthewdeng , I have this PR doing this approved and pending to be merged: #59662

@ray-gardener ray-gardener Bot added the data Ray Data-related issues label Mar 3, 2026
Signed-off-by: Matthew Deng <[email protected]>
Signed-off-by: Matthew Deng <[email protected]>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

elif self.reseed_after_execution:
# Modulo ensures the result is in valid NumPy seed range [0, 2**32 - 1].
return hash((self.seed, execution_idx)) % (2**32)
return make_epoch_seed(self.seed, execution_idx)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

FileShuffleConfig.get_seed formula silently changed from hash to addition

High Severity

FileShuffleConfig.get_seed() previously computed hash((self.seed, execution_idx)) % (2**32), which produces well-distributed, uncorrelated seeds across epochs. The refactoring replaced this with make_epoch_seed, which computes (seed + execution_idx) % (2**32) — a completely different formula. This silently changes the seed values returned for all existing users of FileShuffleConfig with reseed_after_execution=True, breaking reproducibility and producing weakly correlated sequential seeds (e.g., 42, 43, 44…) instead of hashed ones.

Additional Locations (1)

Fix in Cursor Fix in Web

return None
elif self.reseed_after_execution:
# Modulo ensures the result is in valid NumPy seed range [0, 2**32 - 1].
return hash((self.seed, execution_idx)) % (2**32)

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.

Note that this now changes the behavior from using a hash to just incrementing.

@matthewdeng matthewdeng changed the title [Data] vary randomize_block_order seed across executions [Data] vary randomize_block_order and random_shuffle seed across executions Mar 4, 2026
@matthewdeng

Copy link
Copy Markdown
Contributor Author

@wingkitlee0 oh nice, I'll close this one, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

data Ray Data-related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants