forked from openvinotoolkit/openvino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tf_FakeQuantize.py
148 lines (133 loc) · 7.57 KB
/
test_tf_FakeQuantize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# Copyright (C) 2018-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import numpy as np
import pytest
from common.layer_test_class import check_ir_version
from common.tf_layer_test_class import CommonTFLayerTest
from openvino.tools.mo.front.common.partial_infer.utils import int64_array
from unit_tests.utils.graph import build_graph, regular_op_with_shaped_data, connect, \
shaped_data, connect_front, regular_op
class TestFakeQuantize(CommonTFLayerTest):
def _prepare_input(self, inputs_dict, kwargs):
assert len(inputs_dict) == 1
assert np.array(list(inputs_dict.values())[0]) == np.array([11])
assert 'nudged_il' in kwargs and kwargs['nudged_il'] is not None
assert 'nudged_ih' in kwargs and kwargs['nudged_ih'] is not None
assert 'expected_step' in kwargs and kwargs['expected_step'] is not None
expected_nudged_input_min = kwargs['nudged_il']
expected_nudged_input_max = kwargs['nudged_ih']
expected_step = kwargs['expected_step']
return {list(inputs_dict.keys())[0]: np.array([
expected_nudged_input_min - expected_step,
expected_nudged_input_min - 0.01, expected_nudged_input_min,
expected_nudged_input_min + 0.01,
expected_nudged_input_min + expected_step - 0.01,
expected_nudged_input_min + expected_step,
expected_nudged_input_min + expected_step + 0.01,
expected_nudged_input_max - 0.01, expected_nudged_input_max,
expected_nudged_input_max + 0.01,
expected_nudged_input_max + expected_step
])}
def create_fake_quantize_net(self, il, ih, num_bits, narrow_range, nudged_il, nudged_ih,
expected_step, ir_version, use_new_frontend):
# original tf model
import tensorflow as tf
tf.compat.v1.reset_default_graph()
with tf.compat.v1.Session() as sess:
data = tf.compat.v1.placeholder(tf.float32, [11], 'parameter')
input_min = tf.constant(il, name='input_min')
input_max = tf.constant(ih, name='input_max')
tf.quantization.fake_quant_with_min_max_vars(data, input_min, input_max, num_bits,
narrow_range, 'fq')
tf.compat.v1.global_variables_initializer()
tf_net = sess.graph_def
# reference graph to compare with IR
ref_net = None
if check_ir_version(10, None, ir_version) and not use_new_frontend:
levels = 2 ** num_bits - int(narrow_range)
# data (shape, value) -> const (shape, vale) -> data (shape, no value)
const_for_layer_tests = lambda name, value: {
**{name + '_dd': {'kind': 'data', 'value': value, 'shape': value.shape}},
**{name: {'kind': 'op', 'type': 'Const'}},
**shaped_data(name + '_d', int64_array(value.shape))}
connect_const_for_layer_tests = lambda first_tensor_name, second_tensor_name: [
*connect_front(first_tensor_name + '_dd', first_tensor_name),
*connect(first_tensor_name, second_tensor_name)]
nodes = {
**regular_op_with_shaped_data('parameter', [11], {'type': 'Parameter'}),
**const_for_layer_tests('il', np.array([nudged_il], dtype=np.float32)),
**const_for_layer_tests('ih', np.array([nudged_ih], dtype=np.float32)),
**const_for_layer_tests('ol', np.array([nudged_il], dtype=np.float32)),
**const_for_layer_tests('oh', np.array([nudged_ih], dtype=np.float32)),
**regular_op_with_shaped_data('fq', [11],
{'type': 'FakeQuantize', 'levels': levels}),
**regular_op('result', {'type': 'Result'}),
}
edges = [
*connect('parameter', '0:fq'),
*connect_const_for_layer_tests('il', '1:fq'),
*connect_const_for_layer_tests('ih', '2:fq'),
*connect_const_for_layer_tests('ol', '3:fq'),
*connect_const_for_layer_tests('oh', '4:fq'),
*connect('fq', 'result'),
]
ref_net = build_graph(nodes, edges)
return tf_net, ref_net
test_data = [
# with8BitsNoScalingNoNudging
pytest.param(dict(il=0.0, ih=255.0, num_bits=8, narrow_range=False, nudged_il=0.0, nudged_ih=255.0,
expected_step=1.0), marks=pytest.mark.precommit_tf_fe),
# with8BitsScalingAndNudgingDown
dict(il=0.5, ih=128.0, num_bits=8, narrow_range=False, nudged_il=0.0, nudged_ih=127.5,
expected_step=0.5),
# with8BitsScalingAndNudgingUp
dict(il=-128.0, ih=-0.5, num_bits=8, narrow_range=False, nudged_il=-127.5, nudged_ih=0.0,
expected_step=0.5),
# with8BitsScalingAndNudgingBetween
dict(il=-0.1, ih=127.4, num_bits=8, narrow_range=False, nudged_il=0.0, nudged_ih=127.5,
expected_step=0.5),
# with8BitsNarrowRangeNoScalingNoNudging
dict(il=0.0, ih=254.0, num_bits=8, narrow_range=True, nudged_il=0.0, nudged_ih=254.0,
expected_step=1.0),
# with8BitsNarrowRangeScalingAndNudgingDown
dict(il=0.1, ih=127.1, num_bits=8, narrow_range=True, nudged_il=0.0, nudged_ih=127.0,
expected_step=0.5),
# with8BitsNarrowRangeScalingAndNudgingUp
dict(il=-127.1, ih=-0.1, num_bits=8, narrow_range=True, nudged_il=-127.0, nudged_ih=0.0,
expected_step=0.5),
# with8BitsNarrowRangeScalingAndNudgingBetween
dict(il=-0.1, ih=126.9, num_bits=8, narrow_range=True, nudged_il=0.0, nudged_ih=127.0,
expected_step=0.5),
# with7BitsNoScalingNoNudging
dict(il=0.0, ih=127.0, num_bits=7, narrow_range=False, nudged_il=0.0, nudged_ih=127.0,
expected_step=1.0),
# with7BitsScalingAndNudgingDown
dict(il=0.5, ih=64.0, num_bits=7, narrow_range=False, nudged_il=0.0, nudged_ih=63.5,
expected_step=0.5),
# with7BitsScalingAndNudgingUp
dict(il=-64.0, ih=-0.5, num_bits=7, narrow_range=False, nudged_il=-63.5, nudged_ih=0.0,
expected_step=0.5),
# with7BitsScalingAndNudgingBetween
dict(il=-0.1, ih=63.4, num_bits=7, narrow_range=False, nudged_il=0.0, nudged_ih=63.5,
expected_step=0.5),
# with7BitsNarrowRangeNoScalingNoNudging
dict(il=0.0, ih=126.0, num_bits=7, narrow_range=True, nudged_il=0.0, nudged_ih=126.0,
expected_step=1.0),
# with7BitsNarrowRangeScalingAndNudgingDown
dict(il=0.1, ih=63.1, num_bits=7, narrow_range=True, nudged_il=0.0, nudged_ih=63.0,
expected_step=0.5),
# with7BitsNarrowRangeScalingAndNudgingUp
dict(il=-63.1, ih=-0.1, num_bits=7, narrow_range=True, nudged_il=-63.0, nudged_ih=0.0,
expected_step=0.5),
# with7BitsNarrowRangeScalingAndNudgingBetween
dict(il=-0.1, ih=62.9, num_bits=7, narrow_range=True, nudged_il=0.0, nudged_ih=63.0,
expected_step=0.5)]
@pytest.mark.parametrize("params", test_data)
@pytest.mark.nightly
def test_fake_quantize(self, params, ie_device, precision, ir_version, temp_dir,
use_new_frontend):
self._test(*self.create_fake_quantize_net(**params, ir_version=ir_version,
use_new_frontend=use_new_frontend), ie_device,
precision, ir_version,
kwargs_to_prepare_input=params, temp_dir=temp_dir,
use_new_frontend=use_new_frontend)