Skip to content

Commit b8c6494

Browse files
Add default workspace for 3.x (#1553)
Signed-off-by: Kaihui-intel <[email protected]> --------- Signed-off-by: Kaihui-intel <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent d1e994b commit b8c6494

File tree

26 files changed

+262
-26
lines changed

26 files changed

+262
-26
lines changed

neural_compressor/common/__init__.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,41 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from neural_compressor.common.logger import level, log, info, debug, warn, warning, error, fatal
15+
from neural_compressor.common.utils import (
16+
level,
17+
log,
18+
info,
19+
DEBUG,
20+
debug,
21+
warn,
22+
warning,
23+
error,
24+
fatal,
25+
set_random_seed,
26+
set_workspace,
27+
set_resume_from,
28+
set_tensorboard,
29+
Logger,
30+
logger,
31+
)
32+
from neural_compressor.common.base_config import options
33+
34+
35+
__all__ = [
36+
"level",
37+
"log",
38+
"info",
39+
"DEBUG",
40+
"debug",
41+
"warn",
42+
"warning",
43+
"error",
44+
"fatal",
45+
"options",
46+
"Logger",
47+
"logger",
48+
"set_workspace",
49+
"set_random_seed",
50+
"set_resume_from",
51+
"set_tensorboard",
52+
]

neural_compressor/common/base_config.py

Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
from itertools import product
2626
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
2727

28-
from neural_compressor.common.logger import Logger
29-
from neural_compressor.common.utility import (
28+
from neural_compressor.common import Logger
29+
from neural_compressor.common.utils import (
3030
BASE_CONFIG,
3131
COMPOSABLE_CONFIG,
3232
DEFAULT_WHITE_LIST,
33+
DEFAULT_WORKSPACE,
3334
EMPTY_WHITE_LIST,
3435
GLOBAL,
3536
LOCAL,
@@ -38,6 +39,14 @@
3839

3940
logger = Logger().get_logger()
4041

42+
__all__ = [
43+
"ConfigRegistry",
44+
"register_config",
45+
"BaseConfig",
46+
"ComposableConfig",
47+
"Options",
48+
"options",
49+
]
4150

4251
# Dictionary to store registered configurations
4352

@@ -411,3 +420,122 @@ def to_config_mapping(
411420
def register_supported_configs(cls):
412421
"""Add all supported configs."""
413422
raise NotImplementedError
423+
424+
425+
def _check_value(name, src, supported_type, supported_value=[]):
426+
"""Check if the given object is the given supported type and in the given supported value.
427+
428+
Example::
429+
430+
from neural_compressor.common.base_config import _check_value
431+
432+
def datatype(self, datatype):
433+
if _check_value("datatype", datatype, list, ["fp32", "bf16", "uint8", "int8"]):
434+
self._datatype = datatype
435+
"""
436+
if isinstance(src, list) and any([not isinstance(i, supported_type) for i in src]):
437+
assert False, "Type of {} items should be {} but not {}".format(
438+
name, str(supported_type), [type(i) for i in src]
439+
)
440+
elif not isinstance(src, list) and not isinstance(src, supported_type):
441+
assert False, "Type of {} should be {} but not {}".format(name, str(supported_type), type(src))
442+
443+
if len(supported_value) > 0:
444+
if isinstance(src, str) and src not in supported_value:
445+
assert False, "{} is not in supported {}: {}. Skip setting it.".format(src, name, str(supported_value))
446+
elif (
447+
isinstance(src, list)
448+
and all([isinstance(i, str) for i in src])
449+
and any([i not in supported_value for i in src])
450+
):
451+
assert False, "{} is not in supported {}: {}. Skip setting it.".format(src, name, str(supported_value))
452+
453+
return True
454+
455+
456+
class Options:
457+
"""Option Class for configs.
458+
459+
This class is used for configuring global variables. The global variable options is created with this class.
460+
If you want to change global variables, you should use functions from neural_compressor.common.utils.utility.py:
461+
set_random_seed(seed: int)
462+
set_workspace(workspace: str)
463+
set_resume_from(resume_from: str)
464+
set_tensorboard(tensorboard: bool)
465+
466+
Args:
467+
random_seed(int): Random seed used in neural compressor.
468+
Default value is 1978.
469+
workspace(str): The directory where intermediate files and tuning history file are stored.
470+
Default value is:
471+
"./nc_workspace/{}/".format(datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")).
472+
resume_from(str): The directory you want to resume tuning history file from.
473+
The tuning history was automatically saved in the workspace directory
474+
during the last tune process.
475+
Default value is None.
476+
tensorboard(bool): This flag indicates whether to save the weights of the model and the inputs of each layer
477+
for visual display.
478+
Default value is False.
479+
480+
Example::
481+
482+
from neural_compressor.common import set_random_seed, set_workspace, set_resume_from, set_tensorboard
483+
set_random_seed(2022)
484+
set_workspace("workspace_path")
485+
set_resume_from("workspace_path")
486+
set_tensorboard(True)
487+
"""
488+
489+
def __init__(self, random_seed=1978, workspace=DEFAULT_WORKSPACE, resume_from=None, tensorboard=False):
490+
"""Init an Option object."""
491+
self.random_seed = random_seed
492+
self.workspace = workspace
493+
self.resume_from = resume_from
494+
self.tensorboard = tensorboard
495+
496+
@property
497+
def random_seed(self):
498+
"""Get random seed."""
499+
return self._random_seed
500+
501+
@random_seed.setter
502+
def random_seed(self, random_seed):
503+
"""Set random seed."""
504+
if _check_value("random_seed", random_seed, int):
505+
self._random_seed = random_seed
506+
507+
@property
508+
def workspace(self):
509+
"""Get workspace."""
510+
return self._workspace
511+
512+
@workspace.setter
513+
def workspace(self, workspace):
514+
"""Set workspace."""
515+
if _check_value("workspace", workspace, str):
516+
self._workspace = workspace
517+
518+
@property
519+
def resume_from(self):
520+
"""Get resume_from."""
521+
return self._resume_from
522+
523+
@resume_from.setter
524+
def resume_from(self, resume_from):
525+
"""Set resume_from."""
526+
if resume_from is None or _check_value("resume_from", resume_from, str):
527+
self._resume_from = resume_from
528+
529+
@property
530+
def tensorboard(self):
531+
"""Get tensorboard."""
532+
return self._tensorboard
533+
534+
@tensorboard.setter
535+
def tensorboard(self, tensorboard):
536+
"""Set tensorboard."""
537+
if _check_value("tensorboard", tensorboard, bool):
538+
self._tensorboard = tensorboard
539+
540+
541+
options = Options()

neural_compressor/common/base_tuning.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import uuid
1919
from typing import Any, Callable, Dict, Generator, List, Optional, Tuple, Union
2020

21+
from neural_compressor.common import Logger
2122
from neural_compressor.common.base_config import BaseConfig, ComposableConfig
22-
from neural_compressor.common.logger import Logger
2323

2424
logger = Logger().get_logger()
2525

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright (c) 2023 Intel Corporation
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from neural_compressor.common.utils.constants import *
16+
from neural_compressor.common.utils.utility import *
17+
from neural_compressor.common.utils.logger import *

neural_compressor/common/utility.py renamed to neural_compressor/common/utils/constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
GPTQ = "gptq"
3333
FP8_QUANT = "fp8_quant"
3434

35+
# options
36+
import datetime
37+
38+
DEFAULT_WORKSPACE = "./nc_workspace/{}/".format(datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S"))
3539

3640
from typing import Callable, Union
3741

File renamed without changes.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2023 Intel Corporation
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
18+
__all__ = [
19+
"set_workspace",
20+
"set_random_seed",
21+
"set_resume_from",
22+
"set_tensorboard",
23+
]
24+
25+
26+
def set_random_seed(seed: int):
27+
"""Set the random seed in config."""
28+
from neural_compressor.common import options
29+
30+
options.random_seed = seed
31+
32+
33+
def set_workspace(workspace: str):
34+
"""Set the workspace in config."""
35+
from neural_compressor.common import options
36+
37+
options.workspace = workspace
38+
39+
40+
def set_resume_from(resume_from: str):
41+
"""Set the resume_from in config."""
42+
from neural_compressor.common import options
43+
44+
options.resume_from = resume_from
45+
46+
47+
def set_tensorboard(tensorboard: bool):
48+
"""Set the tensorboard in config."""
49+
from neural_compressor.common import options
50+
51+
options.tensorboard = tensorboard

neural_compressor/tensorflow/algorithms/static_quantize/keras.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import tensorflow as tf
2727
import yaml
2828

29-
from neural_compressor.common.logger import Logger
29+
from neural_compressor.common import Logger
3030
from neural_compressor.tensorflow.utils import deep_get, dump_elapsed_time
3131

3232
logger = Logger().get_logger()

neural_compressor/tensorflow/algorithms/static_quantize/quantize_entry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import tensorflow as tf
1919

20-
from neural_compressor.common.utility import STATIC_QUANT
20+
from neural_compressor.common.utils import STATIC_QUANT
2121
from neural_compressor.tensorflow.algorithms.static_quantize.keras import KerasAdaptor
2222
from neural_compressor.tensorflow.quantization.config import StaticQuantConfig
2323
from neural_compressor.tensorflow.utils import register_algo

neural_compressor/tensorflow/quantization/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import tensorflow as tf
2424

2525
from neural_compressor.common.base_config import BaseConfig, config_registry, register_config
26-
from neural_compressor.common.utility import DEFAULT_WHITE_LIST, OP_NAME_OR_MODULE_TYPE, STATIC_QUANT
26+
from neural_compressor.common.utils import DEFAULT_WHITE_LIST, OP_NAME_OR_MODULE_TYPE, STATIC_QUANT
2727

2828
FRAMEWORK_NAME = "keras"
2929

0 commit comments

Comments
 (0)