-
Notifications
You must be signed in to change notification settings - Fork 7k
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
[DataPipe] Properly cleanup unclosed files within generator function #6997
Conversation
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.
SGTM, thanks @ejguan! One question out of curiosity:
I found a corner case that
file.close
is never called because when generator function has never reached to the end. A simple example would bezip
two datepipes with different length. The longer DataPipe would never reach the end of generator and then it will be cleaned up bygc
. So, the line offile.close
is not executed.
Is this a problem for our datasets? Because that would indicate that something is off in our setup. If we zip, the datapipes should always have the same length. If not, did you figure out the issue for the datasets you fixed?
@pmeier |
How are they broken? Are we zipping two pipes of different length like in the example you gave? Because that likely is not intended and thus we should not just mitigate, but investigate why this happens.
Not sure I understand. Are you saying that with this PR (3/4) here, our datasets will be broken after the core PR (2/4) is merged? |
That makes sense. I have validated those datapipes have the same lengths. Changing
I am sorry that I should be clear that this PR is a blocker for pytorch/pytorch#89974. It's totally fine as long this PR is landed before pytorch/pytorch#89974.. |
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.
LGTM!
Makes sense, thanks Erjia! |
@pmeier Please feel free to merge this PR. |
Hey @pmeier! You merged this PR, but no labels were added. The list of valid labels is available at https://github.com/pytorch/vision/blob/main/.github/process_commit.py |
Summary: There is a case that `file.close` is never called because when generator function has never reached to the end. A simple example would be `zip` two datepipes with different length. The longer DataPipe would never reach the end of generator and then it will be cleaned up by `gc`. So, the line of `file.close` is not executed. (This is the reason that Vitaly has to create this [hack](https://github.com/pytorch/pytorch/blob/4451eb24e6287dff62ff8a7ec0eda6a6998807b0/torch/utils/data/datapipes/iter/combining.py#L573-L583) to retrieve all remaining data to make sure generator function is fully executed) However, this hack introduces another problem where an infinite datapipe would make `zip` never end as it would try to deplete the infinite iterator. See: #865 So, in this PR, I am adding a `try-finally` clause to make sure the `file.close` is always executed during the destruction of `generator` object. Then, we don't need the hack within `zip` any more. - pytorch/pytorch#89973 - pytorch/vision#6997 - pytorch/pytorch#89974 Pull Request resolved: #910 Reviewed By: wenleix, NivekT Differential Revision: D41633909 Pulled By: ejguan fbshipit-source-id: 5613e131dc8b2962c22d2bc7e3a4bb3039440c48
…function (#6997) Summary: Co-authored-by: Philip Meier <[email protected]> Reviewed By: datumbox Differential Revision: D41836894 fbshipit-source-id: b3a8c76245298f85b6ff19107bf2ef872bfb033a
There is a case that
file.close
is never called because when generator function has never reached to the end. A simple example would bezip
two datepipes with different length. The longer DataPipe would never reach the end of generator and then it will be cleaned up bygc
. So, the line offile.close
is not executed. (This is the reason that Vitaly has to create this hack to retrieve all remaining data to make sure generator function is fully executed)However, this hack introduces another problem where an infinite datapipe would make
zip
never end as it would try to deplete the infinite iterator. See: pytorch/data#865So, in this PR, I am adding a
try-finally
clause to make sure thefile.close
is always executed during the destruction ofgenerator
object. Then, we don't need the hack withinzip
any more.cc @pmeier @bjuncek