[RLlib] Fix wrong assert variable in _update_env_seed_if_necessary#61823
Conversation
Signed-off-by: nathon-lee <leejianwoo@gmail.com>
There was a problem hiding this comment.
Code Review
This pull request correctly fixes a bug in _update_env_seed_if_necessary by using vector_idx instead of worker_idx in the assertion to prevent seed collisions. The accompanying regression tests are well-designed, covering both the success case for the fix and the failure case for out-of-bounds indices. I have one suggestion to improve the maintainability of the new tests by replacing magic numbers with a named constant.
| def test_update_env_seed_accepts_max_worker_idx_with_valid_vector_idx(self): | ||
| env = SeedRecordingEnv() | ||
|
|
||
| _update_env_seed_if_necessary(env, seed=7, worker_idx=1000, vector_idx=999) | ||
|
|
||
| self.assertEqual(env.last_seed, 1000 * 1000 + 999 + 7) | ||
|
|
||
| def test_update_env_seed_rejects_too_large_vector_idx(self): | ||
| env = SeedRecordingEnv() | ||
|
|
||
| with self.assertRaisesRegex( | ||
| AssertionError, "Too many envs per worker. Random seeds may collide." | ||
| ): | ||
| _update_env_seed_if_necessary( | ||
| env, seed=7, worker_idx=0, vector_idx=1000 | ||
| ) |
There was a problem hiding this comment.
The new tests use the magic number 1000, which corresponds to max_num_envs_per_env_runner in _update_env_seed_if_necessary. This makes the tests brittle if the constant in the implementation ever changes.
To improve maintainability, consider defining this value as a constant and using it in these tests. This makes the intent clearer and simplifies future updates.
Here's a suggestion that refactors both tests to use a local constant:
def test_update_env_seed_accepts_max_worker_idx_with_valid_vector_idx(self):
# This value is hardcoded in `_update_env_seed_if_necessary`.
max_num_envs_per_env_runner = 1000
env = SeedRecordingEnv()
worker_idx = max_num_envs_per_env_runner
vector_idx = max_num_envs_per_env_runner - 1
seed = 7
_update_env_seed_if_necessary(
env, seed=seed, worker_idx=worker_idx, vector_idx=vector_idx
)
self.assertEqual(
env.last_seed,
worker_idx * max_num_envs_per_env_runner + vector_idx + seed,
)
def test_update_env_seed_rejects_too_large_vector_idx(self):
# This value is hardcoded in `_update_env_seed_if_necessary`.
max_num_envs_per_env_runner = 1000
env = SeedRecordingEnv()
with self.assertRaisesRegex(
AssertionError, "Too many envs per worker. Random seeds may collide."
):
_update_env_seed_if_necessary(
env, seed=7, worker_idx=0, vector_idx=max_num_envs_per_env_runner
)Signed-off-by: nathon-lee <leejianwoo@gmail.com>
pseudo-rnd-thoughts
left a comment
There was a problem hiding this comment.
Personally, I think that this assert shouldn't exist as we starting to work with users that certainly have more than a thousand environments per env-runner.
Its easier just not to have this assert and let the user get an error if the sub-environment doesn't exist anymore.
@ArturNiederfahrenhorst
Signed-off-by: nathon-lee <leejianwoo@gmail.com>
Good point — I removed the assert instead of switching it to |
|
Agreed. The assertion does not make sense anymore. |
|
@ArturNiederfahrenhorst @pseudo-rnd-thoughts Thanks! I’ve pushed the fix. |
|
This pull request has been automatically marked as stale because it has not had You can always ask for help on our discussion forum or Ray's public slack channel. If you'd like to keep this open, just leave any comment, and the stale label will be removed. |
| @@ -132,9 +132,6 @@ def _update_env_seed_if_necessary( | |||
| # A single RL job is unlikely to have more than 10K | |||
| # rollout workers. | |||
| max_num_envs_per_env_runner: int = 1000 | |||
There was a problem hiding this comment.
This should be remove as well now along with the comment above
| self.assertTrue(len(unroll_ids_2) > 1) | ||
| ev.stop() | ||
|
|
||
| def test_update_env_seed_accepts_max_worker_idx_with_valid_vector_idx(self): |
There was a problem hiding this comment.
Merge these two tests together
def test_update_env_seed(self):
env = SeedRecordingEnv()
_update_env_seed_if_necessary(env, seed=7, worker_idx=0, vector_idx=1000)
self.assertEqual(env.last_seed, 1007)
_update_env_seed_if_necessary(env, seed=7, worker_idx=1000, vector_idx=999)
self.assertEqual(env.last_seed, 1000 * 1000 + 999 + 7)
|
@nathon-lee Could you make these two changes then we're good to merge |
ok , @pseudo-rnd-thoughts hi, I've made the revisions as per your suggestion, and it's ready to merge |
Signed-off-by: nathon <leejianwoo@gmail.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Reviewed by Cursor Bugbot for commit 13d5812. Configure here.
|
The CI seems to be failing for reasons unrelated to this PR |
I re-ran the CI, but it still failed with the same ray.init() timeout. It seems to be an unrelated flaky test. Could a maintainer please take a look when you have a chance? @pseudo-rnd-thoughts |
|
@nathon-lee Thanks, I lost track of this PR, yeah, we just need to update the branch to fix it |
Thanks, appreciate the merge! |
…ay-project#61823) ## Description This PR fixes a small bug in `rllib`'s `_update_env_seed_if_necessary()` helper. The assert in that function was checking `worker_idx < max_num_envs_per_env_runner`, but the boundary there should apply to `vector_idx`, not `worker_idx`. Because of this, valid cases with a large `worker_idx` could be rejected incorrectly, while the intended guard is to ensure that `vector_idx` stays within the per-worker env limit used in the seed calculation. This PR makes the fix by updating the assert to: - `vector_idx < max_num_envs_per_env_runner` and adds a regression test covering the boundary behavior: - a valid large `worker_idx` with an in-range `vector_idx` should succeed - an out-of-range `vector_idx` should still raise The change is intentionally minimal and should be low risk. ## Related issues Fixes ray-project#61593 ## Additional information I kept the change narrowly scoped to the incorrect assert and the corresponding regression test. I was not able to fully run the targeted test locally in this checkout because the local environment is missing the compiled `ray._raylet` module, but the logic change is straightforward and the added test is designed specifically to prevent regression for this boundary condition. --------- Signed-off-by: nathon-lee <leejianwoo@gmail.com> Signed-off-by: nathon <leejianwoo@gmail.com>
…ay-project#61823) ## Description This PR fixes a small bug in `rllib`'s `_update_env_seed_if_necessary()` helper. The assert in that function was checking `worker_idx < max_num_envs_per_env_runner`, but the boundary there should apply to `vector_idx`, not `worker_idx`. Because of this, valid cases with a large `worker_idx` could be rejected incorrectly, while the intended guard is to ensure that `vector_idx` stays within the per-worker env limit used in the seed calculation. This PR makes the fix by updating the assert to: - `vector_idx < max_num_envs_per_env_runner` and adds a regression test covering the boundary behavior: - a valid large `worker_idx` with an in-range `vector_idx` should succeed - an out-of-range `vector_idx` should still raise The change is intentionally minimal and should be low risk. ## Related issues Fixes ray-project#61593 ## Additional information I kept the change narrowly scoped to the incorrect assert and the corresponding regression test. I was not able to fully run the targeted test locally in this checkout because the local environment is missing the compiled `ray._raylet` module, but the logic change is straightforward and the added test is designed specifically to prevent regression for this boundary condition. --------- Signed-off-by: nathon-lee <leejianwoo@gmail.com> Signed-off-by: nathon <leejianwoo@gmail.com>

Description
This PR fixes a small bug in
rllib's_update_env_seed_if_necessary()helper.The assert in that function was checking
worker_idx < max_num_envs_per_env_runner, but the boundary there should apply tovector_idx, notworker_idx. Because of this, valid cases with a largeworker_idxcould be rejected incorrectly, while the intended guard is to ensure thatvector_idxstays within the per-worker env limit used in the seed calculation.This PR makes the fix by updating the assert to:
vector_idx < max_num_envs_per_env_runnerand adds a regression test covering the boundary behavior:
worker_idxwith an in-rangevector_idxshould succeedvector_idxshould still raiseThe change is intentionally minimal and should be low risk.
Related issues
Fixes #61593
Additional information
I kept the change narrowly scoped to the incorrect assert and the corresponding regression test.
I was not able to fully run the targeted test locally in this checkout because the local environment is missing the compiled
ray._rayletmodule, but the logic change is straightforward and the added test is designed specifically to prevent regression for this boundary condition.