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
93c53503
Unverified
Commit
93c53503
authored
Sep 06, 2021
by
chenbohua3
Committed by
GitHub
Sep 06, 2021
Browse files
should use .data in quantization wrapper (#4113)
parent
da294255
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
2 deletions
+29
-2
nni/compression/pytorch/compressor.py
nni/compression/pytorch/compressor.py
+2
-2
test/ut/sdk/test_compressor_torch.py
test/ut/sdk/test_compressor_torch.py
+27
-0
No files found.
nni/compression/pytorch/compressor.py
View file @
93c53503
...
@@ -529,13 +529,13 @@ class QuantizerModuleWrapper(torch.nn.Module):
...
@@ -529,13 +529,13 @@ class QuantizerModuleWrapper(torch.nn.Module):
else
:
else
:
self
.
module
.
register_parameter
(
'old_weight'
,
torch
.
nn
.
Parameter
(
self
.
module
.
weight
))
self
.
module
.
register_parameter
(
'old_weight'
,
torch
.
nn
.
Parameter
(
self
.
module
.
weight
))
delattr
(
self
.
module
,
'weight'
)
delattr
(
self
.
module
,
'weight'
)
self
.
module
.
register_buffer
(
'weight'
,
self
.
module
.
old_weight
)
self
.
module
.
register_buffer
(
'weight'
,
self
.
module
.
old_weight
.
data
)
# for batch normalization folding
# for batch normalization folding
if
self
.
bn_module
is
not
None
:
if
self
.
bn_module
is
not
None
:
if
_check_bias
(
self
.
module
):
if
_check_bias
(
self
.
module
):
self
.
module
.
register_parameter
(
'old_bias'
,
torch
.
nn
.
Parameter
(
self
.
module
.
bias
))
self
.
module
.
register_parameter
(
'old_bias'
,
torch
.
nn
.
Parameter
(
self
.
module
.
bias
))
init_tensor
=
self
.
module
.
old_bias
init_tensor
=
self
.
module
.
old_bias
.
data
else
:
else
:
init_tensor
=
torch
.
zeros_like
(
self
.
bn_module
.
weight
)
init_tensor
=
torch
.
zeros_like
(
self
.
bn_module
.
weight
)
delattr
(
self
.
module
,
'bias'
)
delattr
(
self
.
module
,
'bias'
)
...
...
test/ut/sdk/test_compressor_torch.py
View file @
93c53503
...
@@ -305,6 +305,33 @@ class CompressorTestCase(TestCase):
...
@@ -305,6 +305,33 @@ class CompressorTestCase(TestCase):
self
.
assertTrue
(
calibration_config
is
not
None
)
self
.
assertTrue
(
calibration_config
is
not
None
)
self
.
assertTrue
(
len
(
calibration_config
)
==
4
)
self
.
assertTrue
(
len
(
calibration_config
)
==
4
)
def
test_torch_quantizer_weight_type
(
self
):
quantizer_list
=
[
torch_quantizer
.
QAT_Quantizer
,
torch_quantizer
.
LsqQuantizer
,
torch_quantizer
.
ObserverQuantizer
,
torch_quantizer
.
NaiveQuantizer
,
torch_quantizer
.
DoReFaQuantizer
]
for
quantizer_type
in
quantizer_list
:
model
=
TorchModel
().
eval
()
config_list
=
[{
'quant_types'
:
[
'weight'
],
'quant_bits'
:
8
,
'op_types'
:
[
'Conv2d'
,
'Linear'
]
}]
optimizer
=
torch
.
optim
.
SGD
(
model
.
parameters
(),
lr
=
0.01
,
momentum
=
0.5
)
dummy
=
torch
.
randn
(
1
,
1
,
28
,
28
)
if
quantizer_type
==
torch_quantizer
.
QAT_Quantizer
:
quantizer_type
(
model
,
config_list
,
optimizer
,
dummy_input
=
dummy
)
else
:
quantizer_type
(
model
,
config_list
,
optimizer
)
self
.
assertFalse
(
isinstance
(
model
.
conv1
.
module
.
weight
,
torch
.
nn
.
Parameter
))
self
.
assertFalse
(
isinstance
(
model
.
conv2
.
module
.
weight
,
torch
.
nn
.
Parameter
))
self
.
assertFalse
(
isinstance
(
model
.
fc1
.
module
.
weight
,
torch
.
nn
.
Parameter
))
self
.
assertFalse
(
isinstance
(
model
.
fc2
.
module
.
weight
,
torch
.
nn
.
Parameter
))
def
test_torch_QAT_quantizer
(
self
):
def
test_torch_QAT_quantizer
(
self
):
model
=
TorchModel
()
model
=
TorchModel
()
config_list
=
[{
config_list
=
[{
...
...
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