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
257
257
258
258
import torch
259
259
from torch import nn
260
+ import copy
260
261
261
262
backend = " fbgemm" # running on a x86 CPU. Use "qnnpack" if running on ARM.
262
263
263
- m = nn.Sequential(
264
+ model = nn.Sequential(
264
265
nn.Conv2d(2 ,64 ,3 ),
265
266
nn.ReLU(),
266
267
nn.Conv2d(64 , 128 , 3 ),
267
268
nn.ReLU()
268
269
)
269
270
270
271
# # EAGER MODE
272
+ m = copy.deepcopy(model)
273
+ m.eval()
271
274
""" Fuse
272
275
- Inplace fusion replaces the first module in the sequence with the fused module, and the rest with identity modules
273
276
"""
@@ -300,10 +303,11 @@ print(m[[1]].weight().element_size()) # 1 byte instead of 4 bytes for FP32
300
303
301
304
# # FX GRAPH
302
305
from torch.quantization import quantize_fx
306
+ m = copy.deepcopy(model)
303
307
m.eval()
304
308
qconfig_dict = {" " : torch.quantization.get_default_qconfig(backend)}
305
309
# Prepare
306
- model_prepared = quantize_fx.prepare_fx(model_to_quantize , qconfig_dict)
310
+ model_prepared = quantize_fx.prepare_fx(m , qconfig_dict)
307
311
# Calibrate - Use representative (validation) data.
308
312
with torch.inference_mode():
309
313
for _ in range (10 ):
You can’t perform that action at this time.
0 commit comments