8000 [DataPipe] adding a finally statement to ensure hook is reset by NivekT · Pull Request #70214 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

[DataPipe] adding a finally statement to ensure hook is reset #70214

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

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions torch/utils/data/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ def reduce_hook(obj):
captured_connections.append(obj)
return stub_unpickler, ()

# TODO(VitalyFedyunin): Better do it as `with` context for safety
IterDataPipe.set_reduce_ex_hook(reduce_hook)
if exclude_primitive:
IterDataPipe.set_getstate_hook(getstate_hook)
try:
Copy link
Contributor Author
@NivekT NivekT Dec 21, 2021

Choose a reason for hiding this comment

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

@ejguan @VitalyFedyunin
I am under the impression that using try ... finally ... here will provide more safety. The reduce and getstate hooks will be re-set to None even if an exception is thrown.

Does that seem better? Or do we have to use a with statement?

IterDataPipe.set_reduce_ex_hook(reduce_hook)
if exclude_primitive:
IterDataPipe.set_getstate_hook(getstate_hook)
p.dump(scan_obj)
except AttributeError: # unpickable DataPipesGraph
pass # TODO(VitalyFedyunin): We need to tight this requirement after migrating from old DataLoader
IterDataPipe.set_reduce_ex_hook(None)
if exclude_primitive:
IterDataPipe.set_getstate_hook(None)
finally:
IterDataPipe.set_reduce_ex_hook(None)
if exclude_primitive:
IterDataPipe.set_getstate_hook(None)
return captured_connections


Expand Down
0