-
Notifications
You must be signed in to change notification settings - Fork 258
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add coding style docs and recommended VS Code setting (#1584)
Signed-off-by: yiliu30 <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
8d21209
commit c1f23ce
Showing
4 changed files
with
281 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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,25 @@ | ||
{ | ||
|
||
// ========== Anylysis ========== | ||
// It is a placeholder to let you know that we can include the extra paths for the better analysis(like go to definition of an extra package). | ||
"python.analysis.extraPaths":[ | ||
], | ||
|
||
// ========== Type Hints ========== | ||
// Use Pylance for insert tpype hints automatically | ||
// Rrequired Plugins: | ||
// - Python, https://marketplace.visualstudio.com/items?itemName=ms-python.python | ||
// - Pylance, https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance | ||
"python.languageServer": "Default", | ||
"python.analysis.inlayHints.callArgumentNames": "off", | ||
"python.analysis.inlayHints.functionReturnTypes": true, | ||
"python.analysis.inlayHints.pytestParameters": true, | ||
"python.analysis.inlayHints.variableTypes": true, | ||
|
||
// ========== Better comments ========== | ||
// Rrequired Plugins: | ||
// - Better Comments, https://marketplace.visualstudio.com/items?itemName=aaron-bond.better-comments | ||
// It is a placeholder to let you know that we can use Better Comments plugin. We use the default settings. | ||
"better-comments.highlightPlainText": false, | ||
|
||
} |
This file contains 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 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,243 @@ | ||
# INC Coding Conventions | ||
|
||
(Mostly for Version 3 and later) | ||
|
||
## Background | ||
|
||
To improve the quality and maintainability of INC code, we summarized some common coding standards and conventions. | ||
|
||
There are many style guides, and they may conflict with each other. To avoid overly arguing formatting, we make decisions based on the following priorities: | ||
|
||
- [Google Python Style](https://google.github.io/styleguide/pyguide.html#s3.8-comments-and-docstrings), [PEP 8](https://peps.python.org/pep-0008/) | ||
- Framework Style | ||
- INC Internal Style | ||
- Sub-module specific Style | ||
|
||
## Rules | ||
|
||
> Note: The sub-tile naming is following [Google Python Style](https://google.github.io/styleguide/pyguide.html#s3.8-comments-and-docstrings) and [PEP 8](https://peps.python.org/pep-0008/). See the relevant section for more details. | ||
|
||
### Imports | ||
|
||
- Recommend | ||
|
||
```python | ||
import os | ||
import sys | ||
|
||
from x import py | ||
from x import y as z | ||
from copy import deepcopy | ||
from subprocess import Popen, PIPE | ||
``` | ||
|
||
- Not recommend | ||
|
||
```python | ||
from sub_module import * # May lead to namespace pollution | ||
import os, sys # Import on separate lines | ||
import copy # May import local copy.py | ||
``` | ||
|
||
### Strings | ||
|
||
- Recommend | ||
|
||
```python | ||
long_string = """This is fine if your use case can accept | ||
extraneous leading spaces.""" | ||
|
||
long_string = "And this is fine if you cannot accept\n" "extraneous leading spaces." | ||
``` | ||
|
||
- Not recommend | ||
|
||
```python | ||
logger.info("This is fine if your use case can accept") | ||
logger.info("extraneous leading spaces.") | ||
``` | ||
|
||
### Logger | ||
|
||
- Recommend | ||
|
||
```python | ||
from neural_compressor.common import logger | ||
|
||
logger.info("Current TensorFlow Version is: %s", tf.__version__) # Use a pattern-string (with %-placeholders) | ||
|
||
logger.info("Current $PAGER is: %s", os.getenv("PAGER", default="")) # Better readability | ||
|
||
# Handle long string | ||
logger.warning( | ||
"All tuning options for the current strategy have been tried. \n" | ||
"If the quantized model does not seem to work well, it might be worth considering other strategies." | ||
) | ||
|
||
logger.warning( | ||
"This is a long string, this is a long string," | ||
"override the user config's smooth quant alpha into the best alpha(%.4f) found in pre-strategy.", | ||
0.65421, | ||
) | ||
``` | ||
|
||
- Not recommend | ||
|
||
```python | ||
logger.info(f"Current TensorFlow Version is: {tf.__version__}") # Use f-string | ||
|
||
logger.info("Current $PAGER is:") # One sentence in two lines | ||
logger.info(os.getenv("PAGER", default="")) | ||
``` | ||
|
||
|
||
### Type Annotations | ||
|
||
- Recommend | ||
|
||
```python | ||
def register_config(framework_name: str, algo_name: str, priority: int = 0) -> Callable[..., Any]: ... | ||
|
||
|
||
eval_result: float = evaluator.eval(model) | ||
|
||
# Declare aliases of complex types | ||
from typing import TypeAlias | ||
|
||
_LossAndGradient: TypeAlias = tuple[tf.Tensor, tf.Tensor] | ||
ComplexTFMap: TypeAlias = Mapping[str, _LossAndGradient] | ||
``` | ||
|
||
- Not recommend | ||
|
||
```python | ||
def xx_func(cls) -> Dict[str, OrderedDict[str, Dict[str, object]]]: # Can't improve the readability | ||
``` | ||
|
||
- Plugs: | ||
- [python](https://marketplace.visualstudio.com/items?itemName=ms-python.python) | ||
- [pylance](https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance) | ||
|
||
|
||
### Comments | ||
|
||
- Recommend | ||
|
||
```python | ||
class CheeseShopAddress: | ||
"""The address of a cheese shop. | ||
... | ||
""" | ||
|
||
|
||
class OutOfCheeseError(Exception): | ||
"""No more cheese is available.""" | ||
``` | ||
|
||
- Not recommend | ||
|
||
```python | ||
class CheeseShopAddress: | ||
"""Class that describes the address of a cheese shop. | ||
... | ||
""" | ||
|
||
|
||
class OutOfCheeseError(Exception): | ||
"""Raised when no more cheese is available.""" | ||
``` | ||
|
||
|
||
### TODO Comments | ||
|
||
- Recommend | ||
|
||
```python | ||
# TODO: crbug.com/192795 - Investigate cpufreq optimizations. | ||
|
||
# * Important information | ||
# ? Need decision | ||
# ! Deprecated method, do not use | ||
``` | ||
|
||
> A `TODO` comment begins with the word `TODO:` for facilitate searching. | ||
- Plug: | ||
[Better Comments](https://marketplace.visualstudio.com/items?itemName=aaron-bond.better-comments) | ||
|
||
|
||
### Public and Internal Interfaces | ||
|
||
Use `__all__` to help the developer and user know the supported interface and components. | ||
|
||
```python | ||
__all__ = [ | ||
"options", | ||
"register_config", | ||
"get_all_config_set_from_config_registry", | ||
"BaseConfig", | ||
"ComposableConfig", | ||
] | ||
``` | ||
|
||
|
||
## Folder structure | ||
|
||
```shell | ||
├── fwk_name | ||
│ ├── __init__.py | ||
│ ├── quantization | ||
│ │ ├── algorithm_entry.py | ||
│ │ ├── autotune.py | ||
│ │ ├── config.py | ||
│ │ ├── __init__.py | ||
│ │ └── quantize.py | ||
│ ├── algorithms | ||
│ │ ├── __init__.py | ||
│ │ ├── smooth_quant | ||
│ │ │ ├── __init__.py | ||
│ │ │ ├── smooth_quant.py | ||
│ │ │ └── utility.py | ||
│ │ ├── static_quant | ||
│ │ │ ├── __init__.py | ||
│ │ │ ├── static_quant.py | ||
│ │ │ └── utility.py | ||
│ │ └── weight_only | ||
│ │ ├── gptq.py | ||
│ │ ├── __init__.py | ||
│ │ └── rtn.py | ||
│ └── utils | ||
│ ├── constants.py | ||
│ ├── __init__.py | ||
│ └── utility.py | ||
└── __init__.py | ||
``` | ||
|
||
```python | ||
# * Note: some code snippets about register algorithm entry | ||
# filepath: neural_compressor/fwk_name/quantization/algorithm_entry.py | ||
@register_algo(RTN) | ||
def rtn_algo_entry() | ||
from neural_compressor.fwk_name.algorithms import rtn | ||
... | ||
|
||
@register_algo(SMOOTH_QUANT) | ||
def smooth_quant_entry(): | ||
from neural_compressor.fwk_name.algorithms import smooth_quant | ||
... | ||
|
||
``` | ||
|
||
|
||
## Recommend VS Code `settings.json` | ||
To keep the coding style consistent, we suggest you replace `.vscode/settings.json` with `neural-compressor/.vscode/settings_recommended.json`. | ||
|
||
|
||
## Reference | ||
|
||
- [Google Python Style](https://google.github.io/styleguide/pyguide.html#s3.8-comments-and-docstrings) | ||
- [PEP 8](https://peps.python.org/pep-0008/) | ||
- [PyTorch](https://github.com/pytorch/pytorch) |