Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
tic
Overview
Overview
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wenyuanbo
tic
Commits
1bc83853
Commit
1bc83853
authored
Sep 01, 2019
by
Animesh Jain
Committed by
Wuwei Lin
Sep 01, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[QNN] Requantize - Optimize lowering for some corner cases. (#3864)
parent
dee52466
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
36 deletions
+44
-36
src/relay/qnn/op/requantize.cc
+43
-36
tests/python/relay/test_qnn_requantize.py
+1
-0
No files found.
src/relay/qnn/op/requantize.cc
View file @
1bc83853
...
@@ -129,48 +129,55 @@ Expr RequantizeLower(const Expr& input_tensor, const RequantizeAttrs* param,
...
@@ -129,48 +129,55 @@ Expr RequantizeLower(const Expr& input_tensor, const RequantizeAttrs* param,
tensor
=
Subtract
(
tensor
,
input_zp
);
tensor
=
Subtract
(
tensor
,
input_zp
);
}
}
// 3) Multiply the integer multiplier
// If the input and output scales are same, we can skip the fixed point multiplication.
if
(
left_shift
!=
0
)
{
auto
scaled_int64_t
=
tensor
;
tensor
=
Multiply
(
tensor
,
MakeConstantScalar
(
hp_dtype
,
1
<<
left_shift
));
if
(
param
->
input_scale
!=
param
->
output_scale
)
{
}
// 3) Multiply the integer multiplier
// Perform the multiplication in higher precision.
if
(
left_shift
!=
0
)
{
// The scalar is a fixed point value of int32 where the decimal point is
tensor
=
Multiply
(
tensor
,
MakeConstantScalar
(
hp_dtype
,
1
<<
left_shift
));
// between bits 31 and 30. After multiplying with input_tensor, the result is
}
// in int64 where the decimal point is sitting between bits 31 and 30 (from
// Perform the multiplication in higher precision.
// the right, rightmost bit is bit 0). The computation is performed in higher
// The scalar is a fixed point value of int32 where the decimal point is
// precision to avoid overflow in multiplying two int32 values.
// between bits 31 and 30. After multiplying with input_tensor, the result is
Expr
scalar
=
MakeConstantScalar
(
hp_dtype
,
fixed_point_multiplier
);
// in int64 where the decimal point is sitting between bits 31 and 30 (from
auto
multiplied_t
=
Multiply
(
tensor
,
scalar
);
// the right, rightmost bit is bit 0). The computation is performed in higher
// precision to avoid overflow in multiplying two int32 values.
Expr
scalar
=
MakeConstantScalar
(
hp_dtype
,
fixed_point_multiplier
);
auto
multiplied_t
=
Multiply
(
tensor
,
scalar
);
// 4) Find the rounding scalar. This depends on where the final decimal point
// 4) Find the rounding scalar. This depends on where the final decimal point
// sits. As we will be right shifting the multiplied_t, we need to first
// sits. As we will be right shifting the multiplied_t, we need to first
// calculate the total_right_shift.
// calculate the total_right_shift.
int
total_right_shift
=
right_shift
+
31
;
int
total_right_shift
=
right_shift
+
31
;
int64_t
pos_rounding_value
=
(
1ll
<<
(
total_right_shift
-
1
));
int64_t
pos_rounding_value
=
(
1ll
<<
(
total_right_shift
-
1
));
tensor
=
multiplied_t
;
tensor
=
multiplied_t
;
Expr
round_scalar
;
Expr
round_scalar
;
if
(
param
->
rounding
==
"UPWARD"
)
{
if
(
param
->
rounding
==
"UPWARD"
)
{
round_scalar
=
MakeConstantScalar
(
hp_dtype
,
pos_rounding_value
);
round_scalar
=
MakeConstantScalar
(
hp_dtype
,
pos_rounding_value
);
}
else
if
(
param
->
rounding
==
"TONEAREST"
)
{
}
else
if
(
param
->
rounding
==
"TONEAREST"
)
{
auto
pos_rounder
=
MakeConstantScalar
(
hp_dtype
,
pos_rounding_value
);
auto
pos_rounder
=
MakeConstantScalar
(
hp_dtype
,
pos_rounding_value
);
auto
neg_rounder
=
MakeConstantScalar
(
hp_dtype
,
pos_rounding_value
-
1
);
auto
neg_rounder
=
MakeConstantScalar
(
hp_dtype
,
pos_rounding_value
-
1
);
auto
pos_rounder_t
=
Full
(
pos_rounder
,
input_shape
,
hp_dtype
);
auto
pos_rounder_t
=
Full
(
pos_rounder
,
input_shape
,
hp_dtype
);
auto
neg_rounder_t
=
Full
(
neg_rounder
,
input_shape
,
hp_dtype
);
auto
neg_rounder_t
=
Full
(
neg_rounder
,
input_shape
,
hp_dtype
);
auto
zero
=
MakeConstantScalar
(
hp_dtype
,
0
);
auto
zero
=
MakeConstantScalar
(
hp_dtype
,
0
);
auto
zero_t
=
Full
(
zero
,
input_shape
,
hp_dtype
);
auto
zero_t
=
Full
(
zero
,
input_shape
,
hp_dtype
);
round_scalar
=
Where
(
GreaterEqual
(
tensor
,
zero_t
),
pos_rounder_t
,
neg_rounder_t
);
round_scalar
=
Where
(
GreaterEqual
(
tensor
,
zero_t
),
pos_rounder_t
,
neg_rounder_t
);
}
}
// Add the rounding scalar.
// Add the rounding scalar.
tensor
=
Add
(
tensor
,
round_scalar
);
tensor
=
Add
(
tensor
,
round_scalar
);
// 5) Simply right shift the result to get the final output.
// 5) Simply right shift the result to get the final output.
auto
scaled_int64_t
=
RightShift
(
tensor
,
MakeConstantScalar
(
hp_dtype
,
total_right_shift
));
scaled_int64_t
=
RightShift
(
tensor
,
MakeConstantScalar
(
hp_dtype
,
total_right_shift
));
}
// 6) Add the output zero point.
// 6) Add the output zero point.
auto
output_zp
=
MakeConstantScalar
(
hp_dtype
,
param
->
output_zero_point
);
auto
shifted_int64_t
=
scaled_int64_t
;
auto
shifted_int64_t
=
Add
(
output_zp
,
scaled_int64_t
);
if
(
param
->
output_zero_point
!=
0
)
{
auto
output_zp
=
MakeConstantScalar
(
hp_dtype
,
param
->
output_zero_point
);
shifted_int64_t
=
Add
(
output_zp
,
scaled_int64_t
);
}
// 7) Clip to the out_dtype min/max.
// 7) Clip to the out_dtype min/max.
auto
q_min
=
GetQmin
(
out_dtype
);
auto
q_min
=
GetQmin
(
out_dtype
);
...
...
tests/python/relay/test_qnn_requantize.py
View file @
1bc83853
...
@@ -64,6 +64,7 @@ def test_requantize():
...
@@ -64,6 +64,7 @@ def test_requantize():
input_scale
=
0.5
,
input_scale
=
0.5
,
output_scale
=
0.5
,
output_scale
=
0.5
,
rounding
=
rounding
)
rounding
=
rounding
)
assert
'right_shift'
not
in
mod
.
astext
()
verify
(
mod
,
(
golden_data
,
golden_output
))
verify
(
mod
,
(
golden_data
,
golden_output
))
def
downscale_test
():
def
downscale_test
():
...
...
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