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

[AIR] Support large checkpoints and other arguments #28826

Merged
merged 6 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions python/ray/train/base_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,16 @@ def fit(self) -> Result:
raise TrainingFailedError from e
return result

def as_trainable(self) -> Type["Trainable"]:
"""Convert self to a ``tune.Trainable`` class."""
def _generate_trainable_cls(self) -> Type["Trainable"]:
"""Generate the base Trainable class.

Returns:
A Trainable class to use for training.
"""

from ray.tune.execution.placement_groups import PlacementGroupFactory
from ray.tune.trainable import wrap_function

base_config = self._param_dict
trainer_cls = self.__class__
scaling_config = self.scaling_config

Expand Down Expand Up @@ -414,9 +418,8 @@ def base_scaling_config(cls) -> ScalingConfig:
"""Returns the unchanged scaling config provided through the Trainer."""
return scaling_config

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def setup(self, config, **kwargs):
base_config = dict(kwargs)
# Create a new config by merging the dicts.
# run_config is not a tunable hyperparameter so it does not need to be
# merged.
Expand All @@ -431,6 +434,7 @@ def __init__(self, *args, **kwargs):
] = self._reconcile_scaling_config_with_trial_resources(
merged_scaling_config
)
super(TrainTrainable, self).setup(config)

def _reconcile_scaling_config_with_trial_resources(
self, scaling_config: ScalingConfig
Expand Down Expand Up @@ -487,3 +491,13 @@ def default_resource_request(cls, config):
return validated_scaling_config.as_placement_group_factory()

return TrainTrainable

def as_trainable(self) -> Type["Trainable"]:
"""Convert self to a ``tune.Trainable`` class."""
from ray import tune
Yard1 marked this conversation as resolved.
Show resolved Hide resolved

base_config = self._param_dict
trainable_cls = self._generate_trainable_cls()

# Wrap with `tune.with_parameters` to handle very large values in base_config
return tune.with_parameters(trainable_cls, **base_config)
4 changes: 2 additions & 2 deletions python/ray/train/gbdt_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ def training_loop(self) -> None:
if checkpoint_at_end:
self._checkpoint_at_end(model, evals_result)

def as_trainable(self) -> Type[Trainable]:
trainable_cls = super().as_trainable()
def _generate_trainable_cls(self) -> Type["Trainable"]:
trainable_cls = super()._generate_trainable_cls()
trainer_cls = self.__class__
scaling_config = self.scaling_config
ray_params_cls = self._ray_params_cls
Expand Down
4 changes: 2 additions & 2 deletions python/ray/train/huggingface/huggingface_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ def setup(self) -> None:
)
)

def as_trainable(self) -> Type[Trainable]:
def _generate_trainable_cls(self) -> Type["Trainable"]:
original_param_dict = self._param_dict.copy()
resume_from_checkpoint: Optional[Checkpoint] = self._param_dict.get(
"resume_from_checkpoint", None
Expand All @@ -444,7 +444,7 @@ def as_trainable(self) -> Type[Trainable]:
resume_from_checkpoint
)
try:
ret = super().as_trainable()
ret = super()._generate_trainable_cls()
finally:
self._param_dict = original_param_dict
return ret
Expand Down
14 changes: 14 additions & 0 deletions python/ray/train/tests/test_base_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
from contextlib import redirect_stderr
from unittest.mock import patch

import numpy as np
import pytest

import ray
from ray import tune
from ray.air import session
from ray.air.checkpoint import Checkpoint
from ray.air.constants import MAX_REPR_LENGTH
from ray.data.preprocessor import Preprocessor
from ray.tune.impl import tuner_internal
Expand Down Expand Up @@ -376,6 +378,18 @@ def training_loop(self):
assert len(representation) < MAX_REPR_LENGTH


def test_large_params(ray_start_4_cpus):
array_size = int(1e8)

def training_loop(self):
checkpoint = self.resume_from_checkpoint.to_dict()["ckpt"]
assert len(checkpoint) == array_size

checkpoint = Checkpoint.from_dict({"ckpt": np.zeros(shape=array_size)})
trainer = DummyTrainer(training_loop, resume_from_checkpoint=checkpoint)
trainer.fit()


if __name__ == "__main__":
import sys

Expand Down
5 changes: 5 additions & 0 deletions python/ray/tune/trainable/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,11 @@ def setup(self, config):
setup_kwargs[k] = parameter_registry.get(prefix + k)
super(_Inner, self).setup(config, **setup_kwargs)

# Workaround for actor name not being logged correctly
# if __repr__ is not directly defined in a class.
def __repr__(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

ah is this always an issue then?

Copy link
Member

Choose a reason for hiding this comment

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

It still hasn't been fixed in core afaik

return super().__repr__()

_Inner.__name__ = trainable_name
return _Inner
else:
Expand Down