Open ejguan opened 1 year ago
I think the solution might be:
finalize_iteration
within DataLoader2
's __iter__
or DataLoader2Iterator
's __init__
to ensure that, if there is an existing RS reading through, that gets terminated properly
__next__
is called whether the iterator is valid, otherwise we can't know?finalize_iteration
, just raise exceptionThoughts?
Thinking a little bit on those options. I think the only possible solution might be always calling finalize_iteration
before initialize_iteration
in DL2
's __iter__
. In the following case with the current implementation:
iter_a = iter(dl)
iter_b = iter(dl) # `initialize_iteration` called first and we never properly call `finalize_iteration` beforehand
So, it might be better to call finalize_iteration
if initialize_iteration
has been called before
Agree with that.
🐛 Describe the bug
We do
finalize_iteration
when__next__
is called for the invalidDataLoader2Iterator
(let's call it Iter_A):https://github.com/pytorch/data/blob/c42587a828d05f24f6f0586d17d3e9d55e1433ed/torchdata/dataloader2/dataloader2.py#L67-L68
However, as a new
DataLoader2Iterator
(Iter_B) has been created and the sameReadingService
has already calledinitialize
here: https://github.com/pytorch/data/blob/c42587a828d05f24f6f0586d17d3e9d55e1433ed/torchdata/dataloader2/dataloader2.py#L135When someone called
next(Iter_A)
afterwards, it willfinalize_iteration
of the sameReadingService
. Then, theIter_B
would exit earlier than expected.Solution
We should move all logic from
__next__
to__init__
forDataLoader2Iterator
cc: @NivekT
Versions
main branch