Skip to content
This repository was archived by the owner on Jan 22, 2025. It is now read-only.

Commit f8633f6

Browse files
metamate123facebook-github-bot
authored andcommitted
Fix cleaning forward hooks in flop counter
Summary: Pull Request resolved: #498 Removing `forward_pre_hooks` was a no-op (i.e. empty list of hooks). Fix similar to: pytorch/pytorch#49739 Reviewed By: frabu6 Differential Revision: D43770684 fbshipit-source-id: 47e8a87bc61e352760115616fe480d616fb9e1e6
1 parent 8cb5023 commit f8633f6

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

d2go/utils/flop_calculator.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,12 @@ def dump_flops_info(model, inputs, output_dir, use_eval_mode=True):
4545
logger.info("Failed to deepcopy the model and skip FlopsEstimation.")
4646
return
4747

48-
# delete other forward_pre_hooks so they are not simultaneously called
49-
for k in model._forward_pre_hooks:
50-
del model._forward_pre_hooks[k]
48+
# Delete other forward_pre_hooks so they are not simultaneously called.
49+
# The keys are wrapped in a list to avoid mutating ordered_dict during iteration.
50+
# See https://github.com/pytorch/pytorch/issues/49739 for more details.
51+
for hook_key in list(model._forward_pre_hooks.keys()):
52+
logger.warning(f"Forward hook with key {hook_key} was removed in flop counter.")
53+
model._forward_pre_hooks.pop(hook_key)
5154

5255
if use_eval_mode:
5356
model.eval()

0 commit comments

Comments
 (0)