-
Notifications
You must be signed in to change notification settings - Fork 26.3k
[DDP] Add PackedSequence support when device_ids is specified
#86614
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
Conversation
[ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/86614
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 4d7c335: This comment was automatically generated by Dr. CI and updates every 15 minutes. |
| @@ -1,10 +1,12 @@ | |||
| from typing import Any, Dict, List, Tuple | |||
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.
Sorted imports.
…ecified" Before this PR, if a user runs DDP with `device_ids` specified and with a `PackedSequence` input, then the execution will error with something like: ``` raise ValueError( ValueError: batch_sizes should always be on CPU. Instances of PackedSequence should never be created manually. They should be instantiated by functions like pack_sequence and pack_padded_sequences in nn.utils.rnn. https://pytorch.org/docs/stable/nn.html... ``` This is because the DDP forward calls `_to_kwargs()`, which calls `_recursive_to()`, which moves the inputs to GPU. However, `_is_namedtuple(packed_sequence)` returns `True`, leading to the branch `return [type(obj)(*args) for args in zip(*map(to_map, obj))]`, which tries to construct a `PackedSequence` directly via `type(obj)(*args)`, leading to the error. Repro for `_is_namedtuple(packed_sequence)` returning `True`: ``` import random import torch import torch.nn.utils.rnn as rnn_utils from torch.nn.parallel.scatter_gather import _is_namedtuple def _ordered_sequence(tensor_type): seqs = [tensor_type(random.randint(1, 256)) for _ in range(32)] seqs = [s.random_(-128, 128) for s in seqs] ordered = sorted(seqs, key=len, reverse=True) return ordered def _padded_sequence(tensor_type): ordered = _ordered_sequence(tensor_type) lengths = [len(i) for i in ordered] padded_tensor = rnn_utils.pad_sequence(ordered) return padded_tensor, lengths padded, lengths = _padded_sequence(torch.Tensor) packed = rnn_utils.pack_padded_sequence( padded, lengths, enforce_sorted=False) print(type(packed), packed.data.device) print(_is_namedtuple(packed)) ``` Test Plan: ``` python test/distributed/test_c10d_nccl.py -k test_ddp_packed_sequence ``` [ghstack-poisoned]
rohan-varma
left a comment
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.
Thanks for adding this!
…ecified" Before this PR, if a user runs DDP with `device_ids` specified and with a `PackedSequence` input, then the execution will error with something like: ``` raise ValueError( ValueError: batch_sizes should always be on CPU. Instances of PackedSequence should never be created manually. They should be instantiated by functions like pack_sequence and pack_padded_sequences in nn.utils.rnn. https://pytorch.org/docs/stable/nn.html... ``` This is because the DDP forward calls `_to_kwargs()`, which calls `_recursive_to()`, which moves the inputs to GPU. However, `_is_namedtuple(packed_sequence)` returns `True`, leading to the branch `return [type(obj)(*args) for args in zip(*map(to_map, obj))]`, which tries to construct a `PackedSequence` directly via `type(obj)(*args)`, leading to the error. Repro for `_is_namedtuple(packed_sequence)` returning `True`: ``` import random import torch import torch.nn.utils.rnn as rnn_utils from torch.nn.parallel.scatter_gather import _is_namedtuple def _ordered_sequence(tensor_type): seqs = [tensor_type(random.randint(1, 256)) for _ in range(32)] seqs = [s.random_(-128, 128) for s in seqs] ordered = sorted(seqs, key=len, reverse=True) return ordered def _padded_sequence(tensor_type): ordered = _ordered_sequence(tensor_type) lengths = [len(i) for i in ordered] padded_tensor = rnn_utils.pad_sequence(ordered) return padded_tensor, lengths padded, lengths = _padded_sequence(torch.Tensor) packed = rnn_utils.pack_padded_sequence( padded, lengths, enforce_sorted=False) print(type(packed), packed.data.device) print(_is_namedtuple(packed)) ``` Test Plan: ``` python test/distributed/test_c10d_nccl.py -k test_ddp_packed_sequence ``` Without the fix, the added unit test fails with the expected error. [ghstack-poisoned]
…ecified" Before this PR, if a user runs DDP with `device_ids` specified and with a `PackedSequence` input, then the execution will error with something like: ``` raise ValueError( ValueError: batch_sizes should always be on CPU. Instances of PackedSequence should never be created manually. They should be instantiated by functions like pack_sequence and pack_padded_sequences in nn.utils.rnn. https://pytorch.org/docs/stable/nn.html... ``` This is because the DDP forward calls `_to_kwargs()`, which calls `_recursive_to()`, which moves the inputs to GPU. However, `_is_namedtuple(packed_sequence)` returns `True`, leading to the branch `return [type(obj)(*args) for args in zip(*map(to_map, obj))]`, which tries to construct a `PackedSequence` directly via `type(obj)(*args)`, leading to the error. Repro for `_is_namedtuple(packed_sequence)` returning `True`: ``` import random import torch import torch.nn.utils.rnn as rnn_utils from torch.nn.parallel.scatter_gather import _is_namedtuple def _ordered_sequence(tensor_type): seqs = [tensor_type(random.randint(1, 256)) for _ in range(32)] seqs = [s.random_(-128, 128) for s in seqs] ordered = sorted(seqs, key=len, reverse=True) return ordered def _padded_sequence(tensor_type): ordered = _ordered_sequence(tensor_type) lengths = [len(i) for i in ordered] padded_tensor = rnn_utils.pad_sequence(ordered) return padded_tensor, lengths padded, lengths = _padded_sequence(torch.Tensor) packed = rnn_utils.pack_padded_sequence( padded, lengths, enforce_sorted=False) print(type(packed), packed.data.device) print(_is_namedtuple(packed)) ``` Test Plan: ``` python test/distributed/test_c10d_nccl.py -k test_ddp_packed_sequence ``` Without the fix, the added unit test fails with the expected error. [ghstack-poisoned]
rohan-varma
left a comment
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.
LG
|
@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 |
|
Hey @awgu. |
Stack from ghstack:
PackedSequencesupport whendevice_idsis specified #86614 [DDP] AddPackedSequencesupport whendevice_idsis specifiedutils.py(moved to_utils.py) #86528 [FSDP] Removeutils.py(moved to_utils.py)summon_full_params(with_grads=True)#85738 [FSDP] Add initialsummon_full_params(with_grads=True)use_orig_params#84911 [FSDP] Adduse_orig_paramsBefore this PR, if a user runs DDP with
device_idsspecified and with aPackedSequenceinput, then the execution will error with something like:This is because the DDP forward calls
_to_kwargs(), which calls_recursive_to(), which moves the inputs to GPU. However,_is_namedtuple(packed_sequence)returnsTrue, leading to the branchreturn [type(obj)(*args) for args in zip(*map(to_map, obj))], which tries to construct aPackedSequencedirectly viatype(obj)(*args), leading to the error.Repro for
_is_namedtuple(packed_sequence)returningTrue:Test Plan:
Without the fix, the added unit test fails with the expected error.