Skip to content

Commit

Permalink
eager quant: fix error with removing forward hooks (pytorch#49813)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: pytorch#49813

pytorch#49739 reports a crash
where removing forward hooks results in a

```
RuntimeError: OrderedDict mutated during iteration
```

Unfortunately I cannot repro this inside the PyTorch module, but the issue
author has a good point and and we should not mutate the dict inside
of the iteration.

Test Plan:
```
// test plan from pytorch#46871 which
// originally added this
python test/test_quantization.py TestEagerModeQATOps
```

Imported from OSS

Reviewed By: jerryzh168

Differential Revision: D25698725

fbshipit-source-id: 13069d0d5017a84038c8f7be439a3ed537938ac6
  • Loading branch information
vkuzo authored and hwangdeyu committed Jan 14, 2021
1 parent 8d51789 commit ae1bdc1
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion torch/quantization/quantize.py
Expand Up @@ -258,9 +258,12 @@ def _remove_activation_post_process(module):
delattr(module, 'activation_post_process')

# remove activation_post_proceess hook
handle_ids_to_remove = set()
for handle_id, hook_fn in module._forward_hooks.items():
if hook_fn is _observer_forward_hook:
module._forward_hooks.pop(handle_id)
handle_ids_to_remove.add(handle_id)
for handle_id in handle_ids_to_remove:
module._forward_hooks.pop(handle_id)

# TODO: rename to something more general
def _remove_qconfig(module):
Expand Down

0 comments on commit ae1bdc1

Please sign in to comment.