Skip to content

Commit e456465

Browse files
kaikaiyaozehao-intel
authored andcommitted
Neural Coder enable support of TensorFlow/Keras models to quantize (preliminary) (#217)
* Create tf_inc_static_quant.yaml * Create inc.py * Delete tf_inc_static_quant.yaml * Update interface.py * Update inc.py Signed-off-by: zehao-intel <[email protected]>
1 parent 16b2fe7 commit e456465

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright (c) 2022 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from ...utils.line_operation import get_line_left_hand_side
16+
17+
class TensorFlowKerasINC(object):
18+
def __init__(self, file) -> None:
19+
self.file = file
20+
self.result = []
21+
22+
def transform(self):
23+
# import pdb
24+
# pdb.set_trace()
25+
lines = self.file.split('\n')
26+
for line in lines:
27+
if self.is_modify(line):
28+
self.result.append(line)
29+
self.result.append("from neural_compressor.conf.config import QuantConf")
30+
self.result.append("from neural_compressor.experimental import Quantization")
31+
self.result.append("from neural_compressor.experimental import common")
32+
self.result.append("quant_config = QuantConf()")
33+
self.result.append("quant_config.usr_cfg.model.framework = 'tensorflow'")
34+
self.result.append("quantizer = Quantization(quant_config)")
35+
self.result.append("quantizer.model = common.Model(" + model_name + ")")
36+
self.result.append(model_name + " = quantizer.fit()")
37+
else:
38+
self.result.append(line)
39+
for index, line in enumerate(self.result):
40+
if index != len(self.result)-1:
41+
self.result[index] += '\n'
42+
return ''.join(self.result)
43+
44+
def is_modify(self, s):
45+
if 'model = tf.' in s:
46+
return True
47+
else:
48+
return False

neural_coder/interface.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ def enable(
139139
"pytorch_aliblade",
140140
"tensorflow_amp",
141141
"keras_amp",
142-
"onnx_inc_static_quant_qlinear"
142+
"tensorflow_inc",
143+
"onnx_inc_static_quant_qlinear",
144+
"onnx_inc_static_quant_qdq",
145+
"onnx_inc_dynamic_quant",
143146
]
144147
'''
145148

@@ -350,6 +353,9 @@ def enable(
350353
if "tensorflow_mixed_precision" in features:
351354
from .coders.tensorflow.amp import TensorFlowKerasAMP
352355
list_transformed_code[i] = TensorFlowKerasAMP(list_transformed_code[i]).transform()
356+
if "tensorflow_inc" in features:
357+
from .coders.tensorflow.inc import TensorFlowKerasINC
358+
list_transformed_code[i] = TensorFlowKerasINC(list_transformed_code[i]).transform()
353359
# Change Trainer to NLPTrainer (only for intel_extension_for_pytorch)
354360
if "change_trainer_to_nlptrainer" in features:
355361
from .coders.pytorch.change_trainer_to_nlptrainer import TrainerToNLPTrainer

0 commit comments

Comments
 (0)