From 160f10890747da7a051f1b42c5ac86eca9a6293c Mon Sep 17 00:00:00 2001 From: kaiyaointel Date: Mon, 5 Dec 2022 16:08:53 +0800 Subject: [PATCH 1/5] Create tf_inc_static_quant.yaml --- .../backends/tf_inc_static_quant.yaml | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 neural_coder/backends/tf_inc_static_quant.yaml diff --git a/neural_coder/backends/tf_inc_static_quant.yaml b/neural_coder/backends/tf_inc_static_quant.yaml new file mode 100644 index 00000000000..ef1e53503e5 --- /dev/null +++ b/neural_coder/backends/tf_inc_static_quant.yaml @@ -0,0 +1,29 @@ +# Copyright (c) 2022 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +transformation: + location: + - ["insert_below_model_definition_line"] + content: + - |- + [+] from neural_compressor.conf.config import QuantConf + [+] from neural_compressor.experimental import Quantization, common + [+] quant_config = QuantConf() + [+] quant_config.usr_cfg.model.framework = "tensorflow" + [+] quantizer = Quantization(quant_config) + [+] quantizer.model = common.Model(MODEL_NAME) + [+] MODEL_NAME = quantizer.fit() + order: + - below: + above: From e2804f69995ac1c0ae12f1ab829da84f616be0cb Mon Sep 17 00:00:00 2001 From: kaiyaointel Date: Mon, 5 Dec 2022 16:18:52 +0800 Subject: [PATCH 2/5] Create inc.py --- neural_coder/coders/tensorflow/inc.py | 49 +++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 neural_coder/coders/tensorflow/inc.py diff --git a/neural_coder/coders/tensorflow/inc.py b/neural_coder/coders/tensorflow/inc.py new file mode 100644 index 00000000000..877f049a8b2 --- /dev/null +++ b/neural_coder/coders/tensorflow/inc.py @@ -0,0 +1,49 @@ +# Copyright (c) 2022 Intel Corporation +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from ...utils.line_operation import get_line_left_hand_side + +class TensorFlowKerasINC(object): + def __init__(self, file) -> None: + self.file = file + self.result = [] + self.keras_edited_flag = False + + def transform(self): + # import pdb + # pdb.set_trace() + lines = self.file.split('\n') + for line in lines: + if self.is_modify(line): + self.result.append(line) + self.result.append("from neural_compressor.conf.config import QuantConf") + self.result.append("from neural_compressor.experimental import Quantization") + self.result.append("from neural_compressor.experimental import common") + self.result.append("quant_config = QuantConf()") + self.result.append("quant_config.usr_cfg.model.framework = 'tensorflow'") + self.result.append("quantizer = Quantization(quant_config)") + self.result.append("quantizer.model = common.Model(" + model_name + ")") + self.result.append(model_name + " = quantizer.fit()") + else: + self.result.append(line) + for index, line in enumerate(self.result): + if index != len(self.result)-1: + self.result[index] += '\n' + return ''.join(self.result) + + def is_modify(self, s): + if 'model = tf.' in s: + return True + else: + return False From 54b9ecdceb41d33fca097f8a4c9d8789b6799d4b Mon Sep 17 00:00:00 2001 From: kaiyaointel Date: Mon, 5 Dec 2022 16:19:01 +0800 Subject: [PATCH 3/5] Delete tf_inc_static_quant.yaml --- .../backends/tf_inc_static_quant.yaml | 29 ------------------- 1 file changed, 29 deletions(-) delete mode 100644 neural_coder/backends/tf_inc_static_quant.yaml diff --git a/neural_coder/backends/tf_inc_static_quant.yaml b/neural_coder/backends/tf_inc_static_quant.yaml deleted file mode 100644 index ef1e53503e5..00000000000 --- a/neural_coder/backends/tf_inc_static_quant.yaml +++ /dev/null @@ -1,29 +0,0 @@ -# Copyright (c) 2022 Intel Corporation -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -transformation: - location: - - ["insert_below_model_definition_line"] - content: - - |- - [+] from neural_compressor.conf.config import QuantConf - [+] from neural_compressor.experimental import Quantization, common - [+] quant_config = QuantConf() - [+] quant_config.usr_cfg.model.framework = "tensorflow" - [+] quantizer = Quantization(quant_config) - [+] quantizer.model = common.Model(MODEL_NAME) - [+] MODEL_NAME = quantizer.fit() - order: - - below: - above: From 78ab81c32670a8e45953a72ad0dc4d51b6dca002 Mon Sep 17 00:00:00 2001 From: kaiyaointel Date: Mon, 5 Dec 2022 16:21:25 +0800 Subject: [PATCH 4/5] Update interface.py --- neural_coder/interface.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/neural_coder/interface.py b/neural_coder/interface.py index ac062b681bf..770baa7fef3 100644 --- a/neural_coder/interface.py +++ b/neural_coder/interface.py @@ -138,7 +138,10 @@ def enable( "pytorch_aliblade", "tensorflow_amp", "keras_amp", - "onnx_inc_static_quant_qlinear" + "tensorflow_inc", + "onnx_inc_static_quant_qlinear", + "onnx_inc_static_quant_qdq", + "onnx_inc_dynamic_quant", ] ''' @@ -332,6 +335,9 @@ def enable( if "tensorflow_mixed_precision" in features: from .coders.tensorflow.amp import TensorFlowKerasAMP list_transformed_code[i] = TensorFlowKerasAMP(list_transformed_code[i]).transform() + if "tensorflow_inc" in features: + from .coders.tensorflow.inc import TensorFlowKerasINC + list_transformed_code[i] = TensorFlowKerasINC(list_transformed_code[i]).transform() logger.info(f"Code transformation for feature: [{feature}] finished.") From 3c0823e974866d871b6e26732a157c8d6d38db86 Mon Sep 17 00:00:00 2001 From: kaiyaointel Date: Mon, 5 Dec 2022 16:23:13 +0800 Subject: [PATCH 5/5] Update inc.py --- neural_coder/coders/tensorflow/inc.py | 1 - 1 file changed, 1 deletion(-) diff --git a/neural_coder/coders/tensorflow/inc.py b/neural_coder/coders/tensorflow/inc.py index 877f049a8b2..06791c855b7 100644 --- a/neural_coder/coders/tensorflow/inc.py +++ b/neural_coder/coders/tensorflow/inc.py @@ -18,7 +18,6 @@ class TensorFlowKerasINC(object): def __init__(self, file) -> None: self.file = file self.result = [] - self.keras_edited_flag = False def transform(self): # import pdb