From 62439f24553b0891d78a665388fe33e177994f73 Mon Sep 17 00:00:00 2001 From: xytpai Date: Fri, 8 Aug 2025 11:58:32 +0800 Subject: [PATCH 1/2] fix build bugs --- src/core/include/half.h | 24 ++++++++++++------------ src/core/include/tensor.h | 11 ----------- src/core/utils/exception.h | 1 + src/core/utils/xstring.h | 11 +++++++++++ 4 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/core/include/half.h b/src/core/include/half.h index eda1f53..f769a20 100644 --- a/src/core/include/half.h +++ b/src/core/include/half.h @@ -215,11 +215,11 @@ struct alignas(2) Half { #if defined(__CUDACC__) DEVICE inline operator float() const; DEVICE inline Half(float f); - DEVICE inline operator BFloat16() const; + DEVICE inline Half(BFloat16 bf); #else inline operator float() const; inline Half(float f); - inline operator BFloat16() const; + inline Half(BFloat16 bf); #endif }; @@ -229,11 +229,11 @@ struct alignas(2) BFloat16 { #if defined(__CUDACC__) DEVICE inline operator float() const; DEVICE inline BFloat16(float f); - DEVICE inline operator Half() const; + DEVICE inline BFloat16(Half h); #else inline operator float() const; inline BFloat16(float f); - inline operator Half() const; + inline BFloat16(Half h); #endif }; @@ -247,8 +247,8 @@ DEVICE inline Half::Half(float f) { x = float_to_half(f); } -DEVICE inline Half::operator BFloat16() const { - return BFloat16(half_to_float(x)); +DEVICE inline Half::Half(BFloat16 bf) { + x = float_to_half(bfloat16_to_float(bf.x)); } DEVICE inline BFloat16::operator float() const { @@ -259,8 +259,8 @@ DEVICE inline BFloat16::BFloat16(float f) { x = float_to_bfloat16(f); } -DEVICE inline BFloat16::operator Half() const { - return Half(bfloat16_to_float(x)); +DEVICE inline BFloat16::BFloat16(Half h) { + x = float_to_bfloat16(half_to_float(h.x)); } #else @@ -273,8 +273,8 @@ inline Half::Half(float f) { x = fp16_ieee_from_fp32_value(f); } -inline Half::operator BFloat16() const { - return BFloat16(fp16_ieee_to_fp32_value(x)); +inline Half::Half(BFloat16 bf) { + x = fp16_ieee_from_fp32_value(f32_from_bits(bf.x)); } inline BFloat16::operator float() const { @@ -285,8 +285,8 @@ inline BFloat16::BFloat16(float f) { x = round_to_nearest_even(f); } -inline BFloat16::operator Half() const { - return Half(f32_from_bits(x)); +inline BFloat16::BFloat16(Half h) { + x = round_to_nearest_even(fp16_ieee_to_fp32_value(h.x)); } #endif diff --git a/src/core/include/tensor.h b/src/core/include/tensor.h index 8c1282a..6e2a1cd 100644 --- a/src/core/include/tensor.h +++ b/src/core/include/tensor.h @@ -30,17 +30,6 @@ Tensor empty_like_reduced(const Tensor &self, int dim, ScalarType dtype); Tensor zeros(std::vector shape, ScalarType dtype, int device = 0); std::ostream &operator<<(std::ostream &os, const Tensor &t); -template -std::ostream &operator<<(std::ostream &os, const std::vector &vec) { - os << "["; - for (auto i = 0; i < vec.size(); ++i) { - os << vec[i]; - if (i + 1 != vec.size()) os << ", "; - } - os << "]"; - return os; -} - template struct d_array { T val[vec_size] = {0}; diff --git a/src/core/utils/exception.h b/src/core/utils/exception.h index 1c6fc18..6dca255 100644 --- a/src/core/utils/exception.h +++ b/src/core/utils/exception.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "xstring.h" diff --git a/src/core/utils/xstring.h b/src/core/utils/xstring.h index 85ec8d8..230c1f6 100644 --- a/src/core/utils/xstring.h +++ b/src/core/utils/xstring.h @@ -4,6 +4,17 @@ #include #include +template +std::ostream &operator<<(std::ostream &os, const std::vector &vec) { + os << "["; + for (auto i = 0; i < vec.size(); ++i) { + os << vec[i]; + if (i + 1 != vec.size()) os << ", "; + } + os << "]"; + return os; +} + namespace utils { inline std::ostream &_str(std::ostream &ss) { From bec7e6192d9d9970ef39763c044b4e9ea50f394a Mon Sep 17 00:00:00 2001 From: xytpai Date: Fri, 8 Aug 2025 12:10:47 +0800 Subject: [PATCH 2/2] add default parallel build --- requirements.txt | 1 + setup.py | 3 ++- tools/cmake.py | 11 +++++++++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/requirements.txt b/requirements.txt index b43a3d1..825e175 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ cmake ninja pybind11 pytest +numpy torch diff --git a/setup.py b/setup.py index d5ca411..6e91532 100644 --- a/setup.py +++ b/setup.py @@ -9,6 +9,7 @@ package_name = 'kfunca' version = '0.1.1' base_dir = os.path.dirname(os.path.abspath(__file__)) +default_parallel_build = 4 # Default number of parallel jobs for building class BuildExt(build_ext): @@ -19,7 +20,7 @@ def run(self): super().run() def build_cmake(self, ext): - cmake = CMake() + cmake = CMake(default_parallel_build) extdir = pathlib.Path(self.get_ext_fullpath(ext.name)).parent.absolute() cmake.generate(rerun=True, output_dir=extdir) cmake.build() diff --git a/tools/cmake.py b/tools/cmake.py index d01f64e..43e9711 100644 --- a/tools/cmake.py +++ b/tools/cmake.py @@ -33,9 +33,10 @@ def which(thefile: str) -> str | None: class CMake: "Manages cmake." - def __init__(self, build_dir: str = BUILD_DIR) -> None: + def __init__(self, parallel_build=None, build_dir=BUILD_DIR) -> None: self._cmake_command = CMake._get_cmake_command() self.build_dir = build_dir + self.parallel_build = parallel_build @property def _cmake_cache_file(self) -> str: @@ -100,4 +101,10 @@ def generate(self, rerun: bool, output_dir: str): self.run(args, os.environ) def build(self): - self.run(['--build', '.'], os.environ) + if not self.parallel_build: + self.run(['--build', '.'], os.environ) + elif isinstance(self.parallel_build, int): + nworkers = str(self.parallel_build) + self.run(['--build', '.', '-j', nworkers], os.environ) + else: + self.run(['--build', '.', '--parallel'], os.environ)