Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
6a9b853
modify IO style (#170)
WenjiaoYue Nov 25, 2022
ffc7800
INC new API tensorflow examples update
lvliang-intel Nov 27, 2022
e55bd42
Remove yaml config setting
lvliang-intel Nov 28, 2022
6da64a1
modify IO document (#181)
WenjiaoYue Nov 28, 2022
fc7d190
remove useless file in doc source (#182)
chensuyue Nov 28, 2022
0077f15
Fixed benchmark error for tensorflow model (#177)
PenghuiCheng Nov 29, 2022
c137f28
Add export() PyTorch backend implementation for new API (#180)
xin3he Nov 29, 2022
474c243
Corrected pruning.md links, updated per channel examples. (#175)
YIYANGCAI Nov 29, 2022
3967be6
Adjust the hyperopt and sigopt as extras requirements (#173)
yiliu30 Nov 29, 2022
e4baa44
IO docs bug fix (#188)
WenjiaoYue Nov 30, 2022
c0fc675
Neural Coder adding support of ONNX RT dynamic quant and static quant…
kaikaiyao Nov 30, 2022
6074008
update installation dependency (#185)
chensuyue Nov 30, 2022
a825e77
Set default value for use_bf16 and fixed random seed setting error (#…
PenghuiCheng Dec 1, 2022
4ca98f7
Turn off ITEX optimization pass (#196)
lvliang-intel Dec 2, 2022
8456099
Fixed UT error for bf16 op list for QAT mode (#200)
PenghuiCheng Dec 2, 2022
efc1aa4
Disable multi instance for ITEX GPU benchmark (#204)
lvliang-intel Dec 2, 2022
4d32541
Revert "remove op-wise cfgs for testing. (#1521)" (#202)
intel-zhangyi Dec 2, 2022
7efc1a9
add examples for GPTJ (#162)
changwangss Dec 2, 2022
d662880
Neural Coder mod launcher arg: "strategy" to "approach" (#201)
kaikaiyao Dec 3, 2022
d947f3b
update examples based on comments
lvliang-intel Dec 3, 2022
5868dfa
update README.md
lvliang-intel Dec 4, 2022
b64e7f4
Update model_params_tensorflow.json
chensuyue Dec 4, 2022
8898e75
update readme and benchmark mode
lvliang-intel Dec 5, 2022
c6f3932
update publication_list.md (#212)
chensuyue Dec 5, 2022
c3fc263
Added distributed training support for distillation of CNN-2. (#208)
XinyuYe-Intel Dec 5, 2022
526d4fa
Added distributed training support for distillation of MobileNetV2. …
XinyuYe-Intel Dec 5, 2022
dad3af4
fix load issue (#194)
changwangss Dec 5, 2022
22fd4ff
Fix NTM-One-Shot failed with KeyError (#210)
lvliang-intel Dec 5, 2022
329ce79
Fix TextRNN and centernet_hg104 tuning issue (#171)
lvliang-intel Dec 5, 2022
a9b7ba0
Merge branch 'master' into lvl/inc_newapi_tf_examples
lvliang-intel Dec 5, 2022
c911d43
add batch_size for benchmark
lvliang-intel Dec 5, 2022
ec10fbc
Merge branch 'master' of https://github.com/intel/neural-compressor i…
lvliang-intel Dec 5, 2022
9eff34a
Merge branch 'lvl/inc_newapi_tf_examples' of https://github.com/intel…
lvliang-intel Dec 5, 2022
bd970a8
Merge branch 'master' of https://github.com/intel/neural-compressor i…
lvliang-intel Dec 5, 2022
3763ffe
set default batch_size to 32
lvliang-intel Dec 5, 2022
9351200
Update __main__.py
chensuyue Dec 5, 2022
118b9a2
Update __main__.py
chensuyue Dec 5, 2022
99abed9
Merge branch 'master' of https://github.com/intel/neural-compressor i…
lvliang-intel Dec 6, 2022
e4ab8d9
Merge branch 'lvl/inc_newapi_tf_examples' of https://github.com/intel…
lvliang-intel Dec 6, 2022
35d0a46
fix tuning and benchmark gap compared with old api
lvliang-intel Dec 6, 2022
8449790
Merge branch 'master' into lvl/inc_newapi_tf_examples
lvliang-intel Dec 6, 2022
08301c1
benchmark refine
lvliang-intel Dec 7, 2022
3f0ce40
Merge branch 'lvl/inc_newapi_tf_examples' of https://github.com/intel…
lvliang-intel Dec 7, 2022
4a29bcd
fix latency issue
lvliang-intel Dec 7, 2022
9f7f621
Merge branch 'master' into lvl/inc_newapi_tf_examples
lvliang-intel Dec 8, 2022
d49f121
Merge branch 'master' of https://github.com/intel/neural-compressor i…
lvliang-intel Dec 9, 2022
a43b1c8
Merge branch 'lvl/inc_newapi_tf_examples' of https://github.com/intel…
lvliang-intel Dec 9, 2022
d28eba7
fix set_random_seed
lvliang-intel Dec 9, 2022
65df7a6
Merge branch 'master' of https://github.com/intel/neural-compressor i…
lvliang-intel Dec 12, 2022
f934386
Update code for PR 244
lvliang-intel Dec 12, 2022
a29235c
fix space issue
lvliang-intel Dec 13, 2022
47a0083
add inters and dump result for debug
lvliang-intel Dec 14, 2022
703d3f6
Merge branch 'master' of https://github.com/intel/neural-compressor i…
lvliang-intel Dec 14, 2022
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
2 changes: 2 additions & 0 deletions .azure-pipelines/scripts/codeScan/pyspelling/inc_dict.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2397,6 +2397,8 @@ grappler
amsgrad
qoperator
apis
PostTrainingQuantConfig
dgpu
CPz
Nsh
UmK
Expand Down
18 changes: 6 additions & 12 deletions examples/.config/model_params_tensorflow.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,8 @@
"model_src_dir": "image_recognition/keras_models/inception_resnet_v2/quantization/ptq",
"dataset_location": "/tf_dataset/dataset/imagenet",
"input_model": "/tf_dataset2/models/tensorflow/inception_resnet_v2_keras/saved_model/",
"yaml": "inception_resnet_v2.yaml",
"strategy": "basic",
"batch_size": 1,
"new_benchmark": true
"main_script": "main.py",
"batch_size": 32
},
"vgg16": {
"model_src_dir": "image_recognition/tensorflow_models/quantization/ptq",
Expand Down Expand Up @@ -292,10 +290,8 @@
"model_src_dir": "image_recognition/keras_models/resnetv2_50/quantization/ptq",
"dataset_location": "/tf_dataset/dataset/imagenet",
"input_model": "/tf_dataset2/models/tensorflow/resnetv2_50_keras/saved_model",
"yaml": "resnetv2_50.yaml",
"strategy": "basic",
"batch_size": 1,
"new_benchmark": true
"main_script": "main.py",
"batch_size": 32
},
"resnetv2_101": {
"model_src_dir": "image_recognition/tensorflow_models/quantization/ptq",
Expand Down Expand Up @@ -2494,10 +2490,8 @@
"model_src_dir": "image_recognition/keras_models/xception/quantization/ptq",
"dataset_location": "/tf_dataset/dataset/imagenet",
"input_model": "/tf_dataset2/models/tensorflow/xception_keras/saved_model/",
"yaml": "xception.yaml",
"strategy": "basic",
"batch_size": 1,
"new_benchmark": true
"main_script": "main.py",
"batch_size": 32
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ Intel Extension for Tensorflow is mandatory to be installed for quantizing the m
```shell
pip install --upgrade intel-extension-for-tensorflow[gpu]
```
For any more details, please follow the procedure in [install-gpu-drivers](https://github.com/intel-innersource/frameworks.ai.infrastructure.intel-extension-for-tensorflow.intel-extension-for-tensorflow/blob/master/docs/install/install_for_gpu.md#install-gpu-drivers)
Please refer to the [Installation Guides](https://dgpu-docs.intel.com/installation-guides/ubuntu/ubuntu-focal-dc.html) for latest Intel GPU driver installation.
For any more details, please follow the procedure in [install-gpu-drivers](https://github.com/intel-innersource/frameworks.ai.infrastructure.intel-extension-for-tensorflow.intel-extension-for-tensorflow/blob/master/docs/install/install_for_gpu.md#install-gpu-drivers).

#### Quantizing the model on Intel CPU(Experimental)
Intel Extension for Tensorflow for Intel CPUs is experimental currently. It's not mandatory for quantizing the model on Intel CPUs.
Expand All @@ -43,12 +44,20 @@ python prepare_model.py --output_model=/path/to/model
```
`--output_model ` the model should be saved as SavedModel format or H5 format.

## Write Yaml config file
In examples directory, there is a inception_resnet_v2.yaml for tuning the model on Intel CPUs. The 'framework' in the yaml is set to 'tensorflow'. If running this example on Intel GPUs, the 'framework' should be set to 'tensorflow_itex' and the device in yaml file should be set to 'gpu'. The inception_resnet_v2_itex.yaml is prepared for the GPU case. We could remove most of items and only keep mandatory item for tuning. We also implement a calibration dataloader and have evaluation field for creation of evaluation function at internal neural_compressor.
## Quantization Config
The Quantization Config class has default parameters setting for running on Intel CPUs. If running this example on Intel GPUs, the 'backend' parameter should be set to 'itex' and the 'device' parameter should be set to 'gpu'.

```
config = PostTrainingQuantConfig(
device="gpu",
backend="itex",
...
)
```

## Run Command
```shell
bash run_tuning.sh --config=inception_resnet_v2.yaml --input_model=./path/to/model --output_model=./result --eval_data=/path/to/evaluation/dataset --calib_data=/path/to/calibration/dataset
bash run_benchmark.sh --config=inception_resnet_v2.yaml --input_model=./path/to/model --mode=performance --eval_data=/path/to/evaluation/dataset
bash run_tuning.sh --input_model=./path/to/model --output_model=./result --dataset_location=/path/to/evaluation/dataset --batch_size=32
bash run_benchmark.sh --input_model=./path/to/model --mode=performance --dataset_location=/path/to/evaluation/dataset --batch_size=1
```

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# -*- coding: utf-8 -*-
#
# Copyright (c) 2018 Intel Corporation
# 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.
Expand All @@ -16,11 +16,9 @@
# limitations under the License.
#
import time
import shutil
import numpy as np
from argparse import ArgumentParser
from neural_compressor import data
import tensorflow as tf
from neural_compressor.utils import logger
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

flags = tf.compat.v1.flags
Expand All @@ -42,87 +40,111 @@
flags.DEFINE_bool(
'benchmark', False, 'whether to benchmark the model')

flags.DEFINE_string(
'config', 'bert.yaml', 'yaml configuration of the model')

flags.DEFINE_string(
'calib_data', None, 'location of calibration dataset')

flags.DEFINE_string(
'eval_data', None, 'location of evaluate dataset')

from neural_compressor.experimental.metric.metric import TensorflowTopK
from neural_compressor.experimental.data.transforms.transform import ComposeTransform
from neural_compressor.experimental.data.datasets.dataset import TensorflowImageRecord
from neural_compressor.experimental.data.transforms.imagenet_transform import LabelShift
from neural_compressor.experimental.data.dataloaders.default_dataloader import DefaultDataLoader
flags.DEFINE_integer('batch_size', 32, 'batch_size')

flags.DEFINE_integer(
'iters', 100, 'maximum iteration when evaluating performance')

from neural_compressor.metric.metric import TensorflowTopK
from neural_compressor.data.transforms.transform import ComposeTransform
from neural_compressor.data.datasets.dataset import TensorflowImageRecord
from neural_compressor.data.transforms.imagenet_transform import LabelShift
from neural_compressor.data.dataloaders.default_dataloader import DefaultDataLoader
from neural_compressor.data.transforms.imagenet_transform import BilinearImagenetTransform

eval_dataset = TensorflowImageRecord(root=FLAGS.eval_data, transform=ComposeTransform(transform_list= \
[BilinearImagenetTransform(height=299, width=299)]))
if FLAGS.benchmark and FLAGS.mode == 'performance':
eval_dataloader = DefaultDataLoader(dataset=eval_dataset, batch_size=1)
else:
eval_dataloader = DefaultDataLoader(dataset=eval_dataset, batch_size=32)
[BilinearImagenetTransform(height=299, width=299)]))

eval_dataloader = DefaultDataLoader(dataset=eval_dataset, batch_size=FLAGS.batch_size)

if FLAGS.calib_data:
calib_dataset = TensorflowImageRecord(root=FLAGS.calib_data, transform=ComposeTransform(transform_list= \
[BilinearImagenetTransform(height=299, width=299)]))
calib_dataloader = DefaultDataLoader(dataset=calib_dataset, batch_size=10)

def evaluate(model, measurer=None):
"""
Custom evaluate function to inference the model for specified metric on validation dataset.

Args:
model (tf.saved_model.load): The input model will be the class of tf.saved_model.load(quantized_model_path).
measurer (object, optional): for benchmark measurement of duration.

Returns:
accuracy (float): evaluation result, the larger is better.
"""
infer = model.signatures["serving_default"]
output_dict_keys = infer.structured_outputs.keys()
output_name = list(output_dict_keys )[0]
postprocess = LabelShift(label_shift=1)
metric = TensorflowTopK(k=1)

def eval_func(dataloader, metric):
results = []
for idx, (inputs, labels) in enumerate(dataloader):
inputs = np.array(inputs)
input_tensor = tf.constant(inputs)
if measurer:
measurer.start()
predictions = infer(input_tensor)[output_name]
if measurer:
measurer.end()
predictions = predictions.numpy()
predictions, labels = postprocess((predictions, labels))
metric.update(predictions, labels)
return results

results = eval_func(eval_dataloader, metric)
acc = metric.result()
return acc
calib_dataset = TensorflowImageRecord(root=FLAGS.calib_data, transform= \
ComposeTransform(transform_list= [BilinearImagenetTransform(height=299, width=299)]))
calib_dataloader = DefaultDataLoader(dataset=calib_dataset, batch_size=10)

def evaluate(model):
"""
Custom evaluate function to inference the model for specified metric on validation dataset.

Args:
model (tf.saved_model.load): The input model will be the class of tf.saved_model.load(quantized_model_path).
measurer (object, optional): for benchmark measurement of duration.

Returns:
accuracy (float): evaluation result, the larger is better.
"""
infer = model.signatures["serving_default"]
output_dict_keys = infer.structured_outputs.keys()
output_name = list(output_dict_keys )[0]
postprocess = LabelShift(label_shift=1)
metric = TensorflowTopK(k=1)
latency_list = []

def eval_func(dataloader, metric):
warmup = 5
iteration = None

if FLAGS.benchmark and FLAGS.mode == 'performance':
iteration = FLAGS.iters
for idx, (inputs, labels) in enumerate(dataloader):
inputs = np.array(inputs)
input_tensor = tf.constant(inputs)
start = time.time()
predictions = infer(input_tensor)[output_name]
end = time.time()
latency_list.append(end - start)
predictions = predictions.numpy()
predictions, labels = postprocess((predictions, labels))
metric.update(predictions, labels)
if iteration and idx >= iteration:
break
latency = np.array(latency_list[warmup:]).mean() / eval_dataloader.batch_size
return latency

latency = eval_func(eval_dataloader, metric)
if FLAGS.benchmark:
logger.info("\n{} mode benchmark result:".format(FLAGS.mode))
for i, res in enumerate(latency_list):
logger.debug("Iteration {} result {}:".format(i, res))
if FLAGS.benchmark and FLAGS.mode == 'performance':
logger.info("Batch size = {}".format(eval_dataloader.batch_size))
logger.info("Latency: {:.3f} ms".format(latency * 1000))
logger.info("Throughput: {:.3f} images/sec".format(1. / latency))
acc = metric.result()
return acc

def main(_):
if FLAGS.tune:
from neural_compressor.experimental import Quantization, common
quantizer = Quantization(FLAGS.config)
quantizer.model = common.Model(FLAGS.input_model)
quantizer.eval_func = evaluate
quantizer.calib_dataloader = calib_dataloader
q_model = quantizer.fit()
q_model.save(FLAGS.output_model)


if FLAGS.benchmark:
from neural_compressor.experimental import Benchmark, common
evaluator = Benchmark(FLAGS.config)
evaluator.model = common.Model(FLAGS.input_model)
evaluator.b_func = evaluate
evaluator.b_dataloader = eval_dataloader
evaluator(FLAGS.mode)
if FLAGS.tune:
from neural_compressor.quantization import fit
from neural_compressor.config import PostTrainingQuantConfig
from neural_compressor.utils.utility import set_random_seed
set_random_seed(9527)
config = PostTrainingQuantConfig(calibration_sampling_size=[50, 100])
q_model = fit(
model=FLAGS.input_model,
conf=config,
calib_dataloader=calib_dataloader,
eval_dataloader=eval_dataloader,
eval_func=evaluate)
q_model.save(FLAGS.output_model)

if FLAGS.benchmark:
from neural_compressor.benchmark import fit
from neural_compressor.config import BenchmarkConfig
if FLAGS.mode == 'performance':
conf = BenchmarkConfig(iteration=100, cores_per_instance=4, num_of_instance=7)
fit(FLAGS.input_model, conf, b_func=evaluate)
else:
from neural_compressor.model.model import Model
accuracy = evaluate(Model(FLAGS.input_model).model)
logger.info('Batch size = %d' % FLAGS.batch_size)
logger.info("Accuracy: %.5f" % accuracy)

if __name__ == "__main__":
tf.compat.v1.app.run()
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
#
# -*- coding: utf-8 -*-
#
# 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.
#

import argparse
import tensorflow as tf
def get_inception_resnet_v2_model(saved_path):
Expand Down
Loading