File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 99import argparse
1010import logging
1111import io
12+ from collections .abc import Iterable
1213
1314import torch
1415from 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
Original file line number Diff line number Diff line change 66
77import argparse
88import os .path
9- from typing import Iterator
9+ from typing import Iterator , Tuple
10+ import itertools
1011
1112import torch
1213import torch .nn as nn
2627class 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
4047class CifarNetModel (nn .Module ):
You can’t perform that action at this time.
0 commit comments