I’ve just started to dive into quantization tools that were introduced in version 1.3.
For now I am trying to train a network with existing model generation code. The code has certain subtleties, one of these are _forward_pre_hooks in several submodules. (See code below)
Here is the problem. After prepare_qat with default config changes submodules to ones with fake quantization and hooks are disappeared. Is it possible to prevent hooks from disappearing during prepare_qat (and submodule.fuse() too)
As I can to prevent hooks from disappearing it is needed to put relevant code somewhere here (during prepare_qat):
But during fusion it is not that obvious because we map up to three modules to one and each of them could have hooks. I think it is possible to work around with torch.quantization.fuse_modules(...fuser_func=<func>...)
Yeah feel free to submit a PR for preserving the pre hooks in swapping.
For fusion I think we probably need to error out if you have a prehook for intermediate moduels like BatchNorm because when we fuse batchnorm into conv, batchnorm is gone.
So Jerry could you explain please for what purpose that was done?
After prepare() is called convert() will remove all hooks.
I think there’s a reason to create such hook than to remove it should we preserve all pre forward / post forward hooks except this one?
Yeah I think we should preserve it, but we need to consider each case carefully, since this is interfering with the quantization. That is, when do we run pre forward hook and post forward hook, do we run it before/after observation/quantization?
Assume that we have block1 who outputs some data, block2 who has pre forward hook and directly obtains data from first one and block3 who waits for the same data but obtains it using hook of second one.
In this particular example (EfficientNet implementation) during preparartion pre forward hook on block2 should be called after observation and therefore after quantization (because we collected statistics for that). If block2 happens to be the first in a row who work with quantized data it is very likely that block3 works with it too. Anyway we can place dequant before block3.
As for post forward hooks we do not modify input of the module so run them after observation and quantization
I think this requires that the hooks will do some meaningful computation for both quantized and unquantized data. But as long as we define the semantics clearly it should be OK. So after quantization, the for pre_forward hooks will work with quantized input from previous later, and the forward hooks will work with quantized output from current later, right?
You are right, we should check whether we are trying to preserve observer or right hook.
In my PR I have handled that case. Without it provided test set fails, with it works well.
I think I should extended test set to test new functionality. Would you mind to guide me in that?
I propose that it is needed to preserve pre and post hooks on fused modules, where it possible. While it hard to define how to preserve hooks for second and third module in sequence (because input data changes and three modules are collapsing into atomic one). But we can easily preserve pre_forward hook of base module.