Repository navigation
Fix accelerate launch --cpu never setting the KMP variables - #4222
Merged
Merged
Conversation
huggingface#3912 gated KMP_AFFINITY and KMP_BLOCKTIME on CPU mode with: parse_flag_from_env(current_env["ACCELERATE_USE_CPU"], False) `parse_flag_from_env` takes an environment variable *name*, not a value, and does `os.environ.get(key, str(default))`. It is handed the string "True" or "False" here, looks for an environment variable by that name, does not find one and returns the default. The guard is therefore always False, so the two variables are never set, including for `accelerate launch --cpu`. Before huggingface#3912 the condition was `if current_env["ACCELERATE_USE_CPU"]:`, which was always True because "False" is a non-empty string, so KMP_AFFINITY was set on GPU launches too. That was the bug huggingface#3912 set out to fix; it swapped one constant for the other. Test the flag itself, the way the line above it already does, and drop the import that was added only for the broken call.
SunMarc
reviewed
Sep 8, 2026
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
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.
What does this PR do?
accelerate launch --cpuno longer setsKMP_AFFINITYandKMP_BLOCKTIME.#3912 gated those two on CPU mode with:
parse_flag_from_envtakes an environment variable name and doesos.environ.get(key, str(default)). Here it is handed the value, the string"True"or"False", so it looks for an environment variable literally calledTrue, does not find one, and returns the default. The guard isFalseon every launch.Before #3912 the line was
if current_env["ACCELERATE_USE_CPU"]:, which was alwaysTruebecause"False"is a non-empty string, so the variables were set on GPU launches too. That was the bug reported in #3911. The fix replaced one constant with the other.On
main, withprepare_simple_launcher_cmd_env:and the mechanism, which is easy to confirm directly:
With this PR:
The flag is now tested the same way the line two above it already tests it (
current_env["ACCELERATE_USE_CPU"] = str(args.cpu or args.use_cpu)), and the rest of the launch code does the same thing (commands/launch.pyusesargs.use_cpuandargs.cpudirectly throughout). The import that #3912 added for the broken call has no other user in the file, so it goes with it.Testing
tests/test_cli.py::AccelerateLauncherTester::test_cpu_launch_sets_kmp_envcovers both directions, so neither the original #3911 behaviour nor this one can come back. It fails onmainwithKeyError: 'KMP_AFFINITY'and passes here.pytest tests/test_cli.py: 37 passed, 3 skipped, 1 failed. The failure istest_accelerate_test, which fails identically onmainon this machine withFileNotFoundError: 'accelerate-launch', since I run from source rather than an installed console script.ruff check .andruff format --check .with the 0.13.1 pinned insetup.py: clean for these files.ruff checkalso reports one unrelated F821 incommands/env.pyonmain, which Fix accelerate env never reading the MPS chip name聽#4221 fixes.Ran on Python 3.13, torch 2.11.0, Linux, CPU.
Before submitting
Who can review?
@SunMarc