Skip to content

Commit ba75648

Browse files
authored
Add Supplemental UTs of TF for INC 3.0 NewAPI (#1677)
Signed-off-by: zehao-intel <zehao.huang@intel.com> Signed-off-by: chensuyue <suyue.chen@intel.com>
1 parent b43eac3 commit ba75648

File tree

88 files changed

+15958
-229
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+15958
-229
lines changed

.azure-pipelines/scripts/install_nc.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ if [[ $1 = *"3x_pt" ]]; then
66
python -m pip install --no-cache-dir -r requirements_pt.txt
77
python setup.py pt bdist_wheel
88
pip install dist/neural_compressor*.whl --force-reinstall
9-
elif [[ $1 = *"3x_tf" ]]; then
9+
elif [[ $1 = *"3x_tf"* ]]; then
1010
python -m pip install --no-cache-dir -r requirements_tf.txt
1111
python setup.py tf bdist_wheel
1212
pip install dist/neural_compressor*.whl --force-reinstall

.azure-pipelines/scripts/ut/3x/run_3x_tf.sh

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ inc_path=$(python -c 'import neural_compressor; print(neural_compressor.__path__
1616
cd /neural-compressor/test/3x || exit 1
1717
rm -rf torch
1818
rm -rf onnxrt
19+
rm -rf tensorflow/quantization/ptq/newapi
1920
mv tensorflow/keras ../3x_keras
21+
mv tensorflow/quantization/itex ./3x_itex
2022

2123
LOG_DIR=/neural-compressor/log_dir
2224
mkdir -p ${LOG_DIR}
@@ -27,8 +29,9 @@ pytest --cov="${inc_path}" --cov-append -vs --disable-warnings --html=report_tf.
2729

2830
rm -rf tensorflow/*
2931
mv ../3x_keras tensorflow/keras
32+
mv ../3x_itex tensorflow/quantization/itex
3033
pip install intel-extension-for-tensorflow[cpu]
31-
pytest --cov="${inc_path}" --cov-append -vs --disable-warnings --html=report_keras.html --self-contained-html ./tensorflow/keras 2>&1 | tee -a ${ut_log_name}
34+
pytest --cov="${inc_path}" --cov-append -vs --disable-warnings --html=report_keras.html --self-contained-html ./tensorflow 2>&1 | tee -a ${ut_log_name}
3235

3336
mkdir -p report
3437
mv *.html report
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
python -c "import neural_compressor as nc"
3+
test_case="run 3x New TF API"
4+
echo "${test_case}"
5+
6+
# install requirements
7+
echo "set up UT env..."
8+
pip install -r /neural-compressor/test/3x/tensorflow/requirements.txt
9+
pip install pytest-html
10+
pip install pytest-html-merger
11+
12+
pip uninstall tensorflow -y
13+
pip install /tf_dataset/tf_binary/230928/tensorflow*.whl
14+
pip install cmake
15+
pip install protobuf==3.20.3
16+
pip install horovod==0.27.0
17+
pip list
18+
19+
cd /neural-compressor/test/3x || exit 1
20+
mv tensorflow/quantization/ptq/newapi ../3x_newapi
21+
rm -rf ./*
22+
23+
LOG_DIR=/neural-compressor/log_dir
24+
mkdir -p ${LOG_DIR}
25+
ut_log_name=${LOG_DIR}/ut_3x_new_tf.log
26+
27+
mkdir -p tensorflow/quantization/ptq
28+
mv ../3x_newapi tensorflow/quantization/ptq/newapi
29+
30+
pytest -vs --disable-warnings --html=report_new_tf_quant_one_case.html --self-contained-html ./tensorflow/quantization/ptq/newapi/test_big_saved_model.py 2>&1 | tee -a ${ut_log_name}
31+
rm -rf tensorflow/quantization/ptq/newapi/test_big_saved_model.py
32+
pytest -vs --disable-warnings --html=report_new_tf_quant.html --self-contained-html ./tensorflow/quantization/ptq/newapi 2>&1 | tee -a ${ut_log_name}
33+
34+
mkdir -p report
35+
mv *.html report
36+
pytest_html_merger -i ./report -o ./report.html
37+
38+
cp report.html ${LOG_DIR}/
39+
40+
if [ $(grep -c '== FAILURES ==' ${ut_log_name}) != 0 ] || [ $(grep -c '== ERRORS ==' ${ut_log_name}) != 0 ] || [ $(grep -c ' passed' ${ut_log_name}) == 0 ]; then
41+
echo "Find errors in pytest case, please check the output..."
42+
echo "Please search for '== FAILURES ==' or '== ERRORS =='"
43+
exit 1
44+
fi
45+
46+
echo "UT finished successfully! "

.azure-pipelines/ut-3x-tf.yml

+13
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ stages:
4141
uploadPath: $(UPLOAD_PATH)
4242
utArtifact: "ut_3x"
4343

44+
- stage: NewTF
45+
displayName: Unit Test 3x New TF API
46+
dependsOn: []
47+
jobs:
48+
- job:
49+
displayName: Unit Test 3x New TF API
50+
steps:
51+
- template: template/ut-template.yml
52+
parameters:
53+
dockerConfigName: "commonDockerConfig"
54+
utScriptFileName: "3x/run_3x_tf_new_api"
55+
uploadPath: $(UPLOAD_PATH)
56+
utArtifact: "ut_3x_tf_new_api"
4457

4558
- stage: TensorFlow_baseline
4659
displayName: Unit Test 3x TensorFlow baseline

neural_compressor/tensorflow/algorithms/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414

1515

1616
from neural_compressor.tensorflow.algorithms.smoother import SmoothQuant
17-
from neural_compressor.tensorflow.algorithms.static_quant import KerasAdaptor, TensorFlowAdaptor
17+
from neural_compressor.tensorflow.algorithms.static_quant import KerasAdaptor, TensorFlowAdaptor, Tensorflow_ITEXAdaptor

neural_compressor/tensorflow/algorithms/smoother/core.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
from neural_compressor.tensorflow.algorithms.smoother.scaler import SmoothQuantScaler, SmoothQuantScalerLLM
2929
from neural_compressor.tensorflow.quantization.config import SmoothQuantConfig
3030
from neural_compressor.tensorflow.quantization.utils.graph_util import GraphAnalyzer
31-
from neural_compressor.tensorflow.utils import SPR_BASE_VERSIONS, BaseModel, TensorflowLLMModel, framework_specific_info
31+
from neural_compressor.tensorflow.utils import SPR_BASE_VERSIONS, BaseModel, TensorflowLLMModel, TFConfig
3232

3333

3434
class SmoothQuant:
@@ -55,8 +55,8 @@ def __init__(
5555
self.calib_iteration = calib_iteration
5656

5757
self.new_api = tf.version.VERSION in SPR_BASE_VERSIONS
58-
self.device = framework_specific_info["device"]
59-
self.itex_mode = framework_specific_info["backend"] == "itex"
58+
self.device = TFConfig.global_config["device"]
59+
self.itex_mode = TFConfig.global_config["backend"] == "itex"
6060

6161
for _, value in self.config.items():
6262
single_config = value

neural_compressor/tensorflow/algorithms/static_quant/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
# limitations under the License.
1414

1515
from neural_compressor.tensorflow.algorithms.static_quant.keras import KerasAdaptor
16-
from neural_compressor.tensorflow.algorithms.static_quant.tensorflow import TensorFlowAdaptor
16+
from neural_compressor.tensorflow.algorithms.static_quant.tensorflow import TensorFlowAdaptor, Tensorflow_ITEXAdaptor

neural_compressor/tensorflow/algorithms/static_quant/tensorflow.py

+30-23
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ def __init__(self, framework_specific_info):
8787
cfg_yaml_name = "{}.yaml".format(self.__class__.__name__[: -len("Adaptor")].lower())
8888
self.itex_mode = self.backend == "itex" or cfg_yaml_name == "tensorflow_itex.yaml"
8989

90-
if self.itex_mode:
91-
self._check_itex()
90+
# if self.itex_mode:
91+
# self._check_itex()
9292

9393
self.query_handler = TensorflowQuery(
9494
local_config_file=os.path.join(os.path.dirname(__file__), cfg_yaml_name),
@@ -440,6 +440,7 @@ def _tuning_cfg_to_fw(self, tuning_cfg):
440440
if "activation" in tuning_cfg["op"][each_op_info]:
441441
is_asymmetric = tuning_cfg["op"][each_op_info]["activation"]["scheme"] == "asym"
442442
self.quantize_config["op_wise_config"][op_name] = (is_perchannel, algorithm, is_asymmetric, weight_bit)
443+
443444
self.fp32_ops = fp32_ops
444445
self.bf16_ops = bf16_ops
445446

@@ -1520,12 +1521,6 @@ def recover_tuned_model(self, model, q_config):
15201521

15211522
return converter.convert_without_calib()
15221523

1523-
def diagnosis_helper(self, fp32_model, quan_model, tune_cfg, save_path):
1524-
"""Tensorflow diagnosis helper function."""
1525-
from neural_compressor.tensorflow.quantization.utils.utility import tf_diagnosis_helper
1526-
1527-
return tf_diagnosis_helper(fp32_model, quan_model, tune_cfg, save_path)
1528-
15291524
def get_output_op_names(self, qmodel):
15301525
"""Get the oupur OPs's names."""
15311526
from neural_compressor.tensorflow.quantization.utils.graph_util import GraphAnalyzer
@@ -1711,7 +1706,14 @@ def __init__(self, framework_specific_info):
17111706
super().__init__(framework_specific_info)
17121707

17131708
@dump_elapsed_time("Pass quantize model")
1714-
def quantize(self, tune_cfg, model, data_loader, q_func=None):
1709+
def quantize(
1710+
self,
1711+
quant_config: StaticQuantConfig,
1712+
model: BaseModel,
1713+
calib_dataloader: Callable = None,
1714+
calib_iteration: int = 100,
1715+
q_func=None,
1716+
):
17151717
"""Execute the quantize process on the specified model.
17161718
17171719
Args:
@@ -1725,17 +1727,19 @@ def quantize(self, tune_cfg, model, data_loader, q_func=None):
17251727
tf.compat.v1.GraphDef: the quantized model
17261728
"""
17271729
assert q_func is None, "quantization aware training mode is not support on tensorflow"
1730+
self.calib_sampling_size = calib_dataloader.batch_size * calib_iteration
1731+
tune_cfg = self.parse_quant_config(quant_config, model, calib_iteration)
17281732
self._tuning_cfg_to_fw(tune_cfg)
17291733
logger.debug("Dump quantization configurations:")
17301734
logger.debug(self.quantize_config)
17311735
from neural_compressor.tensorflow.quantization.utils.graph_converter import GraphConverter
17321736

1733-
calib_sampling_size = tune_cfg.get("calib_sampling_size", 1)
1734-
if isinstance(data_loader, BaseDataLoader):
1735-
batch_size = data_loader.batch_size
1737+
self.calib_sampling_size = tune_cfg.get("calib_sampling_size", 1)
1738+
if isinstance(calib_dataloader, BaseDataLoader):
1739+
batch_size = calib_dataloader.batch_size
17361740
try:
17371741
for i in range(batch_size):
1738-
if calib_sampling_size % (batch_size - i) == 0:
1742+
if self.calib_sampling_size % (batch_size - i) == 0:
17391743
calib_batch_size = batch_size - i
17401744
if i != 0: # pragma: no cover
17411745
logger.warning(
@@ -1744,17 +1748,18 @@ def quantize(self, tune_cfg, model, data_loader, q_func=None):
17441748
"divisible exactly by batch size"
17451749
)
17461750
break
1747-
tmp_iterations = int(math.ceil(calib_sampling_size / calib_batch_size))
1748-
data_loader.batch(calib_batch_size)
1751+
tmp_iterations = int(math.ceil(self.calib_sampling_size / calib_batch_size))
1752+
calib_dataloader.batch(calib_batch_size)
17491753
self.quantize_config["calib_iteration"] = tmp_iterations
1754+
17501755
converted_model = GraphConverter(
17511756
model,
17521757
qt_config=self.quantize_config,
17531758
recipes=self.recipes,
17541759
int8_sequences=self.op_wise_sequences,
17551760
fp32_ops=self.fp32_ops,
17561761
bf16_ops=self.bf16_ops,
1757-
data_loader=data_loader,
1762+
data_loader=calib_dataloader,
17581763
calib_func=q_func,
17591764
itex_mode=self.itex_mode,
17601765
qdq_enabled=self.qdq_enabled,
@@ -1767,32 +1772,32 @@ def quantize(self, tune_cfg, model, data_loader, q_func=None):
17671772

17681773
batch_size = get_model_input_shape(model)
17691774
logger.warning(
1770-
"Fail to forward with batch size={}, set to {} now.".format(data_loader.batch_size, batch_size)
1775+
"Fail to forward with batch size={}, set to {} now.".format(calib_dataloader.batch_size, batch_size)
17711776
)
1772-
data_loader.batch(batch_size)
1773-
self.quantize_config["calib_iteration"] = calib_sampling_size
1777+
calib_dataloader.batch(batch_size)
1778+
self.quantize_config["calib_iteration"] = self.calib_sampling_size
17741779
converted_model = GraphConverter(
17751780
model,
17761781
qt_config=self.quantize_config,
17771782
recipes=self.recipes,
17781783
int8_sequences=self.op_wise_sequences,
17791784
fp32_ops=self.fp32_ops,
17801785
bf16_ops=self.bf16_ops,
1781-
data_loader=data_loader,
1786+
data_loader=calib_dataloader,
17821787
itex_mode=self.itex_mode,
17831788
qdq_enabled=self.qdq_enabled,
17841789
new_api=self.new_api,
17851790
performance_only=self.performance_only,
17861791
use_bf16=self.use_bf16,
17871792
).convert()
17881793
else: # pragma: no cover
1789-
if hasattr(data_loader, "batch_size") and calib_sampling_size % data_loader.batch_size != 0:
1794+
if hasattr(calib_dataloader, "batch_size") and self.calib_sampling_size % calib_dataloader.batch_size != 0:
17901795
iter = self.quantize_config["calib_iteration"]
17911796
logger.warning(
17921797
"Please note that calibration sampling size {} "
17931798
"isn't divisible exactly by batch size {}. "
17941799
"So the real sampling size is {}.".format(
1795-
calib_sampling_size, data_loader.batch_size, data_loader.batch_size * iter
1800+
self.calib_sampling_size, calib_dataloader.batch_size, calib_dataloader.batch_size * iter
17961801
)
17971802
)
17981803
converted_model = GraphConverter(
@@ -1802,7 +1807,7 @@ def quantize(self, tune_cfg, model, data_loader, q_func=None):
18021807
int8_sequences=self.op_wise_sequences,
18031808
fp32_ops=self.fp32_ops,
18041809
bf16_ops=self.bf16_ops,
1805-
data_loader=data_loader,
1810+
data_loader=calib_dataloader,
18061811
calib_func=q_func,
18071812
itex_mode=self.itex_mode,
18081813
qdq_enabled=self.qdq_enabled,
@@ -2438,6 +2443,8 @@ def update_opwise_config(self):
24382443
op_wise_config = {}
24392444
for op_name, op_config in self.quant_config.items():
24402445
op_key_name = (op_name[0], self.unify_op_type_mapping[op_name[1]])
2446+
if op_key_name not in self.capability["opwise"]:
2447+
continue
24412448
single_op_cap = self.capability["opwise"][op_key_name][0]
24422449
single_op_config = {"activation": {}}
24432450

neural_compressor/tensorflow/algorithms/static_quant/tensorflow.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150

151151
-
152152
version:
153-
name: ['2.1.0', '2.2.0', '2.3.0', '2.4.0', '2.5.0', '2.6.0', '2.6.1', '2.6.2', '2.7.0', '2.8.0', '2.9.0', '2.9.1', '2.10.0', '2.11.0', '2.12.0', '2.13.0', '1.15.0-up1', '1.15.0-up2', '1.15.0-up3']
153+
name: ['2.1.0', '2.2.0', '2.3.0', '2.4.0', '2.5.0', '2.6.0', '2.6.1', '2.6.2', '2.7.0', '2.8.0', '2.9.0', '2.9.1', '2.10.0', '2.11.0', '2.12.0', '2.13.0', '2.14.0', '2.14.1', '2.15.0', '2.15.1', '1.15.0-up1', '1.15.0-up2', '1.15.0-up3']
154154

155155
bf16: ['Conv2D', 'Conv3D', 'MatMul', 'BatchMatMul', 'MaxPool', 'MaxPool3D', 'AvgPool', 'AvgPool3D', 'DepthwiseConv2dNative']
156156
fp32: ['*'] # '*' means all op types

0 commit comments

Comments
 (0)