Skip to content

Commit 3b0c34e

Browse files
committed
Pull request pytorch#8: Feature/cifar net integration clenup
Merge in AITEC/executorch from feature/cifar-net-integration-clenup to main-nxp * commit '9d0ab6dd3a35327ed74708447a4ecc3c0ee7f8e2': Cleaup CifarNet integration and add quantization with calibration dataset
2 parents 9dfb46a + 9d0ab6d commit 3b0c34e

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

‎examples/nxp/aot_neutron_compile.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import argparse
1010
import logging
1111
import io
12+
from collections.abc import Iterable
1213

1314
import torch
1415
from torch.ao.quantization.quantize_pt2e import prepare_pt2e, convert_pt2e
@@ -36,7 +37,8 @@ def get_model_and_inputs_from_name(model_name: str):
3637
if model_name in models.keys():
3738
m = models[model_name]()
3839
model = m.get_eager_model()
39-
example_inputs = (next(m.get_example_inputs())[0],) #TODO (Robert): Needs to redesign the CifarNet example.
40+
example_inputs = m.get_example_inputs()
41+
calibration_inputs = m.get_calibration_inputs(64)
4042
# Case 2: Model is defined in executorch/examples/models/
4143
elif model_name in MODEL_NAME_TO_MODEL.keys():
4244
logging.warning(
@@ -63,7 +65,15 @@ def post_training_quantize(model, calibration_inputs):
6365
quantizer.set_global(operator_config)
6466
m = prepare_pt2e(model, quantizer)
6567
# Calibration:
66-
m(*calibration_inputs)
68+
logging.debug(f"Calibrating model")
69+
if not isinstance(calibration_inputs, tuple): # TODO(Robert): Assumption that calibration_inputs is finite.
70+
get_batch_size = lambda data: data[0].shape[0]
71+
for i, data in enumerate(calibration_inputs):
72+
if i % (1000 // get_batch_size(data)) == 0:
73+
logging.debug(f"{i*get_batch_size(data)} inputs done")
74+
m(*data)
75+
else:
76+
m(*calibration_inputs)
6777
m = convert_pt2e(m)
6878
logging.debug(f"Quantized model: {m}")
6979
return m

‎examples/nxp/cifar_net/cifar_net.py‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
import argparse
88
import os.path
9-
from typing import Iterator
9+
from typing import Iterator, Tuple
10+
import itertools
1011

1112
import torch
1213
import torch.nn as nn
@@ -26,15 +27,21 @@
2627
class CifarNet(model_base.EagerModelBase):
2728

2829
def __init__(self, batch_size: int = 1, pth_file: str = 'cifar_net.pth'):
29-
self.batch_size = batch_size
30+
self.batch_size = batch_size # TODO(Robert): Do we need the batch size at all?
3031
self.pth_file = pth_file
3132

3233
def get_eager_model(self) -> torch.nn.Module:
3334
return get_model(self.batch_size, state_dict_file=self.pth_file)
3435

35-
def get_example_inputs(self) -> Iterator[torch.Tensor]:
36-
tl = get_test_loader(self.batch_size)
37-
return iter(tl)
36+
def get_example_inputs(self) -> Tuple[torch.Tensor]:
37+
tl = get_test_loader()
38+
ds, _ = tl.dataset[0] # Dataset returns the data and the class. We need just the data.
39+
return (ds.unsqueeze(0),)
40+
41+
def get_calibration_inputs(self, batch_size: int = 1) -> Iterator[torch.Tensor]:
42+
tl = get_test_loader(batch_size)
43+
get_first = lambda a, b: (a,)
44+
return itertools.starmap(get_first, iter(tl))
3845

3946

4047
class CifarNetModel(nn.Module):

0 commit comments

Comments
 (0)