Skip to content

Commit e81a1a3

Browse files
pyproject.toml (#2981)
### Changes Use pyproject.toml instead of setup.py ### Reason for changes - [PEP 517/518](https://peps.python.org/pep-0517/#source-trees) - Tools like Black, Isort, Ruff, and Mypy can automatically use settings defined in a pyproject.toml file, allowing these tools to apply configurations directly from the repository without requiring additional configuration in text editor or extra arguments in terminal. ### Tests nightly/job/install_onnx/543/ nightly/job/install_ov/567/ nightly/job/install_pt_cpu/566/ nightly/job/ubuntu20_install_tf/672/
1 parent 8ef38ec commit e81a1a3

File tree

17 files changed

+280
-260
lines changed

17 files changed

+280
-260
lines changed

.github/workflows/mypy.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ jobs:
2323
- name: Install mypy
2424
run: pip install mypy==1.8.0
2525
- name: Run mypy
26-
run: mypy --install-types --config-file=.mypy.ini --non-interactive
26+
run: mypy --install-types --non-interactive

.github/workflows/nightly.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ jobs:
1212
steps:
1313
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
1414
- uses: AlexanderDokuchaev/md-dead-link-check@cc3ed55268899a1a6d5fd7068abbc4591eab1f74 # v0.9
15-
with:
16-
config: md_dead_link_check.toml
15+

.github/workflows/pre-commit-linters.yml

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,4 @@ jobs:
2525
steps:
2626
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
2727
- uses: AlexanderDokuchaev/md-dead-link-check@cc3ed55268899a1a6d5fd7068abbc4591eab1f74 # v0.9
28-
with:
29-
config: md_dead_link_check.toml
28+

.github/workflows/python-publish.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ jobs:
3939
env:
4040
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
4141
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
42+
NNCF_RELEASE_BUILD: "1"
4243
run: |
43-
python -m build -C--global-option=--release
44+
python -m build
4445
4546
# Disallow uploading dev packages
4647
for file in dist/*; do

.isort.cfg

-6
This file was deleted.

.mypy.ini

-9
This file was deleted.

.pre-commit-config.yaml

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ repos:
77
hooks:
88
- id: black
99
files: '^.*\.py'
10-
args: ["--line-length", "120"]
1110

1211
- repo: https://github.com/pycqa/isort
1312
rev: 5.12.0

MANIFEST.in

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ graft nncf/common/hardware/configs
33
include LICENSE
44
include licensing/third-party-programs.txt
55
include docs/PyPiPublishing.md
6+
include custom_version.py

custom_version.py

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Copyright (c) 2024 Intel Corporation
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
12+
"""
13+
Dynamic Version Generation for Package Builds
14+
15+
This module is responsible for generating the dynamic version for the package during the build process.
16+
It provides the `version` attribute for setuptools as specified in the `pyproject.toml` configuration file:
17+
18+
[tool.setuptools.dynamic]
19+
version = { attr = "_version_helper.version" }
20+
21+
Environment Variable Instructions:
22+
-----------------------------------
23+
1. **NNCF_RELEASE_BUILD**:
24+
- Set this environment variable to generate a release package using `python -m build` without including
25+
the commit hash in the version.
26+
- If this variable is not set, the file `nncf/version.py` will be overridden with a custom version
27+
that includes the commit hash. Example usage:
28+
29+
NNCF_RELEASE_BUILD=1 python -m build
30+
31+
2. **NNCF_BUILD_SUFFIX**:
32+
- Use this environment variable to specify a particular suffix for the build. Example usage:
33+
34+
NNCF_BUILD_SUFFIX="rc1" python -m build
35+
36+
Post-Build Recommendation:
37+
---------------------------
38+
After generating the package, it is recommended to revert any changes to `nncf/version.py` to avoid potential conflicts.
39+
This can be done using the following command:
40+
41+
git checkout nncf/version.py
42+
43+
This ensures that `nncf/version.py` remains in its original state after the dynamic versioning process.
44+
"""
45+
46+
47+
from __future__ import annotations
48+
49+
import contextlib
50+
import os
51+
import re
52+
import subprocess
53+
from pathlib import Path
54+
55+
NNCF_VERSION_FILE = "nncf/version.py"
56+
57+
58+
def get_custom_version() -> str:
59+
version_match = re.search(
60+
r"^__version__ = ['\"]((\d+\.\d+\.\d+)([^'\"]*))['\"]", Path(NNCF_VERSION_FILE).read_text(), re.M
61+
)
62+
if not version_match:
63+
raise RuntimeError("Unable to find version string.")
64+
65+
version_full = version_match.group(1)
66+
version_value = version_match.group(2)
67+
version_suffix = version_match.group(3)
68+
69+
nncf_build_suffix = os.environ.get("NNCF_BUILD_SUFFIX")
70+
if nncf_build_suffix:
71+
# Suffix expected on build package
72+
return f"{version_value}.{nncf_build_suffix}"
73+
74+
is_building_release = "NNCF_RELEASE_BUILD" in os.environ
75+
if is_building_release or version_suffix:
76+
return version_full
77+
78+
dev_version_id = "unknown_version"
79+
repo_root = os.path.dirname(os.path.realpath(__file__))
80+
81+
# Get commit hash
82+
with contextlib.suppress(subprocess.CalledProcessError):
83+
dev_version_id = (
84+
subprocess.check_output(["git", "rev-parse", "--short", "HEAD"], cwd=repo_root).strip().decode() # nosec
85+
)
86+
87+
# Detect modified files
88+
with contextlib.suppress(subprocess.CalledProcessError):
89+
run = subprocess.run(["git", "diff-index", "--quiet", "HEAD"], cwd=repo_root) # nosec
90+
if run.returncode == 1:
91+
dev_version_id += "dirty"
92+
93+
return version_value + f".dev0+{dev_version_id}"
94+
95+
96+
version: str
97+
98+
99+
def __getattr__(name: str) -> str:
100+
101+
if name == "version":
102+
global version
103+
version = get_custom_version()
104+
105+
# Rewrite version.py to pass custom version to package
106+
if os.environ.get("_PYPROJECT_HOOKS_BUILD_BACKEND"):
107+
content = Path(NNCF_VERSION_FILE).read_text()
108+
version_str = re.search(r"^__version__ = ['\"][^'\"]*['\"]", content, re.M).group(0)
109+
content = content.replace(version_str, f'__version__ = "{version}"')
110+
Path(NNCF_VERSION_FILE).write_text(content)
111+
112+
return version
113+
114+
raise AttributeError(name)

examples/torch/common/models/classification/mobilenet_v3_tv_092.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def _mobilenet_v3_model(
301301
last_channel: int,
302302
pretrained: bool,
303303
progress: bool,
304-
**kwargs: Any
304+
**kwargs: Any,
305305
):
306306
model = MobileNetV3(inverted_residual_setting, last_channel, **kwargs)
307307
if pretrained:

md_dead_link_check.toml

-2
This file was deleted.

nncf/experimental/torch/nas/bootstrapNAS/training/lr_scheduler.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def __init__(
147147
base_lr: float,
148148
num_epochs: float,
149149
warmup_epochs: float = 0,
150-
warmup_lr: float = 3.4e-4
150+
warmup_lr: float = 3.4e-4,
151151
):
152152
super().__init__(optimizer, num_steps_in_epoch)
153153
self._base_lr = base_lr

pyproject.toml

+151
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
[build-system]
2+
requires = ["setuptools>=61.0"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "nncf"
7+
description = "Neural Networks Compression Framework"
8+
readme="docs/PyPiPublishing.md"
9+
license = { text = "Apache-2.0" }
10+
authors = [{ name = "Intel" }, { email = "alexander.kozlov@intel.com" }]
11+
requires-python = ">=3.8"
12+
dynamic = ["version"]
13+
keywords = [
14+
"bert",
15+
"classification",
16+
"compression",
17+
"hawq",
18+
"mixed-precision-training",
19+
"mmdetection",
20+
"nas",
21+
"nlp",
22+
"object-detection",
23+
"pruning",
24+
"quantization",
25+
"quantization-aware-training",
26+
"semantic-segmentation",
27+
"sparsity",
28+
"transformers",
29+
]
30+
classifiers = [
31+
"License :: OSI Approved :: Apache Software License",
32+
"Operating System :: OS Independent",
33+
"Programming Language :: Python :: 3",
34+
]
35+
dependencies = [
36+
"jsonschema>=3.2.0",
37+
"jstyleson>=0.0.2",
38+
"natsort>=7.1.0",
39+
"networkx>=2.6, <=3.3",
40+
"ninja>=1.10.0.post2, <1.12",
41+
"numpy>=1.19.1, <1.27",
42+
"openvino-telemetry>=2023.2.0",
43+
"packaging>=20.0",
44+
"pandas>=1.1.5,<2.3",
45+
"psutil",
46+
"pydot>=1.4.1, <3.0.0",
47+
"pymoo>=0.6.0.1",
48+
"rich>=13.5.2",
49+
"scikit-learn>=0.24.0",
50+
"scipy>=1.3.2",
51+
"tabulate>=0.9.0",
52+
"tqdm>=4.54.1",
53+
]
54+
55+
[project.optional-dependencies]
56+
plots = [
57+
"kaleido>=0.2.1",
58+
"matplotlib>=3.3.4, <3.6",
59+
"pillow>=9.0.0",
60+
"plotly-express>=0.4.1",
61+
]
62+
63+
[project.urls]
64+
Homepage = "https://github.com/openvinotoolkit/nncf"
65+
66+
[tool.setuptools.dynamic]
67+
version = { attr = "custom_version.version" }
68+
69+
[tool.setuptools.packages.find]
70+
where = ["."]
71+
exclude = ["tests", "tests.*", "examples", "examples.*", "tools", "tools.*"]
72+
namespaces = false
73+
74+
[tool.black]
75+
line-length = 120
76+
77+
[tool.md_dead_link_check]
78+
exclude_files = ["ReleaseNotes.md"]
79+
80+
[tool.isort]
81+
line_length = 120
82+
force_single_line = true
83+
profile = "black"
84+
single_line_exclusions = "typing"
85+
skip_glob = "examples/post_training_quantization/torch/ssd300_vgg16/main.py"
86+
87+
[tool.mypy]
88+
follow_imports = "silent"
89+
strict = true
90+
# should be removed later
91+
# mypy recommends the following tool as an autofix:
92+
# https://github.com/hauntsaninja/no_implicit_optional
93+
implicit_optional = true
94+
files = [
95+
"nncf/common/sparsity",
96+
"nncf/common/graph",
97+
"nncf/common/accuracy_aware_training/",
98+
"nncf/common/utils/",
99+
"nncf/common/tensor_statistics",
100+
"nncf/experimental/torch2",
101+
]
102+
103+
[tool.ruff]
104+
line-length = 120
105+
exclude = ["nncf/tensorflow/__init__.py"]
106+
107+
[tool.ruff.lint]
108+
preview = true
109+
ignore-init-module-imports = true
110+
ignore = [
111+
"E201", # whitespace-after-open-bracket
112+
"E203", # whitespace-before-punctuation
113+
"E231", # missing-whitespace
114+
"E251", # unexpected-spaces-around-keyword-parameter-equals
115+
"E731", # lambda-assignment
116+
"SIM108", # if-else-block-instead-of-if-exp
117+
"SIM110", # reimplemented-builtin
118+
"SIM117", # multiple-with-statements
119+
"SIM103", # needless-bool
120+
"NPY002", # numpy-legacy-random
121+
]
122+
select = [
123+
"E", # pycodestyle rules
124+
"F", # pyflakes rules
125+
"CPY001", # copyright check
126+
"NPY", # numpy rules
127+
]
128+
extend-select = [
129+
"SIM", # https://pypi.org/project/flake8-simplify
130+
]
131+
132+
[tool.ruff.lint.per-file-ignores]
133+
"nncf/experimental/torch/nas/bootstrapNAS/__init__.py" = ["F401"]
134+
"nncf/torch/__init__.py" = ["F401", "E402"]
135+
"tests/**/*.py" = ["F403"]
136+
"tests/**/__init__.py" = ["F401"]
137+
"examples/**/*.py" = ["F403"]
138+
139+
[tool.ruff.lint.flake8-copyright]
140+
notice-rgx = """\
141+
# Copyright \\(c\\) (\\d{4}|\\d{4}-\\d{4}) Intel Corporation
142+
# Licensed under the Apache License, Version 2.0 \\(the "License"\\);
143+
# you may not use this file except in compliance with the License.
144+
# You may obtain a copy of the License at
145+
# http://www.apache.org/licenses/LICENSE-2.0
146+
# Unless required by applicable law or agreed to in writing, software
147+
# distributed under the License is distributed on an "AS IS" BASIS,
148+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
149+
# See the License for the specific language governing permissions and
150+
# limitations under the License.
151+
"""

ruff.toml

-49
This file was deleted.

0 commit comments

Comments
 (0)