From f77dda03c42fb2bbba7113059644c26276817c73 Mon Sep 17 00:00:00 2001 From: Justin McFarland Date: Thu, 12 Mar 2026 10:43:48 -0500 Subject: [PATCH 1/2] fix: add const to y_col pointer in ggml_vec_dot_i2_i8_s_Nx1 The function signature declares `vy` as `const void *`. After casting to `int8_t *`, assigning pointer arithmetic on that value to a non-const `int8_t * y_col` drops the const qualifier. Clang 18 rejects this with an implicit-const-conversion error; GCC and older Clang accepted it with a warning or silently. Fix: declare `y_col` as `const int8_t *`, which correctly propagates the const qualifier from the input parameter through the pointer arithmetic. Affected location: src/ggml-bitnet-mad.cpp:811 (ggml_vec_dot_i2_i8_s_Nx1) --- src/ggml-bitnet-mad.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ggml-bitnet-mad.cpp b/src/ggml-bitnet-mad.cpp index 4ba9d6509..ad18bac04 100644 --- a/src/ggml-bitnet-mad.cpp +++ b/src/ggml-bitnet-mad.cpp @@ -808,7 +808,7 @@ void ggml_vec_dot_i2_i8_s_Nx1(int n, float * s, size_t bs, const void * vx, size accu[iy] = _mm256_setzero_si256(); } - int8_t * y_col = y + col * by; + const int8_t * y_col = y + col * by; for (int i = 0; i < group32_num; i++) { const uint8_t *px = x + i * 1024; From d188d35501e0e88cbfd63940b7fcf3faef6b606e Mon Sep 17 00:00:00 2001 From: Justin McFarland Date: Thu, 12 Mar 2026 10:43:59 -0500 Subject: [PATCH 2/2] fix: auto-detect versioned Clang on Linux to resolve OpenMP linker errors On Linux systems where both a distro-packaged clang (e.g. clang-18 from llvm.org/apt) and an alternative clang (e.g. Homebrew clang) coexist, the bare 'clang' alias may point to a build that links against a different libomp than the one installed by the OS package manager. This causes undefined-symbol linker errors at build time: undefined reference to `__kmpc_fork_call@VERSION` Root cause: each clang distribution ships its own libomp. When clang and libomp come from different distributions, the symbol versioning may not match. Distro-packaged versioned binaries (clang-18, clang-17, ...) are guaranteed to pair with the correct libomp. Fix: add _find_clang() which probes for versioned Clang/Clang++ binaries (18, 17, 16, 15) before falling back to the bare 'clang'/'clang++' alias. The probe is Linux-only; macOS and Windows are unaffected. This fixes setup_env.py --hf-repo builds on Ubuntu 22.04/24.04 with clang-18 installed via `apt install clang-18 libomp-18-dev`. Affected location: setup_env.py compile() -- CMake -DCMAKE_C_COMPILER / -DCMAKE_CXX_COMPILER --- setup_env.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/setup_env.py b/setup_env.py index 3bf5fb8f7..7bd153c03 100644 --- a/setup_env.py +++ b/setup_env.py @@ -200,6 +200,27 @@ def gen_code(): raise NotImplementedError() +def _find_clang(): + """Find the best Clang/Clang++ available for BitNet compilation. + + On Linux, prefers versioned installs (clang-18, clang-17, …) over the + bare 'clang' alias. Distro-packaged versioned compilers (e.g. from + llvm.org or apt) ship a matching libomp that the linker can resolve. + Using a mismatched 'clang' (e.g. from Homebrew on a Linux host) links + against a different libomp that may not be on the system library path, + causing undefined-symbol errors at link time. + + Falls back to unversioned 'clang'/'clang++' on non-Linux platforms + (macOS, Windows) where the version-suffixed binaries are not standard. + """ + if platform.system() == "Linux": + for ver in ["18", "17", "16", "15"]: + if shutil.which(f"clang-{ver}") and shutil.which(f"clang++-{ver}"): + logging.info(f"Using clang-{ver}/clang++-{ver}") + return f"clang-{ver}", f"clang++-{ver}" + return "clang", "clang++" + + def compile(): # Check if cmake is installed cmake_exists = subprocess.run(["cmake", "--version"], capture_output=True) @@ -211,7 +232,8 @@ def compile(): logging.error(f"Arch {arch} is not supported yet") exit(0) logging.info("Compiling the code using CMake.") - run_command(["cmake", "-B", "build", *COMPILER_EXTRA_ARGS[arch], *OS_EXTRA_ARGS.get(platform.system(), []), "-DCMAKE_C_COMPILER=clang", "-DCMAKE_CXX_COMPILER=clang++"], log_step="generate_build_files") + c_compiler, cxx_compiler = _find_clang() + run_command(["cmake", "-B", "build", *COMPILER_EXTRA_ARGS[arch], *OS_EXTRA_ARGS.get(platform.system(), []), f"-DCMAKE_C_COMPILER={c_compiler}", f"-DCMAKE_CXX_COMPILER={cxx_compiler}"], log_step="generate_build_files") # run_command(["cmake", "--build", "build", "--target", "llama-cli", "--config", "Release"]) run_command(["cmake", "--build", "build", "--config", "Release"], log_step="compile")