|
| 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 |
0 commit comments