From 5ea397f195d483acec0955d8ee80dbd4526a9dcd Mon Sep 17 00:00:00 2001 From: yiliu30 Date: Tue, 27 Feb 2024 15:50:25 +0800 Subject: [PATCH 1/7] add autotune examples Signed-off-by: yiliu30 --- test/3x/torch/autotune/autotune_demos.py | 55 ++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 test/3x/torch/autotune/autotune_demos.py diff --git a/test/3x/torch/autotune/autotune_demos.py b/test/3x/torch/autotune/autotune_demos.py new file mode 100644 index 00000000000..dd7c1380303 --- /dev/null +++ b/test/3x/torch/autotune/autotune_demos.py @@ -0,0 +1,55 @@ +# Introduction of `autotune` API in INC 3.X + + +# User scripts +from typing import Union + +import torch + + +class UserModel(torch.nn.Module): + def __init__(self, dims): + self.fc1 = torch.nn.Linear(dims, dims) + self.fc2 = torch.nn.Linear(dims, dims) + self.fc3 = torch.nn.Linear(dims, dims) + + def forward(self, x): + x = self.fc1(x) + x = self.fc2(x) + x = self.fc3(x) + return x + + +def build_simple_model(dims=10): + return UserModel(dims) + + +def eval_fn(model) -> Union[float, int]: + # Define a function to evaluate the model and return a score(float or int). + # ... + return 1.0 + + +def calib_fn(model): + # Define a function to calibrate the model. + # ... + pass + + +# 1. Use the default tuning space. +from neural_compressor.torch.quantization import GPTQConfig, RTNConfig, TuningConfig, autotune + +float_model = build_simple_model() +rtn_default_config_set = RTNConfig.get_config_set_for_tuning() + +q_model = autotune(model=float_model, tune_config=TuningConfig(rtn_default_config_set), eval_fns=eval_fn) + +# 2.1 Customize the tuning space for single algorithm. +float_model = build_simple_model() +customize_config_set = TuningConfig(config_set=RTNConfig(bits=[4, 8])) +q_model = autotune(model=float_model, tune_config=TuningConfig(customize_config_set), eval_fns=eval_fn) + +# 2.2 Combine multiple algorithms into the tuning space. +float_model = build_simple_model() +customize_config_set = [RTNConfig(bits=[4, 8]), GPTQConfig(bits=[4, 8])] +q_model = autotune(model=float_model, tune_config=TuningConfig(customize_config_set), run_fn=calib_fn, eval_fns=eval_fn) From fcf24b7680d79df2b246d14a8c22c470d2eaee1d Mon Sep 17 00:00:00 2001 From: yiliu30 Date: Wed, 28 Feb 2024 14:57:10 +0800 Subject: [PATCH 2/7] refine the tuniong config Signed-off-by: yiliu30 --- .gitmodules | 3 +++ 3rd-party/picoGPT | 1 + neural_compressor/common/base_tuning.py | 36 ++++++++++++++----------- 3 files changed, 24 insertions(+), 16 deletions(-) create mode 100644 .gitmodules create mode 160000 3rd-party/picoGPT diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000000..ed797dd3152 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "3rd-party/picoGPT"] + path = 3rd-party/picoGPT + url = https://github.com/jaymody/picoGPT.git diff --git a/3rd-party/picoGPT b/3rd-party/picoGPT new file mode 160000 index 00000000000..817292baea7 --- /dev/null +++ b/3rd-party/picoGPT @@ -0,0 +1 @@ +Subproject commit 817292baea75f194fb0bb8ba2aa5f947af4e45ee diff --git a/neural_compressor/common/base_tuning.py b/neural_compressor/common/base_tuning.py index ca8e9370623..50401e27c7b 100644 --- a/neural_compressor/common/base_tuning.py +++ b/neural_compressor/common/base_tuning.py @@ -14,12 +14,11 @@ import copy -import inspect import uuid from typing import Any, Callable, Dict, Generator, Iterator, List, Optional, Sized, Tuple, Union from neural_compressor.common import Logger -from neural_compressor.common.base_config import BaseConfig, ComposableConfig +from neural_compressor.common.base_config import BaseConfig from neural_compressor.common.utils import TuningLogger logger = Logger().get_logger() @@ -227,19 +226,11 @@ def __iter__(self) -> Generator[BaseConfig, Any, None]: class TuningConfig: - """Base Class for Tuning Criterion. - - Args: - config_set: quantization configs. Default value is empty. - A single config or a list of configs. More details can - be found in the `from_fwk_configs`of `ConfigSet` class. - max_trials: Max tuning times. Default value is 100. Combine with timeout field to decide when to exit. - tolerable_loss: This float indicates how much metric loss we can accept. \ - The metric loss is relative, it can be both positive and negative. Default is 0.01. + """Config for auto tuning pipeline. Examples: # TODO: to refine it - from neural_compressor import TuningConfig + from neural_compressor.torch.quantization import TuningConfig tune_config = TuningConfig( config_set=[config1, config2, ...], max_trials=3, @@ -264,13 +255,26 @@ class TuningConfig: """ def __init__( - self, config_set=None, max_trials=100, sampler: Sampler = default_sampler, tolerable_loss=0.01 - ) -> None: - """Init a TuneCriterion object.""" + self, + config_set: Union[BaseConfig, List[BaseConfig]] = None, + sampler: Sampler = default_sampler, + tolerable_loss=0.01, + max_trials=100, + ): + """Initial a TuningConfig. + + Args: + config_set: A single config or a list of configs. Defaults to None. + sampler: tuning sampler that decide the trials order. Defaults to default_sampler. + tolerable_loss: This float indicates how much metric loss we can accept. + The metric loss is relative, it can be both positive and negative. Default is 0.01. + max_trials: Max tuning times. Default value is 100. Combine with timeout field to decide when to exit. + Defaults to 100. + """ self.config_set = config_set - self.max_trials = max_trials self.sampler = sampler self.tolerable_loss = tolerable_loss + self.max_trials = max_trials class _TrialRecord: From 9808711a3895dc9d99bb9bb76cecc6e9b812f338 Mon Sep 17 00:00:00 2001 From: yiliu30 Date: Wed, 28 Feb 2024 15:09:17 +0800 Subject: [PATCH 3/7] revert change Signed-off-by: yiliu30 --- .gitmodules | 3 --- 3rd-party/picoGPT | 1 - 2 files changed, 4 deletions(-) delete mode 160000 3rd-party/picoGPT diff --git a/.gitmodules b/.gitmodules index ed797dd3152..e69de29bb2d 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +0,0 @@ -[submodule "3rd-party/picoGPT"] - path = 3rd-party/picoGPT - url = https://github.com/jaymody/picoGPT.git diff --git a/3rd-party/picoGPT b/3rd-party/picoGPT deleted file mode 160000 index 817292baea7..00000000000 --- a/3rd-party/picoGPT +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 817292baea75f194fb0bb8ba2aa5f947af4e45ee From 97e67a31e2866f68307abad3cf53efd5399bb33c Mon Sep 17 00:00:00 2001 From: yiliu30 Date: Wed, 28 Feb 2024 15:11:02 +0800 Subject: [PATCH 4/7] revert change Signed-off-by: yiliu30 --- test/3x/torch/autotune/autotune_demos.py | 55 ------------------------ 1 file changed, 55 deletions(-) delete mode 100644 test/3x/torch/autotune/autotune_demos.py diff --git a/test/3x/torch/autotune/autotune_demos.py b/test/3x/torch/autotune/autotune_demos.py deleted file mode 100644 index dd7c1380303..00000000000 --- a/test/3x/torch/autotune/autotune_demos.py +++ /dev/null @@ -1,55 +0,0 @@ -# Introduction of `autotune` API in INC 3.X - - -# User scripts -from typing import Union - -import torch - - -class UserModel(torch.nn.Module): - def __init__(self, dims): - self.fc1 = torch.nn.Linear(dims, dims) - self.fc2 = torch.nn.Linear(dims, dims) - self.fc3 = torch.nn.Linear(dims, dims) - - def forward(self, x): - x = self.fc1(x) - x = self.fc2(x) - x = self.fc3(x) - return x - - -def build_simple_model(dims=10): - return UserModel(dims) - - -def eval_fn(model) -> Union[float, int]: - # Define a function to evaluate the model and return a score(float or int). - # ... - return 1.0 - - -def calib_fn(model): - # Define a function to calibrate the model. - # ... - pass - - -# 1. Use the default tuning space. -from neural_compressor.torch.quantization import GPTQConfig, RTNConfig, TuningConfig, autotune - -float_model = build_simple_model() -rtn_default_config_set = RTNConfig.get_config_set_for_tuning() - -q_model = autotune(model=float_model, tune_config=TuningConfig(rtn_default_config_set), eval_fns=eval_fn) - -# 2.1 Customize the tuning space for single algorithm. -float_model = build_simple_model() -customize_config_set = TuningConfig(config_set=RTNConfig(bits=[4, 8])) -q_model = autotune(model=float_model, tune_config=TuningConfig(customize_config_set), eval_fns=eval_fn) - -# 2.2 Combine multiple algorithms into the tuning space. -float_model = build_simple_model() -customize_config_set = [RTNConfig(bits=[4, 8]), GPTQConfig(bits=[4, 8])] -q_model = autotune(model=float_model, tune_config=TuningConfig(customize_config_set), run_fn=calib_fn, eval_fns=eval_fn) From 7492c33d333a788f995283ca2df0f823d705702c Mon Sep 17 00:00:00 2001 From: yiliu30 Date: Wed, 28 Feb 2024 15:45:48 +0800 Subject: [PATCH 5/7] revert change Signed-off-by: yiliu30 --- .gitmodules | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .gitmodules diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index e69de29bb2d..00000000000 From b5aea7a81c0114f26a12e9feac30426aade579c7 Mon Sep 17 00:00:00 2001 From: Yi30 <106061964+yiliu30@users.noreply.github.com> Date: Wed, 28 Feb 2024 16:19:13 +0800 Subject: [PATCH 6/7] Correct docstring Co-authored-by: Kaihui-intel --- neural_compressor/common/base_tuning.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/neural_compressor/common/base_tuning.py b/neural_compressor/common/base_tuning.py index 50401e27c7b..be991c7d150 100644 --- a/neural_compressor/common/base_tuning.py +++ b/neural_compressor/common/base_tuning.py @@ -268,8 +268,7 @@ def __init__( sampler: tuning sampler that decide the trials order. Defaults to default_sampler. tolerable_loss: This float indicates how much metric loss we can accept. The metric loss is relative, it can be both positive and negative. Default is 0.01. - max_trials: Max tuning times. Default value is 100. Combine with timeout field to decide when to exit. - Defaults to 100. + max_trials: Max tuning times. Combine with timeout field to decide when to exit. Default is 100. """ self.config_set = config_set self.sampler = sampler From 9e5807c43d2e373e6de469a80d79d7a518a8d818 Mon Sep 17 00:00:00 2001 From: yiliu30 Date: Wed, 28 Feb 2024 15:47:16 +0800 Subject: [PATCH 7/7] correct docstring Signed-off-by: yiliu30 --- neural_compressor/common/base_tuning.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/neural_compressor/common/base_tuning.py b/neural_compressor/common/base_tuning.py index be991c7d150..352ac374ab4 100644 --- a/neural_compressor/common/base_tuning.py +++ b/neural_compressor/common/base_tuning.py @@ -268,7 +268,7 @@ def __init__( sampler: tuning sampler that decide the trials order. Defaults to default_sampler. tolerable_loss: This float indicates how much metric loss we can accept. The metric loss is relative, it can be both positive and negative. Default is 0.01. - max_trials: Max tuning times. Combine with timeout field to decide when to exit. Default is 100. + max_trials: Max tuning times. Combine with `tolerable_loss` field to decide when to stop. Default is 100. """ self.config_set = config_set self.sampler = sampler