Repository navigation
Take libsais from the Bazel Central Registry - #1313
Conversation
306075e to
19cb60a
Compare
19cb60a to
90839dc
Compare
|
Thanks for working on this! The Bazel Bzlmod migration looks great and tests pass cleanly. One minor suggestion for consistency with #1312: To keep the CMake targets consistent and adhere to modern target-centric CMake practices, could we also add diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index ca29f81..xxxxxxx 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -217,6 +217,7 @@ target_include_directories(sentencepiece_train-static PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third_party>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third_party/darts_clone>
+ $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third_party/libsais>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
@@ -249,6 +250,7 @@ if (SPM_ENABLE_SHARED)
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third_party>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third_party/darts_clone>
+ $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/third_party/libsais>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
) |
|
Thanks for the review. I would like to push back gently on this one, and I have not changed the CMake yet — I would rather agree on the scope with you first. The consistency argument holds for #1312 but not, I think, here, because the two headers sit at different levels:
It would not break anything, so if you prefer the uniform spelling I will just add it. But my suggestion is the other direction: replace the directory-scope Which would you like? I am happy either way; I just did not want to widen the scope silently. Separately, on the red Windows jobs: those are not caused by this change as such. The libsais archive has a 7-byte |
Fixes a Windows-only build break in libsais 2.10.4 and 2.10.4.bcr.1, reported through a downstream failure in google/sentencepiece#1313: ``` external/libsais+\version(1): error C2059: syntax error: 'constant' C:\...\MSVC\...\include\bitset(31): error C2143: syntax error: missing ';' before '{' external/abseil-cpp+\absl/base/casts.h(50): error C2065: 'bit_cast': undeclared identifier ``` The root of the upstream archive contains a 7-byte file named `VERSION`, whose content is `2.10.4`. Bazel passes the repository root of a dependency to its dependents as a quote-include directory, and since MSVC has no `-iquote`, it lands there as a plain `/I` include directory. On the case-insensitive Windows file system, `#include <version>` then resolves to that file rather than the C++ standard header, so every dependent that reaches `<version>` — directly, or through `<bitset>`, `<utility>` and friends — fails to compile. Only Windows consumers are affected: on Linux and macOS the directory is passed as `-iquote`, which angle-bracket includes never search. This version adds `patches/remove_version_file.patch`, which deletes that file. Nothing reads it: the CMake build hardcodes the version in `CMakeLists.txt`, and the Bazel build does not use it either. To keep this from regressing silently, the overlay adds `test/version_header_test.cc`, a C++ test that includes `<bitset>` and `<version>` alongside `libsais.h`. It is part of `@libsais//:all`, so the existing `verify_targets` and `verify_targets_openmp` tasks run it on Windows as well. ### Verification - With the patch, `bazel test @libsais//...` passes against a local registry, and the fetched repository no longer contains `VERSION`. - The regression test was checked against the failure mode by compiling it with the repository root on the plain include path, which is what MSVC gets: against the unpatched tree it reproduces the reported error (`version:1:1: error: expected unqualified-id` on the `2.10.4` line), and against the patched tree it compiles. Windows itself is covered by presubmit rather than locally. Everything else — the archive, the four targets, the OpenMP flag — is unchanged from 2.10.4.bcr.1. @bazel-io skip_check unstable_url
The Bazel build depends on the libsais module instead of compiling third_party/libsais/libsais.c. The CMake build is unchanged and still compiles the vendored copy. libsais 2.10.4.bcr.2 in the registry is upstream v2.10.4, which is the same release third_party/libsais is a snapshot of: ignoring comments, formatting, include order and one clang-format line continuation, the vendored sources are byte-identical to the upstream tarball, so the vendored copy carries no local patches. The registry revision patches out the archive's VERSION file, which MSVC would otherwise pick up for #include <version> on a case-insensitive file system. The module leaves OpenMP off by default, matching both libsais' own CMake option and the //third_party:libsais target this replaces, so the Bazel build keeps taking the serial path. --@libsais//:openmp turns it on where the toolchain supports -fopenmp. libsais.h is now included unqualified, so src/CMakeLists.txt gains third_party/libsais on its include path to resolve the same spelling against the vendored copy.
90839dc to
af9812a
Compare
The Bazel build depends on the
libsaisBCR module instead of compilingthird_party/libsais/libsais.c. The CMake build is unchanged and still compiles the vendored copy.2.10.4.bcr.2vs.third_party/libsaisBoth are upstream
v2.10.4—third_party/libsais/libsais.hdeclaresLIBSAIS_VERSION_STRING "2.10.4", and the module fetches thev2.10.4tarball. I diffed the vendored sources against that tarball:libsais.hlibsais.cclang-formatline continuation are normalisedSo unlike
third_party/darts_clone, the vendoredlibsaiscarries no local patches — it is upstreamv2.10.4after this repo's IWYU/format pass. The module needs no patch to the library sources either, so it can be taken at a plain version with nothing overridden on this side..bcr.1and.bcr.2are registry-side revisions of the same upstream source:.bcr.1adds the OpenMP toggle described below, and.bcr.2patches out the archive'sVERSIONfile, without which MSVC resolves#include <version>to it on a case-insensitive file system (bazelbuild/bazel-central-registry#10291, reported from this PR's Windows jobs).SentencePiece calls
libsais,libsais_omp,libsais_plcp,libsais_plcp_omp,libsais_lcpandlibsais_lcp_omp, all fromunigram_model_trainer.cc. All six are in the module's@libsais//:libsaistarget (uint8_tinput,int32_tsuffix array); the16/64/16x64variants are unused.OpenMP
2.10.4.bcr.1and later expose an--@libsais//:openmpflag, the counterpart of libsais' ownLIBSAIS_USE_OPENMPCMake option. It adds-fopenmp(or/openmp), and setsLIBSAIS_OPENMPas adefines, so the macro reaches dependents andunigram_model_trainer.cccompiles its_ompbranch — the same way the CMake build'sadd_compile_definitions(LIBSAIS_OPENMP)reaches it.It is off by default, which matches both libsais' own CMake default and the
//third_party:libsaistarget this replaces, so this change does not alter what the Bazel build does today: it still takes the serial path. This PR does not turn it on, because-fopenmpis unsupported by Apple clang, so enabling it in.bazelrcwould break the macOS Bazel job.Checked with a toolchain that does support it (
--repo_env=CC=/opt/homebrew/opt/llvm/bin/clang), building-c optwith and without the flag and timing the seed-extraction phase from the trainer's own log over an 8.7 MB corpus, 6 runs each, interleaved:Extracting seed sentencepieces (libsais)--@libsais//:openmpExtracting seed sentencepieces (libsais_omp, 8 threads)So the flag works end to end and is worth roughly 1.13x here — suffix array construction does not parallelise dramatically at this corpus size. Mentioned only to show the path is wired correctly; the default is unchanged.
Optimisation flags
//third_party:libsaissetcopts = ["-O3"], which the module does not. Perbazel aquery:-c opt-O3-O2-c fastbuild-O3I measured what that actually costs, on macOS/arm64 with Apple clang 21, two ways.
Isolated. A driver running the same
libsais→libsais_plcp→libsais_lcpsequence asExtractSeedSentencePieces, over an 8.7 MB corpus, with onlylibsais.c's optimisation level varied (driver fixed at-O2; median of 24 runs):libsais.c-O0-O2-O3End to end.
spm_train --model_type=unigram --vocab_size=8000over the same corpus, timing the seed-extraction phase from the trainer's log lines (6 runs each, interleaved order):-c opt, module as-is (-O2)-c opt+--per_file_copt=…libsais…@-O3-O3buys nothing over-O2for this code — the two agree to within 1% in both measurements — so dropping thecoptscosts no measurable performance in-c opt. I originally assumed this would be the one real regression in the change; it measures as a non-issue, so I am not proposing a.bazelrcworkaround unless you want one.The
fastbuildrow is the larger change: libsais goes from-O3to unoptimised, which the isolated numbers put at ~2.5x slower for seed extraction. That only affects training run from afastbuildbinary, which was already an unoptimised build of everything else in the trainer.Two caveats. This is one machine and one compiler; GCC on Linux may weigh
-O3differently. And totalspm_trainwall time did shift by ~15% between the two binaries, but that difference sits entirely in the EM iterations, which do not call libsais and whose objects are byte-identical between the two builds —aqueryconfirms exactly one compile action changes. It is binary layout, not codegen, which is why the numbers above time the seed-extraction phase specifically rather than the whole run.Why the module rather than the vendored copy
Upgrades become a version bump. libsais is a single 400 KB
.cfile. Moving it forward today means re-dropping that file and re-running the format pass over it; with the module it is one line inMODULE.bazel.Known provenance. The module pins the upstream
v2.10.4tarball by SHA-256. The vendored copy is a reformatted snapshot whose correspondence to a specific upstream release can only be established by diffing, as above.Visible in dependency tooling. A
bazel_depwith a version appears inMODULE.bazel.lock,bazel mod graph, and any SBOM generated from them. A checked-in.cfile does not.The vendored copy is not deleted: the CMake build still compiles it, so this reduces the number of builds depending on the unlabelled snapshot from two to one rather than to zero.
Include path
The module exports the header as
libsais.h(includes = ["include"]), so the one#include "third_party/libsais/libsais.h"inunigram_model_trainer.ccloses the prefix.src/CMakeLists.txtgainsthird_party/libsaison its include path so the CMake build resolves the same spelling against the vendored copy.libsais.his not reachable from any installed header, so thetarget_include_directories(... PUBLIC ...)blocks need no change.Verified
macOS/arm64:
bazel test //...— 22 of 22 tests passcmake --build—sentencepiece_train-staticbuilds, compiling bothunigram_model_trainer.ccand the vendoredlibsais.cNote
This touches
third_party/BUILD.bazel, as does #1312, so whichever lands second will need a trivial conflict resolution there. The two changes are otherwise independent.