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] Add TorchCheckpoint.from_state_dict #27970

Merged

Conversation

bveeramani
Copy link
Member

@bveeramani bveeramani commented Aug 17, 2022

Signed-off-by: Balaji Veeramani [email protected]

Why are these changes needed?

PyTorch recommends saving state dictionaries instead of modules, but we don't support any way to do this.

Related issue number

Closes #28158

See also #27922 and #27971.

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 :(

python/ray/train/torch/torch_checkpoint.py Outdated Show resolved Hide resolved
python/ray/train/torch/torch_checkpoint.py Outdated Show resolved Hide resolved
python/ray/train/torch/torch_checkpoint.py Outdated Show resolved Hide resolved
*,
preprocessor: Optional["Preprocessor"] = None,
) -> "TorchCheckpoint":
"""Create a :class:`~ray.air.checkpoint.Checkpoint` that stores a model state
Copy link
Contributor

Choose a reason for hiding this comment

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

First line of docstring should be 1 line please!

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'm not sure if we should do that.

If the docstring summary is on one line, then the line is longer than 88 characters. This normally means we should shorten our summary, but the only reason the summary is long is because of the :class:ray.air.checkpoint.Checkpoint Sphinx directive.

So, we either break that line limit convention or the one-line docstring summary convention. Given that the goal of the one-line summary convention is to have short summaries, I think it's fine to wrap summaries when we use Sphinx directives.

What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

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

Also, I don't think there's any existing precedent in our code base. PyTorch wraps their summaries. For example, https://pytorch.org/docs/stable/_modules/torch/nn/modules/linear.html#Bilinear

Copy link
Contributor

Choose a reason for hiding this comment

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

The formatting guidelines we use follow PEP 257:

Multi-line docstrings consist of a summary line just like a one-line docstring, followed by a blank line, followed by a more elaborate description. The summary line may be used by automatic indexing tools; it is important that it fits on one line and is separated from the rest of the docstring by a blank line.

https://peps.python.org/pep-0257/#multi-line-docstrings

I agree though, for long sphinx directives, there doesn't seem to be an ideal solution.

Copy link
Contributor

Choose a reason for hiding this comment

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

cc @richardliaw @maxpumperla for thoughts

"""
checkpoint = cls.from_dict({PREPROCESSOR_KEY: preprocessor, MODEL_KEY: model})
return checkpoint
""" # noqa: E501
Copy link
Contributor

Choose a reason for hiding this comment

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

is this noqa necessary?

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 think so. AFAIK there's no way to wrap

`Saving and Loading Models <https://pytorch.org/tutorials/beginner/saving_loading_models.html#what-is-a-state-dict>`_.

in a way that the line is less than 88 characters.

checkpoint = TorchCheckpoint.from_state_dict(expected_state_dict)
actual_state_dict = checkpoint.get_model(torch.nn.Linear(1, 1)).state_dict()
assert actual_state_dict == expected_state_dict

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 add a test for from_model as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

@justinvyu is on it. See #27056

@bveeramani bveeramani added the tests-ok The tagger certifies test failures are unrelated and assumes personal liability. label Aug 18, 2022
@bveeramani bveeramani self-assigned this Aug 30, 2022
@amogkam amogkam merged commit dad98dc into ray-project:master Aug 30, 2022
@bveeramani bveeramani deleted the bveeramani/torch-checkpoint-state-dict branch August 30, 2022 22:04
XiaodongLv pushed a commit to XiaodongLv/ray that referenced this pull request Sep 2, 2022
PyTorch recommends saving state dictionaries instead of modules, but we don't support any way to do this.

Signed-off-by: Balaji Veeramani [email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tests-ok The tagger certifies test failures are unrelated and assumes personal liability.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[AIR] Add TorchCheckpoint.from_state_dict
3 participants