How To Configure Auto Optimizer

Auto Optimizer is a tool that can be used to automatically search Olive passes combination based on:

  1. input model

  2. target device

  3. target precision: fp32, fp16, int8, int4 and etc.

  4. target evaluation metric: accuracy, latency and etc.

All above information(is called “optimization factors” in this doc) is provided by user through a configuration file now, then run by:

olive run --config <config_file>.json

With the help of Auto Optimizer:

  1. User DOES NOT need to provide the passes combination manually.

  2. User DOES NOT need to write redundant passes configs.

At the same time, user still has the flexibility to specify the accelerator, execution providers and expected precision of output models.

Currently, auto optimizer supports onnx optimization stack only. For original OpenVINO (IR MODEL) and SNPE, user still need to provide passes combination manually by now. Auto Optimizer is still under development actively, and we will keep improving it to make it more powerful and easy to use.

Auto Optimizer Configuration

Config Fields:

  1. opt_level[int]: default 0, to look up built-in passes template.

  2. disable_auto_optimizer[bool]: default False.
    • If set to True, Auto Optimizer will be disabled and user need to provide passes combination manually.

  3. precision[optional[str]]: default None.
    • The precision of output model. If user does not set the precision of output model, it will be determined by above optimization factors. Olive supports “fp32”, “fp16” and “int8” output precision for now.

Here is a simple example of Auto Optimizer configuration, the item which is not provided will use the default value:

{
    "engine": {
        "search_strategy": {
            "execution_order": "joint",
            "search_algorithm": "tpe",
            "search_algorithm_config": {
                "num_samples": 1,
                "seed": 0
            }
        },
        "evaluator": "common_evaluator",
        "cache_dir": "cache",
        "output_dir" : "models/bert_gpu"
    },
    "auto_optimizer_config": {
        "opt_level": 0,
        "disable_auto_optimizer": false,
        "precision": "fp16"
    }
}

Note

In this example, Auto Optimizer will search for the best passes combination for different execution providers, e.g. CUDAExecutionProvider and TensorrtExecutionProvider.

  • For CUDAExecutionProvider, it will try float16 in OrtTransformersOptimization.

  • For TensorrtExecutionProvider, it will try trt_fp16 in OrtPerfTuning.

Here the available pass flows for given accelerator, execution providers and precision:

../_images/pass_flows.png

Auto Optimizer can catch up with manual settings in most cases, and it is more convenient to use.

Here is another quick comparison between Auto Optimizer and manual settings.

 1{
 2    "input_model":{
 3        "type": "PyTorchModel",
 4        "config": {
 5            "hf_config": {
 6                "model_name": "Intel/bert-base-uncased-mrpc",
 7                "task": "text-classification",
 8                "dataset": {
 9                    "data_name":"glue",
10                    "subset": "mrpc",
11                    "split": "validation",
12                    "input_cols": ["sentence1", "sentence2"],
13                    "label_cols": ["label"],
14                    "batch_size": 1
15                }
16            }
17        }
18    },
19    "systems": {
20        "local_system": {
21            "type": "LocalSystem",
22            "config": {
23                "accelerators": [
24                    {"device": "gpu", "execution_providers": ["CUDAExecutionProvider", "TensorrtExecutionProvider"]}
25                ]
26            }
27        }
28    },
29    "evaluators": {
30        "common_evaluator": {
31            "metrics":[
32                {
33                    "name": "accuracy",
34                    "type": "accuracy",
35                    "backend": "huggingface_metrics",
36                    "sub_types": [
37                        {"name": "accuracy", "priority": 1, "goal": {"type": "max-degradation", "value": 0.01}},
38                        {"name": "f1"}
39                    ]
40                },
41                {
42                    "name": "latency",
43                    "type": "latency",
44                    "sub_types": [
45                        {"name": "avg", "priority": 2, "goal": {"type": "percent-min-improvement", "value": 20}},
46                        {"name": "max"},
47                        {"name": "min"}
48                    ]
49                }
50            ]
51        }
52    },
53    "engine": {
54        "search_strategy": {
55            "execution_order": "joint",
56            "search_algorithm": "tpe",
57            "search_algorithm_config": {
58                "num_samples": 1,
59                "seed": 0
60            }
61        },
62        "evaluator": "common_evaluator",
63        "host": "local_system",
64        "target": "local_system",
65        "cache_dir": "cache",
66        "output_dir" : "models/bert_gpu"
67    }
68}

Note

In this example, Auto Optimizer can use default settings to catch up with manual settings. Auto Optimizer is aware of following rules which requires expert knowledge in manual settings:

  1. For CUDAExecutionProvider:
    • it would be better to disable enable_trt_fp16 and enable enable_cuda_graph in OrtPerfTuning pass, and enable float16 in OrtTransformersOptimization pass.

  2. For TensorrtExecutionProvider:
    • it would be better to enable enable_trt_fp16 and disable enable_cuda_graph in OrtPerfTuning pass, and disable float16 in OrtTransformersOptimization pass.

  3. At the same time, for both CUDAExecutionProvider and TensorrtExecutionProvider:
    • it would be better to enable io_bind in OrtPerfTuning pass.