Skip to content

Set the macOS OpenMP defaults when litkit is imported - #49

Draft
wshlavacek wants to merge 1 commit into
mainfrom
fix/16-macos-openmp-guard
Draft

wshlavacek wants to merge 1 commit into
mainfrom
fix/16-macos-openmp-guard

Conversation

@wshlavacek

Copy link
Copy Markdown
Collaborator

Fixes #16.

What was wrong

On macOS, torch, faiss-cpu and scikit-learn each bundle their own libomp. litkit loads torch and faiss in one process for every build and query. Two OpenMP runtimes in one process abort with OMP: Error #15 unless KMP_DUPLICATE_LIB_OK=TRUE is set, and with that set they still segfault or hang unless OpenMP runs one thread. litkit survived only because importing sentence-transformers imports scikit-learn, which happens to set KMP_DUPLICATE_LIB_OK. The thread defaults were set in cli.py and __main__.py, so a library user importing litkit.index got none of them.

On main, with scikit-learn blocked, after import litkit.index, running torch and faiss in one process:

Environment torch first faiss first
nothing set segfault (rc -11) abort, OMP: Error #15 (rc -6)
KMP_DUPLICATE_LIB_OK=TRUE segfault hangs (> 60 s)
KMP_DUPLICATE_LIB_OK=TRUE, OMP_NUM_THREADS=1 ok ok
KMP_DUPLICATE_LIB_OK=TRUE, OMP_NUM_THREADS=8 segfault hangs

FAISS_NUM_THREADS, which litkit and test_build.sh set, has no effect: faiss reads only OMP_NUM_THREADS.

The fix

  • src/litkit/__init__.py sets KMP_DUPLICATE_LIB_OK=TRUE, OMP_NUM_THREADS=1 and MKL_NUM_THREADS=1 on macOS, as defaults only. torch takes its thread count from OMP_NUM_THREADS and then MKL_NUM_THREADS: with MKL_NUM_THREADS=8 a CPU build segfaulted even with OMP_NUM_THREADS=1. The package __init__ runs first for the litkit command, python -m litkit and library imports alike. The copies in cli.py, __main__.py and tests/conftest.py are removed.
  • A value already in the environment wins. If either variable allows more than one thread (its first entry isn't ASCII digits equal to 1, so "", 0, auto and +1, which libomp rejects, count too), litkit issues a RuntimeWarning naming the variable and macOS: builds survive OpenMP clash only because scikit-learn sets KMP_DUPLICATE_LIB_OK #16, since a CPU build then segfaults with no message. MPS builds with OMP_NUM_THREADS=8 were checked and work; the warning says "on the CPU".
  • configure_threads() (exported, not called by litkit) set MKL_NUM_THREADS=4 on macOS; it now sets 1. Docs: LITKIT_MAC_GUIDE.md (environment table and troubleshooting), the configure_threads docstring, test_build.sh and the CHANGELOG no longer claim FAISS_NUM_THREADS does anything.

Evidence

  • tests/test_macos_openmp.py:
    • test_torch_and_faiss_run_in_one_process_without_scikit_learn[litkit.cli|litkit.index × torch-first|faiss-first] runs the table above with scikit-learn blocked. All 4 fail on main (segfault, abort or hang).
    • test_openmp_defaults_are_set_on_macos_only_and_never_override[unset|preset]: unset variables get the defaults, preset values of all three are kept, and nothing changes off macOS. Both fail on main.
    • test_macos_warns_when_openmp_threads_are_set_above_one[OMP_NUM_THREADS|MKL_NUM_THREADS × ...]: warns for 8, 4,2, "", 0, auto and +1; stays silent (controls) for unset, 1, 01, " 1" and 1,4. The tests drop PYTHONWARNINGS from the environment so the developer's shell can't hide the warning.
  • Mutation checks: removing the KMP_DUPLICATE_LIB_OK default fails 5 tests; removing the OMP_NUM_THREADS default fails the defaults test and 4 probe tests; setting the defaults only in cli.py fails 3; warning only for integers above 1 fails 3; comparing the value as a string fails 1 (01); checking only OMP_NUM_THREADS fails 7; accepting +1 fails 2; overriding a preset KMP_DUPLICATE_LIB_OK fails 1.
  • Full suite on top of main: 400 passed, 1 skipped, 8 xfailed (Builds give a paper a second vector when it reappears #7 and Databases created before doc_id can't be opened #10).

Review

  • Round 1: the first version also forced FAISS_NUM_THREADS=1, which does nothing (faiss reads only OMP_NUM_THREADS), and an explicit OMP_NUM_THREADS above 1 crashed silently. Added the warning, removed the FAISS_NUM_THREADS claims, and moved the conftest setup to the package.
  • Round 2 (all LOW): tests didn't cover "", 0 or auto, or an unset variable becoming 1; they inherited PYTHONWARNINGS from the shell; 01 warned though it means one thread; stale FAISS_NUM_THREADS docs in devices.py, test_build.sh and the guide. All fixed, each with a test that a mutant fails.
  • Round 3: MEDIUM, MKL_NUM_THREADS above 1 still segfaulted a CPU build with no warning, because torch reads it after OMP_NUM_THREADS; configure_threads() set it to 4 on macOS. LOW: +1, full-width digits and other values int() accepts but libomp rejects got no warning; a test docstring was wrong about main; forcing KMP_DUPLICATE_LIB_OK over a preset value went unnoticed. All fixed; MKL is now defaulted and checked like OMP.
  • Values checked in round 3 against what torch and faiss actually run: litkit warns exactly when either runs more than one thread, for unset, 1, " 1 ", 1,, 2, 4,2, "", 0, -1, auto, ,4, 1.0, 1e0, 0x1 and 1_0. A simulated Linux import changes nothing and warns about nothing. MPS builds with OMP_NUM_THREADS=8 work.

Left alone

  • chatty runs litkit's retrieval in a subprocess and parses its stderr as one JSON document. When the warning appears (macOS, OMP_NUM_THREADS above 1) and retrieval then fails for another reason, chatty shows the warning followed by raw JSON instead of the error message. That parsing belongs to chatty.
  • An explicit OMP_NUM_THREADS above 1 still crashes a CPU build on macOS; litkit warns but does not override the user's setting.

On macOS, torch, faiss-cpu and scikit-learn each bundle libomp. Two
OpenMP runtimes in one process abort with "OMP: Error #15" unless
KMP_DUPLICATE_LIB_OK is set, and then segfault or hang unless OpenMP runs
one thread. litkit survived only because importing sentence-transformers
imports scikit-learn, which sets KMP_DUPLICATE_LIB_OK. The thread
defaults lived in cli.py and __main__.py, so importing litkit.index got
none of them.

The package __init__ now sets KMP_DUPLICATE_LIB_OK=TRUE,
OMP_NUM_THREADS=1 and MKL_NUM_THREADS=1 on macOS, as defaults only
(torch reads both thread variables). A value already set wins, with a
RuntimeWarning when it allows more than one thread, since a CPU build
then segfaults without a message. FAISS_NUM_THREADS has no effect: faiss
reads OMP_NUM_THREADS. The docs no longer claim otherwise.

Fixes #16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

macOS: builds survive OpenMP clash only because scikit-learn sets KMP_DUPLICATE_LIB_OK

1 participant