docstr: Test the post-training quantization flow, serialization and scripting
  of embedding_bag modules
docstr_tok: ["Test","the","post-training","quantization","flow",",","serialization","and","scripting","of","embedding_bag","modules"]
code: def test_quantized_embedding_bag(self):
      r"""Test the post-training quantization flow, serialization and scripting
      of embedding_bag modules
      """
      indices = torch.tensor(
          [
              9,
              6,
              5,
              7,
              8,
              8,
              9,
              2,
              8,
              6,
              6,
              9,
              1,
              6,
              8,
              8,
              3,
              2,
              3,
              6,
              3,
              6,
              5,
              7,
              0,
              8,
              4,
              6,
              5,
              8,
              2,
              3,
          ]
      )
      offsets = torch.tensor([0, 19, 20, 28, 28, 32])
      weights = torch.randn(10, 12, dtype=torch.float32)
  
      for dtype in [torch.quint8, torch.quint4x2]:
          model = EmbeddingBagModule().eval()
          float_qparams_observer = PerChannelMinMaxObserver.with_args(
              dtype=dtype,
              qscheme=torch.per_channel_affine_float_qparams,
              ch_axis=0,
          )
          float_qparams_qconfig = QConfig(
              activation=default_dynamic_quant_observer,
              weight=float_qparams_observer,
          )
          model.qconfig = float_qparams_qconfig
  
          prepare(model, inplace=True)
          quantized_model = convert(model)
  
          per_sample_weights = torch.from_numpy(
              np.random.uniform(
                  low=0.01, high=0.5, size=[len(indices)]
              ).astype(np.float32)
          )
  
          # Test to make sure module is quantized correctly.
          self.assertTrue(
              "QuantizedEmbeddingBag" in str(quantized_model)
          )
          self.checkDynamicQuantizedModule(
              quantized_model.emb,
              torch.ao.nn.quantized.EmbeddingBag,
              torch.quint8,
          )
          self.checkScriptable(
              quantized_model,
              [[indices, offsets, per_sample_weights]],
              check_save_load=True,
          )
  
          class EmbeddingBagWithLinear(torch.nn.Module):
              def __init__(self):
                  super().__init__()
                  self.emb = torch.nn.EmbeddingBag(
                      num_embeddings=10,
                      embedding_dim=12,
                      include_last_offset=True,
                      scale_grad_by_freq=False,
                      mode="sum",
                  )
                  self.fc = torch.nn.Linear(5, 5)
  
              def forward(
                  self, indices, offsets, per_sample_weights, linear_in
              ):
                  return self.emb(
                      indices, offsets, per_sample_weights
                  ), self.fc(linear_in)
  
          # Test quantization of embedding_bag layer only
          model2 = EmbeddingBagWithLinear().eval()
          model2.emb.qconfig = float_qparams_qconfig
          prepare(model2, inplace=True)
          quantized_model = convert(model2)
  
          self.assertTrue(
              "QuantizedEmbeddingBag" in str(quantized_model)
          )
          self.checkLinear(model2.fc)
          self.checkDynamicQuantizedModule(
              quantized_model.emb,
              torch.ao.nn.quantized.EmbeddingBag,
              torch.quint8,
          )
  
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 15 for calling the TestQuantizeEagerPTQStatic obj's underlying member method code with example usage: obj.test_quantized_embedding_bag() and returns: self,  self