docstr: Test post-training static quantization on preserving
  pre forward and post forward hooks of original model
docstr_tok: ["Test","post-training","static","quantization","on","preserving","pre","forward","and","post","forward","hooks","of","original","model"]
code: def test_forward_hooks_preserved(self):
      r"""Test post-training static quantization on preserving
      pre forward and post forward hooks of original model
      """
      qengine = torch.backends.quantized.engine
      model = QuantStubModel()
      counter = {
          "pre_forwards": 0,
          "forwards": 0,
      }
  
      def fw_pre_hook(h_module, input):
          counter["pre_forwards"] += 1
  
      def fw_hook(h_module, input, output):
          counter["forwards"] += 1
  
      model.fc.register_forward_pre_hook(fw_pre_hook)
      model.fc.register_forward_hook(fw_hook)
  
      model.qconfig = torch.ao.quantization.get_default_qconfig(qengine)
      model = prepare(model)
  
      def checkHooksIsPresent(model, before_convert=True):
          num_fwd_hooks = 1
          if before_convert:
              self.assertEqual(
                  len(model.quant._forward_hooks.values()),
                  1,
                  "Quantization observer hook has disappeared",
              )
              num_fwd_hooks = 2
  
          self.assertObjectIn(
              fw_pre_hook, model.fc._forward_pre_hooks.values()
          )
          self.assertObjectIn(fw_hook, model.fc._forward_hooks.values())
          self.assertEqual(
              len(model.fc._forward_pre_hooks.values()),
              1,
              "Extra pre forward hooks have appeared on a layer",
          )
          # During static quantization non stub layers are provided with quantization observer hook too
          self.assertEqual(
              len(model.fc._forward_hooks.values()),
              num_fwd_hooks,
              "Extra post forward hooks have appeared on a layer",
          )
          # Implicitly check that fw_hook goes after _observer_forward_hook
          self.assertEqual(
              list(model.fc._forward_hooks.values())[-1],
              fw_hook,
              "_observer_forward_hook is not a first entry of the hooks list",
          )
  
      checkHooksIsPresent(model, True)
      test_only_eval_fn(model, self.calib_data)
      torch.ao.quantization.convert(model, inplace=True)
      checkHooksIsPresent(model, False)
  
imports: import torch
  import torch.nn
  import torch.ao.nn.quantized
  from torch.nn.utils.rnn import PackedSequence
  from torch.ao.quantization import quantize, prepare, convert, prepare_qat, quantize_dynamic, QuantWrapper, QuantStub, DeQuantStub, default_qconfig, default_dynamic_qconfig, per_channel_dynamic_qconfig, float16_dynamic_qconfig, float_qparams_weight_only_qconfig, float_qparams_weight_only_qconfig_4bit, FixedQParamsObserver, PerChannelMinMaxObserver, default_dynamic_quant_observer, default_weight_observer, QConfig
  from torch.testing._internal.common_quantization import QuantizationTestCase, AnnotatedSingleLayerLinearModel, QuantStubModel, ModelWithFunctionals, SingleLayerLinearDynamicModel, TwoLayerLinearModel, NestedModel, ResNetBase, RNNDynamicModel, RNNCellDynamicModel, ActivationsTestModel, NormalizationTestModel, test_only_eval_fn, prepare_dynamic, convert_dynamic, skipIfNoFBGEMM, EmbeddingBagModule, EmbeddingModule, EmbeddingWithStaticLinear, LinearReluLinearModel
  from torch.testing._internal.common_quantization import AnnotatedTwoLayerLinearModel, AnnotatedNestedModel, AnnotatedSubNestedModel, AnnotatedCustomConfigNestedModel, AnnotatedSkipQuantModel
  from torch.testing._internal.common_quantized import override_quantized_engine, supported_qengines, override_qengines
  from hypothesis import given
  from hypothesis import strategies
  import torch.testing._internal.hypothesis_utils
  from typing import Tuple
  import numpy
label_desc: Use image node_id 12 for calling the TestQuantizeEagerPTQStatic obj's underlying member method code with example usage: obj.test_forward_hooks_preserved() without return types