Skip to content

Commit 5a6e89e

Browse files
author
Svetlana Karslioglu
authored
Merge pull request pytorch#1088 from pytorch/fix-quant-blogpost
Fix bug in PTQ code example
2 parents a3811b6 + 26d8d86 commit 5a6e89e

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

_posts/2022-2-8-quantization-in-practice.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,17 +257,20 @@ PTQ also pre-quantizes model weights but instead of calibrating activations on-t
257257

258258
import torch
259259
from torch import nn
260+
import copy
260261

261262
backend = "fbgemm" # running on a x86 CPU. Use "qnnpack" if running on ARM.
262263

263-
m = nn.Sequential(
264+
model = nn.Sequential(
264265
nn.Conv2d(2,64,3),
265266
nn.ReLU(),
266267
nn.Conv2d(64, 128, 3),
267268
nn.ReLU()
268269
)
269270

270271
## EAGER MODE
272+
m = copy.deepcopy(model)
273+
m.eval()
271274
"""Fuse
272275
- Inplace fusion replaces the first module in the sequence with the fused module, and the rest with identity modules
273276
"""
@@ -300,10 +303,11 @@ print(m[[1]].weight().element_size()) # 1 byte instead of 4 bytes for FP32
300303

301304
## FX GRAPH
302305
from torch.quantization import quantize_fx
306+
m = copy.deepcopy(model)
303307
m.eval()
304308
qconfig_dict = {"": torch.quantization.get_default_qconfig(backend)}
305309
# Prepare
306-
model_prepared = quantize_fx.prepare_fx(model_to_quantize, qconfig_dict)
310+
model_prepared = quantize_fx.prepare_fx(m, qconfig_dict)
307311
# Calibrate - Use representative (validation) data.
308312
with torch.inference_mode():
309313
for _ in range(10):

0 commit comments

Comments
 (0)