def compare_and_validate_results(float_model, q_model, module_swap_list, data): ob_dict = compare_model_stub(float_model, q_model, module_swap_list, data) self.assertEqual(len(ob_dict), 1) for k, v in ob_dict.items(): self.assertTrue(len(v["float"]) == len(v["quantized"])) for i, val in enumerate(v["quantized"]): self.assertTrue(v["float"][i].shape == v["quantized"][i].shape)
def test_compare_model_stub_functional_static(self): r"""Compare the output of static quantized functional layer and its float shadow module""" qengine = torch.backends.quantized.engine model = ModelWithFunctionals().eval() model.qconfig = torch.ao.quantization.get_default_qconfig("fbgemm") q_model = prepare(model, inplace=False) q_model(self.img_data_2d[0][0]) q_model = convert(q_model) module_swap_list = [nnq.FloatFunctional] ob_dict = compare_model_stub( model, q_model, module_swap_list, self.img_data_2d[0][0] ) self.assertEqual(len(ob_dict), 6) self.assertTrue(isinstance(q_model.mycat, Shadow)) self.assertTrue(isinstance(q_model.myadd, Shadow)) self.assertTrue(isinstance(q_model.mymul, Shadow)) self.assertTrue(isinstance(q_model.myadd_relu, Shadow)) self.assertTrue(isinstance(q_model.my_scalar_add, Shadow)) self.assertTrue(isinstance(q_model.my_scalar_mul, Shadow)) for k, v in ob_dict.items(): self.assertTrue(len(v["float"]) == len(v["quantized"])) for i, val in enumerate(v["quantized"]): self.assertTrue(v["float"][i].shape == v["quantized"][i].shape)
def test_compare_model_stub_submodule_static(self): r"""Compare the output of static quantized submodule and its float shadow module""" qengine = torch.backends.quantized.engine model = ModelWithSubModules().eval() q_model = quantize(model, test_only_eval_fn, [self.img_data_2d]) module_swap_list = [SubModule, nn.Conv2d] ob_dict = compare_model_stub(model, q_model, module_swap_list, self.img_data_2d[0][0]) # Since conv is not quantized, we do not insert a shadow module # mod1 contains a linear that is quantized, so we insert a shadow module self.assertTrue(isinstance(q_model.mod1, Shadow)) self.assertFalse(isinstance(q_model.conv, Shadow))