You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+2-1Lines changed: 2 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -89,14 +89,15 @@ python setup.py install
89
89
90
90
# Deep Dive
91
91
92
-
* [Quantization](docs/Quantization.md) is the processes that enable inference and training by performing computations at low precision data type, such as fixed point integers. LPOT supports [Post-Training Quantization (static and dynamic)](docs/PTQ.md) and [Quantization-Aware Training](docs/QAT.md)
92
+
* [Quantization](docs/Quantization.md) is the processes that enable inference and training by performing computations at low precision data type, such as fixed point integers. LPOT supports [Post-Training Quantization](docs/PTQ.md) and [Quantization-Aware Training](docs/QAT.md)
93
93
* [Pruning](docs/pruning.md) provides a common method forintroducing sparsityin weights and activations.
94
94
* [Benchmarking](docs/benchmark.md) introduces how to utilize the benchmark interface of LPOT.
95
95
* [Mixed precision](docs/mixed_precision.md) introduces how to enable mixed precision, including BFP16 and int8 and FP32, on Intel platforms during tuning.
96
96
* [Transform](docs/transform.md) introduces how to utilize LPOT buildin data processing and how to develop a custom data processing method.
97
97
* [Dataset](docs/dataset.md) introudces how to utilize LPOT buildin dataset and how to develop a custom dataset.
98
98
* [Metric](docs/metric.md) introduces how to utilize LPOT buildin metric and how to develop a custom metric.
99
99
* [TensorBoard](docs/tensorboard.md) provides tensor histogram and execution graph for tuning debugging purpose.
100
+
* [PyTorch Deploy](docs/pytorch_model_saving.md) introduces how LPOT saves and loads quantized PyTorch model.
`quantize` function is used to do calibration and quanitization in post-training quantization.
37
+
`evaluate` function is used to run evaluation on validation dataset.
38
+
`query_fw_capability` function is used to run query framework quantization capability and intersects with user yaml configuration setting to
39
+
`query_fused_patterns` function is used to run query framework graph fusion capability and decide the fusion tuning space.
40
+
41
+
Customize a New Framework Backend
11
42
=================
12
43
Let us take onnxruntime as en example. Onnxruntime is a backend proposed by microsoft, and it's based on MLAS kernel defaultly.
13
44
Onnxruntime already has [quantization tools](https://github.com/microsoft/onnxruntime/tree/master/onnxruntime/python/tools/quantization), so the question becomes how to intergrate onnxruntime quantization tools into LPOT.
Copy file name to clipboardExpand all lines: docs/benchmark.md
+11-6Lines changed: 11 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,12 @@
1
1
Benchmarking
2
2
===============
3
3
4
-
Benchmakring measuring the model performance with the objective settings, user can get the performance of the models between float32 model and quantized low precision model in same scenarios that they configured in yaml. Benchmarking is always used after a quantization process.
4
+
Benchmarking feature of LPOT is used to measure the model performance with the objective settings, user can get the performance of the models between float32 model and quantized low precision model in same scenarios that they configured in yaml. Benchmarking is always used after a quantization process.
5
5
6
6
# how to use it
7
7
## config evaluation filed in yaml file
8
-
'''
8
+
9
+
```
9
10
evaluation: # optional. required if user doesn't provide eval_func in lpot.Quantization.
10
11
accuracy: # optional. required if user doesn't provide eval_func in lpot.Quantization.
11
12
metric:
@@ -41,20 +42,24 @@ evaluation: # optional. required if use
41
42
ToTensor:
42
43
Normalize:
43
44
mean: [0.485, 0.456, 0.406]
44
-
'''
45
+
```
45
46
46
47
in this example config you can see there is 2 sub-fields named 'accuracy' and 'performance', benchmark module will get the accuracy and performance of the model. User can also remove the performance field to only get accuracy of the model or the opposite. It's flexible to configure the benchmark you want.
48
+
47
49
## use user specific dataloader to run benchmark
50
+
48
51
In this case, you should config your dataloader and lpot will construct an evaluation function to run the benchmarking.
49
-
'''python
52
+
53
+
```python
50
54
dataset = Dataset() # dataset class that implement __getitem__ method or __iter__ method
Copy file name to clipboardExpand all lines: docs/introduction.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@ The `conf_fname` parameter used in the class initialization is the path to user
26
26
>
27
27
> Intel® Low Precision Optimization Tool provides template yaml files for the [Post-Training Quantization](../lpot/template/ptq.yaml), [Quantization-Aware Traing](../lpot/template/qat.yaml), and [Pruning](../lpot/template/pruning.yaml) scenarios. Refer to these template files to understand the meaning of each field.
28
28
29
-
> Note that most fields in the yaml templates are optional. View the [HelloWorld Yaml](../examples/helloworld/tf2.x/conf.yaml) example for reference.
29
+
> Note that most fields in the yaml templates are optional. View the [HelloWorld Yaml](../examples/helloworld/tf_example2/conf.yaml) example for reference.
30
30
31
31
For TensorFlow backend, LPOT supports passing the path of keras model, frozen pb, checkpoint, saved model as the input of `model` parameter of `Quantization()`.
32
32
@@ -139,4 +139,4 @@ If `dataloader` and `metric` components get fully configured by yaml, the quanti
139
139
quantizer = Quantization('/path/to/user.yaml')
140
140
q_model = quantizer('/path/to/model')
141
141
```
142
-
Examples of this usage are at [TensorFlow Classification Models](../examples/tensorflow/image_recognition/README.md).
142
+
Examples of this usage are at [TensorFlow Classification Models](../examples/tensorflow/image_recognition/README.md).
| tf1.x saved model | No|| No plan to support it |
19
19
| tf2.x checkpoint | No || As tf2.x checkpoint only has weight and does not contain any description of the computation, please use different tf2.x model for quantization |
approach: post_training_static_quant # optional. default value is post_training_static_quant.
47
47
calibration:
48
48
sampling_size: 1000, 2000 # optional. default value is the size of whole dataset. used to set how many portions of calibration dataset is used. exclusive with iterations field.
49
-
dataloader: # optional. if not specified, user need construct a q_dataloader in code for ilit.Quantization.
49
+
dataloader: # optional. if not specified, user need construct a q_dataloader in code for lpot.Quantization.
50
50
dataset:
51
51
TFRecordDataset:
52
52
root: /path/to/tf_record
@@ -103,12 +103,6 @@ tuning:
103
103
random_seed: 9527# optional. random seed for deterministic tuning.
104
104
tensorboard: True # optional. dump tensor distribution in evaluation phase for debug purpose. default value is False.
105
105
```
106
-
## Customize a new strategy
107
-
108
-
Users can use the basic `TuneStrategy` class to enable a new strategy with a
109
-
new `self.next_tune_cfg()` function implementation. If the new strategy
110
-
needs additional information, users can override the `self.traverse()` in
111
-
the new strategy, such as `TPE` strategy.
112
106
113
107
### Basic
114
108
@@ -296,3 +290,39 @@ tuning configs to generate a better-performance quantized model.
296
290
`Random` usage is similar to `Basic`:
297
291
298
292
```yaml
293
+
tuning:
294
+
strategy:
295
+
name: random
296
+
accuracy_criterion:
297
+
relative: 0.01
298
+
exit_policy:
299
+
timeout: 0
300
+
random_seed: 9527
301
+
302
+
```
303
+
304
+
Customize a New Tuning Strategy
305
+
======================
306
+
307
+
Intel® Low Precision Optimization Tool supports new strategy extension by implementing a subclass of `TuneStrategy` class in lpot.strategy package
308
+
and registering this strategy by `strategy_registry` decorator.
309
+
310
+
for example, user can implement a `Abc` strategy like below:
The `next_tune_cfg` function is used to yield the next tune configuration according to some algorithm or strategy. `TuneStrategy` base class will traverse
325
+
all the tuning space till a quantization configuration meets pre-defined accuray criterion.
326
+
327
+
If the traverse behavior of `TuneStrategy` base class does not meet new strategy requirement, it could re-implement `traverse` function with self own logic.
328
+
An example like this is under [TPE Strategy](../lpot/strategy/tpe.py).
Copy file name to clipboardExpand all lines: docs/tutorial.md
+19-54Lines changed: 19 additions & 54 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -38,110 +38,75 @@ To define a customized dataloader or evaluator for quantization, user can implem
38
38
39
39
Next, let's introduce how to do quantization in different scenarios.
40
40
41
-
# Coding free quantization
42
-
The examples/helloworld/tf_coding_free demonstrates how to utilize LPOT builtin dataloader and evalautors for quantizaiton, and how to use LPOT Benchmark class for performance and accuracy measurement, and user only need to add 3 lines of launcher code for tuning. See[README](examples/helloworld/tf_coding_free/README.md)
41
+
# Buildin dataloader and metric
42
+
The [tf_example1](examples/helloworld/tf_example1) demonstrates how to utilize LPOT builtin dataloader and evalautors for quantizaiton. User only needs to add 3 lines of launcher code for tuning, see[README](examples/helloworld/tf_example1/README.md) for more details.
43
43
44
44
45
45
# Customized dataloader
46
-
With a Keras saved model as example, [examples/helloworld/tf2.x_custom_dataloader](examples/helloworld/tf2.x_custom_dataloader] demonstrates how to define a customized dataloader.
46
+
With a Keras saved model as example, [examples/helloworld/tf_example2](examples/helloworld/tf_example2] demonstrates how to define a customized dataloader and metric for quantization.
47
+
48
+
First define a dataset class on mnist, it implements a __getitem() interface and return the next (image, label) pair.
Example examples/helloworld/tf2.x_custom_metric shows how to define and use a customized evaluator for quantization, this evaluator calculate accuracy and it will be registered in Qunatization object with metric() funciton. See [README](examples/helloworld/tf2.x_custom_metric/README.md)
83
-
```
84
-
quantizer.metric('hello_metric', MyMetric)
85
-
```
66
+
Then define a customized metric to caculate accuracy. The update() function record the predict result and result() function provide the summary of accurate rate.
86
67
87
68
```
69
+
import lpot
70
+
from lpot.metric import Metric
88
71
class MyMetric(Metric):
89
72
def __init__(self, *args):
90
-
# TODO:initialize metric related info here
91
73
self.pred_list = []
92
74
self.label_list = []
93
75
self.samples = 0
94
76
pass
95
77
96
78
def update(self, predict, label):
97
-
# TODO:metric evaluation per evaluation
98
79
self.pred_list.extend(np.argmax(predict, axis=1))
99
80
self.label_list.extend(label)
100
81
self.samples += len(label)
101
82
pass
102
83
103
84
def reset(self):
104
-
# TODO:reset variable if needed
105
85
self.pred_list = []
106
86
self.label_list = []
107
87
self.samples = 0
108
88
pass
109
89
110
90
def result(self):
111
-
# TODO:calculate the whole batch final evaluation result
# The interface is similiar for different TensorFlow models
121
-
1. see example on tf1.x frozen pb at [tf1.x_pb](examples/helloworld/tf1.x_pb/).
122
-
2. see example on tf1.x checkpoint at [tf1.x_ckpt](examples/helloworld/tf1.x_ckpt/).
123
-
3. To quantize a slim .ckpt model, we need to get the graph. See a full example on slim at [examples/helloworld/tf1.x_slim](examples/helloworld/tf1.x_slim).
96
+
Then define a dataloader based on the mnist dataset, and register the customer metric to run quantization. q_model is the quantized model generated.
124
97
```
125
-
import lpot
126
-
quantizer = lpot.Quantization('./conf.yaml')
127
-
128
-
# Get graph from slim checkpoint
129
-
from tf_slim.nets import inception
130
-
model_func = inception.inception_v1
131
-
arg_scope = inception.inception_v1_arg_scope()
132
-
kwargs = {'num_classes': 1001}
133
-
inputs_shape = [None, 224, 224, 3]
134
-
images = tf.compat.v1.placeholder(name='input', \
135
-
dtype=tf.float32, shape=inputs_shape)
136
98
137
-
from lpot.adaptor.tf_utils.util import get_slim_graph
# The interface is similiar for different TensorFlow models
109
+
1. TensorFlow checkpoint: see [tf_example4](examples/helloworld/tf_example4)
110
+
2. Enable benchmark for performanace and accuracy measurement: see [tf_example5](examples/helloworld/tf_example5)
111
+
3. TensorFlow slim model: see [tf_example3](examples/helloworld/tf_example3), while to quantize a slim .ckpt model we need to get the graph first, see [README](examples/helloworld/tf_example3/README.md).
Copy file name to clipboardExpand all lines: examples/helloworld/tf_example4/conf.yaml
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ model: # mandatory. lpot uses this
5
5
6
6
quantization: # optional. tuning constraints on model-wise for advance user to reduce tuning space.
7
7
calibration:
8
-
sampling_size: 20# optional. default value is the size of whole dataset. used to set how many portions of calibration dataset is used. exclusive with iterations field.
8
+
sampling_size: 20# optional. default value is the size of whole dataset. used to set how many portions of calibration dataset is used. exclusive with iterations field.
9
9
10
10
evaluation: # optional. required if user doesn't provide eval_func in lpot.Quantization.
11
11
accuracy: # optional. required if user doesn't provide eval_func in lpot.Quantization.
0 commit comments