From 2068355e0883023663a8b286668e47eded09f562 Mon Sep 17 00:00:00 2001 From: Xin He Date: Tue, 12 Dec 2023 10:39:52 +0800 Subject: [PATCH 1/4] fix bug in ipex Signed-off-by: Xin He --- neural_compressor/adaptor/torch_utils/util.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/neural_compressor/adaptor/torch_utils/util.py b/neural_compressor/adaptor/torch_utils/util.py index 5cd81ea5f7e..efa4483232d 100644 --- a/neural_compressor/adaptor/torch_utils/util.py +++ b/neural_compressor/adaptor/torch_utils/util.py @@ -461,12 +461,13 @@ def get_quantizable_ops_from_cfgs(ops_name, op_infos_from_cfgs, input_tensor_ids for input_tensor in input_tensors: if "inf_dtype" not in input_tensor.keys(): continue - if input_tensor["inf_dtype"] == torch.float32: - pre_op_name = input_tensor_ids_op_name[input_tensor["id"]] - if pre_op_name[1] in ["q_op_infos"]: - print(pre_op_name, "is not the fuse ops first op.") - start = False - continue + if input_tensor["inf_dtype"] == "torch.float32": + pre_op_names = input_tensor_ids_op_name[input_tensor["id"]] + for pre_op_name in pre_op_names: + if pre_op_name[1] in ["q_op_infos"]: + logger.info(pre_op_name, "is not the fuse ops first op.") + start = False + break if not start: continue # add quantizable ops, include op and fuse ops. From c2453efc8559bf5aaa5655e5a360ffe164d18a91 Mon Sep 17 00:00:00 2001 From: Xin He Date: Tue, 12 Dec 2023 10:53:54 +0800 Subject: [PATCH 2/4] skip logger Signed-off-by: Xin He --- neural_compressor/adaptor/torch_utils/util.py | 1 - 1 file changed, 1 deletion(-) diff --git a/neural_compressor/adaptor/torch_utils/util.py b/neural_compressor/adaptor/torch_utils/util.py index efa4483232d..4ab17e0f9d8 100644 --- a/neural_compressor/adaptor/torch_utils/util.py +++ b/neural_compressor/adaptor/torch_utils/util.py @@ -465,7 +465,6 @@ def get_quantizable_ops_from_cfgs(ops_name, op_infos_from_cfgs, input_tensor_ids pre_op_names = input_tensor_ids_op_name[input_tensor["id"]] for pre_op_name in pre_op_names: if pre_op_name[1] in ["q_op_infos"]: - logger.info(pre_op_name, "is not the fuse ops first op.") start = False break if not start: From c12bc21ec6759a3ace0e5b5a3da5268158799f7f Mon Sep 17 00:00:00 2001 From: Xin He Date: Tue, 12 Dec 2023 13:56:43 +0800 Subject: [PATCH 3/4] rewrite logic Signed-off-by: Xin He --- neural_compressor/adaptor/torch_utils/util.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/neural_compressor/adaptor/torch_utils/util.py b/neural_compressor/adaptor/torch_utils/util.py index 4ab17e0f9d8..3d07ce8846b 100644 --- a/neural_compressor/adaptor/torch_utils/util.py +++ b/neural_compressor/adaptor/torch_utils/util.py @@ -458,15 +458,13 @@ def get_quantizable_ops_from_cfgs(ops_name, op_infos_from_cfgs, input_tensor_ids op_info = op_infos_from_cfgs[name] output_tensors = op_info["output_tensor_infos"] input_tensors = op_info["input_tensor_infos"] - for input_tensor in input_tensors: - if "inf_dtype" not in input_tensor.keys(): - continue - if input_tensor["inf_dtype"] == "torch.float32": - pre_op_names = input_tensor_ids_op_name[input_tensor["id"]] - for pre_op_name in pre_op_names: - if pre_op_name[1] in ["q_op_infos"]: - start = False - break + start = any( + [ + input_tensor["inf_dtype"] != "torch.float32" + for input_tensor in input_tensors + if "inf_dtype" in input_tensor.keys() + ] + ) if not start: continue # add quantizable ops, include op and fuse ops. From d41b513eb8a7cf572ea28d4b3a89965695500cc1 Mon Sep 17 00:00:00 2001 From: Xin He Date: Mon, 25 Dec 2023 13:39:03 +0800 Subject: [PATCH 4/4] remove ut due to no add ops Signed-off-by: Xin He --- test/ipex/test_adaptor_ipex.py | 117 --------------------------------- 1 file changed, 117 deletions(-) diff --git a/test/ipex/test_adaptor_ipex.py b/test/ipex/test_adaptor_ipex.py index 6603b3460f0..7cb3555e2e6 100644 --- a/test/ipex/test_adaptor_ipex.py +++ b/test/ipex/test_adaptor_ipex.py @@ -316,62 +316,6 @@ def forward(self, a): ) self.assertTrue(isinstance(q_model._model, torch.jit.ScriptModule)) - def test_tune_add(self): - class M(torch.nn.Module): - def __init__(self): - super().__init__() - self.conv = torch.nn.Conv2d(3, 1, 1) - self.linear = torch.nn.Linear(224 * 224, 5) - - def forward(self, a): - x = self.conv(a) - x = x.view(1, -1) - x += x - x = self.linear(x) - return x - - model = M() - from neural_compressor import PostTrainingQuantConfig, quantization - - acc_lst = [1, 0.8, 1.1, 1.2] - - def fake_eval(model): - res = acc_lst.pop(0) - return res - - conf = PostTrainingQuantConfig(backend="ipex", quant_level=0) - calib_dataloader = Dataloader() - q_model = quantization.fit(model, conf, calib_dataloader=calib_dataloader, eval_func=fake_eval) - self.assertTrue(isinstance(q_model._model, torch.jit.ScriptModule)) - - def test_tune_add_with_recipe(self): - class M(torch.nn.Module): - def __init__(self): - super().__init__() - self.linear = torch.nn.Linear(224 * 224 * 3, 5) - - def forward(self, x): - x += x - x = x.view(1, -1) - x = self.linear(x) - return x - - model = M() - from neural_compressor import PostTrainingQuantConfig, quantization - - acc_lst = [1, 0.8, 1.1, 1.2] - - def fake_eval(model): - res = acc_lst.pop(0) - return res - - conf = PostTrainingQuantConfig( - backend="ipex", quant_level=0, recipes={"smooth_quant": True, "smooth_quant_args": {"alpha": 0.5}} - ) - calib_dataloader = Dataloader() - q_model = quantization.fit(model, conf, calib_dataloader=calib_dataloader, eval_func=fake_eval) - self.assertTrue(isinstance(q_model._model, torch.jit.ScriptModule)) - def test_tune_minmax_obs(self): class M(torch.nn.Module): def __init__(self): @@ -527,67 +471,6 @@ def forward(self, a): ) self.assertTrue(isinstance(q_model._model, torch.jit.ScriptModule)) - def test_tune_add(self): - class M(torch.nn.Module): - def __init__(self): - super().__init__() - self.conv = torch.nn.Conv2d(3, 1, 1) - self.linear = torch.nn.Linear(224 * 224, 5) - - def forward(self, a): - x = self.conv(a) - x = x.view(1, -1) - x += x - x = self.linear(x) - return x - - model = M().to("xpu") - from neural_compressor import PostTrainingQuantConfig, quantization - - acc_lst = [1, 0.8, 1.1, 1.2] - - def fake_eval(model): - res = acc_lst.pop(0) - return res - - conf = PostTrainingQuantConfig(backend="ipex", device="xpu", quant_level=0) - calib_dataloader = Dataloader(device="xpu") - q_model = quantization.fit(model, conf, calib_dataloader=calib_dataloader, eval_func=fake_eval) - self.assertTrue(isinstance(q_model._model, torch.jit.ScriptModule)) - - def test_tune_add_with_recipe(self): - class M(torch.nn.Module): - def __init__(self): - super().__init__() - self.conv = torch.nn.Conv2d(3, 1, 1) - self.linear = torch.nn.Linear(224 * 224, 5) - - def forward(self, a): - x = self.conv(a) - x = x.view(1, -1) - x += x - x = self.linear(x) - return x - - model = M().to("xpu") - from neural_compressor import PostTrainingQuantConfig, quantization - - acc_lst = [1, 0.8, 1.1, 1.2] - - def fake_eval(model): - res = acc_lst.pop(0) - return res - - conf = PostTrainingQuantConfig( - backend="ipex", - device="xpu", - quant_level=0, - recipes={"smooth_quant": True, "smooth_quant_args": {"alpha": 0.5}}, - ) - calib_dataloader = Dataloader(device="xpu") - q_model = quantization.fit(model, conf, calib_dataloader=calib_dataloader, eval_func=fake_eval) - self.assertTrue(isinstance(q_model._model, torch.jit.ScriptModule)) - class TestMixedPrecision(unittest.TestCase): @classmethod