-
Notifications
You must be signed in to change notification settings - Fork 278
Add framework-agnostic tests for common components #1575
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
"""Tests for common components. | ||
|
||
!!! 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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.