Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tune] Add repr for ResultGrid class #31941

Merged
merged 7 commits into from
Feb 7, 2023

Conversation

woshiyyya
Copy link
Member

@woshiyyya woshiyyya commented Jan 25, 2023

Why are these changes needed?

Add repr() for ResultGrid class and prettify repr() of Result class.

The example representation:

>>> result_grid: ResultGrid = tuner.fit()
>>> result_grid
[
  Result(
    error='RayTaskError(RuntimeError)',
    metrics={'trial_id': '820f5_00000'},
    log_dir=PosixPath('/private/var/folders/0w/7cw9tmnx6nsfxtknpt99s7gc0000gn/T/pytest-of-yunxuanx/pytest-18/test_result_grid_repr0/ray_results/test_GridResults_repr/f_820f5_00000_0_x=1_2023-01-26_17-18-30'),
    checkpoint=None
  ),
  Result(
    metrics={'loss': 1, '_metadata': _CheckpointMetadata(checkpoint_type=<class 'ray.air.checkpoint.Checkpoint'>, checkpoint_state={}), 'should_checkpoint': True, 'done': True, 'trial_id': '820f5_00001', 'experiment_tag': '1_x=2'},
    log_dir=PosixPath('/private/var/folders/0w/7cw9tmnx6nsfxtknpt99s7gc0000gn/T/pytest-of-yunxuanx/pytest-18/test_result_grid_repr0/ray_results/test_GridResults_repr/f_820f5_00001_1_x=2_2023-01-26_17-18-31'),
    checkpoint=Checkpoint(local_path=/private/var/folders/0w/7cw9tmnx6nsfxtknpt99s7gc0000gn/T/pytest-of-yunxuanx/pytest-18/test_result_grid_repr0/ray_results/test_GridResults_repr/f_820f5_00001_1_x=2_2023-01-26_17-18-31/checkpoint_000000)
  ),
]

Related issue number

Closes #31716

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

@woshiyyya woshiyyya added the tune Tune-related issues label Jan 25, 2023
@woshiyyya woshiyyya requested a review from justinvyu January 25, 2023 23:36
@woshiyyya woshiyyya force-pushed the tune/result_grid_repr branch from ace2f6b to ea9a222 Compare January 26, 2023 00:12
@woshiyyya woshiyyya marked this pull request as ready for review January 26, 2023 00:14
@woshiyyya woshiyyya force-pushed the tune/result_grid_repr branch 2 times, most recently from 7ae7aed to b22bed7 Compare January 26, 2023 02:18
Copy link
Contributor

@justinvyu justinvyu left a comment

Choose a reason for hiding this comment

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

Looks good! Just a few minor change suggestions.

with tune.checkpoint_dir(step=0) as checkpoint_dir:
path = os.path.join(checkpoint_dir, "checkpoint")
with open(path, "w") as f:
f.write(json.dumps({"step": 0}))

Copy link
Contributor

Choose a reason for hiding this comment

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

We can save a checkpiont with session.report. This tune.checkpoint_dir was the old API.

Suggested change
with tune.checkpoint_dir(step=0) as checkpoint_dir:
path = os.path.join(checkpoint_dir, "checkpoint")
with open(path, "w") as f:
f.write(json.dumps({"step": 0}))

python/ray/tune/tests/test_result_grid.py Outdated Show resolved Hide resolved
python/ray/tune/tests/test_result_grid.py Show resolved Hide resolved
python/ray/tune/tests/test_result_grid.py Outdated Show resolved Hide resolved
Signed-off-by: Yunxuan Xiao <yunxuanx@Yunxuans-MBP.local.meter>
@woshiyyya woshiyyya force-pushed the tune/result_grid_repr branch from b22bed7 to fac8f78 Compare January 27, 2023 01:39
Copy link
Contributor

@justinvyu justinvyu left a comment

Choose a reason for hiding this comment

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

Looks good!

@woshiyyya woshiyyya requested a review from krfricke January 27, 2023 02:03
Copy link
Contributor

@krfricke krfricke left a comment

Choose a reason for hiding this comment

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

I have a few minor questions for the repr implementation.

For testing, this is currently and end to end test, but I don't think this is needed. We can just create Result objects manually and a ResultGrid containing these elements. This will lead to a test execution time of a few milliseconds compared to the few seconds it takes now to run the tune experiment.

Generally we should aim to use minimalistic tests if possible - we don't get any benefit from a full end to end test here, so we should just stick with a simple unit test.

Comment on lines 237 to 247
tuner = tune.Tuner(
f,
run_config=air.RunConfig(
name="test_result_grid_repr",
local_dir=str(tmpdir / "test_result_grid_repr_results"),
),
param_space={"x": tune.grid_search([1, 2])},
)
result_grid = tuner.fit()

representation = result_grid.__repr__()
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we instead construct the result grid manually and assert the layout?

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree. Manually creating a result grid will be way faster than running a full tuning experiment.

Comment on lines 71 to 72
kws_repr = " " + ",\n ".join(kws)
return "{}(\n{}\n)".format(type(self).__name__, kws_repr)
Copy link
Contributor

Choose a reason for hiding this comment

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

It's very hard to imagine how this looks like, can we add a comment what we expect here?

Comment on lines 246 to 248
for result in self:
result_repr = " " + result.__repr__().replace("\n", "\n ")
all_results_repr += f"{result_repr},\n"
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Can we use a list comprehension here?
  2. The replace looks a bit fishy. Can we maybe instead use a private method in Result for both Result.__repr__ and ResultGrid.__repr__?

for result in self:
result_repr = " " + result.__repr__().replace("\n", "\n ")
all_results_repr += f"{result_repr},\n"
return f"[\n{all_results_repr}]"
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we still have ResultGrid<[ ... ]> in here?

Signed-off-by: Yunxuan Xiao <yunxuanx@Yunxuans-MBP.local.meter>
Copy link
Contributor

@justinvyu justinvyu left a comment

Choose a reason for hiding this comment

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

I have a few more small suggestions, but should be good after that! The new _results is definitely better, and +1 to creating the result objects directly in the test without running an actual experiment.

python/ray/tune/tests/test_result_grid.py Outdated Show resolved Hide resolved
python/ray/tune/tests/test_result_grid.py Outdated Show resolved Hide resolved
python/ray/tune/tests/test_result_grid.py Outdated Show resolved Hide resolved
python/ray/tune/result_grid.py Outdated Show resolved Hide resolved
python/ray/tune/result_grid.py Outdated Show resolved Hide resolved
python/ray/tune/tests/test_result_grid.py Show resolved Hide resolved
python/ray/tune/tests/test_result_grid.py Show resolved Hide resolved
python/ray/tune/tests/test_result_grid.py Outdated Show resolved Hide resolved
python/ray/tune/tests/test_result_grid.py Outdated Show resolved Hide resolved
python/ray/tune/tests/test_result_grid.py Outdated Show resolved Hide resolved
Signed-off-by: Yunxuan Xiao <yunxuanx@Yunxuans-MBP.local.meter>
Copy link
Contributor

@justinvyu justinvyu left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Contributor

@krfricke krfricke left a comment

Choose a reason for hiding this comment

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

Nice! There's one failing test please ping me once resolved and CI is passing

Yunxuan Xiao added 4 commits February 6, 2023 17:14
Signed-off-by: Yunxuan Xiao <yunxuanx@Yunxuans-MBP.local.meter>
Signed-off-by: Yunxuan Xiao <yunxuanx@Yunxuans-MBP.local.meter>
Signed-off-by: Yunxuan Xiao <yunxuanx@Yunxuans-MBP.local.meter>
@krfricke krfricke merged commit cf95514 into ray-project:master Feb 7, 2023
edoakes pushed a commit to edoakes/ray that referenced this pull request Mar 22, 2023
Add __repr__() for ResultGrid class and prettify __repr__() of Result class.

Signed-off-by: Yunxuan Xiao <yunxuanx@Yunxuans-MBP.local.meter>
Co-authored-by: Yunxuan Xiao <yunxuanx@Yunxuans-MBP.local.meter>
Signed-off-by: Edward Oakes <ed.nmi.oakes@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tune Tune-related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Tune] Improve ResultGrid repr to make its accessible attributes more obvious
3 participants