Skip to content

Commit

Permalink
Add quantize related test (#784)
Browse files Browse the repository at this point in the history
* add onnxruntime backend support for quantize model

* add mul support

* update mergeconvadd

* fix bug in mergeconvadd and mergeconvbn

* Add func note

* add mnist quantize model test

* rerun_ci

* fix test

* update code

* remove useless code

* fix bugs

* add more quantize algo test

* rerun_ci

* add GetTensorValue in helper

* fix test

* add new type support

* fix GetTensorValue in onnx_helper

* fix ops and change name func

* fix quantize_linear op

* fix quantize_model ci

* add quantize ci

* fix bugs
  • Loading branch information
yeliang2258 authored Jun 22, 2022
1 parent 354f9b3 commit ed60964
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
25 changes: 15 additions & 10 deletions paddle2onnx/mapper/quantize_helper.cc
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -539,15 +539,18 @@ void QuantizeModelProcessor::SortNodes() {
if (input_node->op_type() == "Constant") {
continue;
}
if (input == input_node->output(0)) {
if (i2o_mapper.find(input_node->name()) == i2o_mapper.end()) {
i2o_mapper[input_node->name()] = {node->name()};
} else {
auto iter =
std::find(i2o_mapper[input_node->name()].begin(),
i2o_mapper[input_node->name()].end(), node->name());
if (iter == i2o_mapper[input_node->name()].end()) {
i2o_mapper[input_node->name()].push_back(node->name());
for (int64_t out_index = 0; out_index < input_node->output_size();
out_index++) {
if (input == input_node->output(out_index)) {
if (i2o_mapper.find(input_node->name()) == i2o_mapper.end()) {
i2o_mapper[input_node->name()] = {node->name()};
} else {
auto iter =
std::find(i2o_mapper[input_node->name()].begin(),
i2o_mapper[input_node->name()].end(), node->name());
if (iter == i2o_mapper[input_node->name()].end()) {
i2o_mapper[input_node->name()].push_back(node->name());
}
}
}
}
Expand Down Expand Up @@ -587,6 +590,7 @@ void QuantizeModelProcessor::SortNodes() {
}
index++;
}
// for(auto node : new_nodes)std::cout<< node->op_type()<<std::endl;
for (auto& node : constant_nodes) {
new_nodes.push_back(node);
}
Expand Down Expand Up @@ -731,7 +735,8 @@ bool QuantizeModelProcessor::GetTensorByName(const std::string& name,
bool QuantizeModelProcessor::CanBeQuantize(
const std::vector<std::string>& tensor_names) {
for (auto& tensor : tensor_names) {
if (helper_->quantize_info.find(tensor) == helper_->quantize_info.end()) {
if (IsGraphOutput(tensor) ||
helper_->quantize_info.find(tensor) == helper_->quantize_info.end()) {
return false;
}
}
Expand Down
4 changes: 0 additions & 4 deletions tests/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ mkdir ~/.cache/paddle/dataset/int8/download/small_data/ && tar xf ~/.cache/paddl

cases=`find . -name "test*.py" | sort`
ignore="test_expand_as.py \
test_quantize_model_minist.py \
test_quantize_model.py \
test_auto_scan_quantize_linear.py \
test_auto_scan_dequantize_linear.py \
test_split.py \
test_auto_scan_softmax_with_cross_entropy.py \
test_auto_scan_pool_adaptive_max_ops.py \
Expand Down
14 changes: 7 additions & 7 deletions tests/test_quantize_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def generate_quantized_model(self,
model_path,
quantizable_op_type,
algo="mse",
round_type="round",
round_type="TiesToEven",
is_full_quantize=False,
is_use_cache_file=False,
is_optimize_model=False,
Expand Down Expand Up @@ -385,7 +385,7 @@ class TestPostTrainingMseONNXFormatForMobilenetv1(TestPostTrainingQuantization):
def test_post_training_mse_onnx_format_mobilenetv1(self):
model = "MobileNetV1_infer"
algo = "mse"
round_type = "round"
round_type = "TiesToEven"
data_urls = [
'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/inference/MobileNetV1_infer.tar'
]
Expand All @@ -399,7 +399,7 @@ def test_post_training_mse_onnx_format_mobilenetv1(self):
is_use_cache_file = False
is_optimize_model = False
onnx_format = True
diff_threshold = 0.05
diff_threshold = 0.09
self.run_test(
model,
algo,
Expand All @@ -420,7 +420,7 @@ def test_post_training_hist_kl_avg_onnx_format_mobilenetv1(self):
model = "MobileNetV1_infer"
algos = ["hist", "KL", "avg"]
algo = np.random.choice(algos)
round_type = "round"
round_type = "TiesToEven"
data_urls = [
'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/inference/MobileNetV1_infer.tar'
]
Expand All @@ -434,7 +434,7 @@ def test_post_training_hist_kl_avg_onnx_format_mobilenetv1(self):
is_use_cache_file = False
is_optimize_model = False
onnx_format = True
diff_threshold = 0.08
diff_threshold = 0.09
self.run_test(
model,
algo,
Expand All @@ -453,7 +453,7 @@ class TestPostTrainingMseONNXFormatForResnet50(TestPostTrainingQuantization):
def test_post_training_mse_onnx_format_resnet50(self):
model = "ResNet50_infer"
algo = "mse"
round_type = "round"
round_type = "TiesToEven"
data_urls = [
'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/inference/ResNet50_infer.tar'
]
Expand Down Expand Up @@ -484,7 +484,7 @@ def test_post_training_hist_kl_avg_onnx_format_resnet50(self):
model = "ResNet50_infer"
algos = ["hist", "KL", "avg"]
algo = np.random.choice(algos)
round_type = "round"
round_type = "TiesToEven"
data_urls = [
'https://paddle-imagenet-models-name.bj.bcebos.com/dygraph/inference/ResNet50_infer.tar'
]
Expand Down
10 changes: 5 additions & 5 deletions tests/test_quantize_model_minist.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def run_program(self,
def generate_quantized_model(self,
model_path,
algo="KL",
round_type="round",
round_type="TiesToEven",
quantizable_op_type=["conv2d"],
is_full_quantize=False,
is_use_cache_file=False,
Expand Down Expand Up @@ -281,7 +281,7 @@ def test_post_training_mse_onnx_format_full_quant(self):
data_url = "http://paddle-inference-dist.bj.bcebos.com/int8/mnist_model.tar.gz"
data_md5 = "be71d3997ec35ac2a65ae8a145e2887c"
algo = "mse"
round_type = "round"
round_type = "TiesToEven"
quantizable_op_type = ["conv2d", "depthwise_conv2d", "mul"]
is_full_quantize = True
is_use_cache_file = False
Expand Down Expand Up @@ -315,7 +315,7 @@ def test_post_training_kl_onnx_format_full_quant(self):
data_url = "http://paddle-inference-dist.bj.bcebos.com/int8/mnist_model.tar.gz"
data_md5 = "be71d3997ec35ac2a65ae8a145e2887c"
algo = "KL"
round_type = "round"
round_type = "TiesToEven"
quantizable_op_type = ["conv2d", "depthwise_conv2d", "mul"]
is_full_quantize = True
is_use_cache_file = False
Expand Down Expand Up @@ -349,7 +349,7 @@ def test_post_training_hist_onnx_format_full_quant(self):
data_url = "http://paddle-inference-dist.bj.bcebos.com/int8/mnist_model.tar.gz"
data_md5 = "be71d3997ec35ac2a65ae8a145e2887c"
algo = "hist"
round_type = "round"
round_type = "TiesToEven"
quantizable_op_type = ["conv2d", "depthwise_conv2d", "mul"]
is_full_quantize = True
is_use_cache_file = False
Expand Down Expand Up @@ -383,7 +383,7 @@ def test_post_training_abg_onnx_format_full_quant(self):
data_url = "http://paddle-inference-dist.bj.bcebos.com/int8/mnist_model.tar.gz"
data_md5 = "be71d3997ec35ac2a65ae8a145e2887c"
algo = "avg"
round_type = "round"
round_type = "TiesToEven"
quantizable_op_type = ["conv2d", "depthwise_conv2d", "mul"]
is_full_quantize = True
is_use_cache_file = False
Expand Down

0 comments on commit ed60964

Please sign in to comment.