-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Remove dependency on numpy for serialization for XLA/open registration devices without numpy #137444
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
Remove dependency on numpy for serialization for XLA/open registration devices without numpy #137444
Conversation
…n devices without numpy [ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/137444
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 9c0b63b with merge base f80ed0b ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good!
…registration devices without numpy" Related: pytorch/xla#7799 (comment) Follow ups: Do the same for maia and mtia ## Motivation With the move to `weights_only` by default, we are making an explicit decision not to allowlist GLOBALs required to deserialize `numpy` tensors by default. The implication is that backends relying on numpy for serialization will fail loudly when `torch.load` flips `weights_only`. However, we make the observation that this dependency on numpy was legacy and is not actually needed anymore. So we can remove it, which aligns with our weights_only strategy. ## Why is this ok? The following comment on why numpy is necessary for serialization is legacy https://github.com/pytorch/pytorch/blob/c87c9f0a01f4840bd19ac5058960c9766dd15ef8/torch/_tensor.py#L303-L312 We no longer do the following, though it was the case 5 years ago in the PR that added this > CPU storage is reconstructed with randomly initialized data, moved onto backend device, and then storage is updated to the serialized content **Instead what now happens is that CPU storage is constructed with data from the file **and then** moved onto backend device.** Old behavior (`legacy_load`): https://github.com/ailzhang/pytorch/blob/67adda891a839691790a0dcd99062430050eff3b/torch/serialization.py#L620 [ghstack-poisoned]
|
@pytorchbot merge |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
…tion" See rationale in #137444 description [ghstack-poisoned]
See rationale in #137444 description [ghstack-poisoned]
…tion" See rationale in #137444 description [ghstack-poisoned]
See rationale in #137444 description [ghstack-poisoned]
…tion" See rationale in #137444 description [ghstack-poisoned]
See rationale in #137444 description [ghstack-poisoned]
See rationale in #137444 description Pull Request resolved: #137600 Approved by: https://github.com/albanD
See rationale in pytorch#137444 description Pull Request resolved: pytorch#137600 Approved by: https://github.com/albanD
See rationale in pytorch#137444 description Pull Request resolved: pytorch#137600 Approved by: https://github.com/albanD
Summary: NumPy based tensor rebuilding from serialization has been deprecated by other backends (eg. [XLA](pytorch#137444)). The new flow has CPU storage being constructed with data from the file and then moved to the target backend device. Furthermore, relying on numpy for serialization will fail loudly when torch.load flips weights_only. Reviewed By: andyanwang Differential Revision: D77843238
Summary: NumPy based tensor rebuilding from serialization has been deprecated by other backends (eg. [XLA](#137444)). The new flow has CPU storage being constructed with data from the file and then moved to the target backend device. Furthermore, relying on numpy for serialization will fail loudly when torch.load flips weights_only. Reviewed By: andyanwang Differential Revision: D77843238 Pull Request resolved: #157884 Approved by: https://github.com/albanD
Related: pytorch/xla#7799 (comment)
Follow ups: Do the same for maia and mtia
Motivation
With the move to
weights_onlyby default, we are making an explicit decision not to allowlist GLOBALs required to deserializenumpytensors by default. The implication is that backends relying on numpy for serialization will fail loudly whentorch.loadflipsweights_only.However, we make the observation that this dependency on numpy was legacy and is not actually needed anymore. So we can remove it, which aligns with our weights_only strategy.
Why is this ok?
The following comment on why numpy is necessary for serialization is legacy
pytorch/torch/_tensor.py
Lines 303 to 312 in c87c9f0
We no longer do the following, though it was the case 5 years ago in the PR that added this
Instead what now happens is that CPU storage is constructed with data from the file and then moved onto backend device.
Old behavior (
legacy_load): https://github.com/ailzhang/pytorch/blob/67adda891a839691790a0dcd99062430050eff3b/torch/serialization.py#L620Stack from ghstack (oldest at bottom):