Skip to content

[DataPipe] Fix error message coming from single iterator constraint#79547

Closed
NivekT wants to merge 2 commits intogh/nivekt/50/basefrom
gh/nivekt/50/head
Closed

[DataPipe] Fix error message coming from single iterator constraint#79547
NivekT wants to merge 2 commits intogh/nivekt/50/basefrom
gh/nivekt/50/head

Conversation

@NivekT
Copy link
Contributor

@NivekT NivekT commented Jun 14, 2022

Stack from ghstack:

Fixes meta-pytorch/data#516

  • Prevent confusion by not adding the extra string about __iter__ when the exception is raised because of single iterator constraint
  • Added a missing space
  • Make sure __len__ exists before calling it during exception handling
  • Handles the case where there was no initial exception message (previously skipped)

Exception example:

from torchdata.datapipes.iter import IterableWrapper

source_dp = IterableWrapper(range(10))
it1 = iter(source_dp)
next(it1)
it2 = iter(source_dp)
next(it2)
next(it1)

Will raise:

Traceback (most recent call last):
  File "/Users/scratch/fast_forward.py", line 32, in <module>
    next(it1)
  File "/Users/pytorch/torch/utils/data/datapipes/_typing.py", line 524, in wrap_generator
    _check_iterator_valid(datapipe, iterator_id)
  File "/Users/pytorch/torch/utils/data/datapipes/_typing.py", line 445, in _check_iterator_valid
    raise RuntimeError(_gen_invalid_iterdatapipe_msg(datapipe) + _feedback_msg)
RuntimeError: This iterator has been invalidated because another iterator has been created from the same IterDataPipe: IterableWrapperIterDataPipe(deepcopy=True, iterable=range(0, 10))
This may be caused multiple references to the same IterDataPipe. We recommend using `.fork()` if that is necessary.
For feedback regarding this single iterator per IterDataPipe constraint, feel free to comment on this issue: https://github.com/pytorch/data/issues/45.

@facebook-github-bot
Copy link
Contributor

facebook-github-bot commented Jun 14, 2022

🔗 Helpful links

❌ 1 New Failures

As of commit 1a00d1f (more details on the Dr. CI page):

Expand to see more
  • 1/1 failures introduced in this PR

🕵️ 1 new failure recognized by patterns

The following CI failures do not appear to be due to upstream breakages

See GitHub Actions build pull / linux-focal-py3.7-gcc7 / test (backwards_compat, 1, 1, linux.2xlarge) (1/1)

Step: "Test" (full log | diagnosis details | 🔁 rerun)

2022-06-14T19:43:17.1103458Z RuntimeError:
2022-06-14T19:43:16.4834522Z Author: PyTorch Team
2022-06-14T19:43:16.4834929Z Author-email: packages@pytorch.org
2022-06-14T19:43:16.4835186Z License: BSD-3
2022-06-14T19:43:16.4835452Z Location: /opt/conda/lib/python3.7/site-packages
2022-06-14T19:43:16.4835704Z Requires: typing-extensions
2022-06-14T19:43:16.4835925Z Required-by: 
2022-06-14T19:43:16.5194953Z + python check_forward_backward_compatibility.py --existing-schemas nightly_schemas.txt
2022-06-14T19:43:17.1102440Z Traceback (most recent call last):
2022-06-14T19:43:17.1102995Z   File "check_forward_backward_compatibility.py", line 345, in <module>
2022-06-14T19:43:17.1103274Z     s = parse_schema(line.strip())
2022-06-14T19:43:17.1103458Z RuntimeError: 
2022-06-14T19:43:17.1103722Z Unknown custom class type c10d.ProcessGroup. Please ensure it is registered.:
2022-06-14T19:43:17.1104351Z c10d::broadcast(__torch__.torch.classes.c10d.ProcessGroup _0, Tensor[] _1, int _2, int _3, int _4) -> __torch__.torch.classes.c10d.Work _0
2022-06-14T19:43:17.1104738Z                                              ~~~~~~~~~~~~ <--- HERE
2022-06-14T19:43:17.1104854Z 
2022-06-14T19:43:17.2151400Z + cleanup
2022-06-14T19:43:17.2151737Z + retcode=1
2022-06-14T19:43:17.2151985Z + set +x
2022-06-14T19:43:17.2191897Z ##[error]Process completed with exit code 1.
2022-06-14T19:43:17.2229802Z ##[group]Run pytorch/pytorch/.github/actions/get-workflow-job-id@master
2022-06-14T19:43:17.2230054Z with:

This comment was automatically generated by Dr. CI (expand for details).

Please report bugs/suggestions to the (internal) Dr. CI Users group.

Click here to manually regenerate this comment.

NivekT added a commit that referenced this pull request Jun 14, 2022
@NivekT NivekT requested a review from ejguan June 14, 2022 18:15
@NivekT NivekT changed the title [DataPipe] Fix error message coming from singler iterator constraint [DataPipe] Fix error message coming from single iterator constraint Jun 14, 2022
@NivekT NivekT added module: data torch.utils.data release notes: dataloader release notes category topic: improvements topic category labels Jun 14, 2022
full_msg = f"{msg} {datapipe.__class__.__name__}({_generate_input_args_string(datapipe)})"
if len(e.args) >= 1 and msg not in e.args[0]:
single_iterator_msg = "single iterator per IterDataPipe constraint"
has_len = hasattr(e.args, '__len__') and len(e.args) >= 1
Copy link
Contributor

Choose a reason for hiding this comment

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

Noob question: What would be the cause of this Error with __len__? Someone calling len(dp) in the middle of iteration?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The extra logic here is not related to the DataPipe,but rather, the exception being raised.

It is checking if e.args (arguments of the exception) has __len__ and if it does, there is more than one element. I have seen cases where e.args does not mean that criteria.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. Thanks for the explanation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Your question prompted me to take another look and handle the case where len(e.args) == 0 when there is no exception message. Have a look.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

class MyDataPipe(IterDataPipe):

    def __init__(self, x):
        self.x = x

    def __iter__(self):
        raise RuntimeError
        yield 0


dp = MyDataPipe(1)
it = iter(dp)
next(it)

Previously, it will just show:

Traceback (most recent call last):
  File "/Users/scratch/graphvisualization.py", line 27, in <module>
    next(it)
  File "/Users/pytorch/torch/utils/data/datapipes/_typing.py", line 514, in wrap_generator
    response = gen.send(None)
  File "/Users/scratch/graphvisualization.py", line 21, in __iter__
    raise RuntimeError
RuntimeError: 

Now it adds the extra message in the end:

This exception is thrown by __iter__ of MyDataPipe(x=1)

Copy link
Contributor

Choose a reason for hiding this comment

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

Makes sense. I hope at least TorchData doesn't have such kind of Error without message. lol

Copy link
Contributor

@ejguan ejguan left a comment

Choose a reason for hiding this comment

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

LGTM

full_msg = f"{msg} {datapipe.__class__.__name__}({_generate_input_args_string(datapipe)})"
if len(e.args) >= 1 and msg not in e.args[0]:
single_iterator_msg = "single iterator per IterDataPipe constraint"
has_len = hasattr(e.args, '__len__') and len(e.args) >= 1
Copy link
Contributor

Choose a reason for hiding this comment

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

I see. Thanks for the explanation.

…onstraint"


Fixes meta-pytorch/data#516

* Prevent confusion by not adding the extra string about `__iter__` when the exception is raised because of single iterator constraint
* Added a missing space
* Make sure `__len__` exists before calling it during exception handling


Exception example:
```python
from torchdata.datapipes.iter import IterableWrapper

source_dp = IterableWrapper(range(10))
it1 = iter(source_dp)
next(it1)
it2 = iter(source_dp)
next(it2)
next(it1)
```
Will raise:
```
Traceback (most recent call last):
  File "/Users/scratch/fast_forward.py", line 32, in <module>
    next(it1)
  File "/Users/pytorch/torch/utils/data/datapipes/_typing.py", line 524, in wrap_generator
    _check_iterator_valid(datapipe, iterator_id)
  File "/Users/pytorch/torch/utils/data/datapipes/_typing.py", line 445, in _check_iterator_valid
    raise RuntimeError(_gen_invalid_iterdatapipe_msg(datapipe) + _feedback_msg)
RuntimeError: This iterator has been invalidated because another iterator has been created from the same IterDataPipe: IterableWrapperIterDataPipe(deepcopy=True, iterable=range(0, 10))
This may be caused multiple references to the same IterDataPipe. We recommend using `.fork()` if that is necessary.
For feedback regarding this single iterator per IterDataPipe constraint, feel free to comment on this issue: meta-pytorch/data#45.
```

[ghstack-poisoned]
NivekT added a commit that referenced this pull request Jun 14, 2022
@NivekT NivekT requested a review from ejguan June 14, 2022 19:21
@NivekT
Copy link
Contributor Author

NivekT commented Jun 14, 2022

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

@pytorchbot successfully started a merge job. Check the current status here

@facebook-github-bot facebook-github-bot deleted the gh/nivekt/50/head branch June 18, 2022 14:17
facebook-github-bot pushed a commit that referenced this pull request Jun 20, 2022
…79547)

Summary:
Pull Request resolved: #79547

Approved by: https://github.com/ejguan

Test Plan: contbuild & OSS CI, see https://hud.pytorch.org/commit/pytorch/pytorch/22c7b1ddb593ca390e58c5b94f8ad10d21ed0f73

Reviewed By: malfet

Differential Revision: D37157039

Pulled By: NivekT

fbshipit-source-id: 36d5b68375fc1ef4e3326525003fcfbd46252124
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants