def test_compare_model_stub_linear_dynamic_fx(self): r"""Compare the output of dynamic quantized linear layer and its float shadow module""" qconfig_dict = {"object_type": [(nn.Linear, default_dynamic_qconfig)]} float_model = SingleLayerLinearDynamicModel() float_model.eval() prepared_model = prepare_fx(float_model, qconfig_dict) prepared_float_model = copy.deepcopy(prepared_model) prepared_float_model.eval() q_model = convert_fx(prepared_model) linear_data = self.calib_data[0][0] module_swap_list = [nn.Linear] expected_ob_dict_keys = {"fc1.stats"} self.compare_and_validate_model_stub_results_fx( prepared_float_model, q_model, module_swap_list, expected_ob_dict_keys, linear_data, )
def test_compare_weights_linear_dynamic_fx(self): r"""Compare the weights of float and dynamic quantized linear layer""" def compare_and_validate_results(float_model, q_model): weight_dict = compare_weights_fx(float_model.state_dict(), q_model.state_dict()) self.assertEqual(len(weight_dict), 1) for k, v in weight_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) float_model = SingleLayerLinearDynamicModel() float_model.eval() qconfig = torch.quantization.qconfig.default_dynamic_qconfig qconfig_dict = {"": qconfig} prepared_model = prepare_fx(float_model, qconfig_dict) backup_prepared_model = copy.deepcopy(prepared_model) backup_prepared_model.eval() q_model = convert_fx(prepared_model) compare_and_validate_results(backup_prepared_model, q_model)
def test_compare_weights_linear_dynamic_fx(self): r"""Compare the weights of float and dynamic quantized linear layer""" qconfig_dict = {"object_type": [(nn.Linear, default_dynamic_qconfig)]} float_model = SingleLayerLinearDynamicModel() float_model.eval() prepared_model = prepare_fx(float_model, qconfig_dict) prepared_float_model = copy.deepcopy(prepared_model) prepared_float_model.eval() q_model = convert_fx(prepared_model) expected_weight_dict_keys = {"fc1._packed_params._packed_params"} self.compare_and_validate_model_weights_results_fx( prepared_float_model, q_model, expected_weight_dict_keys)
def test_compare_weights_linear_dynamic_fx(self): r"""Compare the weights of float and dynamic quantized linear layer""" qconfig = torch.quantization.qconfig.default_dynamic_qconfig qconfig_dict = {"": qconfig} float_model = SingleLayerLinearDynamicModel() float_model.eval() prepared_model = prepare_fx(float_model, qconfig_dict) backup_prepared_model = copy.deepcopy(prepared_model) backup_prepared_model.eval() q_model = convert_fx(prepared_model) expected_weight_dict_keys = {"fc1._packed_params._packed_params"} self.compare_and_validate_model_weights_results_fx( backup_prepared_model, q_model, expected_weight_dict_keys )
def test_compare_model_outputs_linear_dynamic_fx(self): r"""Compare the output of linear layer in dynamic quantized model and corresponding output of linear layer in float model """ qconfig_dict = {"object_type": [(nn.Linear, default_dynamic_qconfig)]} float_model = SingleLayerLinearDynamicModel() float_model.eval() prepared_model = prepare_fx(float_model, qconfig_dict) prepared_float_model = copy.deepcopy(prepared_model) q_model = convert_fx(prepared_model) linear_data = self.calib_data[0][0] expected_act_compare_dict_keys = {"x.stats", "fc1.stats"} self.compare_and_validate_model_outputs_results_fx( prepared_float_model, q_model, expected_act_compare_dict_keys, linear_data)