Repository navigation
[Train] Allow DataParallelTrainer's train_fn to return data - #62021
Conversation
Signed-off-by: Mark Towers <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request successfully implements the feature allowing DataParallelTrainer's train_fn to return data directly, which is then accessible via result.return_values. This provides a cleaner and more direct way to retrieve metrics from validation functions, moving away from the previous 'hacky' solution involving dummy checkpoints and ray.train.report. The changes are well-implemented across the codebase, including updates to Result and WorkerStatus dataclasses, and the logic in the controller and worker to capture and propagate these return values. The documentation and example code have been updated accordingly, and new tests have been added to validate the new functionality. Overall, this is a valuable improvement for user experience and code clarity.
Signed-off-by: Mark Towers <[email protected]>
…nd update `train_loop_per_worker` output from `None` to `Any` Signed-off-by: Mark Towers <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
# Conflicts: # doc/source/train/doc_code/asynchronous_validation.py # doc/source/train/user-guides/asynchronous-validation.rst # python/ray/train/v2/_internal/execution/controller/controller.py # python/ray/train/v2/_internal/execution/worker_group/poll.py
Signed-off-by: Mark Towers <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
| checkpoint: The latest checkpoint saved by the training function | ||
| via :func:`ray.train.report`. | ||
| return_value: The value returned by the training function on the | ||
| rank-0 worker. |
There was a problem hiding this comment.
- Can you add documentation to the
results.rstuser guide? Also call out what types of data are supported. - Can you update the description of the PR to be more generalized? Specifically, explain the general use case first for distributed training, and then describe validation as use case.
There was a problem hiding this comment.
Thanks for the review,
- added documentation to
results.rst. For the types of data support, should this be the same asray.train.report(metrics)'s supported values? - Updated
There was a problem hiding this comment.
@matthewdeng I've added an error for torch tensors and documentation
There was a problem hiding this comment.
I think it should be slightly different since these values do not need to be in a dictionary and are not written to disk.
Maybe something like:
The value returned by the user-defined training function on the
rank 0 worker, or ``None`` if no value was returned or if training did not
complete successfully. The return value must be serializable.
…a is returned. Signed-off-by: Mark Towers <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
Signed-off-by: Mark Towers <[email protected]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Reviewed by Cursor Bugbot for commit 902d5b1. Configure here.
Signed-off-by: Mark Towers <[email protected]>
…project#62021) ## Description Ray Train's Trainer has no mechanism for train functions to return data directly — the only way to extract results is via ray.train.report with metrics or checkpoints. This is unnecessarily limiting for any distributed function that needs to return output to the caller (e.g. validation, inference, feature extraction). This PR adds a return_value attribute to Result, populated from worker 0's return value, mirroring how ray.train.report metrics only capture worker 0's data. ### Example use case: distributed validation A common pattern is running a DataParallelTrainer purely for distributed validation (https://docs.ray.io/en/latest/train/user-guides/asynchronous-validation.html#write-a-distributed-validation-function). Previously, returning a score required a dummy checkpoint report: ```python # OLD — hacky workaround ray.train.report( metrics={"score": mean_valid_loss.compute().item()}, checkpoint=ray.train.Checkpoint( ray.train.get_context() .get_storage() .build_checkpoint_path_from_name("placeholder") ), checkpoint_upload_mode=ray.train.CheckpointUploadMode.NO_UPLOAD, ) ``` Now the train function can simply return its result: ```python # New return {"score": mean_valid_loss.compute().item()} ``` And the caller accesses it via: ```python results = trainer.fit() return results.return_value # instead of results.metrics ``` Changes: - Add `return_value: Optional[Any]` to `Result` and expand its docstring to cover all attributes - Add `return_value` field to `WorkerStatus`; populate it from the training thread runner on completion - `TrainController` captures rank-0's `return_value` and passes it through to `Result`. Torch tensors aren't supported values. - Both local-mode controllers (`LocalController`, `LocalTorchController`) capture the train function's return value - Update `train_loop_per_worker` type hints from `Callable[[], None]` to `Callable[[], Any]` across all trainers (DataParallel, Torch, JAX, TensorFlow, LightGBM, XGBoost) - Three new tests: old report-based approach, new return-based approach, multi-worker isolation (asserts only rank-0's value is captured) - Docs: new `return_value` section in results.rst; asynchronous_validation example updated to the return-based style Unsupported features - The `return_value` isn't recorded like the checkpoint state so on resumption it isn't recorded (though this should be very rare issue) and `Result.from_path("...").return_value is None` --------- Signed-off-by: Mark Towers <[email protected]> Co-authored-by: Mark Towers <[email protected]>

Description
Ray Train's Trainer has no mechanism for train functions to return data directly — the only way to extract results is via ray.train.report with metrics or checkpoints. This is unnecessarily limiting for any distributed function that needs to return output to the caller (e.g. validation, inference, feature extraction).
This PR adds a return_value attribute to Result, populated from worker 0's return value, mirroring how ray.train.report metrics only capture worker 0's data.
Example use case: distributed validation
A common pattern is running a DataParallelTrainer purely for distributed validation (https://docs.ray.io/en/latest/train/user-guides/asynchronous-validation.html#write-a-distributed-validation-function). Previously, returning a score required a dummy checkpoint report:
Now the train function can simply return its result:
And the caller accesses it via:
Changes:
return_value: Optional[Any]toResultand expand its docstring to cover all attributesreturn_valuefield toWorkerStatus; populate it from the training thread runner on completionTrainControllercaptures rank-0'sreturn_valueand passes it through toResult. Torch tensors aren't supported values.LocalController,LocalTorchController) capture the train function's return valuetrain_loop_per_workertype hints fromCallable[[], None]toCallable[[], Any]across all trainers (DataParallel, Torch, JAX, TensorFlow, LightGBM, XGBoost)return_valuesection in results.rst; asynchronous_validation example updated to the return-based styleUnsupported features
return_valueisn't recorded like the checkpoint state so on resumption it isn't recorded (though this should be very rare issue) andResult.from_path("...").return_value is None