Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix adptor
Signed-off-by: zehao-intel <[email protected]>
  • Loading branch information
zehao-intel committed Jul 17, 2024
commit 2122b8e6ab8155643e3ea1bbd59f820f04de92a7
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def quantize(
self.approach != "quant_aware_training"
), "Quantize Aware Training is not supported on TensorFlow framework now!"

self.calib_sampling_size = calib_dataloader.batch_size * calib_iteration
self.calib_sampling_size = calib_dataloader.batch_size * calib_iteration if calib_dataloader else 100
tune_cfg = self.parse_quant_config(quant_config, model, calib_iteration)
self._tuning_cfg_to_fw(tune_cfg)
self.bf16_ops.extend(self.smooth_quant_mul_ops)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ def _inference(self, model):
Args:
model(TensorflowBaseModel): input TensorflowBaseModel
"""
if self.calib_func:
self.calib_func(model)
return

if model.model_type == "llm_saved_model":
self._inference_llm(model)
return
Expand Down
29 changes: 11 additions & 18 deletions test/3x/tensorflow/test_quantize_model.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (c) 2022 Intel Corporation
# Copyright (c) 2024 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -15,7 +15,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import os
import math
import shutil
import time
import unittest
Expand All @@ -24,7 +24,7 @@
import tensorflow as tf
from tensorflow import keras

from neural_compressor.common import Logger
from neural_compressor.common import logger
from neural_compressor.tensorflow.utils import version1_gte_version2

def build_model():
Expand Down Expand Up @@ -67,7 +67,7 @@ def build_model():

print("Baseline test accuracy:", baseline_model_accuracy)
if version1_gte_version2(tf.__version__, "2.16.1"):
model.save("baseline_model.keras")
model.export("baseline_model")
else:
model.save("baseline_model")

Expand Down Expand Up @@ -109,8 +109,6 @@ def __len__(self):


def evaluate(model):
from neural_compressor.tensorflow import Model
model = Model(model)
input_tensor = model.input_tensor
output_tensor = model.output_tensor if len(model.output_tensor)>1 else \
model.output_tensor[0]
Expand All @@ -134,16 +132,11 @@ class TestQuantizeModel(unittest.TestCase):
@classmethod
def setUpClass(self):
build_model()
self.fp32_model_path = (
"baseline_model.keras" if version1_gte_version2(tf.__version__, "2.16.1") else "baseline_model"
)
self.fp32_model_path = "baseline_model"

@classmethod
def tearDownClass(self):
if self.fp32_model_path.endswith(".keras"):
os.remove(self.fp32_model_path)
else:
shutil.rmtree(self.fp32_model_path, ignore_errors=True)
shutil.rmtree(self.fp32_model_path, ignore_errors=True)

def test_calib_func(self):
logger.info("Run test_calib_func case...")
Expand All @@ -154,14 +147,14 @@ def test_calib_func(self):
set_random_seed(9527)
quant_config = StaticQuantConfig()
q_model = quantize_model(self.fp32_model_path, quant_config, calib_func=evaluate)
conv2d_quantized = False
for node in qmodel.graph_def.node:
quantized = False
for node in q_model.graph_def.node:
if "Quantized" in node.op:
conv2d_quantized = True
quantized = True
break

self.assertEqual(conv2d_quantized, True)
self.assertEqual(quantized, True)


if __name__ == "__main__":
unittest.main()
unittest.main()