class_docstr: This series of tests checks that various steps of the quantization and sparsity flow
  compose cleanly despite variation in sequencing.
class_docstr_tok: ["This","series","of","tests","checks","that","various","steps","of","the","quantization","and","sparsity","flow","compose","cleanly","despite","variation","in","sequencing","."]
docstr: This test checks that the ordering of prepare_fx -> sparse prepare -> convert_fx
  compose cleanly without issue and that the final result is sparsified without
  having to call squash mask between sparse prepare and convert_fx. This also tests the
  automatic fusion that occurs during prepare_fx.
docstr_tok: ["This","test","checks","that","the","ordering","of","prepare_fx","-",">","sparse","prepare","-",">","convert_fx","compose","cleanly","without","issue","and","that","the","final","result","is","sparsified","without","having","to","call","squash","mask","between","sparse","prepare","and","convert_fx",".","This","also","tests","the","automatic","fusion","that","occurs","during","prepare_fx","."]
code: def test_q_prep_fx_before_s_prep(self):
      r"""
      This test checks that the ordering of prepare_fx -> sparse prepare -> convert_fx
      compose cleanly without issue and that the final result is sparsified without
      having to call squash mask between sparse prepare and convert_fx. This also tests the
      automatic fusion that occurs during prepare_fx.
      """
      (
          mod,
          sparsifier,
          _,
      ) = _get_model_and_sparsifier_and_sparse_config()
  
      example = torch.randn(1, 4, 4, 4)
      qconfig = tq.get_default_qconfig("fbgemm")
      qconfig_mapping = (
          tq.QConfigMapping()
          .set_module_name("4", qconfig)
          .set_module_name("5", qconfig)
      )
  
      mod = prepare_fx(mod, qconfig_mapping, (example,))
  
      # its absolutely broken by auto fusion in fx
      # but will still work if you put the correct fqn in
      sparse_config = [
          {
              "tensor_fqn": "5.0.weight",
              "sparsity_level": 0.7,
              "sparse_block_shape": (1, 4),
              "zeros_per_block": 4,
          },
          {"tensor_fqn": "0.0.weight"},
      ]
      sparsifier.prepare(mod, config=sparse_config)
  
      # check that correct modules had parametrizations added and
      # that none were lost during prepare
      self.assertTrue(
          hasattr(fqn_to_module(mod, "0.0"), "parametrizations")
      )
      self.assertTrue(
          hasattr(fqn_to_module(mod, "5.0"), "parametrizations")
      )
  
      # check that correct observers were inserted and that matching
      # occurred successfully
      self.assertTrue(_module_has_activation_post_process(mod, "5"))
      sparsifier.step()
      sparsity_level = _calculate_sparsity(
          fqn_to_module(mod, "5.0.weight")
      )
      mod(example)
      mod = convert_fx(mod)
  
      # check that final module is the expected quantized module and that the model runs
      self.assertTrue(
          isinstance(
              fqn_to_module(mod, "5"),
              torch.ao.nn.intrinsic.quantized.LinearReLU,
          )
      )
      self.assertEqual(mod(example).shape, torch.Size([1, 4, 4, 4]))
  
      # check that module was actually sparsified
      cur_sparsity = _calculate_sparsity(
          fqn_to_module(mod, "5")._weight_bias()[0]
      )
      self.assertGreaterAlmostEqual(cur_sparsity, sparsity_level)
      self.assertGreaterAlmostEqual(
          sparsity_level, sparse_config[0]["sparsity_level"]
      )
      self.assertGreaterAlmostEqual(
          cur_sparsity, sparse_config[0]["sparsity_level"]
      )
  
imports: import logging
  import torch
  import torch.ao.quantization
  from torch import nn
  from torch.ao import pruning
  from torch.testing._internal.common_utils import TestCase
  from torch.ao.quantization.quantize_fx import prepare_fx, convert_fx, convert_to_reference_fx, prepare_qat_fx
  from torch.ao.pruning import fqn_to_module
label_desc: Use image node_id 1 for calling the TestFxComposability obj's underlying member method code with example usage: obj.test_q_prep_fx_before_s_prep() without return types