Repository navigation
Set the macOS OpenMP defaults when litkit is imported - #49
Draft
wshlavacek wants to merge 1 commit into
Draft
wshlavacek wants to merge 1 commit into
wshlavacek wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 withOMP: Error #15unlessKMP_DUPLICATE_LIB_OK=TRUEis 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 setKMP_DUPLICATE_LIB_OK. The thread defaults were set incli.pyand__main__.py, so a library user importinglitkit.indexgot none of them.On main, with scikit-learn blocked, after
import litkit.index, running torch and faiss in one process:OMP: Error #15(rc -6)KMP_DUPLICATE_LIB_OK=TRUEKMP_DUPLICATE_LIB_OK=TRUE,OMP_NUM_THREADS=1KMP_DUPLICATE_LIB_OK=TRUE,OMP_NUM_THREADS=8FAISS_NUM_THREADS, which litkit andtest_build.shset, has no effect: faiss reads onlyOMP_NUM_THREADS.The fix
src/litkit/__init__.pysetsKMP_DUPLICATE_LIB_OK=TRUE,OMP_NUM_THREADS=1andMKL_NUM_THREADS=1on macOS, as defaults only. torch takes its thread count fromOMP_NUM_THREADSand thenMKL_NUM_THREADS: withMKL_NUM_THREADS=8a CPU build segfaulted even withOMP_NUM_THREADS=1. The package__init__runs first for thelitkitcommand,python -m litkitand library imports alike. The copies incli.py,__main__.pyandtests/conftest.pyare removed."",0,autoand+1, which libomp rejects, count too), litkit issues aRuntimeWarningnaming 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 withOMP_NUM_THREADS=8were checked and work; the warning says "on the CPU".configure_threads()(exported, not called by litkit) setMKL_NUM_THREADS=4on macOS; it now sets 1. Docs:LITKIT_MAC_GUIDE.md(environment table and troubleshooting), theconfigure_threadsdocstring,test_build.shand the CHANGELOG no longer claimFAISS_NUM_THREADSdoes 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 for8,4,2,"",0,autoand+1; stays silent (controls) for unset,1,01," 1"and1,4. The tests dropPYTHONWARNINGSfrom the environment so the developer's shell can't hide the warning.KMP_DUPLICATE_LIB_OKdefault fails 5 tests; removing theOMP_NUM_THREADSdefault fails the defaults test and 4 probe tests; setting the defaults only incli.pyfails 3; warning only for integers above 1 fails 3; comparing the value as a string fails 1 (01); checking onlyOMP_NUM_THREADSfails 7; accepting+1fails 2; overriding a presetKMP_DUPLICATE_LIB_OKfails 1.Review
FAISS_NUM_THREADS=1, which does nothing (faiss reads onlyOMP_NUM_THREADS), and an explicitOMP_NUM_THREADSabove 1 crashed silently. Added the warning, removed theFAISS_NUM_THREADSclaims, and moved the conftest setup to the package."",0orauto, or an unset variable becoming1; they inheritedPYTHONWARNINGSfrom the shell;01warned though it means one thread; staleFAISS_NUM_THREADSdocs indevices.py,test_build.shand the guide. All fixed, each with a test that a mutant fails.MKL_NUM_THREADSabove 1 still segfaulted a CPU build with no warning, because torch reads it afterOMP_NUM_THREADS;configure_threads()set it to 4 on macOS. LOW:+1, full-width digits and other valuesint()accepts but libomp rejects got no warning; a test docstring was wrong about main; forcingKMP_DUPLICATE_LIB_OKover a preset value went unnoticed. All fixed; MKL is now defaulted and checked like OMP.1," 1 ",1,,2,4,2,"",0,-1,auto,,4,1.0,1e0,0x1and1_0. A simulated Linux import changes nothing and warns about nothing. MPS builds withOMP_NUM_THREADS=8work.Left alone
OMP_NUM_THREADSabove 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.OMP_NUM_THREADSabove 1 still crashes a CPU build on macOS; litkit warns but does not override the user's setting.