File tree Expand file tree Collapse file tree 1 file changed +6
-2
lines changed
Expand file tree Collapse file tree 1 file changed +6
-2
lines changed Original file line number Diff line number Diff line change @@ -257,17 +257,20 @@ PTQ also pre-quantizes model weights but instead of calibrating activations on-t
257257
258258import torch
259259from torch import nn
260+ import copy
260261
261262backend = " 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
302305from torch.quantization import quantize_fx
306+ m = copy.deepcopy(model)
303307m.eval()
304308qconfig_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.
308312with torch.inference_mode():
309313 for _ in range (10 ):
You can’t perform that action at this time.
0 commit comments