Skip to content

Commit

Permalink
Implement pyproject.toml to build AIMET package
Browse files Browse the repository at this point in the history
There are 3 dynamic fields in metadata:
 - version
 - dependencies
 - name (not PEP compatible!)
A plugin of scikit-build-core build system generates dependencies
and package name based on `CMAKE_ARGS` environment variable.

Signed-off-by: Evgeny Mironov <[email protected]>
  • Loading branch information
quic-emironov authored and quic-hitameht committed Sep 19, 2024
1 parent 75f0682 commit 431f265
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
74 changes: 74 additions & 0 deletions packaging/plugins/local/aimet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# =============================================================================
# @@-COPYRIGHT-START-@@
#
# Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause
#
# @@-COPYRIGHT-END-@@
# =============================================================================

from __future__ import annotations

import itertools
import os
import pathlib
import shlex

__all__ = ["dynamic_metadata"]


def __dir__() -> list[str]:
return __all__


def is_cmake_option_enabled(option_name: str) -> bool:
"""Returns True if CMAKE_ARGS environment variable contains `-D{option_name}=ON/YES/1/TRUE` and False otherwise."""
cmake_args = {k:v for k,v in (arg.split("=", 1) for arg in shlex.split(os.environ.get("CMAKE_ARGS", "")))}
return not cmake_args.get(f"-D{option_name}", "").upper() in {"OFF", "NO", "FALSE", "0", "N" }


def get_aimet_variant() -> str:
"""Return a variant based on CMAKE_ARGS environment variable"""
enable_cuda = is_cmake_option_enabled("ENABLE_CUDA")
enable_torch = is_cmake_option_enabled("ENABLE_TORCH")
enable_tensorflow = is_cmake_option_enabled("ENABLE_TENSORFLOW")
enable_onnx = is_cmake_option_enabled("ENABLE_ONNX")

variant = ""
if enable_tensorflow:
variant += "tf-"
if enable_torch:
variant += "torch-"
if enable_onnx:
variant = "onnx-"
variant += "gpu" if enable_cuda else "cpu"
return variant


def get_aimet_dependencies() -> list[str]:
"""Read dependencies form the corresponded files and return them as a list (!) of strings"""
deps_path = pathlib.Path("packaging", "dependencies", get_aimet_variant())
deps_files = [deps_path.parent / "reqs_pip_common.txt", *deps_path.glob("reqs_pip_*.txt")]
print(f"CMAKE_ARGS='{os.environ.get('CMAKE_ARGS', '')}'")
print(f"Read dependencies for variant '{get_aimet_variant()}' from the following files: {deps_files}")
deps = {d for d in itertools.chain.from_iterable(line.replace(" -f ", "\n-f ").split("\n") for f in deps_files for line in f.read_text(encoding="utf8").splitlines()) if not d.startswith(("#", "-f"))}
return list(deps)


def get_version() -> str:
return pathlib.Path("packaging", "version.txt").read_text(encoding="utf8")


def dynamic_metadata(
field: str,
settings: dict[str, object] | None = None,
) -> str:
if settings:
raise ValueError("No inline configuration is supported")
if field == "name":
return f"aimet-{get_aimet_variant()}"
if field == "dependencies":
return get_aimet_dependencies()
if field == "version":
return get_version()
raise ValueError(f"Unsupported field '{field}'")
40 changes: 40 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[build-system]
requires = [
"scikit-build-core[wheels]>=0.9",
]
build-backend = "scikit_build_core.build"

[project]
#name = "aimet"
requires-python = ">=3.8"
dynamic = ["name", "dependencies", "version"]

[project.optional-dependencies]
dev = [
# duplicate build-system.requires for editable mode (non-isolated)
"scikit-build-core[wheels]>=0.9",
# and the rest
]
test = [
"pytest",
]
docs = [
]

[tool.scikit-build]
experimental = true
metadata.name = { provider = "aimet", provider-path = "packaging/plugins/local" }
metadata.dependencies = { provider = "aimet", provider-path = "packaging/plugins/local" }
metadata.version = { provider="aimet", provider-path = "packaging/plugins/local" }
build-dir = "build"
sdist.cmake = false
logging.level = "DEBUG"
strict-config = false
wheel.license-files=[]
wheel.packages=["TrainingExtensions/common/src/python/aimet_common"]

[tool.scikit-build.cmake.define]
CMAKE_BUILD_TYPE="RelWithDebInfo"
CMAKE_CUDA_ARCHITECTURES="70;75;80"
CMAKE_CUDA_FLAGS="--threads=8"

0 comments on commit 431f265

Please sign in to comment.