Skip to content

Commit 46b0b34

Browse files
committed
[update] Formatter and Project Structure
1 parent f12d53c commit 46b0b34

File tree

10 files changed

+132
-35
lines changed

10 files changed

+132
-35
lines changed

.github/workflows/ci-auto-format-and-commit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ jobs:
3232
wget https://apt.llvm.org/llvm.sh && sudo bash ./llvm.sh 20 && rm ./llvm.sh
3333
sudo apt-get install clang-format-20
3434
sudo ln -sf $(which clang-format-20) /usr/bin/clang-format
35-
python -m pip install black
35+
python -m pip install ruff
3636
3737
- name: Run format script
3838
shell: bash

pyproject.toml

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,12 @@
11
[build-system]
2-
requires = ["setuptools>=42", "wheel", "torch>=2.0.0", "torchvision"]
2+
requires = ["setuptools>=42", "wheel", "torch>=2.0.0"]
33
build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "example_package"
77
version = "0.1.0"
8-
description = "A Template of Python Project in Visual Studio Code with Github Actions CI/CD (Especially for PyTorch based Deeplearning Projects)."
98
authors = [{ name = "jamesnulliu", email = "jamesnulliu@gmail.com" }]
109
readme = "README.md"
1110
requires-python = ">=3.10, <3.13"
1211
license = { file = "LICENSE" }
13-
classifiers = [
14-
"Development Status :: 2 - Pre-Alpha",
15-
"Environment :: Console",
16-
"Intended Audience :: Science/Research",
17-
"License :: OSI Approved :: MIT License",
18-
"Operating System :: Unix",
19-
"Programming Language :: Python :: 3",
20-
"Programming Language :: Python :: 3.10",
21-
"Programming Language :: Python :: 3.11",
22-
"Programming Language :: Python :: 3.12",
23-
"Topic :: Software Development :: Libraries :: Python Modules",
24-
"Topic :: Scientific/Engineering :: Artificial Intelligence",
25-
]
26-
dependencies = [
27-
"numpy",
28-
"torch>=2.0.0",
29-
"torchvision",
30-
"torchaudio"
31-
]
12+
dynamic = ["dependencies"]

requirements/common.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
torch==2.8.0
2+
numpy

ruff.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Set the maximum line length to 79.
2+
line-length = 79
3+
4+
[lint]
5+
# Add the `line-too-long` rule to the enforced rule set.
6+
extend-select = ["E501"]
7+
8+
# [lint.per-file-ignores]
9+
# "/path/to/file/that/should/ignore/E501" = ["E501"]

scripts/format.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# [NOTE]
2-
# |- This script is used to format the code using clang-format and black.
2+
# |- This script is used to format the code using clang-format and ruff.
33
# |- Although CI pipeline is configured to format the code after pushing to main,
44
# |- it is still recommended to run this script manually before committing the code.
55

@@ -40,7 +40,7 @@ if [[ "$FORMAT_PYTHON" == "true" ]]; then
4040
echo "Formatting Python files..."
4141
files=$(git ls-files '*.py')
4242
if [[ -n "$files" ]]; then
43-
black --quiet --fast $files
43+
ruff format $files
4444
fi
4545
fi
4646

setup.py

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from pathlib import Path
22
import sys
3-
from os import path as osp
43
import subprocess
54
from setuptools import setup, Extension, find_packages
65
from setuptools.command.build_ext import build_ext
76

87
""" You may want to change the following variables to customize your project """
8+
# Root directory:
9+
ROOT_DIR = Path(__file__).parent
910
# Name of your package; Must match the directory name under `CSRC_DIR`:
1011
PKG_NAME = "example_package"
1112
# Path to the directory of setup.py file:
@@ -31,7 +32,6 @@ def __init__(self, name, source_dir, build_dir, install_dir):
3132

3233

3334
class CMakeBuild(build_ext):
34-
3535
def run(self):
3636
try:
3737
subprocess.check_output(["cmake", "--version"])
@@ -42,20 +42,19 @@ def run(self):
4242
self.build_extension(ext)
4343

4444
def build_extension(self, ext: CMakeExtension):
45-
build_args = [
46-
"-S",
47-
ext.source_dir,
48-
"-B",
49-
ext.build_dir,
50-
"Release",
51-
]
45+
build_args = ["-S", ext.source_dir,
46+
"-B", ext.build_dir,
47+
"Release"] # fmt: skip
5248
# If Current Platform is Windows
5349
if sys.platform == "win32":
5450
subprocess.check_call(
55-
[R"csrc\scripts\msvc-bash.bat", R"csrc\scripts\build.sh"] + build_args
51+
[R"csrc\scripts\msvc-bash.bat", R"csrc\scripts\build.sh"]
52+
+ build_args
5653
)
5754
else:
58-
subprocess.check_call(["bash", "csrc/scripts/build.sh"] + build_args)
55+
subprocess.check_call(
56+
["bash", "csrc/scripts/build.sh"] + build_args
57+
)
5958
install_args = [
6059
"--install",
6160
ext.build_dir,
@@ -65,6 +64,29 @@ def build_extension(self, ext: CMakeExtension):
6564
subprocess.check_call(["cmake"] + install_args)
6665

6766

67+
def get_requirements(root_dir: Path) -> list[str]:
68+
"""Get Python package dependencies from requirements.txt."""
69+
requirements_dir = root_dir / "requirements"
70+
71+
def _read_requirements(filename: str) -> list[str]:
72+
with open(requirements_dir / filename) as f:
73+
requirements = f.read().strip().split("\n")
74+
resolved_requirements = []
75+
for line in requirements:
76+
if line.startswith("-r "):
77+
resolved_requirements += _read_requirements(line.split()[1])
78+
elif (
79+
not line.startswith("--")
80+
and not line.startswith("#")
81+
and line.strip() != ""
82+
):
83+
resolved_requirements.append(line)
84+
return resolved_requirements
85+
86+
requirements = _read_requirements("common.txt")
87+
return requirements
88+
89+
6890
setup(
6991
ext_modules=[
7092
CMakeExtension(
@@ -81,4 +103,5 @@ def build_extension(self, ext: CMakeExtension):
81103
# Use relative path here
82104
PKG_NAME: ["_torch_ops/lib/*.so", "_torch_ops/lib/*.dll"]
83105
},
106+
install_requires=get_requirements(ROOT_DIR),
84107
)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "template_project_name/system.hpp"
2+
3+
namespace template_project_name::cuda
4+
{
5+
VSC_PYTHON_TEMPLATE_API void launch_vec_add(const float* a, const float* b,
6+
float* c, int n);
7+
}
8+
9+
namespace template_project_name::cpu
10+
{
11+
VSC_PYTHON_TEMPLATE_API void launch_vec_add(const float* a, const float* b,
12+
float* c, int n);
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#if defined(_WIN32) || defined(__WIN32__)
4+
#if defined(VSC_PYTHON_TEMPLATE_EXPORT)
5+
#define VSC_PYTHON_TEMPLATE_API __declspec(dllexport)
6+
#elif defined(VSC_PYTHON_TEMPLATE_IMPORT)
7+
#define VSC_PYTHON_TEMPLATE_API __declspec(dllimport)
8+
#else
9+
#define VSC_PYTHON_TEMPLATE_API
10+
#endif
11+
#else
12+
#if defined(VSC_PYTHON_TEMPLATE_EXPORT)
13+
#define VSC_PYTHON_TEMPLATE_API __attribute__((visibility("default")))
14+
#else
15+
#define VSC_PYTHON_TEMPLATE_API
16+
#endif
17+
#endif
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#pragma once
2+
3+
#include <cassert>
4+
#include <cstddef>
5+
6+
#if __cplusplus < 202002L
7+
#include <array>
8+
#else
9+
#include <tuple>
10+
#include <utility>
11+
#endif
12+
13+
namespace template_project_name
14+
{
15+
/**
16+
* @brief Compute the offset of a multi-dimensional array.
17+
*
18+
* @param args First half is the indexes, second half is the size of each
19+
* dimension.
20+
* @return std::uint32_t The offset of the multi-dimensional array.
21+
*
22+
* @example computeOffset(1, 2, 3, 4, 5, 6) -> 3*1 + 2*6 + 1*6*5 = 45
23+
*/
24+
template <typename OffsetT, typename... ArgsT>
25+
constexpr auto computeOffset(ArgsT... args) -> OffsetT
26+
{
27+
constexpr std::size_t nArgs = sizeof...(ArgsT);
28+
constexpr std::size_t nDims = nArgs / 2;
29+
30+
OffsetT offset = 0, stride = 1;
31+
32+
#if __cplusplus >= 202002L
33+
auto params = std::make_tuple(static_cast<OffsetT>(args)...);
34+
[&]<std::size_t... I>(std::index_sequence<I...>) {
35+
((I < nDims ? (offset += std::get<nDims - 1 - I>(params) * stride,
36+
stride *= std::get<nArgs - 1 - I>(params))
37+
: 0),
38+
...);
39+
}(std::make_index_sequence<nDims>{});
40+
#else
41+
auto params = std::array{static_cast<OffsetT>(args)...};
42+
for (std::size_t i = 0; i < nDims; ++i) {
43+
offset += params[nDims - 1 - i] * stride;
44+
stride *= params[nArgs - 1 - i];
45+
}
46+
#endif
47+
48+
return offset;
49+
}
50+
51+
} // namespace template_project_name

uv.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python-preference = "only-managed"

0 commit comments

Comments
 (0)