Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
N
ngraph
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
ngraph
Commits
baf1cb00
Commit
baf1cb00
authored
Jun 12, 2019
by
nishant.b.patel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Changed the onnx rt conv test case to use ngraph ops
parent
aba06374
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
57 additions
and
67 deletions
+57
-67
builder_quantization.cpp
test/builder_quantization.cpp
+57
-67
No files found.
test/builder_quantization.cpp
View file @
baf1cb00
...
@@ -1489,40 +1489,6 @@ TEST(builder, scaled_QDot_u8u8)
...
@@ -1489,40 +1489,6 @@ TEST(builder, scaled_QDot_u8u8)
EXPECT_EQ
((
vector
<
uint8_t
>
{
3
,
13
,
23
}),
read_vector
<
uint8_t
>
(
result
));
EXPECT_EQ
((
vector
<
uint8_t
>
{
3
,
13
,
23
}),
read_vector
<
uint8_t
>
(
result
));
}
}
void
FindMinMax
(
const
vector
<
float
>&
vec
,
float
*
min
,
float
*
max
)
{
*
min
=
*
max
=
0
;
*
min
=
*
std
::
min_element
(
vec
.
begin
(),
vec
.
end
());
*
max
=
*
std
::
max_element
(
vec
.
begin
(),
vec
.
end
());
}
// uses quantization range 0-255
void
FindScaleAndZeroPoint
(
float
min
,
float
max
,
float
*
scale
,
uint8_t
*
zero_point
)
{
min
=
std
::
min
(
min
,
0.
f
);
max
=
std
::
max
(
max
,
0.
f
);
float
qmin
=
0
;
float
qmax
=
255
;
*
scale
=
(
max
-
min
)
/
(
qmax
-
qmin
);
const
auto
initial_zero_point
=
qmin
-
min
/
*
scale
;
*
zero_point
=
static_cast
<
uint8_t
>
(
std
::
round
(
std
::
max
(
0.
f
,
std
::
min
(
255.
f
,
initial_zero_point
))));
}
void
Quantize
(
float
scale
,
uint8_t
zero_point
,
const
std
::
vector
<
float
>&
input
,
std
::
vector
<
uint8_t
>*
input_quantized
)
{
for
(
size_t
i
=
0
;
i
<
input
.
size
();
i
++
)
{
const
float
clamped_val
=
std
::
max
(
0.
f
,
std
::
min
(
255.
f
,
std
::
round
(
static_cast
<
float
>
(
input
[
i
])
/
scale
)
+
zero_point
));
(
*
input_quantized
)[
i
]
=
static_cast
<
uint8_t
>
(
clamped_val
);
}
}
TEST
(
builder
,
scaled_QC_non_zero_zero_point
)
TEST
(
builder
,
scaled_QC_non_zero_zero_point
)
{
{
Shape
shape_a
{
1
,
1
,
7
,
7
};
// input shape
Shape
shape_a
{
1
,
1
,
7
,
7
};
// input shape
...
@@ -1546,7 +1512,8 @@ TEST(builder, scaled_QC_non_zero_zero_point)
...
@@ -1546,7 +1512,8 @@ TEST(builder, scaled_QC_non_zero_zero_point)
0.034442365169525146
f
,
-
0.33322954177856445
f
,
0.06049239635467529
f
,
0.034442365169525146
f
,
-
0.33322954177856445
f
,
0.06049239635467529
f
,
0.42619407176971436
f
};
0.42619407176971436
f
};
vector
<
float
>
W
=
{
-
0.4406261742115021
f
};
vector
<
float
>
W
=
{
-
0.4406261742115021
f
};
auto
expected_vals
=
{
-
0.19936637580394745
f
,
-
0.06828942894935608
f
,
-
0.04934731498360634
f
,
vector
<
float
>
expected_vals
=
{
-
0.19936637580394745
f
,
-
0.06828942894935608
f
,
-
0.04934731498360634
f
,
0.17369966208934784
f
,
-
0.11574628204107285
f
,
-
0.05910799279808998
f
,
0.17369966208934784
f
,
-
0.11574628204107285
f
,
-
0.05910799279808998
f
,
0.1197819635272026
f
,
0.18959586322307587
f
,
0.1182001456618309
f
,
0.1197819635272026
f
,
0.18959586322307587
f
,
0.1182001456618309
f
,
-
0.17154212296009064
f
,
0.06006614491343498
f
,
0.0042258151806890965
f
,
-
0.17154212296009064
f
,
0.06006614491343498
f
,
0.0042258151806890965
f
,
...
@@ -1564,32 +1531,56 @@ TEST(builder, scaled_QC_non_zero_zero_point)
...
@@ -1564,32 +1531,56 @@ TEST(builder, scaled_QC_non_zero_zero_point)
-
0.015176207758486271
f
,
0.14682966470718384
f
,
-
0.02665453404188156
f
,
-
0.015176207758486271
f
,
0.14682966470718384
f
,
-
0.02665453404188156
f
,
-
0.18779225647449493
f
};
-
0.18779225647449493
f
};
float
lhs_min
,
lhs_max
,
rhs_min
,
rhs_max
,
result_min
,
result_max
;
auto
lhs
=
make_shared
<
op
::
Parameter
>
(
element
::
f32
,
shape_a
);
FindMinMax
(
X
,
&
lhs_min
,
&
lhs_max
);
auto
rhs
=
make_shared
<
op
::
Parameter
>
(
element
::
f32
,
shape_b
);
FindMinMax
(
W
,
&
rhs_min
,
&
rhs_max
);
auto
result
=
make_shared
<
op
::
Parameter
>
(
element
::
f32
,
shape_r
);
FindMinMax
(
expected_vals
,
&
result_min
,
&
result_max
);
AxisSet
quantization_axes
;
op
::
Quantize
::
RoundMode
round_mode
=
op
::
Quantize
::
RoundMode
::
ROUND_NEAREST_TOWARD_EVEN
;
auto
lhs_scale
=
op
::
Constant
::
create
(
element
::
f32
,
Shape
{},
{
0.00369205
});
auto
lhs_zero_point
=
op
::
Constant
::
create
(
element
::
u8
,
Shape
{},
{
132
});
auto
rhs_scale
=
op
::
Constant
::
create
(
element
::
f32
,
Shape
{},
{
0.00172795
});
auto
rhs_zero_point
=
op
::
Constant
::
create
(
element
::
u8
,
Shape
{},
{
255
});
auto
result_scale
=
op
::
Constant
::
create
(
element
::
f32
,
Shape
{},
{
0.00162681
});
auto
result_zero_point
=
op
::
Constant
::
create
(
element
::
u8
,
Shape
{},
{
123
});
float
lhs_scale
,
rhs_scale
,
result_scale
;
auto
quantize_lhs
=
make_shared
<
op
::
Quantize
>
(
uint8_t
lhs_zero_point
,
rhs_zero_point
,
result_zero_point
;
lhs
,
lhs_scale
,
lhs_zero_point
,
element
::
u8
,
quantization_axes
,
round_mode
);
FindScaleAndZeroPoint
(
lhs_min
,
lhs_max
,
&
lhs_scale
,
&
lhs_zero_point
);
auto
quantize_rhs
=
make_shared
<
op
::
Quantize
>
(
FindScaleAndZeroPoint
(
rhs_min
,
rhs_max
,
&
rhs_scale
,
&
rhs_zero_point
);
rhs
,
rhs_scale
,
rhs_zero_point
,
element
::
u8
,
quantization_axes
,
round_mode
);
FindScaleAndZeroPoint
(
result_min
,
result_max
,
&
result_scale
,
&
result_zero_point
);
auto
quantize_result
=
make_shared
<
op
::
Quantize
>
(
result
,
result_scale
,
result_zero_point
,
element
::
u8
,
quantization_axes
,
round_mode
);
vector
<
uint8_t
>
x_quantized
(
X
.
size
()),
w_quantized
(
W
.
size
()),
auto
lhs_f
=
make_shared
<
Function
>
(
quantize_lhs
,
ParameterVector
{
lhs
});
result_quantized
(
expected_vals
.
size
());
auto
rhs_f
=
make_shared
<
Function
>
(
quantize_rhs
,
ParameterVector
{
rhs
});
Quantize
(
lhs_scale
,
lhs_zero_point
,
X
,
&
x_quantized
);
auto
result_f
=
make_shared
<
Function
>
(
quantize_result
,
ParameterVector
{
result
});
Quantize
(
rhs_scale
,
rhs_zero_point
,
W
,
&
w_quantized
);
Quantize
(
result_scale
,
result_zero_point
,
expected_vals
,
&
result_quantized
);
auto
backend
=
runtime
::
Backend
::
create
(
"CPU"
);
auto
lhs_data
=
backend
->
create_tensor
(
element
::
f32
,
shape_a
);
auto
rhs_data
=
backend
->
create_tensor
(
element
::
f32
,
shape_b
);
auto
result_data
=
backend
->
create_tensor
(
element
::
f32
,
shape_r
);
auto
lhs_output
=
backend
->
create_tensor
(
element
::
u8
,
shape_a
);
auto
rhs_output
=
backend
->
create_tensor
(
element
::
u8
,
shape_b
);
auto
result_output
=
backend
->
create_tensor
(
element
::
u8
,
shape_r
);
copy_data
(
lhs_data
,
X
);
copy_data
(
rhs_data
,
W
);
copy_data
(
result_data
,
expected_vals
);
auto
lhs_handle
=
backend
->
compile
(
lhs_f
);
auto
rhs_handle
=
backend
->
compile
(
rhs_f
);
auto
result_handle
=
backend
->
compile
(
result_f
);
lhs_handle
->
call_with_validate
({
lhs_output
},
{
lhs_data
});
rhs_handle
->
call_with_validate
({
rhs_output
},
{
rhs_data
});
result_handle
->
call_with_validate
({
result_output
},
{
result_data
});
auto
A
=
make_shared
<
op
::
Parameter
>
(
element
::
u8
,
shape_a
);
auto
A
=
make_shared
<
op
::
Parameter
>
(
element
::
u8
,
shape_a
);
auto
B
=
make_shared
<
op
::
Parameter
>
(
element
::
u8
,
shape_b
);
auto
B
=
make_shared
<
op
::
Parameter
>
(
element
::
u8
,
shape_b
);
auto
input_scale
=
op
::
Constant
::
create
(
element
::
f32
,
Shape
{},
{
lhs_scale
});
auto
filter_scale
=
op
::
Constant
::
create
(
element
::
f32
,
Shape
{},
{
rhs_scale
});
auto
output_scale
=
op
::
Constant
::
create
(
element
::
f32
,
Shape
{},
{
result_scale
});
auto
input_zero_point
=
op
::
Constant
::
create
(
element
::
u8
,
Shape
{},
{
lhs_zero_point
});
auto
filter_zero_point
=
op
::
Constant
::
create
(
element
::
u8
,
Shape
{},
{
rhs_zero_point
});
auto
output_zero_point
=
op
::
Constant
::
create
(
element
::
u8
,
Shape
{},
{
result_zero_point
});
auto
CV
=
make_shared
<
ngraph
::
op
::
QuantizedConvolution
>
(
A
,
auto
CV
=
make_shared
<
ngraph
::
op
::
QuantizedConvolution
>
(
A
,
B
,
B
,
Strides
{
1
,
1
},
// move_strides
Strides
{
1
,
1
},
// move_strides
...
@@ -1597,30 +1588,29 @@ TEST(builder, scaled_QC_non_zero_zero_point)
...
@@ -1597,30 +1588,29 @@ TEST(builder, scaled_QC_non_zero_zero_point)
CoordinateDiff
{
0
,
0
},
// below_pads
CoordinateDiff
{
0
,
0
},
// below_pads
CoordinateDiff
{
0
,
0
},
// above_pads
CoordinateDiff
{
0
,
0
},
// above_pads
Strides
{
1
,
1
},
// data_dilation
Strides
{
1
,
1
},
// data_dilation
input
_scale
,
lhs
_scale
,
input
_zero_point
,
lhs
_zero_point
,
filter
_scale
,
rhs
_scale
,
filter
_zero_point
,
rhs
_zero_point
,
outpu
t_scale
,
resul
t_scale
,
outpu
t_zero_point
,
resul
t_zero_point
,
element
::
u8
,
element
::
u8
,
AxisSet
{});
AxisSet
{});
auto
f
=
make_shared
<
Function
>
(
NodeVector
{
CV
},
ParameterVector
{
A
,
B
});
auto
f
=
make_shared
<
Function
>
(
NodeVector
{
CV
},
ParameterVector
{
A
,
B
});
constant_fold
(
f
);
constant_fold
(
f
);
auto
backend
=
runtime
::
Backend
::
create
(
"CPU"
);
// Create some tensors for input/output
// Create some tensors for input/output
auto
a
=
backend
->
create_tensor
(
element
::
u8
,
shape_a
);
auto
a
=
backend
->
create_tensor
(
element
::
u8
,
shape_a
);
copy_data
(
a
,
x_quantized
);
copy_data
(
a
,
read_vector
<
uint8_t
>
(
lhs_output
)
);
auto
b
=
backend
->
create_tensor
(
element
::
u8
,
shape_b
);
auto
b
=
backend
->
create_tensor
(
element
::
u8
,
shape_b
);
copy_data
(
b
,
w_quantized
);
copy_data
(
b
,
read_vector
<
uint8_t
>
(
rhs_output
)
);
auto
result
=
backend
->
create_tensor
(
element
::
u8
,
shape_r
);
auto
final_
result
=
backend
->
create_tensor
(
element
::
u8
,
shape_r
);
auto
handle
=
backend
->
compile
(
f
);
auto
handle
=
backend
->
compile
(
f
);
handle
->
call_with_validate
({
result
},
{
a
,
b
});
handle
->
call_with_validate
({
final_
result
},
{
a
,
b
});
for
(
int
i
=
0
;
i
<
49
;
++
i
)
for
(
int
i
=
0
;
i
<
49
;
++
i
)
{
{
EXPECT_EQ
(
result_quantized
[
i
],
(
read_vector
<
uint8_t
>
(
result
))[
i
])
EXPECT_EQ
(
(
read_vector
<
uint8_t
>
(
result_output
))[
i
],
(
read_vector
<
uint8_t
>
(
final_
result
))[
i
])
<<
"Vectors x and y differ at index "
<<
i
;
<<
"Vectors x and y differ at index "
<<
i
;
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment