Skip to content

Commit

Permalink
pyproject.toml (openvinotoolkit#2981)
Browse files Browse the repository at this point in the history
### 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/
  • Loading branch information
AlexanderDokuchaev authored Oct 15, 2024
1 parent 8ef38ec commit e81a1a3
Show file tree
Hide file tree
Showing 17 changed files with 280 additions and 260 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ jobs:
- name: Install mypy
run: pip install mypy==1.8.0
- name: Run mypy
run: mypy --install-types --config-file=.mypy.ini --non-interactive
run: mypy --install-types --non-interactive
3 changes: 1 addition & 2 deletions .github/workflows/nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ jobs:
steps:
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
- uses: AlexanderDokuchaev/md-dead-link-check@cc3ed55268899a1a6d5fd7068abbc4591eab1f74 # v0.9
with:
config: md_dead_link_check.toml

3 changes: 1 addition & 2 deletions .github/workflows/pre-commit-linters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,4 @@ jobs:
steps:
- uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
- uses: AlexanderDokuchaev/md-dead-link-check@cc3ed55268899a1a6d5fd7068abbc4591eab1f74 # v0.9
with:
config: md_dead_link_check.toml

3 changes: 2 additions & 1 deletion .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ jobs:
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
NNCF_RELEASE_BUILD: "1"
run: |
python -m build -C--global-option=--release
python -m build
# Disallow uploading dev packages
for file in dist/*; do
Expand Down
6 changes: 0 additions & 6 deletions .isort.cfg

This file was deleted.

9 changes: 0 additions & 9 deletions .mypy.ini

This file was deleted.

1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ repos:
hooks:
- id: black
files: '^.*\.py'
args: ["--line-length", "120"]

- repo: https://github.com/pycqa/isort
rev: 5.12.0
Expand Down
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ graft nncf/common/hardware/configs
include LICENSE
include licensing/third-party-programs.txt
include docs/PyPiPublishing.md
include custom_version.py
114 changes: 114 additions & 0 deletions custom_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# Copyright (c) 2024 Intel Corporation
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
Dynamic Version Generation for Package Builds
This module is responsible for generating the dynamic version for the package during the build process.
It provides the `version` attribute for setuptools as specified in the `pyproject.toml` configuration file:
[tool.setuptools.dynamic]
version = { attr = "_version_helper.version" }
Environment Variable Instructions:
-----------------------------------
1. **NNCF_RELEASE_BUILD**:
- Set this environment variable to generate a release package using `python -m build` without including
the commit hash in the version.
- If this variable is not set, the file `nncf/version.py` will be overridden with a custom version
that includes the commit hash. Example usage:
NNCF_RELEASE_BUILD=1 python -m build
2. **NNCF_BUILD_SUFFIX**:
- Use this environment variable to specify a particular suffix for the build. Example usage:
NNCF_BUILD_SUFFIX="rc1" python -m build
Post-Build Recommendation:
---------------------------
After generating the package, it is recommended to revert any changes to `nncf/version.py` to avoid potential conflicts.
This can be done using the following command:
git checkout nncf/version.py
This ensures that `nncf/version.py` remains in its original state after the dynamic versioning process.
"""


from __future__ import annotations

import contextlib
import os
import re
import subprocess
from pathlib import Path

NNCF_VERSION_FILE = "nncf/version.py"


def get_custom_version() -> str:
version_match = re.search(
r"^__version__ = ['\"]((\d+\.\d+\.\d+)([^'\"]*))['\"]", Path(NNCF_VERSION_FILE).read_text(), re.M
)
if not version_match:
raise RuntimeError("Unable to find version string.")

version_full = version_match.group(1)
version_value = version_match.group(2)
version_suffix = version_match.group(3)

nncf_build_suffix = os.environ.get("NNCF_BUILD_SUFFIX")
if nncf_build_suffix:
# Suffix expected on build package
return f"{version_value}.{nncf_build_suffix}"

is_building_release = "NNCF_RELEASE_BUILD" in os.environ
if is_building_release or version_suffix:
return version_full

dev_version_id = "unknown_version"
repo_root = os.path.dirname(os.path.realpath(__file__))

# Get commit hash
with contextlib.suppress(subprocess.CalledProcessError):
dev_version_id = (
subprocess.check_output(["git", "rev-parse", "--short", "HEAD"], cwd=repo_root).strip().decode() # nosec
)

# Detect modified files
with contextlib.suppress(subprocess.CalledProcessError):
run = subprocess.run(["git", "diff-index", "--quiet", "HEAD"], cwd=repo_root) # nosec
if run.returncode == 1:
dev_version_id += "dirty"

return version_value + f".dev0+{dev_version_id}"


version: str


def __getattr__(name: str) -> str:

if name == "version":
global version
version = get_custom_version()

# Rewrite version.py to pass custom version to package
if os.environ.get("_PYPROJECT_HOOKS_BUILD_BACKEND"):
content = Path(NNCF_VERSION_FILE).read_text()
version_str = re.search(r"^__version__ = ['\"][^'\"]*['\"]", content, re.M).group(0)
content = content.replace(version_str, f'__version__ = "{version}"')
Path(NNCF_VERSION_FILE).write_text(content)

return version

raise AttributeError(name)
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def _mobilenet_v3_model(
last_channel: int,
pretrained: bool,
progress: bool,
**kwargs: Any
**kwargs: Any,
):
model = MobileNetV3(inverted_residual_setting, last_channel, **kwargs)
if pretrained:
Expand Down
2 changes: 0 additions & 2 deletions md_dead_link_check.toml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ def __init__(
base_lr: float,
num_epochs: float,
warmup_epochs: float = 0,
warmup_lr: float = 3.4e-4
warmup_lr: float = 3.4e-4,
):
super().__init__(optimizer, num_steps_in_epoch)
self._base_lr = base_lr
Expand Down
151 changes: 151 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "nncf"
description = "Neural Networks Compression Framework"
readme="docs/PyPiPublishing.md"
license = { text = "Apache-2.0" }
authors = [{ name = "Intel" }, { email = "alexander.kozlov@intel.com" }]
requires-python = ">=3.8"
dynamic = ["version"]
keywords = [
"bert",
"classification",
"compression",
"hawq",
"mixed-precision-training",
"mmdetection",
"nas",
"nlp",
"object-detection",
"pruning",
"quantization",
"quantization-aware-training",
"semantic-segmentation",
"sparsity",
"transformers",
]
classifiers = [
"License :: OSI Approved :: Apache Software License",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
]
dependencies = [
"jsonschema>=3.2.0",
"jstyleson>=0.0.2",
"natsort>=7.1.0",
"networkx>=2.6, <=3.3",
"ninja>=1.10.0.post2, <1.12",
"numpy>=1.19.1, <1.27",
"openvino-telemetry>=2023.2.0",
"packaging>=20.0",
"pandas>=1.1.5,<2.3",
"psutil",
"pydot>=1.4.1, <3.0.0",
"pymoo>=0.6.0.1",
"rich>=13.5.2",
"scikit-learn>=0.24.0",
"scipy>=1.3.2",
"tabulate>=0.9.0",
"tqdm>=4.54.1",
]

[project.optional-dependencies]
plots = [
"kaleido>=0.2.1",
"matplotlib>=3.3.4, <3.6",
"pillow>=9.0.0",
"plotly-express>=0.4.1",
]

[project.urls]
Homepage = "https://github.com/openvinotoolkit/nncf"

[tool.setuptools.dynamic]
version = { attr = "custom_version.version" }

[tool.setuptools.packages.find]
where = ["."]
exclude = ["tests", "tests.*", "examples", "examples.*", "tools", "tools.*"]
namespaces = false

[tool.black]
line-length = 120

[tool.md_dead_link_check]
exclude_files = ["ReleaseNotes.md"]

[tool.isort]
line_length = 120
force_single_line = true
profile = "black"
single_line_exclusions = "typing"
skip_glob = "examples/post_training_quantization/torch/ssd300_vgg16/main.py"

[tool.mypy]
follow_imports = "silent"
strict = true
# should be removed later
# mypy recommends the following tool as an autofix:
# https://github.com/hauntsaninja/no_implicit_optional
implicit_optional = true
files = [
"nncf/common/sparsity",
"nncf/common/graph",
"nncf/common/accuracy_aware_training/",
"nncf/common/utils/",
"nncf/common/tensor_statistics",
"nncf/experimental/torch2",
]

[tool.ruff]
line-length = 120
exclude = ["nncf/tensorflow/__init__.py"]

[tool.ruff.lint]
preview = true
ignore-init-module-imports = true
ignore = [
"E201", # whitespace-after-open-bracket
"E203", # whitespace-before-punctuation
"E231", # missing-whitespace
"E251", # unexpected-spaces-around-keyword-parameter-equals
"E731", # lambda-assignment
"SIM108", # if-else-block-instead-of-if-exp
"SIM110", # reimplemented-builtin
"SIM117", # multiple-with-statements
"SIM103", # needless-bool
"NPY002", # numpy-legacy-random
]
select = [
"E", # pycodestyle rules
"F", # pyflakes rules
"CPY001", # copyright check
"NPY", # numpy rules
]
extend-select = [
"SIM", # https://pypi.org/project/flake8-simplify
]

[tool.ruff.lint.per-file-ignores]
"nncf/experimental/torch/nas/bootstrapNAS/__init__.py" = ["F401"]
"nncf/torch/__init__.py" = ["F401", "E402"]
"tests/**/*.py" = ["F403"]
"tests/**/__init__.py" = ["F401"]
"examples/**/*.py" = ["F403"]

[tool.ruff.lint.flake8-copyright]
notice-rgx = """\
# Copyright \\(c\\) (\\d{4}|\\d{4}-\\d{4}) Intel Corporation
# Licensed under the Apache License, Version 2.0 \\(the "License"\\);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
49 changes: 0 additions & 49 deletions ruff.toml

This file was deleted.

Loading

0 comments on commit e81a1a3

Please sign in to comment.