Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add framework-agnostic tests for common components #1575

Merged
merged 5 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .azure-pipelines/scripts/ut/3x/run_3x_ort.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export COVERAGE_RCFILE=/neural-compressor/.azure-pipelines/scripts/ut/3x/coverag
inc_path=$(python -c 'import neural_compressor; print(neural_compressor.__path__[0])')
cd /neural-compressor/test || exit 1
find ./3x/onnxrt/* -name "test*.py" | sed 's,\.\/,coverage run --source='"${inc_path}"' --append ,g' | sed 's/$/ --verbose/'> run.sh
find ./3x/common/* -name "test*.py" | sed 's,\.\/,coverage run --source='"${inc_path}"' --append ,g' | sed 's/$/ --verbose/'>> run.sh

LOG_DIR=/neural-compressor/log_dir
mkdir -p ${LOG_DIR}
Expand Down
1 change: 1 addition & 0 deletions .azure-pipelines/scripts/ut/3x/run_3x_pt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export COVERAGE_RCFILE=/neural-compressor/.azure-pipelines/scripts/ut/3x/coverag
inc_path=$(python -c 'import neural_compressor; print(neural_compressor.__path__[0])')
cd /neural-compressor/test || exit 1
find ./3x/torch/* -name "test*.py" | sed 's,\.\/,coverage run --source='"${inc_path}"' --append ,g' | sed 's/$/ --verbose/'> run.sh
find ./3x/common/* -name "test*.py" | sed 's,\.\/,coverage run --source='"${inc_path}"' --append ,g' | sed 's/$/ --verbose/'>> run.sh

LOG_DIR=/neural-compressor/log_dir
mkdir -p ${LOG_DIR}
Expand Down
1 change: 1 addition & 0 deletions .azure-pipelines/scripts/ut/3x/run_3x_tf.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export COVERAGE_RCFILE=/neural-compressor/.azure-pipelines/scripts/ut/3x/coverag
inc_path=$(python -c 'import neural_compressor; print(neural_compressor.__path__[0])')
cd /neural-compressor/test || exit 1
find ./3x/tensorflow/* -name "test*.py" | sed 's,\.\/,coverage run --source='"${inc_path}"' --append ,g' | sed 's/$/ --verbose/'> run.sh
find ./3x/common/* -name "test*.py" | sed 's,\.\/,coverage run --source='"${inc_path}"' --append ,g' | sed 's/$/ --verbose/'>> run.sh

LOG_DIR=/neural-compressor/log_dir
mkdir -p ${LOG_DIR}
Expand Down
1 change: 1 addition & 0 deletions .azure-pipelines/ut-3x-pt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pr:
- neural_compressor/common
- neural_compressor/torch
- test/3x/torch
- test/3x/common
chensuyue marked this conversation as resolved.
Show resolved Hide resolved
- setup.py
- requirements_pt.txt
- .azure-pipelines/scripts/ut/3x/collect_log_3x.sh
Expand Down
142 changes: 142 additions & 0 deletions test/3x/common/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
"""Tests for common components.
Owner(s): ["module: common | auto-tune"]
yiliu30 marked this conversation as resolved.
Show resolved Hide resolved

!!! Please do not import any framework-specific modules in this file. !!!
* Note, we may need to add some auto check mechanisms to ensure this.

These tests aim to assess the fundamental functionalities of common components and enhance code coverage.
All tests will be included for each framework CI.

* Note
The folder structure:
.
├── 3x
│ ├── common
│ ├── onnxrt
│ ├── tensorflow
│ └── torch

For each fwk CI:

onnxrt_included_folder:
├── 3x
│ ├── common
│ ├── onnxrt

tensorflow_included_folder:
├── 3x
│ ├── common
│ ├── tensorflow


torch_included_folder:
├── 3x
│ ├── common
│ ├── torch
"""

import unittest

from neural_compressor.common import Logger

logger = Logger().get_logger()

from typing import Any, Callable, List, Optional, Tuple, Union

from neural_compressor.common.base_config import BaseConfig, get_all_config_set_from_config_registry, register_config
from neural_compressor.common.utils import DEFAULT_WHITE_LIST, OP_NAME_OR_MODULE_TYPE

PRIORITY_FAKE_ALGO = 100
FAKE_CONFIG_NAME = "fake"
DEFAULT_WEIGHT_BITS = [4, 6]

FAKE_FRAMEWORK_NAME = "FAKE_FWK"


@register_config(framework_name=FAKE_FRAMEWORK_NAME, algo_name=FAKE_CONFIG_NAME, priority=PRIORITY_FAKE_ALGO)
class FakeAlgoConfig(BaseConfig):
"""Config class for fake algo."""

supported_configs: List = []
params_list = [
"weight_dtype",
"weight_bits",
]
name = FAKE_CONFIG_NAME

def __init__(
self,
weight_dtype: str = "int",
weight_bits: int = 4,
white_list: Optional[List[OP_NAME_OR_MODULE_TYPE]] = DEFAULT_WHITE_LIST,
):
"""Init fake config.

Args:
weight_dtype (str): Data type for weights, default is "int".
weight_bits (int): Number of bits used to represent weights, default is 4.
"""
super().__init__(white_list=white_list)
self.weight_bits = weight_bits
self.weight_dtype = weight_dtype
self._post_init()

def to_dict(self):
return super().to_dict()

@classmethod
def from_dict(cls, config_dict):
return super(FakeAlgoConfig, cls).from_dict(config_dict=config_dict)

@classmethod
def register_supported_configs(cls) -> List:
pass

@staticmethod
def get_model_info(model: Any) -> List[Tuple[str, Callable]]:
pass

@classmethod
def get_config_set_for_tuning(cls) -> Union[None, "FakeAlgoConfig", List["FakeAlgoConfig"]]:
return FakeAlgoConfig(weight_bits=DEFAULT_WEIGHT_BITS)


FakeAlgoConfig.register_supported_configs()
yiliu30 marked this conversation as resolved.
Show resolved Hide resolved


def get_default_fake_config() -> FakeAlgoConfig:
"""Generate the default fake config.

Returns:
the default fake config.
"""
return FakeAlgoConfig()


def get_all_config_set() -> Union[BaseConfig, List[BaseConfig]]:
return get_all_config_set_from_config_registry(fwk_name=FAKE_FRAMEWORK_NAME)


class TestBaseConfig(unittest.TestCase):
@classmethod
def setUpClass(self):
pass

@classmethod
def tearDownClass(self):
pass

def setUp(self):
# print the test name
logger.info(f"Running TestBaseConfig test: {self.id()}")

def test_api(self):
fake_default_config = get_default_fake_config()
self.assertEqual(fake_default_config.weight_dtype, "int")
config_set = get_all_config_set()
self.assertEqual(len(config_set), 1)
self.assertEqual(config_set[0].weight_bits, DEFAULT_WEIGHT_BITS)


if __name__ == "__main__":
unittest.main()