Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
nni
Commits
9520f251
Unverified
Commit
9520f251
authored
Dec 23, 2020
by
Dalong
Committed by
GitHub
Dec 23, 2020
Browse files
fix potential bug in quantize device (#3212)
Co-authored-by:
yuanxiulong
<
yuanxiulong@bytedance.com
>
parent
5f0ac599
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
6 additions
and
15 deletions
+6
-15
nni/algorithms/compression/pytorch/quantization/quantizers.py
...algorithms/compression/pytorch/quantization/quantizers.py
+6
-15
No files found.
nni/algorithms/compression/pytorch/quantization/quantizers.py
View file @
9520f251
...
...
@@ -85,16 +85,10 @@ def update_quantization_param(bits, rmin, rmax):
# extend the [min, max] interval to ensure that it contains 0.
# Otherwise, we would not meet the requirement that 0 be an exactly
# representable value.
if
rmin
.
is_cuda
:
rmin
=
torch
.
min
(
rmin
,
torch
.
Tensor
([
0
]).
cuda
())
rmax
=
torch
.
max
(
rmax
,
torch
.
Tensor
([
0
]).
cuda
())
qmin
=
torch
.
Tensor
([
0
]).
cuda
()
qmax
=
torch
.
Tensor
([(
1
<<
bits
)
-
1
]).
cuda
()
else
:
rmin
=
torch
.
min
(
rmin
,
torch
.
Tensor
([
0
]))
rmax
=
torch
.
max
(
rmax
,
torch
.
Tensor
([
0
]))
qmin
=
torch
.
Tensor
([
0
])
qmax
=
torch
.
Tensor
([(
1
<<
bits
)
-
1
])
rmin
=
torch
.
min
(
rmin
,
torch
.
Tensor
([
0
]).
to
(
rmin
.
device
))
rmax
=
torch
.
max
(
rmax
,
torch
.
Tensor
([
0
]).
to
(
rmin
.
device
))
qmin
=
torch
.
Tensor
([
0
]).
to
(
rmin
.
device
)
qmax
=
torch
.
Tensor
([(
1
<<
bits
)
-
1
]).
to
(
rmin
.
device
)
# First determine the scale.
scale
=
(
rmax
-
rmin
)
/
(
qmax
-
qmin
)
...
...
@@ -103,7 +97,6 @@ def update_quantization_param(bits, rmin, rmax):
initial_zero_point
=
qmin
-
rmin
/
scale
# Now we need to nudge the zero point to be an integer
nudged_zero_point
=
0
if
initial_zero_point
<
qmin
:
nudged_zero_point
=
qmin
elif
initial_zero_point
>
qmax
:
...
...
@@ -199,10 +192,8 @@ class QAT_Quantizer(Quantizer):
-------
Tensor
"""
if
real_val
.
is_cuda
:
op
.
zero_point
=
op
.
zero_point
.
cuda
()
op
.
scale
=
op
.
scale
.
cuda
()
op
.
zero_point
=
op
.
zero_point
.
to
(
real_val
.
device
)
op
.
scale
=
op
.
scale
.
to
(
real_val
.
device
)
transformed_val
=
op
.
zero_point
+
real_val
/
op
.
scale
qmin
=
0
qmax
=
(
1
<<
bits
)
-
1
...
...
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